// 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,"Your Name");
	checker(fm.to_name,"Friend's Name");
	checker(fm.email,"Friend's Email Address");
	email_validator(fm.email)
	if (empty == false){
		document.friend_form.submit()
	}else{
		document.getElementById('form_buttons').innerHTML = "<a href='#' onclick='formEmailAFriend()' class='link_97'>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 = fm.email2.className + " input_warning";
		fm.tel.className = 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 = 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_97'>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_97'>Add Review</a>";
	}
}

function replaceButton(){
	document.getElementById('form_buttons').innerHTML = "<img src='/images/c4b_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 = inputs[i].className.replace(" input_warning", "");
	}
	document.getElementById('prompt_text').innerHTML = msg;
	document.getElementById('prompt_text').className = "";
}

function checker(obj,inputdefault){
	if(obj.value=="" || obj.value==inputdefault){
		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 = 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";
		}
		if(id != "rating_num"){
			document.getElementById(id+'_lbl').className = "label_warning";
		}
		empty=true;
	}
}
	
//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 = obj.className + " input_warning"
	empty=true;
}

function change_price(){
	if(document.getElementById("bed_sizes")){
		base_price = document.getElementById("bed_sizes").value
		base_price = base_price.split("::")
		base_price = base_price[2]
	}else
		base_price = document.getElementById("base_price").value
	new_price = parseFloat(base_price)
	if(document.getElementById("divans")){
		divan_price = document.getElementById("divans").value
		divan_price = divan_price.split("::")
		divan_price = divan_price[1]
		if(divan_price)
			new_price += parseFloat(divan_price)
	}
	if(document.getElementById("divan_bases")){
		divan_base_price = document.getElementById("divan_bases").value
		divan_base_price = divan_base_price.split("::")
		divan_base_price = divan_base_price[1]
		if(divan_base_price)
			new_price += parseFloat(divan_base_price)
	}
	num = document.getElementById("quantity").value
	document.getElementById("show_price").innerHTML = number_format(new_price*num)
}

function number_format(num){
	num="" + Math.floor(num*100.0 + 0.5)/100.0;
	var i=num.indexOf(".");
	if(i<0)
		num+=".00";
	else{
		num=num.substring(0,i) + "." + num.substring(i + 1);
		i=(num.length - i) - 1;
		if(i==0)
			num+="00";
		else if(i==1)
			num+="0";
		else if(i>2)
			num=num.substring(0,i + 3);
	}
	return num;
}

function bookmarkthis(title,url) {
  if (window.sidebar) { // firefox
     window.sidebar.addPanel(title, url, "");
  } else if (document.all) { // IE
     window.external.AddFavorite(url, title);
  } else if (window.opera && window.print) { // opera
     var elem = document.createElement('a');
     elem.setAttribute('href',url);
     elem.setAttribute('title',title);
     elem.setAttribute('rel','sidebar');
     elem.click();
  }
}
