// JavaScript Document

function shutterchoicechange(id) {
	if($(id).className == "") {
		$(id).className = "imagechoice-on";
	}
	else {
		$(id).className = "";
	}
	
	shuttercoloursARRAY = $('shuttercolours').select('.imagechoice-on');
	
	if(shuttercoloursARRAY.size() > 3){
		$('shuttercolourserror').style.display = "";
		$(id).className = "";
	}
	else {
		$('shuttercolourserror').style.display = "NONE";
	}
	
	dynamic_height();
	
}

//	shuttercolours.each(function(s) {
//		alert(s.title);
//	});

function dynamic_height() {	
	$$('.columns').each(function(item) {item.style.height = "";});
}


function formValidate(){
	var error=0;
	$("errorname").style.display="none";
 	$("erroremail").style.display="none";
	$("errorsaddress").style.display="none";
	$("erroremailinvalid").style.display="none";
	
	if($("txtName").value== "") {
		error=1;
		$("errorname").style.display="block";
	}
		
	if($("txtAddress").value== "") {
		error=1;
		$("errorsaddress").style.display="block";		
	}

	if($("txtEmail").value == ""){
		error=1;
		$("erroremail").style.display="block";		
	}
	else {
		if (!echeck($("txtEmail").value)) { 
			$("erroremailinvalid").style.display="block";		
			error=1
		}

	}
	dynamic_height();
	if(error==0){
		shuttercoloursTEXT = "";
		shuttercoloursARRAY = $('shuttercolours').select('.imagechoice-on');
		
		shuttercoloursARRAY.each(function(s) {
			shuttercoloursTEXT += s.title + ",";
		});
		
		var pars = 'name=' + $('txtName').value + '&email=' + $('txtEmail').value + '&telephone=' + $('txtTelephone').value + '&address=' + $('txtAddress').value + '&heardfrom=' + $('cmb_hear').value + '&shutters=' + shuttercoloursTEXT;
		new Ajax.Request('/sitestyle/modules/htmlcontent/shuttersamples/sendmail.php', { method:'get', parameters: pars, onSuccess: emailsent});

	}
}

function emailsent(transport) { 
	 $("msgThankyou").style.display="block"; 
	 $("bs-wrapper").style.display="none";
	 dynamic_height();
}

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){return false;}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return false;}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return false}
	if (str.indexOf(at,(lat+1))!=-1){return false;}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return false;}
	if (str.indexOf(dot,(lat+2))==-1){return false;}
	if (str.indexOf(" ")!=-1){return false;}
	return true;			
}
