/*
REGISTER.JS
Author : Martin Chaput
*/

function Validate(f) 
{
    
	var ary1 = new Array('Email Address','Check Payable To','Address Line 1','City','Zip/Postal Code','Password'); 
	var ary2 = new Array(f.email.value, f.name_payout.value, f.address_line1.value, f.city.value, f.zip.value, f.password.value);
	var ary3 = new Array('email', 'name_payout', 'address_line1', 'city', 'zip', 'password');
	
	
	
	//check if all fields are filled
	for(i=0; i<ary2.length; i++) {
		var check1=IsEmpty(ary2[i]);
		if(!check1) {
			alert("The field '"+ ary1[i]+ "' can not be empty." );
			return false;
		}
	}
	
	
	//check if we have a valid email
	var flag=valid_email(ary2[0],ary1[0]);
	if (!flag) return false;
	
	
	//check that one state has been selected
	var flag=isSelected(f.state);
	if (!flag) 
	{
		alert("Please select a state/province from the list");
		document.data_form.state.focus();
		return false;
	}
	
	
	//check that one country has been selected
	var flag=isSelected(f.country);
	if (!flag) 
	{
		alert("Please select a country from the list");
		document.data_form.country.focus();
		return false;
	}
	
	
	//check if we have a valid zip for USA & Canada
	var flag=valid_zip(ary2[6],ary1[6]);
	if (!flag) return false;
	
	
	//check that we have a valid password
	var flag=valid_pwd(ary2[7],ary1[7]);
	if (!flag) return false;
	
	
	//check that webmaster has agreed to terms & conditions
	var flag=f.checkbox_terms.checked;
	if (!flag)
	{
		alert("You must read & agree to the \n Terms & Conditions to continue.");
		f.checkbox_terms.focus();
		return false;
	}
	
	
	return true;
}



function valid_email(val, desc) {
	re=/^[0-9a-z]([-_\.]?[0-9a-z])*@[0-9a-z]([-_\.]?[0-9a-z])*\.[a-z]{2,3}$/i;

	spaces = / /gi;
	val = val.replace(spaces, "");
	
	if(re.test(val)) 
	{
		re2=/^[0-9a-z]([-_\.]?[0-9a-z])*@(hotmail|yahoo)\..{1,}$/i;
		if(re2.test(val)) 
		{
			alert("Sorry, due to anti-spamming regulations, we can't accept 'hotmail' or 'yahoo' emails.\n");
			document.data_form.email.select();
			document.data_form.email.focus();
			return false;
		}
		else
		{
			return true;
		}
	} 
	else 
	{
		alert("Invalid '"+ desc + "'.\n");
		document.data_form.email.select();
		document.data_form.email.focus();
		return false;
	}
}



function valid_pwd(val, desc) 
{
	re=/^[a-z0-9A-Z]{4,15}$/;
	if(re.test(val)) {
		return true;
	} else {
		alert("Invalid '"+ desc + "',\nShould only contain alphanumeric characters.\nMin 4, max 15"); 
		document.data_form.password.select();
		document.data_form.password.focus();
		return false;
	}
}



function valid_zip(val,desc)
{
	//check for United States
	//if (document.data_form.country.options[document.data_form.country.selectedIndex].value == "840")
	//{
//		if (val.length != 5 && val.length != 9 )
	//	{
		//	alert("Invalid '"+ desc + "'.\n");
			//document.data_form.zip.select();
//			document.data_form.zip.focus();
	//		return false;
		//}
//		else
	//	{
			return true;
		//}
//	}
	
	//check for Canada
//	if (document.data_form.country.options[document.data_form.country.selectedIndex].value == "124")
	//{
//		regexp=/^([a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}[\s]*[-]*[\.]*[0-9]{1}[a-zA-Z]{1}[0-9]{1}){1}$/i;
	//	if(regexp.test(val))
		//{
//			return true;
	//	}
		//else
//		{
	//		alert("Invalid '"+ desc + "'.\n");
		//	document.data_form.zip.select();
			//document.data_form.zip.focus();
//			return false;
	//	}
//	}
	//else
//	{
	//	return true;
//	}
}



function IsEmpty(field) 
{
	if(field=='') 
	{
		return false;
	} 
	else 
	{   
		return true;
	}
}



function isSelected(fieldObject)
{
	if (!fieldObject.options[fieldObject.selectedIndex].value) { return false; }
	else { return true;}
}
