function addLoadEvent(func) {	
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;		
	} 
	else {
		window.onload = function() {
		oldonload();
		func();
		}
	}
}

// JScript source code
function isValidEmail(str) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str)){
		return true;
	}
	else {
		return false;
	}
}

function showerror(errtext){
    if (errtext!==''){
		alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + errtext + '\n\nPlease correct the above errors and try again.');
		return false;
	}
	else {
		return true;
	}
}

function checkEnquiryForm(){
    var ftxt = '';
    
    if (document.contactform.Name.value==''){
        ftxt += '\n- Please enter your Name.';
    }
    
    if (document.contactform.Phone.value==''){
        ftxt += '\n- Please enter your Telephone Number.';
    }
    
    if (isValidEmail(document.contactform.Email.value)==false){
        ftxt += '\n- Please enter your Email Address.';
    }
    
    if (document.contactform.Message.value==''){
        ftxt += '\n- Please enter your Message.';
    }
    
    return showerror(ftxt);
}

/* Forgotten Password */
function checkforgottenform(){
    if (isValidEmail(document.forgottenform.ecomms_email.value)==false){
        alert('Please enter your Email Address.');
        return false;
    }
    else {
        return true;
    }
}

/* Login Form */
function checkloginform(){
    var ftxt = '';
    
    if (isValidEmail(document.theloginform.ecomms_email.value)==false){
        ftxt += '\n- Please enter your Email Address.';
    }
    
    if (document.theloginform.ecomms_password.value==''){
        ftxt += '\n- Please enter your Password.';
    }
    
    return showerror(ftxt);  
}

/* Quick Register */
function checkquickregister(){
    var ftxt = '';
    
    if (isValidEmail(document.quickregister.ecomms_email.value)==false){
        ftxt += '\n- Please enter your Emai Address.';
    }
    
    if (document.quickregister.ecomms_password.value==''){
        ftxt += '\n- Please enter your Password.';
    }
    else {
        if (document.quickregister.ecomms_password.value!==document.quickregister.ecomms_password_confirm.value){
            ftxt += '\n- Please confirm your Password.';
        }
    }

    return showerror(ftxt);
}

/* Main Register */
function checkregisterform(){
    var ftxt = '';
    
    /* Personal */
    var ptxt = '';
    if (document.registerform.ecomms_Name.value==''){
        ptxt += '\n- Please enter your Name.';
    }
    
    /*
    if (document.registerform.ecomms_Company.value==''){
        ptxt += '\n- Please enter your Company Name.';
    }
    
    if (document.registerform.ecomms_Telephone.value==''){
        ptxt += '\n- Please enter your Telephone Number.';
    }
    */
    
    if (ptxt!==''){
        ftxt += '\n** Personal Details **\n' + ptxt;
    }
    
    /* Account Login Details */
    var atxt = '';
    if (isValidEmail(document.registerform.ecomms_Email.value)==false){
        atxt += '\n- Please enter your Email Address.';
    }
    
    if (document.registerform.FromForm.value=='Full'){
        // Main Registration
        if (document.registerform.ecomms_Password.value==''){
            atxt += '\n- Please enter your Password.';
        }
        else {
            if (document.registerform.ecomms_Password.value!==document.registerform.ecomms_Password_Confirm.value){
                atxt += '\n- Please confirm your Password.';
            }
        }          
    }
    else {
        // Update
        if (document.registerform.ecomms_Password.value!==document.registerform.ecomms_Password_Confirm.value){
            atxt += '\n- Please enter Matching Passwords.';
        }
    }
    
    if (atxt!==''){
        if (ftxt!==''){
            ftxt += '\n';
        }
        ftxt += '\n** Account Login Details **\n' + atxt;
    }
    
    /* Billing/Card Address Details */
    var btxt = '';
    if (document.registerform.ecomms_Address1.value==''){
        btxt += '\n- Please enter your Address 1.';
    }    
    
    if (document.registerform.ecomms_Town.value==''){
        btxt += '\n- Please enter your Town/City.';
    }
    
    if (document.registerform.ecomms_County.value==''){
        btxt += '\n- Please enter your County.';
    }
    
    if (document.registerform.ecomms_Postcode.value==''){
        btxt += '\n- Please enter your Postcode.';
    }
    
    if (btxt!==''){
        if (ftxt!==''){
            ftxt += '\n';
        }
        ftxt += '\n** Billing/Card Address Details **\n' + btxt;
    }
    

    return showerror(ftxt);
}

/* Logout */
function confirmLogout(){
    if (confirm('Are you sure you want to logout?')){
        location.href='/Scripts/LoginFunctions.asp?Logout=True';
    }
}

/* Delivery Address */
function deliveryAddressFields(theval){
    var fieldArr = new Array("Name","Company","Address1","Address2","Address3","City","County","Postcode","Country");
    for (var i=0;i<fieldArr.length;i++){
        if (theval){
            eval('document.ordersubmit.Delivery' + fieldArr[i] + '.value=document.ordersubmit.Billing' + fieldArr[i] + '.value');            
            document.getElementById("Delivery" + fieldArr[i]).readOnly=true;
        }
        else {
            eval('document.ordersubmit.Delivery' + fieldArr[i] + '.value=""');
            document.getElementById("Delivery" + fieldArr[i]).readOnly=false;
        }
    }
}

/* Order Submit */
function checkordersubmit(){
    var ftxt = '';
    
    if (document.ordersubmit.DeliveryName.value==''){
        ftxt += '\n- Please enter a Delivery Name.';
    }
    
    if (document.ordersubmit.DeliveryAddress1.value==''){
        ftxt += '\n- Please enter a Delivery Address 1.';
    }
    
    if (document.ordersubmit.DeliveryCity.value==''){
        ftxt += '\n- Please enter a Delivery City.';
    }
    
    if (document.ordersubmit.DeliveryCounty.value==''){
        ftxt += '\n- Please enter a Delivery County.';
    }
    
    if (document.ordersubmit.DeliveryPostcode.value==''){
        ftxt += '\n- Please enter a Delivery Postcode.';
    }
    
    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;
	}
}