/*
	This file contains .js functions for use with various forms for verifying input, changing locations etc
*/


/*
	subscribe() first does a simple check to see if the provided email address is more or less correctly formatted (e.g. is of the format abc@xyz):
*/
function subscribe() {
	if ((document.frm.address.value.indexOf("@") < 2) || !(document.frm.address.value.indexOf("@"))) {
		// If there are not at least 2 characters before the '@' OR there is no '@' at all:
		// Ask the user to correct the error:
		alert("please input a valid email");
		// But do nothing else:
  	return false;
	} else if (document.frm.myAction[1].checked) {
		// If the 'unsubscribe' option is checked, go to the unsubscribe page:
		window.open('http://www.masglobal.com/beautymark/test1.asp?myAction=Unsubscribe&Address='+document.frm.address.value,'new_win','left=240,top=140,width=360,height=280,toolbar=no,menubar=no,location=no,status=no,directories=no,scrollbars=no');
		return true;
	} else {
		// If the 'subscribe' options is checked, go to the subscribe page:
		window.open('http://www.masglobal.com/beautymark/test1.asp?Address='+document.frm.address.value,'new_win','left=240,top=140,width=360,height=280,toolbar=no,menubar=no,location=no,status=no,directories=no,scrollbars=no');
		return true;
	}
}


/*
	New function added by aviva for select & go drop down brands menu:
*/
/*
function jumpPage(newLoc){
	newPage = newLoc.options[newLoc.selectedIndex].value;
	if (newPage != "") 
		newPage = 'products/' + newPage;
		window.location.href= newPage;
}
*/


/*
	New jumpPage function very slightly rewritten (& commented) to help me debug errors/warnings caused by other js functions in the page [i.e. by not causing 'does not always return a value' warnings like the some of the DW ones...] :) 
	
	Nothing is really different about the function except that I've added the 'siteRoot' var. Given the value 'http://www.beautymark.ca/', the function should work unchanged no matter what page on the site calls it. Using the full 'http://www.beautymark.ca/' url will also allow the function to be tested locally (i.e. without uploading).
	
	Also, I suggest it be called more simply by:
	
	<select name='foo' onchange='jumpPage(this);'>
	
*/
function jumpPage(newLoc) {
	var siteRoot = 'http://beautymark.ca/';
	// Directory name:
	var newDir = 'products';
	// New page; the value corresponding to the selected option in the select element calling this function:
	var newPage = newLoc.options[newLoc.selectedIndex].value;
	if (!newPage) {
		// If the selected option has no value,
		// Do nothing:
		return false;
	} else {
		// Put together a new location out of the site root, the value, and the directory, and redirect to that page:
		window.location.href = siteRoot + newDir + '/' + newPage;
		return true;	
	}
}


function checkIt(){
	if (!validEmail(document.form1.sendto.value)) {
		alert("Your email address is invalid. Please correct and submit again");
		document.form1.sendto.focus();
		document.form1.sendto.select();
		return false;
	}
	if (!validEmail(document.form1.emailfrom.value)) {
		alert("Your email address in invalid. Please correct and submit again");
		document.form1.emailfrom.focus();
		document.form1.emailfrom.select();
		return false;
	}

	return true;
}


function validEmail(email){
	invalidChars = " /:,;";
	
	if (email == "") {			// cannot be empty
		return false;
	}
	for (i=0; i<invalidChars.length; i++) {	// does it contain any invalid characters?
		badChar = invalidChars.charAt(i);
		if (email.indexOf(badChar,0) > -1) {
			return false;
		}
	}
	atPos = email.indexOf("@",1)			// there must be one "@" symbol
	if (atPos == -1) {
		return false;
	}
	if (email.indexOf("@",atPos+1) != -1) {	// and only one "@" symbol
		return false;
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {					// and at least one "." after the "@"
		return false;
	}
	if (periodPos+3 > email.length)	{		// must be at least 2 characters after the "."
		return false;
	}
	return true;
}