/******************************************/
/* Coded with: Macromedia Dreamweaver 8   */
/* File Name: LoginCheckingFunctions.js   */
/* Created By: PricelessSurveys.com       */
/* Created: Jan. 14, 2006                 */
/* Last Modified: Mar. 1, 2006            */
/* Comment: this is only for free-surveys */
/*          section                       */
/******************************************/


// bust frames - make this at top
if (window != top) { top.location.href=location.href; }

// check login
function checkLogin()
{
	// give out proper errors
	var errors = "";
	errors += checkLoginEmail(document.login.useremail.value);
	errors += checkLoginPassword(document.login.password.value);

	if (errors != "")
	{
		alert("Login Error! Please fix the following:\n\n" + errors);
		document.login.useremail.focus();
		return false;
	}
	return true;
}
// check login
function checkLogin2()
{
	// give out proper errors
	var errors = "";
	errors += checkLoginEmail(document.login2.useremail.value);
	errors += checkLoginPassword(document.login2.password.value);

	if (errors != "")
	{
		alert("Login Error! Please fix the following:\n\n" + errors);
		document.login2.useremail.focus();
		return false;
	}
	return true;
}
// check forgot
function checkForgot()
{
	// give out proper errors
	var errors = "";
	errors += checkLoginEmail(document.forgot.forgotemail.value);

	if (errors != "")
	{
		alert("Your password cannot be sent!\n" + errors);
		document.forgot.forgotemail.focus();
		return false;
	}
	return true;
}
// check useremail for login
function checkLoginEmail (useremail)
{
	var error = "";

	// check to see if email is in valid generic form
	if(isGenericEmail(useremail))
	{
		return error;
	}
	else
	{
		// if not, place error in string for output
		error = "You entered an invalid Email Address!\n";
		//document.login.useremail.focus();
		return error;
	}
}
// check password for login
function checkLoginPassword (password)
{
	var error = "";
	if ((password.length < 4) || (password.length > 20))
	{
		error = "Your Password must be 4-20 characters.\n";
	}
	return error;
}


// check new form (register)
function checkRegister()
{
	var errors = "";
	errors += checkFirst(document.register.first_name.value);
	errors += checkLast(document.register.last_name.value);
	errors += checkEmail(document.register.email.value);
//	errors += checkEmailMatch(document.register.email.value,document.register.email2.value);
//	errors += checkCountryZip(document.register.country.selectedIndex,document.register.zipcode.value);
	errors += checkCountryZip(document.register.country.value,document.register.zipcode.value);
	errors += checkDOB(document.register.bmonth.selectedIndex,document.register.bday.selectedIndex,document.register.byear.selectedIndex);
	errors += checkPassword(document.register.password.value);
	errors += checkPasswordMatch(document.register.password.value, document.register.password2.value);

	if (errors != "")
	{
		alert("Oops! Please fix the following errors:\n\n" + errors);
		return false;
	}
    return true;
}
// check edit profile
function checkEdit()
{
	var errors = "";
	errors += checkFirst(document.edit.first_name.value);
	errors += checkLast(document.edit.last_name.value);
	errors += checkEmail(document.edit.email.value);
	errors += checkCountryZip(document.edit.country.value,document.edit.zipcode.value);
	errors += checkPassword(document.edit.password.value);

	if (errors != "")
	{
		alert("Oops! Please fix the following errors:\n\n" + errors);
		return false;
	}
    return true;
}
// in register: check firstname
function checkFirst (firstname)
{
	var error = "";
	// check to see if firstname is in valid generic form
	if(isGenericName(firstname))
	{
		return error;
	}
	else
	{
		// if not, place error in string for output
		error = "You forgot to enter your First Name.\n";
		return error;
	}
}
// in register: check lastname
function checkLast (lastname)
{
	var error = "";
	// check to see if lastname is in valid generic form
	if(isGenericName(lastname))
	{
		return error;
	}
	else
	{
		// if not, place error in string for output
		error = "You forgot to enter your Last Name.\n";
		return error;
	}
}
// in register: check email
function checkEmail (email)
{
	var error = "";

	// check to see if email is in valid generic form
	if(isGenericEmail(email))
	{
		return error;
	}
	else
	{
		// if not, place error in string for output
		error = "You did not enter a valid Email Address.\n";
		return error;
	}
}
// in register: check email match (email and email2)
function checkEmailMatch (email,email2)
{
	var error = "";
	if (email != email2)
	{
		error = "Your Email Addresses do not match.\n";
	}
	return error;
}
// in register: check password
function checkPassword (password)
{
	var error = "";
	if ((password.length < 4) || (password.length > 20))
	{
		error = "Your Password must be 4-20 characters long.\n";
	}
	return error;
}
// in register: check password match (password and password2)
function checkPasswordMatch(password,password2)
{
	var error = "";
	if (password != password2)
	{
		error = "Your Password entries do not match.\n";
	}
	return error;
}
// in register: check country and zipcode
function checkCountryZip (country, zip)
{
	var error = "";
	var valid_zip_AU = /^([0-9]{4})+$/;
	var valid_zip_CA = /^([A-Za-z0-9]{6})+$/;
	var valid_zip_NZ = /^([0-9]{4})+$/;
	var valid_zip_UK = /^([A-Za-z0-9 ]{6,8})+$/;
	var valid_zip_US = /^([0-9]{5})+$/;
	
	// for Australia
	if (country == "AU" && zip.length != 4)
	{
		error = "Your Zip Code must be four digits long.\n";
		return error;
	}
	// for Canada
	if (country == "CA" && zip.length != 6)
	{
		error = "Your Zip Code must be six characters long.\n";
		return error;
	}
	// for New Zealand
	if (country == "NZ" && zip.length != 4)
	{
		error = "Your Zip Code must be four digits long.\n";
		return error;
	}
	// for UK
	if (country == "UK" && (zip.length < 6 || zip.length > 8))
	{
		error = "Your Zip Code must be six to eight characters long.\n";
		return error;
	}
	// for US
	if (country == "US" && zip.length != 5)
	{
		error = "Your Zip Code must be five digits long.\n";
		return error;
	}
	return error;
}
// in register: check birthday
function checkDOB (bmonth,bday,byear)
{
	var error = "";
	if (bmonth == 0 || bday == 0 || byear == 0)
	{
		error = "You forgot to select your Birthday.\n";
	}
	return error;
}


// not being used yet... need to incorporate coreg links
function showHide(){

	if (document.getElementById) {
//		path1 = document.getElementById('coreg1').style;
		path2 = document.getElementById('coreg2').style;
	}
	else if (document.all) {
//		path1 = document.all.coreg1.style;
		path2 = document.all.coreg2.style;
	}

	if (document.forms['join'].elements['lstCountry'].selectedIndex != 0) {
//			path1.display = "block";
			path2.display = "block";
	 }

}


function bringFocus(){
	self.focus();
}

var	timerID=setTimeout('bringFocus()',10000);



// check if email is a correct, generic version
function isGenericEmail(email)
{
	var email_str;  // e-mail string
	// var emstr_confirm;
	var at_position;    // '@' position in e-mail
	var dot_position;    // '.' position in e-mail
	var invalid_symbol = '"' + " :*'!%+=;,/#<>\\"; // these characters are not allowed

	// first let's make email lowercase
	email_str = email.toLowerCase();
	// find positions of @ and . symbols
	at_position = email_str.indexOf('@');
	dot_position = email_str.indexOf('.');

	// let's check to make sure we have "text@text.text" as our email form
	if ((at_position == -1) || (dot_position == -1))
	{
		return false;
	}
	if ((at_position == 0) || (dot_position == 0))
	{
		return false;
	}
	if ((email_str.charAt(email_str.length-1) == '@') || (email_str.charAt(email_str.length-1) == '.'))
	{
		return false;
	}
	if (email_str.split('@').length != 2)
	{
		return false;
	}
	if ((email_str.split('@')[1].indexOf('.') == -1) || (email_str.split('@')[1].indexOf('.') == 0))
	{
		return false;
	}

	// check if there is any invalid symbol in the email field
	for (i=0; i<email_str.length; i++)
	{
		if (invalid_symbol.indexOf(email_str.charAt(i)) != -1)
		{
			return false;
		}
	}
	
	// if everything is ok, let's return that it's a correct generic email
	return true;
}

// check for correct chars in generic name field
function isGenericName(name)
{
	// declare local vars
	var name_to_check = name;
	var valid_symbol = /^([A-Za-z]{1})+([A-Za-z ]{0,71})+$/;
	// check if there is any invalid symbol in name field
	if (!valid_symbol.test(name_to_check))
	{
		return false;
	}
	else
	{
		return true;
	}
}

// check for correct chars in generic name field
function isGenericName(name)
{
	// declare local vars
	var name_to_check = name;
	var valid_symbol = /^([A-Za-z]{1})+([A-Za-z ]{0,71})+$/;
	// check if there is any invalid symbol in name field
	if (!valid_symbol.test(name_to_check))
	{
		return false;
	}
	else
	{
		return true;
	}
}
