// focusField(thisField) and blurField(thisField) creates pink border around selected field
function focusField(thisField)
{
	thisField.className = "fieldFocus";
}
function blurField(thisField)
{
	thisField.className = "";
}

// isValidEmail(emailStr) Validates email address in form
function isValidEmail(emailStr)
{
	/*var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray == null)
	{
		return false;
	}
	
	var user = matchArray[1];
	var domain = matchArray[2];
	
	if (user.match(userPat) == null)
	{
		 return false;
	}
	
	var IPArray = domain.match(ipDomainPat);
	
	if (IPArray != null)
	{
		for (var i=1;i<=4;i++)
		{
			if (IPArray[i]>255)
			{
				return false;
			}
		}
		return true;
	}
	
	var domainArray = domain.match(domainPat);
	if (domainArray == null)
	{
		return false;
	}
	
	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)
	{
		return false;
	}
	
	if (len<2)
	{
		return false;
	}
	
	return true;*/
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(emailStr) == false)
	{
		return false;
	}
	else
	{
		return true;
	}
}

// showErrorField(getField) Changes fieldname to error style if validation fails
function showErrorField(getField)
{
	var thisField = document.getElementById(getField);
	thisField.className = "fieldError";
}

// shippingSame() Fills shipping address with billing details
function shippingSame()
{
	if (document.getElementById('shipping_same').checked == true)
	{
		getHouse = document.getElementById('house_no').value;
		getStreet = document.getElementById('street').value;
		getTown = document.getElementById('town').value;
		getCounty = document.getElementById('county').value;
		getPostcode = document.getElementById('postcode').value;
		
		document.getElementById('house_no2').value = getHouse;
		document.getElementById('street2').value = getStreet;
		document.getElementById('town2').value = getTown;
		document.getElementById('county2').value = getCounty;
		document.getElementById('postcode2').value = getPostcode;
	}
}

// replicateShipping(thisField,newField) Automatically fills shipping fields with billing field values as they are typed
function replicateShipping(thisField,newField)
{
	signupForm = document.getElementById('signupForm');
	if (signupForm.shipping_same.checked == true)
	{
		document.getElementById(newField).value = document.getElementById(thisField).value;
	}
}

// checkSignUp() Validates form information before database checking
function checkSignUp()
{
	var error = 0;
	var errorList = "";
	
	getFirstname = document.getElementById('firstname').value;
	getSurname = document.getElementById('surname').value;
	DD = document.getElementById('dd').selectedIndex;
	getDD = document.getElementById('dd').options[DD].value;
	MM = document.getElementById('mm').selectedIndex;
	getMM = document.getElementById('mm').options[MM].value;
	YYYY = document.getElementById('yyyy').selectedIndex;
	getYYYY = document.getElementById('yyyy').options[YYYY].value;
	getDOB = getDD+"_"+getMM+"_"+getYYYY;
	getSex = document.getElementById('sex').selectedIndex;
	getSex = document.getElementById('sex').options[getSex].value;
	getHouse = document.getElementById('house_no').value;
	getStreet = document.getElementById('street').value;
	getTown = document.getElementById('town').value;
	getCounty = document.getElementById('county').value;
	loc = document.getElementById('location').selectedIndex;
	getLocation = document.getElementById('location').options[loc].value;
	getPostcode = document.getElementById('postcode').value;
	if (document.getElementById('shipping_same').checked == true)
	{
		getShippingSame = "y";
	}
	else
	{
		getShippingSame = "n";
	}
	getHouse2 = document.getElementById('house_no2').value;
	getStreet2 = document.getElementById('street2').value;
	getTown2 = document.getElementById('town2').value;
	getCounty2 = document.getElementById('county2').value;
	loc2 = document.getElementById('location2').selectedIndex;
	getLocation2 = document.getElementById('location2').options[loc2].value;
	getPostcode2 = document.getElementById('postcode2').value;
	getHomeTel = document.getElementById('home_phone').value;
	getMobileTel = document.getElementById('mobile_phone').value;
	getEmail = document.getElementById('email').value;
	getPassword = document.getElementById('password').value;
	
	getErrorDiv = document.getElementById('signupErrors');
	getErrorDiv.style.display = "none";
	
	if (getFirstname == "")
	{
		error = 1;
		showErrorField("firstname");
		errorList = errorList+"<li>Please enter your first name</li>";
	}
	if (getSurname == "")
	{
		error = 1;
		showErrorField("surname");
		errorList = errorList+"<li>Please enter your surname</li>";
	}
	if (document.getElementById('dd').selectedIndex == 0)
	{
		error = 1;
		showErrorField("dd");
		errorList = errorList+"<li>Please select the day you were born</li>";
	}
	if (document.getElementById('mm').selectedIndex == 0)
	{
		error = 1;
		showErrorField("mm");
		errorList = errorList+"<li>Please select the month you were born</li>";
	}
	if (document.getElementById('yyyy').selectedIndex == 0)
	{
		error = 1;
		showErrorField("yyyy");
		errorList = errorList+"<li>Please select the year you were born</li>";
	}
	if (getHouse == "")
	{
		error = 1;
		showErrorField("house_no");
		errorList = errorList+"<li>Please enter the house number/name of your billing address</li>";
	}
	if (getStreet == "")
	{
		error = 1;
		showErrorField("street");
		errorList = errorList+"<li>Please enter the street name of your billing address</li>";
	}
	if (getTown == "")
	{
		error = 1;
		showErrorField("town");
		errorList = errorList+"<li>Please enter the town of your billing address</li>";
	}
	if (getCounty == "")
	{
		error = 1;
		showErrorField("county");
		errorList = errorList+"<li>Please enter the county of your billing address</li>";
	}
	if (getPostcode == "")
	{
		error = 1;
		showErrorField("postcode");
		errorList = errorList+"<li>Please enter the postcode of your billing address</li>";
	}
	if (getHouse2 == "")
	{
		error = 1;
		showErrorField("house_no2");
		errorList = errorList+"<li>Please enter the house number/name of your shipping address</li>";
	}
	if (getStreet2 == "")
	{
		error = 1;
		showErrorField("street2");
		errorList = errorList+"<li>Please enter the street name of your shipping address</li>";
	}
	if (getTown2 == "")
	{
		error = 1;
		showErrorField("town2");
		errorList = errorList+"<li>Please enter the town of your shipping address</li>";
	}
	if (getCounty2 == "")
	{
		error = 1;
		showErrorField("county2");
		errorList = errorList+"<li>Please enter the county of your shipping address</li>";
	}
	if (getPostcode2 == "")
	{
		error = 1;
		showErrorField("postcode2");
		errorList = errorList+"<li>Please enter the postcode of your shipping address</li>";
	}
	if (getHomeTel == "")
	{
		error = 1;
		showErrorField("home_phone");
		errorList = errorList+"<li>Please enter your home telephone number</li>";
	}
	if ((getEmail == "") || (!isValidEmail(getEmail)))
	{
		error = 1;
		showErrorField("email");
		errorList = errorList+"<li>Please enter a valid email address</li>";
	}
	if ((document.getElementById('confirm_email').value == "") || (document.getElementById('confirm_email').value != document.getElementById('email').value))
	{
		error = 1;
		showErrorField("confirm_email");
		errorList = errorList+"<li>Your email confirmation does not match your email. Please check.</li>";
	}
	getPassword = document.getElementById('password').value;
	if ((getPassword == "") || (getPassword.length < 6))
	{
		error = 1;
		showErrorField("password");
		errorList = errorList+"<li>Please choose a password of at least 6 characters for your account</li>";
	}
	if ((document.getElementById('confirm_password').value == "") || (document.getElementById('confirm_password').value != document.getElementById('password').value))
	{
		error = 1;
		showErrorField("confirm_password");
		errorList = errorList+"<li>Your password confirmation does not match your password. Please check.</li>";
	}
	
	if (error == 1)
	{
		getList = document.getElementById('errorList');
		getList.innerHTML = errorList;
		getErrorDiv.style.display = "block";
	}
	else
	{
		ajaxValidate();
	}
}

// checkEnquiry()
function checkEnquiry()
{
	var error = 0;
	var errorList = "";
	
	contactForm = document.getElementById('contactForm');
	getName = contactForm.name.value;
	getEmail = contactForm.email.value;
	getEnquiry = contactForm.enquiry.value;
	
	getErrorDiv = document.getElementById('signupErrors');
	getErrorDiv.style.display = "none";
	
	if (getName == "")
	{
		error = 1;
		showErrorField("name");
		errorList = errorList+"<li>Please enter your name</li>";
	}
	if ((getEmail == "") || (!isValidEmail(getEmail)))
	{
		error = 1;
		showErrorField("email");
		errorList = errorList+"<li>Please enter a valid email address</li>";
	}
	
	if (error == 0)
	{
		contactForm.submit();
	}
}

// ajaxValidate() Sends form values to ajax page for further validation and database checking
function ajaxValidate()
{
	newUrl = "ajax/sign_up.php?firstname="+getFirstname+"&surname="+getSurname+"&dob="+getDOB+"&sex="+getSex+"&house="+getHouse+"&street="+getStreet+"&town="+getTown+"&county="+getCounty+"&location="+getLocation+"&postcode="+getPostcode+"&shippingsame="+getShippingSame+"&house2="+getHouse2+"&street2="+getStreet2+"&town2="+getTown2+"&county2="+getCounty2+"&location2="+getLocation2+"&postcode2="+getPostcode2+"&hometel="+getHomeTel+"&mobiletel="+getMobileTel+"&email="+getEmail+"&password="+getPassword;
	throbberDiv = document.getElementById('ajaxLoader');
	throbberDiv.style.display = "block";
	sendRequest(newUrl,validateResponse);
}

// validateResponse() Returns success/error messages based on validation passing/failing
function validateResponse()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{
		throbberDiv = document.getElementById('ajaxLoader');
		throbberDiv.style.display = "none";
		
		response = xmlHttp.responseText;
		if (response.indexOf("error") == -1)// if no error found, show success text
		{
			getHolder = document.getElementById('formHolder');
			getHolder.innerHTML = "<p><strong>Thank you for signing up with Designer Desires!</strong></p><p>A confirmation email has been sent to the email address supplied. You must click on the link in the email before your account is fully activated.</p>";
		}
		else
		{
			errorArray = response.split("=");
			errorType = errorArray[1];
			showErrorField("email");
			getList = document.getElementById('errorList');
			if (errorType == "invalid")// if invalid email address found, show error
			{
				getList.innerHTML = "<li>The email address you have supplied is in an invalid format. Please check.</li>";
				getErrorDiv.style.display = "block";
			}
			else if (errorType == "exists")// if email already exists in accounts
			{
				getList.innerHTML = "<li>The email address you have supplied has already been used in a registererd account. If you have already set up an account using this email address, but have forgotten the password to log in, <a href=\"javascript:passwordReminder()\">click here</a> so we can send you a new password by email.</li>";
				getErrorDiv.style.display = "block";
			}
		}
	}
}

// passwordReminder() Requests an email to be sent to the supplied address with a password reminder
function passwordReminder()
{
	newUrl = "ajax/password_reminder.php?email="+getEmail;
	throbberDiv = document.getElementById('ajaxLoader');
	throbberDiv.style.display = "block";
	sendRequest(newUrl,reminderResponse);
}

// reminderResponse() Return success message if email sent
function reminderResponse()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{
		throbberDiv = document.getElementById('ajaxLoader');
		throbberDiv.style.display = "none";
		
		getHolder = document.getElementById('formHolder');
		getHolder.innerHTML = "<p>An email containing a new password for this account has been sent to <strong>"+getEmail+"</strong>.</p>";
	}
}
