function numericInput(e){	
   /*
	* This function only allows the input of numeric characters (Key codes 48 - 57 incl.),
	* The period key (Key Code 46) and the tab and backspace key (Key Codes 0 and 8 respectively (Non IE only)) 
	*/
	var key;
	var keyChar;
	var result;
	if (window.event){
   		key = window.event.keyCode;
	}
	else if (e){
   		key = e.which;
	}
	if(key == 0 || key == 8){
		result = true;	
	}else{
		if(key >= 46 && key <= 57){
			if(key == 47){
				result = false;	
			}else{
				result = true;	
			}
		}else{
			result = false;	
		}
	}
	return result;
}

function calculateWallpaper(){
	var wallHeight 	= document.getElementById('wallHeight').value;
	var roomWidth 	= document.getElementById('roomWidth').value;
	var roomLength 	= document.getElementById('roomLength').value;
	var rollWidth 	= document.getElementById('rollWidth').value;
	var rollLength	= document.getElementById('rollLength').value;
	var pattern 	= document.getElementById('pattern').checked;
	
	var message = "";
	var doCalc = true;
	
	if(wallHeight == ""){
		message += "Please enter a wall height\n\n";	
		doCalc = false;
	}
	if(roomWidth == ""){
		message += "Please enter a room width\n\n";	
		doCalc = false;
	}
	if(roomLength == ""){
		message += "Please enter a room length\n\n";	
		doCalc = false;
	}
	if(rollWidth == ""){
		message += "Please enter a roll width\n\n";	
		doCalc = false;
	}
	if(rollLength == ""){
		message += "Please enter a roll length\n\n";	
		doCalc = false;
	}
	
	if(message != ""){
		alert(message);	
	}
	
	if(doCalc){
		wallHeight 	= parseFloat(wallHeight);
		roomWidth 	= parseFloat(roomWidth);
		roomLength 	= parseFloat(roomLength);
		rollWidth 	= parseFloat(rollWidth);
		rollLength	= parseFloat(rollLength);
		
		var wallArea = ((roomWidth * 2) + (roomLength * 2)) * wallHeight;
		if(pattern){
			wallArea = wallArea * 1.1;
		}
		
		var rollArea = rollWidth * rollLength;
		
		var rolls = wallArea / rollArea;
		rolls = Math.ceil(rolls);
		
		var resultArea = document.getElementById('wallpaperReqd');
		
		resultArea.innerHTML = "Approx "+rolls+" rolls required.";
	}
	
	return false;
}

function calculatePaint(){
	var wallHeight 	= document.getElementById('wallHeight').value;
	var roomWidth 	= document.getElementById('roomWidth').value;
	var roomLength 	= document.getElementById('roomLength').value;
	
	var message = "";
	var doCalc = true;
	
	if(wallHeight == ""){
		message += "Please enter a wall height\n\n";	
		doCalc = false;
	}
	if(roomWidth == ""){
		message += "Please enter a room width\n\n";	
		doCalc = false;
	}
	if(roomLength == ""){
		message += "Please enter a room length\n\n";	
		doCalc = false;
	}
	
	if(message != ""){
		alert(message);	
	}
	
	if(doCalc){
		wallHeight 	= parseFloat(wallHeight);
		roomWidth 	= parseFloat(roomWidth);
		roomLength 	= parseFloat(roomLength);
		
		var wallArea = ((roomWidth * 2) + (roomLength * 2)) * wallHeight;
		
		var litres = wallArea / 14;
		litres = litres.toFixed(1);
		
		var resultArea = document.getElementById('paintReqd');
		
		resultArea.innerHTML = "Approx "+litres+" litres required. (per coat)";
	}
	
	return false;
}

function showForm(type){
	var formDiv = document.createElement("div");
	formDiv.setAttribute("id", "floatForm");
	
	var formTitle = document.createElement("div");
	formTitle.setAttribute("id", "floatTitle");
	formTitle.innerHTML = "<h1 id=\"formTitle\" class=\"font12em\"><span class=\"white\">Request A Colour Card</span></h1>";
	
	formDiv.appendChild(formTitle);
	
	var formBody = document.createElement("div");
	formBody.setAttribute("id", "floatBody");
	
	bodyHTML = "<div class=\"font075em\"><span class=\"grey\">You can request a FREE colourcard by entering your details below:<br/></span><br/>";
	bodyHTML+= "<form action=\"\" method=\"post\" onsubmit=\"return requestSample()\">";							
	bodyHTML+= "<div class=\"padleft\"><strong><span class=\"purple\">Name</span><br/></strong>";
	bodyHTML+= "<input type=\"hidden\" id=\"type\" name=\"type\" value=\""+type+"\"/>";
        bodyHTML+= "<input class=\"calcInput\" type=\"text\" id=\"custName\" name=\"custName\"/><br/>";
        bodyHTML+= "<strong><span class=\"purple\">Address</span><br/></strong>";
        bodyHTML+= "<textarea class=\"calcInput\" id=\"custAddr\" name=\"custAddr\" rows=\"\" cols=\"\"></textarea><br/>";
        bodyHTML+= "<strong><span class=\"purple\">Telephone Number (inc Area Code)</span><br/></strong>";
        bodyHTML+= "<input class=\"calcInput\" type=\"text\" id=\"custTel\" name=\"custTel\"/><br/>";
        bodyHTML+= "<strong><span class=\"purple\">Email Address</span><br/></strong>";
        bodyHTML+= "<input class=\"calcInput\" type=\"text\" id=\"custEmail\" name=\"custEmail\"/><br/>";
        bodyHTML+= "<strong><span class=\"purple\">Manufacturer&rsquo;s Name</span><br/></strong>";
        bodyHTML+= "<input class=\"calcInput\" type=\"text\" id=\"manuName\" name=\"manuName\" value=\""+type+"\" onfocus=\"this.blur()\"/><br/>";
	bodyHTML+= "<image id=\"floatSubmit\"  src=\"images/btn_submit.gif\" alt=\"Submit\" onclick=\"return requestSample()\" style=\"cursor:pointer;\" /><br/><br/>";
	bodyHTML+= "<a href=\"Javascript: closeFloat();\" class=\"grey\">X Close Window</a><br/><br/></div></div>";
	formBody.innerHTML = bodyHTML;
	
	formDiv.appendChild(formBody);
	
	
	document.getElementById("sampleFormHolder").appendChild(formDiv);
}

function requestSample(){
	var custName 	= document.getElementById('custName').value;
	var custAddr 	= document.getElementById('custAddr').value;
	var custTel 	= document.getElementById('custTel').value;
	var custEmail 	= document.getElementById('custEmail').value;
    var manuName 	= document.getElementById('manuName').value;
	var type		= document.getElementById('type').value;
	
	var message = "";
	var result = true;
	
	if(custName == "" || custName == " "){
		message+= "Please enter your name\n\n";
		result = false;
	}
	if(custAddr == "" || custAddr == " "){
		message+= "Please enter your address\n\n";
		result = false;
	}
	if(custTel == "" || custTel == " "){
		message+= "Please enter your telephone number\n\n";
		result = false;
	}
	if(custEmail == "" || custEmail == " "){
		message+= "Please enter your email address\n\n";
		result = false;
	}else{
		if(!validateEmail()){
			message+= "Please enter a valid email address\n\n";
			result = false;
		}
	}
	
        if(manuName == "" || manuName == " "){
		message+= "Please enter the manufactutrer's name";
		result = false;
	}
	
	if(message != ""){
		alert(message);	
	}
	if(result){
		doRequest(custName, custAddr, custTel, custEmail, null, manuName, type);
	}
	return false;
}

function validateEmail(){
	var valid = true;
	var email = document.getElementById("custEmail");
	if(email.value == "" || email.value == " " || email.value == null){
		valid = false;
	}else{
		var emailValid = false;
		var email = email.value;
		var containsAt = false;
		for(var j = 0; j < email.length; j++){
			if(email.charAt(j) == '@'){
				containsAt = true;
			}
		}
		if(containsAt){
			if(email.charAt(email.length - 3) == '.' || email.charAt(email.length - 4) == '.'){
				if(!(email.charAt(email.length - 3) == '.' && email.charAt(email.length - 4) == '.')){
					emailValid = true;	
				}
			}
		}
		if(!emailValid){
			valid = false;
		}
	}
	return valid;
}

function testTelField(num){
	//alert(num);
	var valid = true;
	var telno = num;
	var newTelno = "";
	var telIsNumeric = true;
	for(var i = 0; i < telno.length; i++){
		if((telno.charAt(i) < '0' || telno.charAt(i) > '9') && telno.charAt(i) != ' '){
			telIsNumeric = false;
			valid = false;
		}
	}		
	if(telIsNumeric){			
		for(var i = 0; i < telno.length; i++){
			if(telno.charAt(i) != " "){
				newTelno += telno.charAt(i);	
			}
		}
		if(newTelno.length != 11){
			valid = false;
		}
	}else{
		valid = false;
	}
	return valid;
}

function doRequest(aName, aAddr, aTel, aEmail, aProd, aManu, aType){
        //alert(location.pathname);
	if (window.XMLHttpRequest)
        {
  		xmlhttp=new XMLHttpRequest();
        }
	else
  	{
  	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	
	xmlhttp.onreadystatechange=function()
  	{
  		if (xmlhttp.readyState==4 && xmlhttp.status==200)
    	{
    		document.getElementById("floatBody").innerHTML= "<div class=\"font075em\"><span class=\"grey\">Thank you, your request has been accepted.<br/><br/><a href=\"Javascript: closeFloat();\"><img src=\"images/btn_close.gif\" alt=\"Close\" width=\"120\" height=\"36\"/></a><br/><br/></span></div>";
    	}
  	}
	var details = "custName="+aName+"&custAddr="+aAddr+"&custTel="+aTel+"&custEmail="+aEmail+"&custProd="+aProd+"&manu="+aManu+"&type="+type;
	xmlhttp.open("POST","colourCardRequest.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send(details);
}

function closeFloat(){
	var holder = document.getElementById("sampleFormHolder");
	while(holder.firstChild){
		holder.removeChild(holder.firstChild);	
	}
}
