/*************************Javascript form completer*************************************/
function setShip()
{
	if(document.getElementById('ship').checked != true)
	{
		//as the user fills in the billing details they call the setShip()
		//function, as they complete the form this function copies the billing
		//details into the shipping form. This only occurs if the user has not
		//check the box which states they want to use a shipping address.
		document.getElementById('ShippingFirstName').value = document.getElementById('BillingFirstName').value;
		document.getElementById('ShippingLastName').value = document.getElementById('BillingLastName').value;
		document.getElementById('ShippingAddress1').value = document.getElementById('BillingAddress1').value;
		document.getElementById('ShippingAddress2').value = document.getElementById('BillingAddress2').value;
		document.getElementById('ShippingCity').value = document.getElementById('BillingCity').value;
		document.getElementById('ShippingCounty').value = document.getElementById('BillingCounty').value;
		document.getElementById('ShippingPostal').value = document.getElementById('BillingPostal').value;
		set_selected(document.getElementById('BillingCountry').selectedIndex, 'ShippingCountry');
	}
}

function set_selected(option, dropdown_name) {
	
	var current_dropdown = document.getElementById(dropdown_name);
	
	
	for(var i = 0; i < current_dropdown.options.length; i++) {
		
		if(current_dropdown.options[i].value == option) {
			current_dropdown.options[i].selected = true;
		}
	}
}

function showShipping()
{
	if(document.getElementById('ship').checked == true)
	{
		//here we are assuming that if they check the box
		//they want to enter different shipping details
		//so we clear out the values of these text boxes.
		document.getElementById('ShippingFirstName').value = '';
		document.getElementById('ShippingLastName').value = '';
		document.getElementById('ShippingAddress1').value = '';
		document.getElementById('ShippingAddress2').value = '';
		document.getElementById('ShippingCity').value = '';
		document.getElementById('ShippingCounty').value = '';
		document.getElementById('ShippingPostal').value = '';
		//document.getElementById('ship').disabled = true;
		
		//Now the fields are empty we can show the textfields
		document.getElementById('shipping_fieldset').style.display = 'inline';
	}
	else
	{
		//now we're assuming that they don't want to 
		//use separate shipping details, we don't know if
		//they have filled out the billing details, but if they
		//have this will get the shipping details back to the correct state
		//if not the setShip() function will take over anyway.
		document.getElementById('ShippingFirstName').value = document.getElementById('BillingFirstName').value;
		document.getElementById('ShippingLastName').value = document.getElementById('BillingLastName').value;
		document.getElementById('ShippingAddress1').value = document.getElementById('BillingAddress1').value;
		document.getElementById('ShippingAddress2').value = document.getElementById('BillingAddress2').value;
		document.getElementById('ShippingCity').value = document.getElementById('BillingCity').value;
		document.getElementById('ShippingCounty').value = document.getElementById('BillingCounty').value;
		document.getElementById('ShippingPostal').value = document.getElementById('BillingPostal').value;	
		
		//So now we've set the fields back to the same
		//as the Billing fields we know we can hide the fields
		document.getElementById('shipping_fieldset').style.display = 'none';
	}
}

var action;
function changeAction(action)
{
	switch(action)
	{
		case 1:
			document.forms[2].action = 'confirm_cc.php';
			break
		case 2:
			document.forms[2].action = 'confirm_other.php';
			break
		case 3:
			document.forms[2].action = 'confirm_other.php';
			break
		case 4:
			document.forms[2].action = 'confirm_other.php';
			break
		case 5:
			document.forms[2].action = 'directdebit.pdf';
			break
	}		
}
/*************************HSBC form field values*************************************/

//This function sets up the orderid and timestamp for the CPI form.
//These fields are hidden for our form.
function setDynamicFields()
{
	//This is left here for completeness as it is supplied and 
	//pretty much required for the HSBC integration - however,
	//i'm not using it in my scripts at all as I have moved these
	//functions on-page - I believe the <iframe> tag on this page was
	//causing issues with the onload() javascript tag which called this function,
	//and consquently these statements, to not execute correctly.
	document.cpiForm.OrderId.value = Math.round(Math.random()*10000000000);
	document.cpiForm.TimeStamp.value=new Date().valueOf();
	document.getElementById('shipping_fieldset').style.display = 'none';
}

/**************************Dreamweaver form validation ************************************/

function MM_findObj(n, d) 
{
	var p,i,x;  
	if(!d) d=document; 
	if((p=n.indexOf("?"))>0&&parent.frames.length) 
	{
		d=parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) x=d.all[n]; 
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); 
	return x;
}

function MM_validateForm() 
{
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  
  for (i=0; i<(args.length-2); i+=3) 
  { 
  	test=args[i+2]; 
	val=MM_findObj(args[i]);
    
	if (val) 
	{ 
		//I've commented these out until we need them, this is just a 
		//method to change the variable names in the JavaScript popup
		//that tells the user they need to complete more of the form
		//fields.
		/*if(val.name == 'tcs')
		{	
			nm = 'Terms & Conditions';
		}		
		if(val.name == 'BillingFirstName')
		{	
			nm = 'First Name';
		}		
		if(val.name == 'BillingLastName')
		{	
			nm = 'Last Name';
		}
		if(val.name == 'cphone')
		{	
			nm = 'Contact Phone';
		}
		if(val.name == 'email')
		{	
			nm = 'Email';
		}
		if(val.name == 'passw')
		{	
			nm = 'Password';
		}*/
		
		nm=val.name; 
		if ((val=val.value)!="") 
		{
      		if (test.indexOf('isEmail')!=-1) 
			{ 
				p=val.indexOf('@');
        		if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      		} 
			else if (test!='R') 
			{ 
				num = parseFloat(val);
        		if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        		if (test.indexOf('inRange') != -1) 
				{ 
					p=test.indexOf(':');
          			min=test.substring(8,p); 
					max=test.substring(p+1);
          			if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    			} 
			} 
		} 
		else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; 
	}
  } 
  
  if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

/**************************************************************/

function swapImage() 
{ 	
	var i, j=0, x, a = swapImage.arguments; 
	document.MM_sr = new Array; 
	
	for(i=0; i<(a.length-2); i+=3)
	{
		if ((x = findObj(a[i]))!=null)
		{
			document.MM_sr[j++]=x; 
			if(!x.oSrc) x.oSrc=x.src; 
			x.src=a[i+2];
		}
	}
}

function swapImgRestore() 
{ 
	var i, x, a = document.MM_sr; 
	for(i=0; a&&i<a.length&&(x=a[i])&&x.oSrc; i++) x.src = x.oSrc;
}

function findObj(n, d) 
{ 
	var p,i,x;  
	if(!d) d = document; 
	if((p = n.indexOf("?"))>0&&parent.frames.length) 
	{
		d = parent.frames[n.substring(p+1)].document;
		n = n.substring(0,p);
	}
	
	if(!(x=d[n])&&d.all) x = d.all[n];
	for (i=0; !x&&i<d.forms.length; i++) x = d.forms[i][n];
	for(i=0; !x&&d.layers&&i<d.layers.length; i++) x = findObj(n,d.layers[i].document);
  	if(!x && d.getElementById) x = d.getElementById(n); return x;
}