


deliveryFieldArray=new Array();
registerFieldArray=new Array();

tempArray=new Array();


deliveryFieldArray=["title","first_name","surname","phone","email","add1","town","post_code","message"];

registerFieldArray=["title","first_name","surname","add1","post_code","phone","email","conemail","username","town"];


function swapList(listError){
	if(listError=="title")document.getElementById("errorList").innerHTML="<li>Title</li>";
	if(listError=="first_name")document.getElementById("errorList").innerHTML+="<li>First name</li>";
	if(listError=="surname")document.getElementById("errorList").innerHTML+="<li>Surname</li>";
	if(listError=="phone")document.getElementById("errorList").innerHTML+="<li>Phone number</li>";
	if(listError=="email" || listError=="title")document.getElementById("errorList").innerHTML+="<li>Email address</li>";
	if(listError=="add1")document.getElementById("errorList").innerHTML+="<li>Billing address</li>";
	if(listError=="town")document.getElementById("errorList").innerHTML+="<li>Town</li>";
	if(listError=="post_code")document.getElementById("errorList").innerHTML+="<li>Post code</li>";
	if(listError=="message")document.getElementById("errorList").innerHTML+="<li>Delivery instructions</li>";
	
	if(listError=="conemail")document.getElementById("errorList").innerHTML+="<li>Confirm email</li>";
	if(listError=="username")document.getElementById("errorList").innerHTML+="<li>Username</li>";
	
	if(updateStrVar!="update" || document.forms['register'].password.value!=""){
		if(listError=="password")document.getElementById("errorList").innerHTML+="<li>Password</li>";
	}

}

var allowSubmit=true;

//<form name="register" action="/cgi/eb_person/update_details.cgi" method="post" onSubmit="this.pagelocation.value=document.location.href; var location = this.pagelocation.value.split('?'); this.pagelocation.value=location[0];">

var formSelected="";

function alphaNumericCheck(str){
var regex=/^([a-zA-Z0-9_-]+)$/;
if(regex.test(str)){
return true;
} else {
return false;
}
}


var updateStrVar="";

function validateForm(formStr,updateStr){

updateStrVar=updateStr;
if(formStr=="register"){

if(updateStr!="update" || document.forms['register'].password.value!=""){
	if(!alphaNumericCheck(document.forms['register'].password.value)){
		showPopup("Your password should contain letters and numbers only.");
		return;
		}
	}
	}

formSelected=formStr;
if(formStr=="delivery")tempArray=deliveryFieldArray;
if(formStr=="register")tempArray=registerFieldArray;

if(formStr=="register"){
	if(updateStr!="update" || document.forms['register'].password.value!=""){
		pwordStr=document.forms['register'].password.value;
		if((pwordStr.length>8 || pwordStr.length<2) && document.forms['register'].password.value!="<password>" ){
			showPopup("Your password should be between 2 and 8 characters\nand contain letters and numbers only.");
			return;
		}
	}
}


	allowSubmit=true;
	document.getElementById("errorList").innerHTML="";
	
	formObj=document.forms[formStr];
	document.getElementById("errorDiv").style.display="block";
		  for (var i = 0; i < tempArray.length; i++) {
		  		fieldObj=document.getElementById(tempArray[i]);
		  		if((fieldObj.value=="") || (tempArray[i]=="email" && !isEmail(fieldObj.value))  || (tempArray[i]=="post_code" && !checkPostCode(fieldObj.value)) || (tempArray[i]=="phone" && !checkUKTelephone(fieldObj.value) || (tempArray[i]=="conemail" && (fieldObj.value!=document.getElementById("email").value))) ){
		  			
		  			
							 if (checkPostCode (document.getElementById('post_code').value)) {
								document.getElementById('post_code').value = checkPostCode (document.getElementById('post_code').value);
							    }
						
		  				swapBack(fieldObj);
		  				swapList(tempArray[i]);
		  				allowSubmit=false;
		  				backToTop();		  			
		 
		  			

		  		}
		  	}
		  	
		  	if(allowSubmit){
		  	
		  		document.getElementById("errorDiv").style.display="none";
		  		//alert("ok");	
		  		if(formStr=="register"){
		  			document.forms['register'].action="/cgi/eb_person/update_details.cgi";
		  			document.forms['register'].pagelocation.value=document.location.href;
		  			var location = document.forms['register'].pagelocation.value.split('?');
		  			document.forms['register'].pagelocation.value=location[0];
		  			document.forms['register'].submit();
		  		}
		  		
		  		if(formStr=="delivery"){
		  			document.forms['delivery'].submit();
		  			//getdeliverydetails();
		  		}
		  	}
		  	
	return false;		  
}


var xmlHttpBasket;
function createAjax(){
try
  	{xmlHttpBasket=new XMLHttpRequest();if ( xmlHttpBasket.overrideMimeType ) xmlHttpBasket.overrideMimeType ( "text/xml" );}
	catch (e){try{xmlHttpBasket=new ActiveXObject("Msxml2.XMLHTTP");}
	catch (e){try{xmlHttpBasket=new ActiveXObject("Microsoft.XMLHTTP");}
	catch (e){ alert("Your browser does not support AJAX!"); return false;} 
	} 
	} 
	//alert(xmlHttpBasket);
  }
  
  
var xmlHttpStr="";



function getdeliverydetails(){
	//alert(allowSubmit);	
	var basketForm  = document.forms['delivery'];
	var queryString = '?title=' + escape(basketForm.title.value) + '&first_name=' + escape(basketForm.first_name.value) + 
	'&surname=' + escape(basketForm.surname.value) + '&phone=' + escape(basketForm.phone.value) +
	'&email=' + escape(basketForm.email.value) +'&add1=' + escape(basketForm.add1.value) +
	'&add2=' + escape(basketForm.add2.value)+ '&town=' + escape(basketForm.town.value) + 
	'&county=' + escape(basketForm.county.value)+ '&post_code=' + escape(basketForm.post_code.value) +
	'&backvalue=' + escape(basketForm.backvalue.checked)+ '&message=' + escape(basketForm.message.value) 
	;
	//alert(xmlHttpBasket);
	//alert(queryString);
	ajaxFunction("billing_details.cgi",queryString);
}


function ajaxFunction(fileStr,urlStr){
	createAjax();
	//alert(fileStr);

  	xmlHttpBasket.onreadystatechange=function(){
		 if(xmlHttpBasket.readyState==4){
			xmlHttpStr=xmlHttpBasket.responseText;
			//document.getElementById("emptyDiv").innerHTML=xmlHttpStr;
			alert(xmlHttpStr);
			}
			else
			{
			//alert("fail");
			}
	}
  	xmlHttpBasket.open("GET",fileStr,true);
  	xmlHttpBasket.send(urlStr);	

}
 








function swapBack(obj){	
	//if(updateStrVar=="update" && document.forms['register'].password.value=="")return;
	bgStr="";
	imgName="";
	if(obj.currentStyle) { 	  
		bgStr=obj.currentStyle.backgroundImage;  
	}

	else { 
		bgStr=getComputedStyle(obj,'').getPropertyValue('background-image');
		}

	if(!bgStr.match("_error")){
	imgName=bgStr.substring(bgStr.lastIndexOf("/")+1,bgStr.lastIndexOf(".gif"));
	imgName=bgStr.replace(imgName,imgName+"_error");
	obj.style.background=imgName;
	
	var ypos=0;
	
		if(formSelected=="register" || obj.className=="discInput"){
			if(obj.className=="input_1") ypos=0;
			if(obj.className=="input_2") ypos=-30;
			if(obj.className=="input_3") ypos=-60;
			if(obj.className=="input_4") ypos=-90;
			if(obj.className=="discInput") ypos=-90;
			obj.style.backgroundPosition="0 "+ ypos +"";

			if(!obj.currentStyle){
				obj.style.backgroundPosition="0px "+ ypos +"px";
				}
		}
	}
	
	

}


function resetThis(obj){
	bgStr="";
	imgName="";
	if(obj.currentStyle) { 	  
		bgStr=obj.currentStyle.backgroundImage;  
	}

	else { 
		bgStr=getComputedStyle(obj,'').getPropertyValue('background-image');
		}
	bgStr=bgStr.replace("_error","");
	obj.style.background=bgStr;
	
	if(document.forms["register"] || obj.className=="discInput"){
		if(obj.className=="input_1") ypos=0;
		if(obj.className=="input_2") ypos=-30;
		if(obj.className=="input_3") ypos=-60;
		if(obj.className=="input_4") ypos=-90;
		if(obj.className=="discInput") ypos=-90;
		obj.style.backgroundPosition="0 "+ ypos +"";
		
		if(!obj.currentStyle){
			obj.style.backgroundPosition="0px "+ ypos +"px";
			}
	}
		
	
	
	
}

function isEmail(str){


if (str.search(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/) != -1)
return true;
else
return false;

}




function backToTop() {
   // var x1 = x2 = x3 = -0.01;
    var y1 = y2 = y3 = 0;

    if (document.documentElement) {
    //    x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }

    if (document.body) {
    //    x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }

   // x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;

   // var x = Math.max(x1, Math.max(x2, x3));
    var y = Math.max(y1, Math.max(y2, y3));

    //window.scrollTo(0, Math.floor(y / 2));
    window.scrollTo(0,0);

    if ( y > 0) {
        window.setTimeout("backToTop()", 200);
    }
}




function init()
{
    if (document.addEventListener)
    {
       document.addEventListener("keydown",keydown,false);
//       document.addEventListener("keypress",keypress,false);
//       document.addEventListener("keyup",keyup,false);
//       document.addEventListener("textInput",textinput,false);
    }
    else if (document.attachEvent)
    {
       document.attachEvent("onkeydown", keydown);
 //      document.attachEvent("onkeypress", keypress);
 //      document.attachEvent("onkeyup", keyup);
//       document.attachEvent("ontextInput", textinput);
    }
    else
    {
       document.onkeydown= keydown;
 //      document.onkeypress= keypress;
//       document.onkeyup= keyup;
 //      document.ontextinput= textinput;   // probably doesn't work
    }

   // document.testform.t.value+= '';
   // lines= 0;
}

function keydown(e)
{
   if (!e) e= event;
  if(e.keyCode==17){
  	return;
  	if(document.forms['delivery']){
  		document.getElementById('title').value="Mr";
  		document.getElementById('first_name').value="Bill";
  		document.getElementById('surname').value="Gates";
  		document.getElementById('phone').value="0117 927 7750";
  		document.getElementById('email').value="info@truedigital.co.uk";
  		document.getElementById('add1').value="1st Floor Royal London Buildings";
  		document.getElementById('add2').value="42-46 Baldwin Street";
  		document.getElementById('add2').value="42-46 Baldwin Street";
  		document.getElementById('town').value="Bristol";
		//document.getElementById('county').value="Avon";
		document.getElementById('post_code').value="BS1 1PN";
		document.getElementById('backvalue').checked=true;
		document.getElementById('message').value="Test Test Test";


  	}
  }
  
}


 init();