// Date Function
function displaydate(){
	// Get today's current date.
	var now = new Date();

	// Array list of days.
	var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

	// Array list of months.
	var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

	// Calculate the number of the current day in the week.
	var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();

	// Calculate four digit year.
	function fourdigits(number)	{
		return (number < 1000) ? number + 1900 : number;
	}
	
	// Sort out the Time	
	var Hours = now.getHours();
	var Mins = now.getMinutes();
	
	if (Mins < 10) {
		Mins = "0" + Mins;
	}

	// Join it all together
	today =  days[now.getDay()] + " " +date + " " + months[now.getMonth()] + " " + (fourdigits(now.getYear())) + " - " + Hours + ":" + Mins;

	// Print out the data.
	document.write(today);
}

var pcrex = new RegExp("^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$","i");

// Postcode Validation
function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  
  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}

// Form Validation Functions
function isValidEmail(str) {
	//return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str)){
		return true;
	}
	else {
		return false;
	}
}

function checkcallform(){
	var ftxt = "";
	
	if (document.callusback.Name.value==''){
		ftxt += '\n- Please enter Your Name.';
	}
	
	if (document.callusback.TelephoneNumber.value==''){
		ftxt += '\n- Please enter Your Telephone Number.';
	}
	
	if (document.callusback.NatureEnquiry.value==''){
		ftxt += '\n- Please select your Nature of Enquiry.';
	}
	
	if (ftxt!==''){
		alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again.');
		return false;
	}
	else{
		return true;
	}
}

function checkenquiry(){
	var ftxt = "";
	
	if (document.enquiry.Name.value==''){
		ftxt += '\n- Please enter your Name.';
	}
	
	if (document.enquiry.Company.value==''){
		ftxt += '\n- Please enter your Company Name.';
	}
			
	if (document.enquiry.Telephone.value==''){
		ftxt += '\n- Please enter your Telephone Number.';
	}
	
	if (isValidEmail(document.enquiry.email.value)==false){
		ftxt += '\n- Please enter your Email Address.';
	}
	
	if (document.enquiry.subject.value==''){
		ftxt += '\n- Please select the subject of this Enquiry.';
	}
	
	if (ftxt!==''){
		alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again');
		return false		
	}
	else{
		return true;
	}
}

function checkfreighttracker(){
	var ftxt = "";
	
	if (document.freighttracker.Name.value==''){
		ftxt += '\n- Please enter your Name.';
	}	
	
	if (isValidEmail(document.freighttracker.email.value)==false){
		ftxt += '\n- Please enter your Email Address.';
	}
	
	if (document.freighttracker.ConsignmentNumber.value==''){
		ftxt += '\n- Please select the Consignment Number.';
	}
	
	if (ftxt!==''){
		alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again');
		return false		
	}
	else{
		return true;
	}
}

function checkquote(){
	var frmtxt = "";
		
	//origin details
	var orgtxt = "";
	if (document.quote.Origin_DestinationInformation.value==''){
		orgtxt += '\n - Please enter the Destination Information.';
	}
	
	if (document.quote.Origin_ServiceRequired.value==''){
		orgtxt += '\n - Please select the Required Service.';
	}
	
	if (orgtxt!==''){
		frmtxt += '\nOrigin Details\n' + orgtxt;
	}	
	
	//senders details
	var sendtxt = "";
	if (document.quote.Sender_FullName.value==''){
		sendtxt += '\n - Please enter the Full Name.';
	}
	
	if (document.quote.Sender_CompanyName.value==''){
		sendtxt += '\n - Please enter the Company Name.';
	}
	
	if (document.quote.Sender_Postcode.value==''){
		sendtxt += '\n - Please enter the Postcode.';
	}
	
	if (sendtxt!==''){
		if (frmtxt!==''){
			frmtxt += '\n'
		}
		frmtxt += '\nSenders Details\n' + sendtxt;
	}
	
	//contact details
	var contxt = "";
	if (isValidEmail(document.quote.email.value)==false){
		contxt += '\n - Please enter your Email Address.';
	}	
	
	if (document.quote.Contact_TelephoneNumber.value==''){
		contxt += '\n - Please enter your Telephone Number.';
	}
	
	if (contxt!==''){
		if (frmtxt!==''){
			frmtxt += '\n'
		}
		frmtxt += '\nContact Details\n' + contxt;
	}
	
	//receivers details
	var rectxt = "";
	if (document.quote.Receiver_Surname.value==''){
		rectxt += '\n - Please enter a Surname.';
	}
	
	if (document.quote.Receiver_CompanyName.value==''){
		rectxt += '\n - Please enter a Company Name.';
	}
	
	if (document.quote.Receiver_PostcodeCountry.value==''){
		rectxt += '\n - Please enter a Postcode & Country.';
	}
	
	if (document.quote.Receiver_GoodsDescription.value==''){
		rectxt += '\n - Please enter a Full Description of the Goods.';
	}
	
	if (rectxt!==''){
		if (frmtxt!==''){
			frmtxt += '\n'
		}
		frmtxt += '\nReceivers Details\n' + rectxt;
	}
	
	//details of goods
	var dectxt = "";
	if (document.quote.GoodsDetails_Height.value==''){
		dectxt += '\n - Please enter a Height (cms).';		
	}
	
	if (document.quote.GoodsDetails_Width.value==''){
		dectxt += '\n - Please enter a Width (cms).';
	}
	
	if (document.quote.GoodsDetails_Length.value==''){
		dectxt += '\n - Please enter a Length (cms).';
	}
	
	if (document.quote.GoodsDetails_Quantity.value==''){
		dectxt += '\n - Please enter a Quantity.';
	}
	
	if (document.quote.GoodsDetails_TotalWeight.value==''){
		dectxt += '\n - Please enter a Total Weight (kgs).';
	}
	
	if (dectxt!==''){
		if (frmtxt!==''){
			frmtxt += '\n'
		}
		frmtxt += '\nDetails of Goods\n' + dectxt;
	}
	
	//terms conditions
	if (document.quote.TermsAgreed.checked==false){
		if (frmtxt!==''){
			frmtxt += '\n'
		}
		frmtxt += '\nTerms & Conditions\n\n - You must have read and agree to our Terms & Conditions to submit this quotation form.';
	}
	
	//handler
	if (frmtxt!==''){
		alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + frmtxt + '\n\nPlease correct the above errors and try again.');
		return false;
	}
	else{
		return true;
	}
}

function checkquicksearch(){
    if (document.quicksearch.Criteria.value==''){
        alert('Please enter your search criteria.');
        return false;
    }
    else {
        return true;
    }
}

function checkvirtuallogin(){
    var ftxt = '';
    
    if (document.login.Username.value==''){
        ftxt += '\n- Please enter your Username.';
    }
    
    if (document.login.Password.value==''){
        ftxt += '\n- Please enter your Password.';
    }
    
    if (ftxt!==''){
		alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again.');
		return false;
	}
	else{
		return true;
	}
}

function confirmLogout(){
    if (confirm('Are you sure you want to logout?')){
        location.href='virtualwarehouse_myaccount.asp?Logout=True';
    }
}

function checkmainlogin(){
    var ftxt = '';
    
    if (document.mainlogin.MainUsername.value==''){
        ftxt += '\n- Please enter your Username.';
    }
    
    if (document.mainlogin.MainPassword.value==''){
        ftxt += '\n- Please enter your Password.';
    }
    
    if (ftxt!==''){
		alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again.');
		return false;
	}
	else{
		return true;
	}
}

function checkeditregistrationform(){
    var ftxt = '';
    
    if (document.editregistrationform.BillingName.value==''){
        ftxt += '\n- Please enter your Name.';
    }
    
    if (isValidEmail(document.editregistrationform.EmailAddress.value)==false){
        ftxt += '\n- Please enter your Email Address.';
    }
    
    /* Passwords */
    if (document.editregistrationform.Password.value!==document.editregistrationform.Password_Rpt.value){
        ftxt += '\n- Please ensure Both Passwords Match.';
    }
    else if ((document.editregistrationform.Password.value.length<=6)&&document.editregistrationform.Password.value.length>0){
        ftxt += '\n- Please enter a Minimum Password Length of 6 Characters.';
    }

    if (ftxt!==''){
		alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again.');
		return false;
	}
	else{
		return true;
	} 
}

function confirmProductDelete(productID){
    if (confirm('Are you sure you want to remove this product from your basket?')){
        location.href='virtualwarehouse_basket.asp?DelProductID=' + productID;
    }
}

var bnewaddress = false;
function switchCheckoutDelivery(bswitch){
    bnewaddress = bswitch;

    if (bswitch){
        document.getElementById("storeddelivery").style.display='none';
        document.getElementById("newdelivery").style.display='';                
    }
    else {
        document.getElementById("storeddelivery").style.display='';
        document.getElementById("newdelivery").style.display='none';
    }
}

function checkcheckout(){
    var ftxt = '';

    if (bnewaddress){
        if (document.checkout.DeliveryName.value==''){
            ftxt += '\n- Please enter a Delivery Name.';    
        }
        
        if (document.checkout.DeliveryAddress1.value==''){
            ftxt += '\n- Please enter a Delivery Address 1.';
        }
        
        if (document.checkout.DeliveryCity.value==''){
            ftxt += '\n- Please enter a Delivery City.';
        }
        
        if (document.checkout.DeliveryCountryID.value=='226'){
            if (document.checkout.DeliveryCounty.value==''){
                ftxt += '\n- Please enter a Delivery County.';
            }
            
            if (checkPostCode(document.checkout.DeliveryPostcode.value)==false){
                ftxt += '\n- Please enter a Delivery Postcode.';
            }
        }
        else {
            if (document.checkout.DeliveryPostcode.value==''){
                ftxt += '\n- Please enter a Delivery Postcode.';
            }
        }
        
        if (document.checkout.DeliveryTelephone.value==''){
            ftxt += '\n- Please enter a Delivery Telephone Number.';
        }
        
        if (isValidEmail(document.checkout.DeliveryEmailAddress.value)==false){
            ftxt += '\n- Please enter a Delivery Email Address.';
        }
    }
    
    if (ftxt!==''){
		alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again.');
		return false;
	}
	else{
		return true;
	} 
}

function displayrequiredfields(){    
    var countyrequired = document.getElementById("countyrequired");
    
    if (document.checkout.DeliveryCountryID.value=='226'){
        countyrequired.style.display='';
    }
    else {
        countyrequired.style.display='none';
    }   
}