// Functions
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 checkcontactform(){
	var ftxt = '';

	if (document.contactform.Name.value==''||document.contactform.Name.value=='name'){
		ftxt += '\n- Please enter your Name.';
	}
	
	if (document.contactform.Club.value==''||document.contactform.Club.value=='club'){
		ftxt += '\n- Please enter your Club.';
	}
	
	if (document.contactform.Telephone.value==''||document.contactform.Telephone.value=='tel.'){
		ftxt += '\n- Please enter your Telephone Number.';
	}
	
	if (isValidEmail(document.contactform.email.value)==false){
		ftxt += '\n- Please enter your 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 checkenquiryform(){
	var ftxt = '';

	if (document.contactform.Name.value==''||document.contactform.Name.value=='name'){
		ftxt += '\n- Please enter your Name.';
	}
	
	if (document.contactform.Club.value==''||document.contactform.Club.value=='club'){
		ftxt += '\n- Please enter your Club.';
	}
	
	if (document.contactform.Telephone.value==''||document.contactform.Telephone.value=='tel.'){
		ftxt += '\n- Please enter your Telephone Number.';
	}
	
	if (isValidEmail(document.contactform.email.value)==false){
		ftxt += '\n- Please enter your 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 clearText(field){
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
}

/* Basket Functions */
function IsNumeric(sText){
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;   
}

function addToBasket(bcheckout){    
    var ftxt = '';
    
    if (document.basketcontrol.PrimaryColour.value==''){
        ftxt += '\n- Please select a Primary Colour.';   
    }
    
    if (document.basketcontrol.SecondaryColour.value==''){
        ftxt += '\n- Please select a Secondary Colour.';
    }
    
    var blogo = false;
    if (document.basketcontrol.LogoOption.value=='True'){    
        blogo = true;
        if (document.basketcontrol.Logo.value==''){
            ftxt += '\n- Please select if you require a Logo.';
        }
    }
    
    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 {
        if (IsNumeric(document.basketcontrol.Quantity.value)&&document.basketcontrol.Quantity.value!==''){
            var Primary = document.basketcontrol.PrimaryColour.value.split("|");
            var Secondary = document.basketcontrol.SecondaryColour.value.split("|"); 
        
            var qstring = 'shop_basket.asp?ID=' + document.basketcontrol.ID.value + '&Quantity=' + document.basketcontrol.Quantity.value + '&PrimaryColour='+ Primary[0] +'&SecondaryColour='+ Secondary[0] + '&ForceCheckout=' + bcheckout;
            if (blogo){
                qstring += '&Logo='+ document.basketcontrol.Logo.value;
            }
        
            location.href = qstring;
        }
        else {
            alert('Please enter a Valid Quantity.');
            return false;
        }
    }
} 

/* Lead Time */
function controlLeadTime(){
    var Primary = document.basketcontrol.PrimaryColour.value.split("|");
    var Secondary = document.basketcontrol.SecondaryColour.value.split("|");
    
    var i = 0;
    if (Primary[2]=='True'){
        i += 1;
    }
    if (Secondary[2]=='True'){
        i += 1;
    }
    
    var LeadTime = document.getElementById("LeadTime");
    
    if (i>0){
        LeadTime.innerHTML = 'Non standard colour products have a 2-3 week delivery time';
    }
    else {
        LeadTime.innerHTML = 'Standard colour products have a 2-3 day delivery time';
    }
}

/* Colour Control */
function controlColour(theval,thecontrol){
    //0 = ID
    //1 = Colour Name
    //2 = Lead Time (bool)
    //3 = HEX
    controlLeadTime();
    controlLogo();
    
    var thecolour = theval.split("|");   
            
    if (thecontrol=='Primary'){
        var PrimaryColour = document.getElementById("PrimaryColour");    
        var PrimaryHex = document.getElementById("PrimaryHex");
        
        if (thecolour.length=='1'){
            PrimaryColour.innerHTML = 'Gold';    
            PrimaryHex.style.background = '#FFC318';             
        }
        else {        
            PrimaryColour.innerHTML = thecolour[1];    
            PrimaryHex.style.background = thecolour[3]; 
        }
    }
    
    if (thecontrol=='Secondary'){
        var SecondaryColour = document.getElementById("SecondaryColour");
        var SecondaryHex = document.getElementById("SecondaryHex");
        
        if (thecolour.length=='1'){
            SecondaryColour.innerHTML = 'Black';
            SecondaryHex.style.background = '#000000'; 
        }
        else {        
            SecondaryColour.innerHTML = thecolour[1];
            SecondaryHex.style.background = thecolour[3];                       
        }
    }  
}

/* Logo Control */
function controlLogo(){    
    var theval = document.basketcontrol.Logo.value;
    var Logo = document.getElementById("LogoValue");
    
    var PrimaryColour = document.basketcontrol.PrimaryColour.value;
    var SecondaryColour = document.basketcontrol.SecondaryColour.value;
    
    var PrimaryArray = PrimaryColour.split("|");
    var SecondaryArray = SecondaryColour.split("|");
    
    var icount = 0;
    if (PrimaryArray[2]=='True'){
        icount += 1;
    }    
    
    if (SecondaryArray[2]=='True'){
        icount += 1;
    }
    
    //alert(icount);
    
    if (icount>0){
        document.basketcontrol.Logo.disabled = true;
        document.basketcontrol.Logo.selectedIndex = 2;
    }
    else {
        document.basketcontrol.Logo.disabled = false;
        //document.basketcontrol.Logo.selectedIndex = 0;
    }
    
    if (theval=='True'){
        Logo.innerHTML = 'Yes';
    }
    else {
        Logo.innerHTML = 'No';
    }
}
