function checkAddress(address, type){
	//type="S" means checking for Store info only
	//type="A" means checking for both store and contact info
	//type="C" means checking for contact info only
	
	if (address.txtCompanyName.value.trim() == "" || address.txtContact.value.trim() == ""
		    || address.txtPostal.value.trim() == "" || address.txtAddress.value.trim() == "" 
		    || address.txtCity.value.trim() == "" || address.txtTeleArea.value.trim() == ""
		    || address.txtTelephone.value.trim() == ""){
			alert("Please go back and Fill in all Necessary Information");
			return false;
	}
	
	if ((address.txtEmail.value.trim() == "") && type=="C"){
		alert("Please go back and fill in the email address");
		return false;
	}
	if (type=="S" && (address.selRegion.value == "" || address.selCity.value == "")){
		alert("Please go back and fill in the Location information");
		return false;
	}
	if (address.txtPostal.value.trim() != ""){
		var reg = /(([A-Z][0-9][A-Z][0-9][A-Z]))/i;
		var ar = reg.exec(address.txtPostal.value.trim());
		if (!ar){
			address.txtPostal.focus();
			alert("Your Postal Code is not valid.\n");
			return false;
		}
	}
	
	if (address.txtTelephone.value.trim() != ""){
		//var reg = /((\([0-9]{3}\)-[0-9]{3}-[0-9]{4})|(\([0-9]{3}\)[0-9]{3}-[0-9]{4})|([0-9]{3}-[0-9]{3}-[0-9]{4})|(\([0-9]{3}\)[0-9]{7})|([0-9]{3}-[0-9]{7})|([0-9]{10}))/i;
		var reg = /((\([0-9]{3}\)[0-9]{3}[0-9]{4}))/i;
		var ar = reg.exec('('+address.txtTeleArea.value.trim()+')'+address.txtTelephone.value.trim());
		if (!ar){
			address.txtTelephone.focus();
			alert("Your Phone Number is not valid.\nHere is the valid format:\n(###)#######");
			return false;
		}
	}
	
	if (address.txtFax.value.trim() != ""){
		//var reg = /((\([0-9]{3}\)-[0-9]{3}-[0-9]{4})|(\([0-9]{3}\)[0-9]{3}-[0-9]{4})|([0-9]{3}-[0-9]{3}-[0-9]{4})|(\([0-9]{3}\)[0-9]{7})|([0-9]{3}-[0-9]{7})|([0-9]{10}))/i;
		var reg = /((\([0-9]{3}\)[0-9]{3}[0-9]{4}))/i;
		var ar = reg.exec('('+address.txtFaxArea.value.trim()+')'+address.txtFax.value.trim());
		if (!ar){
			address.txtFax.focus();
			alert("Your Fax Number is not valid.\nHere is the valid format:\n(###)#######");
			return false;
		}
	}
	
	if (address.txtEmail.value.length >30){
		address.txtEmail.focus();
		alert("Email address cannot be longer than 30 characters");
		return false;
	}
	
}

String.prototype.trim = function() {
	return this.replace( /^\s*/, '').replace( /\s*$/, '' );
}


// extract front part of string prior to searchString
function getFront(mainStr,searchStr){
	foundOffset = mainStr.indexOf(searchStr);
	if (foundOffset == -1) {
		return null;
	}
	return mainStr.substring(0,foundOffset);
}

// extract back end of string after searchString
function getEnd(mainStr,searchStr) {
	foundOffset = mainStr.indexOf(searchStr);
	if (foundOffset == -1) {
	return null;
	}
	return mainStr.substring(foundOffset+searchStr.length,mainStr.length);
}
// insert insertString immediately before searchString
function insertString(mainStr,searchStr,insertStr) {
	var front = getFront(mainStr,searchStr);
	var end = getEnd(mainStr,searchStr);
	if (front != null && end != null) {
		return front + insertStr + searchStr + end;
	}
	return null;
}
// remove deleteString
function deleteString(mainStr,deleteStr) {
	return replaceString(mainStr,deleteStr,"");
}
// replace searchString with replaceString
function replaceString(mainStr,searchStr,replaceStr) {
	var front = getFront(mainStr,searchStr);
	var end = getEnd(mainStr,searchStr);
	if (front != null && end != null) {
		return front + replaceStr + end;
	}
	return null;
}

function setupString(str){
	str = str.trim();
	
	//remove all the white spaces
	while (exist(str," "))
		str = replaceString(str," ","");	
	while (exist(str,",,"))
		str = replaceString(str,",,",",");
	if (str.charAt(0)== ","){	
		str = str.substring(1,str.length);
	}
	if (str.charAt(str.length-1) == ",")
		str=str.substring(0,str.length-1);
	return str;
	
}
//check if prodString has val already
function exist(word, val){	
	if (word.indexOf(val) == -1)
		return false;
	else
		return true;
}
function checkDrawReferralForm(f,divId){
	
	if (f.name.value.trim()==""){
		alert("Please enter your name");
		f.name.focus();
		return false;
	}
	if (f.email.value.trim()==""){
		
		alert("Please enter your email");
		f.email.focus();
		return false;
	}
	else{
		if (!emailCheck(f.email.value.trim(),f.email)){
			return false;
		}
	}
	var num = f.number.value;
	var send = false;
	for (var i=0;i<num;i++){
		var checkName = 'check_'+i;
		var nameName = 'name_'+i;
		var emailName = 'emailAddress_'+i;
		
		var checkObj = document.getElementById(checkName);
		var nameObj = document.getElementById(nameName);
		var emailObj = document.getElementById(emailName);
		
		if (checkObj.checked){
			send = true;
			if(nameObj.value.trim() == ""){
				alert("Please enter the recipient's name");
				nameObj.focus();
				return false;
			}
			if(emailObj.value.trim() == ""){
				alert("Please enter the recipient's email address");
				emailObj.focus();
				return false;
			}
			if (!emailCheck(emailObj.value.trim(),emailObj))
				return false;
			
		}
	}
	if (send){
		sendDrawReferral(f,divId);
		return true;
	}
	alert("Please enter a name and an email address to send");
	return false;
}

function checkEmailCouponForm(f){
	
	if (f.name.value.trim()==""){
		alert("Please enter your name");
		f.name.focus();
		return false;
	}
	var num = f.number.value;
	var send = false;
	for (var i=0;i<num;i++){
		var checkName = 'check_'+i;
		var nameName = 'name_'+i;
		var emailName = 'emailAddress_'+i;
		
		var checkObj = document.getElementById(checkName);
		var nameObj = document.getElementById(nameName);
		var emailObj = document.getElementById(emailName);
		
		if (checkObj.checked){
			send = true;
			if(nameObj.value.trim() == ""){
				alert("Please enter the recipient's name");
				nameObj.focus();
				return false;
			}
			if(emailObj.value.trim() == ""){
				alert("Please enter the recipient's email address");
				emailObj.focus();
				return false;
			}
			if (!emailCheck(emailObj.value.trim(),emailObj))
				return false;
			
		}
	}
	if (send)
		return true;
	alert("Please enter a name and an email address to send");
	return false;
}

function validatePriceAlert(f,divId){
	if (f.email.value.trim() == ''){
		alert("Please enter an email address");
		f.email.focus();
		return false;
	}
	if (!emailCheck(f.email.value.trim(),f.email)){
		return false;
	}
	
	if (isNaN(f.price.value.trim())){
		alert("Please enter a valid price");
		f.price.focus();
		return false;
	}

	if (parseFloat(f.price.value) >= parseFloat(f.minPrice.value)){
		alert("The alert price has been met already. Please try again");
		f.price.focus();
		return false;
	}
	sendAlert(f,divId);
}

function validateDrawEntry(f,divId){
	
	if (f.firstname.value.trim() == ''){
		alert("Please enter a first name.");
		f.firstname.focus();
		return false;
	}
	
	if (f.lastname.value.trim() == ''){
		alert("Please enter a last name.");
		f.lastname.focus();
		return false;
	}
		
	if (f.email.value.trim() == ''){
		alert("Please enter an email address");
		f.email.focus();
		return false;
	}
	if (!emailCheck(f.email.value.trim(),f.email)){
		return false;
	}
	//alert(!f.certify.checked);
	
	if (!f.certify.checked){
		alert("You must be 13 years old or older to enter the draw");
		return false;
	}
	
	sendDrawEntry(f,divId);
}


function emailCheck (emailStr,emailObj) {
	/* The following pattern is used to check if the entered e-mail address
	   fits the user@domain format.  It also is used to separate the username
	   from the domain. */
	var emailPat=/^(.+)@(.+)$/
	/* The following string represents the pattern for matching all special
	   characters.  We don't want to allow special characters in the address. 
	   These characters include ( ) < > @ , ; : \ " . [ ]    */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	/* The following string represents the range of characters allowed in a 
	   username or domainname.  It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]"
	/* The following pattern applies if the "user" is a quoted string (in
	   which case, there are no rules about which characters are allowed
	   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	   is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")"
	/* The following pattern applies for domains that are IP addresses,
	   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	   e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* The following string represents an atom (basically a series of
	   non-special characters.) */
	var atom=validChars + '+'
	/* The following string represents one word in the typical username.
	   For example, in john.doe@somewhere.com, john and doe are words.
	   Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	   domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	
	/* Finally, let's start trying to figure out if the supplied address is
	   valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	   different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
	  /* Too many/few @'s or something; basically, this address doesn't
	     even fit the general mould of a valid e-mail address. */
		alert("Email address seems incorrect (check @ and .'s)");
		emailObj.focus();
		return false;
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	// See if "user" is valid 
	if (user.match(userPat)==null) {
	    // user is not valid
	    alert("The username doesn't seem to be valid.")
	    emailObj.focus();
	    return false
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	   host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	    // this is an IP address
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
		        alert("Destination IP address is invalid!")
		        emailObj.focus();
			return false
		    }
	    }
	    return true
	}
	
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("The domain name doesn't seem to be valid.")
		emailObj.focus();
	    return false
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	   three-letter word (like com, edu, gov) or a two-letter word,
	   representing country (uk, nl), and that there's a hostname preceding 
	   the domain or country. */
	
	/* Now we need to break up the domain to get a count of how many atoms
	   it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
	   // the address must end in a two letter or three letter word.
	   alert("The address must end in a three-letter domain, or two letter country.")
	   emailObj.focus();
	   return false
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   alert(errStr)
	   emailObj.focus();
	   return false
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}
