// 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 = "";});
	setEqualHeightColumns('columns');
}


function formValidate(){
	var error=0;
	$("errorname").style.display="none";
 	$("erroremail").style.display="none";
	$("errorsaddress").style.display="none";
	$("erroremailinvalid").style.display="none";
	
	$("txtName").value = $("txtName").value.trim()
	$("txtEmail").value = $("txtEmail").value.trim()
	$("txtTelephone").value = $("txtTelephone").value.trim()
	$("txtAddress").value = $("txtAddress").value.trim()
	
	
	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 image = new Image(1,1);
		image.src = "http://www.googleadservices.com/pagead/conversion/1044480187/?value=10&label=zKshCK3fpwEQu4GG8gM&script=0";
		
		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;			
}
function isValidEmail(email){
    var RegExp = /^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/
    if(RegExp.test(email)){
        return true;
    }else{
        return false;
    }
}

String.prototype.trim = function() {  return this.replace(/^\s+|\s+$/g, '');  }
