// jsForms.js

function formEmailAFriend(){
	replaceButton();
	var msg = "Fill out the form below and we will send your friend an email telling him about this product:";
	resetForm('friend_form',msg);
	empty = false;
	fm 	= document.friend_form;
	checker(fm.from_name);
	checker(fm.to_name);
	checker(fm.email);
	email_validator(fm.email)
	if (empty == false){
		document.friend_form.submit()
	}else{
		document.getElementById('form_buttons').innerHTML = "<a href='#' onclick='formEmailAFriend()' class='link_102'>Send</a>";
	}
}

function formBeatThePrice(){
	var msg = "Fill out the form below and we will try our best to beat it:";
	resetForm('beat_form',msg);
	empty = false;
	fm 	= document.beat_form;
	checker(fm.name);
	if(fm.email2.value=="" && fm.tel.value==""){
			if(empty == false){
			document.getElementById('prompt_text').innerHTML = "An error has occured, please ensure all the information you have entered is correct:";
			document.getElementById('prompt_text').className = "label_warning";
		}
		fm.email2.className = "input_warning";
		fm.tel.className = "input_warning"
		empty=true;
	}
	if(fm.price.value.length <= 1){
			if(empty == false){
			document.getElementById('prompt_text').innerHTML = "An error has occured, please ensure all the information you have entered is correct:";
			document.getElementById('prompt_text').className = "label_warning";
		}
		fm.price.className = "input_warning";
		empty=true;
	}
	checker(fm.website);
	if(fm.email2.value!=""){
		email_validator(fm.email2);
	}
	if (empty == false){
		document.beat_form.submit()
	}else{
		document.getElementById('form_buttons').innerHTML = "<a href='#' onclick='formBeatThePrice()' class='link_102'>Send</a>";
	}	
}

function formAddReview(){
	replaceButton();
	var msg = "Fill out the form below and your review will be available for everyone to read:";
	resetForm('review_form',msg);
	empty = false;
	fm 	= document.review_form;
	checker(fm.name);
	lbl_checker(fm.rating,'rating_num');
	checker(fm.review);
	if (empty == false){
		document.review_form.submit()
	}else{
		document.getElementById('form_buttons').innerHTML = "<a href='#' onclick='formAddReview()' class='link_102'>Send</a>";
	}
}

function replaceButton(){
	document.getElementById('form_buttons').innerHTML = "<img src='/images/ukb_sending.gif' alt='Sending' />";
}

function resetForm(obj,msg){
	
	var inputs = document.getElementById(obj).getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++) {
		inputs[i].className = '';
	}
	document.getElementById('prompt_text').innerHTML = msg;
	document.getElementById('prompt_text').className = "";
}

function checker(obj){
	if(obj.value==""){
		if(empty == false){
			document.getElementById('prompt_text').innerHTML = "An error has occured, please ensure all the information you have entered is correct:";
			document.getElementById('prompt_text').className = "label_warning";
		}
		obj.className = "input_warning";
		empty=true;
	}
}

function lbl_checker(obj,id){
	if(obj.value==""){
		if(empty == false){
			document.getElementById('prompt_text').innerHTML = "An error has occured, please ensure all the information you have entered is correct:";
			document.getElementById('prompt_text').className = "label_warning";
		}
		document.getElementById(id+'_lbl').className = "label_warning";
		empty=true;
	}
}

function alterPayment(obj){
	//alert(document.forms['payment_option'].action)
	if(obj=="sec"){
		document.forms['payment_option'].action = "https://www.secpay.com/java-bin/ValCard";
		document.getElementById('payment_type').value = "sec";
	}else{
		document.forms['payment_option'].action = "/shop/print.html";
	}
	if(obj=="cheque"){
		document.getElementById('payment_type').value = "cheque";
	}
	if(obj=="tel"){
		document.getElementById('payment_type').value = "tel";
	}
}

function viewShipping(){
	if(document.getElementById('shipping_details').style.display == "none"){
		document.getElementById('shipping_details').style.display = "block";
	}else{
		document.getElementById('shipping_details').style.display = "none";
	}	
}
	
//Email Validator Functions
function email_validator(obj){
	var emailStr=obj.value;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) && empty==false){message("Email address seems incorrect (check @ and .'s)",obj)}
	if (empty==false){var user=matchArray [1];var domain=matchArray [2];if (user.match(userPat)==null){message("The username doesn't seem to be valid.",obj)}}
	if (empty==false){var IPArray=domain.match(ipDomainPat);if (IPArray!=null){for (var j=1;j<=4;j++){if (IPArray[j]>255){message("Destination IP address is invalid!",obj)}}}}
	if (empty==false){var domainArray=domain.match(domainPat);if (domainArray==null){message("The domain name doesn't seem to be valid.",obj)}}
	if (empty==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){message("The address must end in a three-letter domain, or two letter country.",obj)}}
	if ((len<2) && empty==false){message("This address is missing a hostname!",obj)}
}
	
function message(msg,obj){
	if(empty == false){
			document.getElementById('prompt_text').innerHTML = "An error has occured, please ensure all the information you have entered is correct:";
	}
	document.getElementById('prompt_text').className = "label_warning";
	obj.className = "input_warning"
	empty=true;
}