// JavaScript Document

function addLoadEvent(func) 
{
	var oldonload = window.onload;
  	if (typeof window.onload != 'function') 
  	{
    	window.onload = func;
  	}
  	else 
  	{
    	window.onload = function() 
    	{
      		if (oldonload) 
      		{
        		oldonload();
      		}
      		func();
    	}
  	}
}

function submitBasket()
{
	document.basket.submit();
}

function completeDeliveryDetails() 
{	
	document.checkout.deliveryName.value = document.checkout.firstname.value + " " + document.checkout.surname.value;
	document.checkout.deliveryAddress1.value = document.checkout.billingAddress1.value;
	document.checkout.deliveryAddress2.value = document.checkout.billingAddress2.value;
	document.checkout.deliveryAddress3.value = document.checkout.billingAddress3.value;
	document.checkout.deliveryTown.value = document.checkout.billingTown.value;
	document.checkout.deliveryCounty.value = document.checkout.billingCounty.value;
	document.checkout.deliveryPostcode.value = document.checkout.billingPostcode.value;
	document.checkout.deliveryCountry.selectedIndex= document.checkout.billingCountry.selectedIndex;
	
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}



// ------------------------
// Javascript Controller - ADDS ON CLICK EVENTS
// ------------------------
function javascriptController() {
    
    // Check the browser supports getElementById so we can find our link
    if(!document.getElementById) 
    {
    	return;
    }
    
    // ------------------------
    // Auto Complete Address Field
    // ------------------------
    
    var jsAutoComplete = document.getElementById("jsAutoComplete");
    
    // If it isn't there return
    if (!jsAutoComplete) 
    {
    	return;
    }
    
    // We've made it here so a link with with the id exists. Wait for onclick and do something
    jsAutoComplete.onclick = function()
    {
        autoCompleteAddress();
        return false;
    }
    
    // ------------------------
    // Convert First email address to lower case and check is valid
    // ------------------------
    
     var jsEmailLowerCase = document.getElementById("jsEmail1");
    
    // If it isn't there return
    if (!jsEmailLowerCase) 
    {
    	return;
    }
    
    jsEmailLowerCase.onchange = function()
    {
        // Get email address and convert to lowercase
        var email = document.getElementById("jsEmail1").value;
        
        // Check that email is valid
        checkValidEmail(email);
        
        // Update form
        document.getElementById("jsEmail1").value = email.toLowerCase();
        
        return false;
    }
    
    // ------------------------
    // Check email addresses are the same
    // ------------------------
    
     var jsEmailCheck = document.getElementById("jsEmail2");
    
    // If it isn't there return
    if (!jsEmailCheck) 
    {
    	return;
    }
    
    jsEmailCheck.onchange = function()
    {
    	// Get values
        var email1 = document.getElementById("jsEmail1").value;
        var email2 = document.getElementById("jsEmail2").value;
        
        // Convert email2 to lowercase
        document.getElementById("jsEmail2").value = email2.toLowerCase();
        
        if(email1.toLowerCase() != email2.toLowerCase())
        {
        	// The email addresses don't match
        	alert("Please check that you have correctly entered your email address into both boxes. Thanks");
        	return false;
        }
        else
        {
        	return false;
        }
    }
    
    // ------------------------
    // Check New customer form has been completed correctly
    // ------------------------
    
    var jsNewCustomerValidation = document.getElementById("jsNewCustomerValidation");
    
    // If it isn't there return
    if (!jsNewCustomerValidation) 
    {
    	return;
    }
    
    // We've made it here so a link with with the id exists. Wait for onclick and do something
    jsNewCustomerValidation.onclick = function()
    {
        validateNewCustomer();
        return false;
    }
    
    // ------------------------
    // Check Update Customer Form has been correctly entered
    // ------------------------
    
    var jsEditCustomerValidation = document.getElementById("jsEditCustomerValidation");
    
    // If it isn't there return
    if (!jsEditCustomerValidation) 
    {
    	return;
    }
    else
    {
    	alert("ok");
    }
    
    // We've made it here so a link with with the id exists. Wait for onclick and do something
    jsEditCustomerValidation.onclick = function()
    {
        alert("here");
        validateEditCustomer();
        return false;
    }
	

	
	
	
} 

//------------------------
//Function to check new customer details are correct
//------------------------
function validateNewCustomer()
{
	// Assume we are okay unless otherwise
	var valid = true;
	var message = "";
	
	// Go through the required fields
	if(document.checkout.firstname.value == "")
	{
		valid = false;
		message = "Please enter your firstname";
		alert(message);
		return;
	}
	
	if(document.checkout.surname.value == "")
	{
		valid = false;
		message = "Please enter your surname";
		alert(message);
		return;
	}
	
	if(document.checkout.telephone.value == "")
	{
		valid = false;
		message = "Please enter your telephone number";
		alert(message);
		return;
	}
	
	if(document.checkout.email1.value == "")
	{
		valid = false;
		message = "Please enter your email address into both boxes";
		alert(message);
		return;
	}
	
	if(document.checkout.email2.value == "")
	{
		valid = false;
		message = "Please enter your email address into both boxes";
		alert(message);
		return;
	}
	
	/*
	// Is this a valid email address
	if(isUnusedEmail(document.checkout.email1.value))
	{
		valid = false;
		message = "This email address is already being used any another customer. Please enter a unique email address for this account.";
		alert(message);
		return false;
	} 
	*/
	
	if(document.checkout.password1.value == "")
	{
		valid = false;
		message = "Please enter a password into both boxes";
		alert(message);
		return;
	}
	
	if(document.checkout.password2.value == "")
	{
		valid = false;
		message = "Please enter a password into both boxes";
		alert(message);
		return;
	}
	
	if(document.checkout.password1.value.length < 6)
	{
		valid = false;
		message = "Please enter a password which is at least 6 characters long";
		alert(message);
		return;
	}
	
	if(document.checkout.password2.value.length < 6)
	{
		valid = false;
		message = "Please enter a password which is at least 6 characters long";
		alert(message);
		return;
	}
	
	if(document.checkout.password1.value != document.checkout.password2.value)
	{
		valid = false;
		message = "Please check that you have entered the same password into both boxes";
		alert(message);
		return;
	}
     		
	if(document.checkout.delivery1.value == "")
	{
		valid = false;
		message = "Please enter your delivery address";
		alert(message);
		return;
	}
	
	if(document.checkout.deliveryTown.value == "")
	{
		valid = false;
		message = "Please enter your delivery town";
		alert(message);
		return;
	}
	
	if(document.checkout.deliveryCounty.value == "")
	{
		valid = false;
		message = "Please enter your delivery county";
		alert(message);
		return;
	}
	
	if(document.checkout.deliveryPostcode.value == "")
	{
		valid = false;
		message = "Please enter your delivery postcode";
		alert(message);
		return;
	}
	
	if(document.checkout.billing1.value == "")
	{
		valid = false;
		message = "Please enter your billing address";
		alert(message);
		return;
	}
	
	if(document.checkout.billingTown.value == "")
	{
		valid = false;
		message = "Please enter your billing town";
		alert(message);
		return;
	}
	
	if(document.checkout.billingCounty.value == "")
	{
		valid = false;
		message = "Please enter your billing county";
		alert(message);
		return;
	}
	
	if(document.checkout.billingPostcode.value == "")
	{
		valid = false;
		message = "Please enter your billing postcode";
		alert(message);
		return;
	}
	
	if(valid == true)
	{
		document.checkout.submit();
	}
}


// ------------------------
// Function to check new customer details are correct
// ------------------------
function validateEditCustomer()
{
	// Assume we are okay unless otherwise
	var valid = true;
	var message = "";
	
	// Go through the required fields
	if(document.checkout.firstname.value == "")
	{
		valid = false;
		message = "Please enter your firstname";
		alert(message);
		return false;
	}
	
	if(document.checkout.surname.value == "")
	{
		valid = false;
		message = "Please enter your surname";
		alert(message);
		return false;
	}
	
	if(document.checkout.telephone.value == "")
	{
		valid = false;
		message = "Please enter your telephone number";
		alert(message);
		return false;
	}
	
	if(document.checkout.email.value == "")
	{
		valid = false;
		message = "Please enter your email address";
		alert(message);
		return false;
	}
	/*
	// Is this a valid email address
	if(validateEditEmail(document.checkout.email.value) == 1)
	{
		valid = false;
		message = "This email address is already being used any another customer. Please enter a unique email address for this account.";
		alert(message);
		return false;
	} 
	*/
	
	
	// Are we updating the password
	if(document.checkout.password1.value != "")
	{
		if(document.checkout.password2.value == "")
		{
			valid = false;
			message = "Please enter a password into both boxes";
			alert(message);
			return false;
		}
	
		if(document.checkout.password1.value.length < 6)
		{
			valid = false;
			message = "Please enter a password which is at least 6 characters long";
			alert(message);
			return false;
		}
	
		if(document.checkout.password2.value.length < 6)
		{
			valid = false;
			message = "Please enter a password which is at least 6 characters long";
			alert(message);
			return false;
		}
	
		if(document.checkout.password1.value != document.checkout.password2.value)
		{
			valid = false;
			message = "Please check that you have entered the same password into both boxes";
			alert(message);
			return false;
		}
	}
	
	if(document.checkout.delivery1.value == "")
	{
		valid = false;
		message = "Please enter your delivery address";
		alert(message);
		return false;
	}
	
	if(document.checkout.deliveryTown.value == "")
	{
		valid = false;
		message = "Please enter your delivery town";
		alert(message);
		return false;
	}
	
	if(document.checkout.deliveryCounty.value == "")
	{
		valid = false;
		message = "Please enter your delivery county";
		alert(message);
		return false;
	}
	
	if(document.checkout.deliveryPostcode.value == "")
	{
		valid = false;
		message = "Please enter your delivery postcode";
		alert(message);
		return false;
	}
	
	if(document.checkout.billing1.value == "")
	{
		valid = false;
		message = "Please enter your billing address";
		alert(message);
		return false;
	}
	
	if(document.checkout.billingTown.value == "")
	{
		valid = false;
		message = "Please enter your billing town";
		alert(message);
		return false;
	}
	
	if(document.checkout.billingCounty.value == "")
	{
		valid = false;
		message = "Please enter your billing county";
		alert(message);
		return false;
	}
	
	if(document.checkout.billingPostcode.value == "")
	{
		valid = false;
		message = "Please enter your billing postcode";
		alert(message);
		return false;
	}
	
	
	
	if(valid == true)
	{
		document.checkout.submit();
	}
}

// ------------------------
// Function to check that email is valid address
// ------------------------

function checkValidEmail(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	if (str.indexOf(at)==-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}
		
	if (str.indexOf(" ")!=-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

 		return true;				
	}

// ------------------------
// Billing and Delivery Address are the same so lets auto complete
// ------------------------
function autoCompleteAddress() 
{	
	document.checkout.billing1.value = document.checkout.delivery1.value;
	document.checkout.billing2.value = document.checkout.delivery2.value;
	document.checkout.billing3.value = document.checkout.delivery3.value;
	document.checkout.billingTown.value = document.checkout.deliveryTown.value;
	document.checkout.billingCounty.value = document.checkout.deliveryCounty.value;
	document.checkout.billingPostcode.value = document.checkout.deliveryPostcode.value;
	
	// Country Menu
	document.getElementById("form-billingCountry").selectedIndex = document.getElementById("form-deliveryCountry").selectedIndex;
	
	return false;
}

function clearSearch(obj)
{
	obj.value = "";
}

addLoadEvent(javascriptController); 
