Event.observe(window, 'load', init, false);

function init() {
allFields = Form.getElements("ajaxform");

for(i = 0; i < allFields.length; i++) {
	//$("formStatus").innerHTML = "";

	element = allFields[i].id +"_err";
	$(element).removeClassName('show')
}
}




function setFormStatus(value, msg){
	//$("formStatus").innerHTML = value;
	
	//if(msg !=""){
	//	$("formStatus").innerHTML = msg;
	//} else {
	//	$("formStatus").innerHTML = '<p class="msg">You have errors in your form. Please correct and re-submit</p>';
	//}
			$(value.id + "_err").show();
		$(value.id + "_err").addClassName('show')
	}
	
	
	
/*=====================
validation functions
======================*/
function isNotEmpty(value){
	//document.write("check empty");
	$(value.id+"_err").removeClassName('show')
isvalid = true;
if($F(value).length < 2){
	isvalid = false;
	setFormStatus(value,"");
}
return isvalid;
}


function isMatch(value){
	$(value.id+"_err").removeClassName('show')
matchField = value.id.gsub('_2', '');
isvalid = true;
if($F(value) != $F(matchField)){
isvalid = false;
setFormStatus(value,"");
}
return isvalid;
}

function isValidEmail(value){
	$(value.id+"_err").removeClassName('show')
	isvalid = false;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	isvalid = filter.test($F(value))
	if(!isvalid) setFormStatus(value,"");
	return isvalid;
}

function isAlpha(value){
isvalid = false;
$(value.id+"_err").removeClassName('show')
var filter =  /^[a-zA-z\s]+$/	;
	isvalid = filter.test($F(value))
	if(!isvalid) setFormStatus(value,"");
	return isvalid;
}

function isAlphaNumeric(value){
isvalid = false;
$(value.id+"_err").removeClassName('show')
var filter =   /^[a-zA-Z0-9]+$/	;
	isvalid = filter.test($F(value))
	if(!isvalid) setFormStatus(value,"");
	return isvalid;
}

function isNumeric(value){
isvalid = false;
$(value.id+"_err").removeClassName('show')
var filter =  /^[-]?\d+(\.\d+)?$/	;
	isvalid = filter.test($F(value))
	if(!isvalid) setFormStatus(value,"");
	return isvalid;
}

function isSecurityNum(value){
	isvalid = true;
$(value.id+"_err").removeClassName('show')



var filter =  /^[-]?\d+(\.\d+)?$/	;
	isvalid = filter.test($F(value))
	
	
if($F(value).length < 3) isvalid = false;
if(!isvalid) setFormStatus(value,"");
	return isvalid;

}

function isValidCCNum(value){
	isvalid = false;
$(value.id+"_err").removeClassName('show')
var filter =  /^((4\d{3})|(5[1-5]\d{2})|(6011))([- ])?\d{4}([- ])?\d{4}([- ])?\d{4}|3[4,7]\d{13}$/
	isvalid = filter.test($F(value))
	if(!isvalid) setFormStatus(value,"");
	return isvalid;
}


function isValidTel(value){
	tel = $F(value).replace(" ","");
	$(value.id+"_err").removeClassName('show')
	isvalid = false;
	var filter  = /^([0-9]{11})$/;
	isvalid = filter.test(tel)
	if(!isvalid) setFormStatus(value, "");
	return isvalid;
	
}



function isValidCard(value){
	var isvalid = true;
	var cardno = $F(value).replace(" ","");
	var strValidChars = "0123456789";
	var strChar;
	$(value.id+"_err").removeClassName('show')
	var isvalid = false;
	//return false if number less than 13 chars or higher than 16
	if(cardno.length < 13 || cardno.length > 18) return false
	//check is numeric
	 for (i = 0; i < cardno.length && isvalid == true; i++){
		  strChar = cardno.charAt(i);
		  if (strValidChars.indexOf(strChar) == -1) isvalid = false;
	 }
	 
	if(!isvalid) setFormStatus(value,"");
	return isvalid;
}


function isValidCardDate(){
	$("payment_cardStartMonth_err").removeClassName('show')
	$("payment_cardExpiryMonth_err").removeClassName('show');
	
	var today= new Date();
var thismonth= today.getMonth()+1;
var thisyear= today.getFullYear();
	//first check start date has been completed
	if($("payment_cardStartMonth").selectedIndex > 0 && $("payment_cardStartYear").selectedIndex >0){
	var sMonth = $F("payment_cardStartMonth");
	//check for valid start 
	if(sMonth > thismonth){
		$("payment_cardStartMonth_err").addClassName('show')
		isvalid = false;
	}
	}
	//check valid expiry
		var eMonth = $F("payment_cardExpiryMonth");
	var eYear = $F("payment_cardExpiryYear");
	if(eYear = thisyear && eMonth < thismonth){
		$("payment_cardExpiryMonth_err").addClassName('show')
		isvalid = false;
	
}
return isvalid;


}	



function isValidPC(value){
	$(value.id+"_err").removeClassName('show')
	isvalid = false;
	pc = $F(value).replace(" ","");
	var filter  = /^([A-Za-z]{1,2})([0-9]{2,3})([A-Za-z]{2})$/;
	isvalid = filter.test(pc)
	if(!isvalid) setFormStatus(value, "");
	return isvalid;
	}
	
function isValidPass(value){
	$(value.id+"_err").removeClassName('show')
	isvalid = true;
	if($F(value).length > 20 || $F(value).length < 8 || $F(value).indexOf(" ")> -1){
		isvalid = false;
		setFormStatus(value,"");
	}
	return isvalid;
}

function isSelected(value){
	$(value.id+"_err").removeClassName('show')
	isvalid = true;
	if(value.selectedIndex < 1){
	isvalid = false;
	setFormStatus(value,"");
	}
	return isvalid;
}

/*=====================
end validation functions
======================*/

function setAddress(){
	if($("customer_delivery").checked){
	$("address_address1_del").value = 	$("address_address1").value;
	$("address_address2_del").value = 	$("address_address2").value;
	$("address_towncity_del").value = 	$("address_towncity").value;
	$("address_county_del").value = 	$("address_county").value;
	$("address_postcode_del").value = 	$("address_postcode").value;
	$("address_country_del").selectedIndex = $("address_country").selectedIndex;
} else {
$("address_address1_del").value = 	"";
	$("address_address2_del").value = 	"";
	$("address_towncity_del").value = 	"";
	$("address_county_del").value = 	"";
	$("address_postcode_del").value = "";
	$("address_country_del").selectedIndex = 0;
}
}


function checkTerms(){
	
	if($("payment_terms").checked){
		return true
	} else {
		alert("Please agree to the terms and conditions of purchase");
		return false
	}
	
}


validateForm = function(formid){
	var isvalid = true;
	allFields = Form.getElements(formid);
	
for(i = 0; i < allFields.length; i++) {
	
//first check field has value or is required
	if($(allFields[i]).hasClassName("required") || $F(allFields[i]) > 0 ||  $F(allFields[i]) != 0){
		
		
if($(allFields[i]).hasClassName('validemail')){
	isvalid = isValidEmail(allFields[i]);


} else if ($(allFields[i]).hasClassName('notempty')){
	isvalid = isNotEmpty(allFields[i]);


} else if ($(allFields[i]).hasClassName('validpc')){
	isvalid = isValidPC(allFields[i]);


} else if ($(allFields[i]).hasClassName('validtel')){
	isvalid = isValidTel(allFields[i]);

} else if ($(allFields[i]).hasClassName('match')){
	isvalid = isMatch(allFields[i]);
	
} else if ($(allFields[i]).hasClassName('validpass')){
	isvalid = isValidPass(allFields[i]);

} else if ($(allFields[i]).hasClassName('isselect')){
	isvalid = isSelected(allFields[i]);
	

} else if ($(allFields[i]).hasClassName('isvalidcc')){
	isvalid = isValidCard(allFields[i]);
	
} else if ($(allFields[i]).hasClassName('validcarddate')){
	isvalid = isValidCardDate();
	
} else if ($(allFields[i]).hasClassName('isnumeric')){
	isvalid = isNumeric(allFields[i]);

} else if ($(allFields[i]).hasClassName('issecnum')){
	isvalid = isSecurityNum(allFields[i]);
	
} else {
// do nothing
}
	}
	
}
return isvalid;
}








submitContactForm = function(){
	
	isvalid = validateForm("ajaxform");
if(isvalid){
var params = Form.serialize($('ajaxform'));
//document.write(params);
var url = 'ajax/ajax.contact.php';
$('ajaxform').disable();
$("formStatus").innerHTML = '<p class="error show">Sending message - please wait a moment</p>';
newAjax = sendRequest(url,params,contactSuccess,contactError);
}
}


submitLoginForm = function(id){
	isvalid = validateForm("ajaxform");
	
	if(isvalid){
		
	var params = Form.serialize($('ajaxform'));
	var url = 'ajax/ajax.login.php';
	$('ajaxform').disable();
$("formStatus").innerHTML = '<p class="error show">Signing in - Please wait</p>';
if(id==1){
newAjax = sendRequest(url,params,loginSuccess,loginError);
} else {
	newAjax = sendRequest(url,params,loginSuccessPod,loginError);
}

}
}



/*=====================
ajax handler functions
======================*/


sendRequest = function(url,params,hf,ef){
	//alert("send");
new Ajax.Request(
	url,
	{
		method: 'post',
		parameters: params,
		onSuccess: hf,
		onFailure: ef
		});
}



loginSuccess = function (t){
	//alert(t.responseText);
if(t.responseText == "error"){
	loginError;
} else if (t.responseText == 0){
	$("formStatus").innerHTML = '<p class="error show">Login not found</p>';
	$('ajaxform').enable();
	$('ajaxform').focusFirstElement();
} else {
	location.href="gallery.php";
}
}

loginSuccessPod = function (t){
	//alert(t.responseText);
if(t.responseText == "error"){
	loginError;
} else if (t.responseText == 0){
	$("formStatus").innerHTML = '<p class="error show">Login not found</p>';
	$('ajaxform').enable();
	$('ajaxform').focusFirstElement();
} else {
	location.href="client.php";
}
}


loginError = function(t){
	
	
}




contactSuccess = function(t){
		//alert(t.responseText);
	//show success message
if(t.responseText == "error"){
	contactError;
} else {
	$('formwrap').innerHTML = '<div class="tile"><h3>Thank you for your enquiry</h3><p>We have received your message and will respond shortly</p><p><a href="index.php" class="withArrow" title="Home">Continue</a></p></div>';

}
}

contactError = function(){
	$('formwrap').innerHTML = '<h3>Sorry, at this time we are unable to accept your message due to a server error.</h3><p>Please try again later, or contact our customer services department</p><p>[Try Again]</p>';
}


passwordSuccess = function(t){
	//alert("back");
	//show success message
if(t.responseText == 0){
	contactError;
} else if(t.responseText == 2){
	$("formStatus").innerHTML = '<p class="msg">Email Address not found - please try again</p>';
	Form.reset('ajaxform');
	$('ajaxform').enable();
	$('ajaxform').focusFirstElement();
} else {
	$('formwrap').innerHTML = '<h2>Thank you for your request</h2><p>We have forwarded you an email with a reminder of your Jupiter Blue account details.</p><p><a href="index.php" class="withArrow" title="Home">Continue</a></p>';

}
}

passwordError = function(){
	$('formwrap').innerHTML = '<h3>Sorry, at this time we are unable to accept your request due to a server error.</h3><p>Please try again later, or contact our customer services department</p><p>[Try Again]</p>';
}
