// Validation rounites for Advansys eCommerce with CMS

function MM_swapImgRestore() { //v3.0
  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 MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  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_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function popImage(imageURL,imageTitle){
	openPopup(imageURL,imageTitle);
}

function openPopup (imageURL, caption) {

/*
This routine creates a pop-up window, and ensures that it takes focus. It is 
intended to be called from an anchor tag. The new window will resize itself to 
the optimum size, so we make it as large as the largest required window to
overcome bugs in various manifestations of various browsers.
  
Author:   John Gardner
Written:  8th November 2003
Updated:  27th January 2004

Calling sequence: <a href="a.jpg" onClick="return openLargeImage('a.jpg','Caption');">

The first parameter is the URL of the image to be opened, and the second 
parameter is the caption for the image which is displayed in the window title
and in the alt property of the image tag.

Note that the calling sequence will simply open the image in the main window if
JavaScript isn't enabled.
  
*/

  // Constants - change these to suit your requirements Note that the defaultWidth
  // and defaultHeight variables should be set to more than your largest image to
  // overcome a bug in Mozilla (at least up to Firefox 0.9).

  var windowTop = 50;                // Top position of popup
  var windowLeft = 100                // Left position of popup
  var defaultWidth = 730;             // Default width (for browsers that cannot resize)
  var defaultHeight = 532;            // Default height (for browsers that cannot resize)
  var onLoseFocusExit = true;         // Set if window to exit when it loses focus
  var undefined;

  // Set up the window open options
  var Options = "width=" + defaultWidth + ",height=" + defaultHeight + ",top=" + windowTop + ",left=" + windowLeft + ",scrollbars=yes,resizable"
  
  // Now write the HTML markup to the new window, ensuring that we insert the 
  // parameter URL of the image and the parameter description of the image in 
  // the right place.
  var myScript = "<html>\n" + 
    "<head>\n" + 
	"<title>" + caption + "\</title>\n" +
	"<link href=/css/style.css rel=StyleSheet type=text/css>\n" +     
    "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\n" +
    "<meta http-equiv=\"Content-Language\" content=\"en-gb\">\n" +
    "<script language=\"JavaScript\" type=\"text/javascript\">\n" +
    "function resizewindow () {\n" +
    "  var width = document.myimage.width;\n" + 
    "  var height = document.myimage.height;\n";
  
  // Netscape  
  if (navigator.appName.indexOf("Netscape") != -1) {  
    myScript = myScript +  "  window.innerHeight = height+30;\n  window.innerWidth = width+20;\n"
  }
   
  // Opera 
  else if (navigator.appName.indexOf("Opera") != -1) {
    myScript = myScript +  "  window.resizeTo (width+20, height+60);\n"
  }
  
  // Microsoft 
  else if (navigator.appName.indexOf("Microsoft") != -1) { 
    //alert(imageURL);
	var img = new Image();
	
	img.src = imageURL;
	//alert(img.src);
	var height = img.height;
	height = img.height;
	//alert(height);
	
	if (height<=500) {
		 myScript = myScript + "  window.resizeTo (width+50, height+75);\n" 
	 } else {
		 myScript = myScript + "  window.resizeTo (width+50, height+75);\n" 
	}
	
  }
  
  // Assume a frig factor for any other browsers
  else {
    myScript = myScript + "  window.resizeTo (width+20, height+30);\n"
  }
      
  myScript = myScript + "}\n" + "window.onload = resizewindow;\n" +
    "</script>\n</head>\n" + "<body ";
    
  // If the window is required to close when it loses focus.
  if (onLoseFocusExit) {myScript = myScript + "onblur=\"self.close()\" ";}
    
  myScript = myScript + "style=\"margin: 5px; padding-right: 5px; padding-bottom: 5px; margin-bottom: 5px;\">\n" +  
	"<p align=center>\n" + 
    "<img src=\"" + imageURL + "\" alt=\"" + caption + "\" title=\"" + caption + "\" name=\"myimage\" style=\"border: solid 1px #3f3f3f\">\n" + 
    "</p></body>\n" +  "</html>\n";
        
  // Diagnostic - uncomment the next line if you wish to see the script generated.   
  //alert (myScript);
  
  // Create the popup window
  var imageWindow = window.open ("","",Options);
  //alert(myScript);
  imageWindow.document.write (myScript)
  imageWindow.document.close ();
  if (window.focus) imageWindow.focus();
  return false;
  
}
function CheckSearch(form){
	if (form.search.value.length <=1){alert('Please enter one or more keywords'); return false;}
	return true;
}

function CheckCustomerLogin(form){
	if (form.email.value=='') {alert('Please enter your email address');form.email.focus(); return false;}
	if (!CheckEmail(form.email.value)) {alert('Please enter a valid email address');form.email.focus(); return false;}
	if (form.password.value=='') {alert('Please enter your password'); form.password.focus(); return false;}
	return true;
}

function CheckCustomerData(form){
	if (form.title.value=='') {alert('Please select a title'); form.title.focus(); return false;}
	if (form.firstname.value=='') {alert('Please enter your first name'); form.firstname.focus(); return false;}
	if (form.lastname.value=='') {alert('Please enter your last name'); form.lastname.focus(); return false;}
	if (form.password.value == '') { alert('Please enter your password'); form.password.focus(); return false; }
	if (form.password.value.length < 6) {alert('Minimum length of password should be at least 6 characters');form.password.focus(); return false; }
	if (form.password.value.length > 18) { alert('Maximum length of password is 18 characters'); form.password.focus(); return false; }
	if (form.password.value.match(/[\d\.]+/g) == null) { alert('Numerical characters are required in password'); form.password.focus(); return false; }
	if (form.cpassword.value != form.password.value) {alert('Passwords do not match');form.cpassword.focus();return false; }
	if (form.address1.value=='') {alert('Please enter your address'); form.address1.focus(); return false;}
	if (form.city.value=='') {alert('Please enter your town or city'); form.city.focus(); return false;}
	if (form.county.value=='') {alert('Please enter your county'); form.county.focus(); return false;}
	if (form.postcode.value=='') {alert('Please enter your postcode'); form.postcode.focus(); return false;}
	if (form.country.value=='') {alert('Please select your country of residence'); form.country.focus(); return false;}
	if (form.firstname.value=='') {alert('Please enter your first name'); form.firstname.focus(); return false;}
	if (form.telephone.value=='') {alert('Please enter a contact telephone number'); form.telephone.focus(); return false;}
	if (form.email.value=='') {alert('Please enter your email address');form.email.focus(); return false;}
	if (!CheckEmail(form.email.value)) {alert('Please enter a valid email address');form.email.focus(); return false;}
	if (form.password.value == '') { alert('Please enter a password'); form.password.focus(); return false; }
	
	return true;
}

function CheckCardData(form, gateway){

	if (gateway == 'EPAYMENTS'){		
		if (form.ePaymentsStreet.value=="") {alert('Please enter your street'); form.ePaymentsStreet.focus(); return false;}
		if (form.ePaymentsCity.value=="") {alert('Please enter your town or city'); form.ePaymentsCity.focus(); return false;}
		if (form.ePaymentsState.value=="") {alert('Please enter your state'); form.ePaymentsState.focus(); return false;}
		if (form.ePaymentsPostCode.value=="") {alert('Please enter your postcode'); form.ePaymentsPostCode.focus(); return false;}	
		if (form.ePaymentsEmail.value=="") {alert('Please enter your email address');form.ePaymentsEmail.focus(); return false;}
		if (!CheckEmail(form.ePaymentsEmail.value)) {alert('Please enter a valid email address');form.ePaymentsEmail.focus(); return false;}
		if (form.ePaymentsPhoneNumber.value=="") {alert('Please enter a contact telephone number'); form.ePaymentsPhoneNumber.focus(); return false;}	
		if (form.ePaymentsCardType.value==""){alert('Please select the Card Type');form.ePaymentsCardType.focus();  return false;}
		if (form.ePaymentsCardNumber.value==""){alert('Please specify the Card Number');form.ePaymentsCardNumber.focus();  return false;}
		if (form.ePaymentsCardName.value==""){alert('Please specify the Card Name'); form.ePaymentsCardName.focus(); return false;}
		if (form.ePaymentsExpiryDateMM.value==""){alert('Please specify the Expiry Date');form.ePaymentsExpiryDateMM.focus();  return false;}
		if (form.ePaymentsExpiryDateYY.value==""){alert('Please specify the Expiry Date'); form.ePaymentsExpiryDateYY.focus(); return false;}
		if (form.ePaymentsCV2.value==""){alert('Please specify the CVV (last 3 digits on the reverse of you card)');form.ePaymentsCV2.focus();  return false;}
		if (form.termsagreed.checked != 1){alert('You need to agree to the terms and conditions');form.termsagreed.focus();  return false;}
		
		return true;

	}
	else if (gateway  == 'VELOCITY') {
		if (form.VPBillingStreet.value=='') {alert('Please enter your street'); form.VPBillingStreet.focus(); return false;}
		if (form.VPBillingCity.value=='') {alert('Please enter your town or city'); form.VPBillingCity.focus(); return false;}
		if (form.VPBillingState.value=='') {alert('Please enter your state'); form.VPBillingState.focus(); return false;}
		if (form.VPBillingPostCode.value=='') {alert('Please enter your postcode'); form.VPBillingPostCode.focus(); return false;}
		if (form.VPBillingPhoneNumber.value=='') {alert('Please enter a contact telephone number'); form.VPBillingPhoneNumber.focus(); return false;}
		if (form.VPBillingEmail.value=='') {alert('Please enter your email address');form.VPBillingEmail.focus(); return false;}
		if (!CheckEmail(form.VPBillingEmail.value)) {alert('Please enter a valid email address');form.VPBillingEmail.focus(); return false;}
		if (form.VPCardNumber.value==''){alert('Please specify the Card Number');form.VPCardNumber.focus();  return false;}
		if (form.VPCardName.value==''){alert('Please specify the Card Name'); form.VPCardName.focus(); return false;}
		if (form.VPStartDateMM.value==''){alert('Please specify the Start Date');form.VPStartDateMM.focus();  return false;}
		if (form.VPStartDateMM.value==''){alert('Please specify the Start Date');form.VPStartDateMM.focus();  return false;}
		if (form.VPExpiryDateMM.value==''){alert('Please specify the Expiry Date');form.VPExpiryDateMM.focus();  return false;}
		if (form.VPExpiryDateYY.value==''){alert('Please specify the Expiry Date'); form.VPExpiryDateYY.focus(); return false;}
		if (form.VPCV2.value==''){alert('Please specify the CVV (last 3 digits on the reverse of you card)');form.VPCV2.focus();  return false;}
		return true;
	
	}
	else if (gateway  == 'METACHARGE') {

		if (form.strAddress.value=='') {alert('Please enter your address'); form.strAddress.focus(); return false;}
		if (form.strCity.value=='') {alert('Please enter your town or city'); form.strCity.focus(); return false;}
		if (form.strState.value=='') {alert('Please enter your state'); form.strState.focus(); return false;}
		if (form.strPostcode.value=='') {alert('Please enter your postcode'); form.strPostcode.focus(); return false;}		
		if (form.strEmail.value=='') {alert('Please enter your email address');form.strEmail.focus(); return false;}
		if (!CheckEmail(form.strEmail.value)) {alert('Please enter a valid email address');form.strEmail.focus(); return false;}
		if (form.strTel.value=='') {alert('Please enter a contact telephone number'); form.strTel.focus(); return false;}
		if (form.strCardType.value==""){alert('Please specify the Card Type');form.strCardType.focus();  return false;}		
		if (form.strCardHolder.value==''){alert('Please specify the Card Name'); form.strCardHolder.focus(); return false;}
		if (form.strCardNumber.value==''){alert('Please specify the Card Number');form.strCardNumber.focus();  return false;}
		//if (form.MCStartDateMM.value==''){alert('Please specify the Start Date');form.MCStartDateMM.focus();  return false;}
		//if (form.MCStartDateYY.value==''){alert('Please specify the Start Date');form.MCStartDateYY.focus();  return false;}
		if (form.MCExpiryDateMM.value==''){alert('Please specify the Expiry Date');form.MCExpiryDateMM.focus();  return false;}
		if (form.MCExpiryDateYY.value==''){alert('Please specify the Expiry Date'); form.MCExpiryDateYY.focus(); return false;}
		if (form.intCV2.value==''){alert('Please specify the CVV (last 3 digits on the reverse of you card)');form.intCV2.focus();  return false;}
		return true;
	
	}
}

function CheckDeliveryData(form){
	if (form.delname.value==''){alert('Please specify the Delivery Name'); return false;}
	if (form.deladdress1.value==''){alert('Please specify the Delivery Address'); return false;}
	if (form.delcity.value==''){alert('Please specify the Delivery City'); return false;}
	if (form.delcountrycode.value==''){alert('Please specify the Delivery Country'); return false;}
	if (form.deltelephone.value==''){alert('Please specify the Telephone Number'); return false;}
	if (form.delpostcode.value==''){alert('Please specify the Delivery PostCode'); return false;}
	return true;
}

function CheckOrder(form){
	if (!form.termsagreed.checked){ alert('You must agree to the terms and conditions before placing an order'); return false;}
	return true;
}
	
function CheckEmail(address) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(address)){
	return (true)
	}
	return (false)
}

function CheckIntegerNow(e) {
var isNS4 = (navigator.appName=="Netscape")?1:0;
 if(!isNS4){
  if ((e.keyCode > 7 && e.keyCode < 10) || (e.keyCode > 27 && e.keyCode < 30) || (e.keyCode == 46) || e.keyCode > 47 && e.keyCode < 58)
  {
   e.returnValue = e.keyCode ;
  }
  else
  {
   e.cancelBubble = true ;
   e.returnValue = false ;
  }
 } else {
  if ((e.which > 7 && e.which < 10) || (e.which > 27 && e.which < 30) || (e.which == 46) || e.which > 47 && e.which < 58) {
   return true;
  }
  return false;
 }
}
var oldcolorid="depdesc1"
function changecolor(colorid){
	document.getElementById(oldcolorid).className="depdesc";
	document.getElementById(colorid).className="depdesc_sel";
	oldcolorid=colorid;
}


    function CheckAccountCustomer(form)
    {
        if(document.getElementById("title").value=="")
        {
            alert("Select the required field - Title");
            document.getElementById("title").focus();
            return false;
        }   
        if(document.getElementById("x_firstname").value=="")
        {
            alert("Enter the required field - First Name");
            document.getElementById("x_firstname").focus();
            return false;
        }
        if(document.getElementById("cmpname").value=="")
        {
            alert("Enter the required field - Company name");
            document.getElementById("cmpname").focus();
            return false;
        }
        if(document.getElementById("jobtitle").value=="")
        {
            alert("Enter the required field - Job Title");
            document.getElementById("jobtitle").focus();
            return false;
        }
        if(document.getElementById("x_address1").value=="")
        {
            alert("Enter the required field - Address 1");
            document.getElementById("x_address1").focus();
            return false;
        }
        if(document.getElementById("town").value=="")
        {
            alert("Enter the required field - Town");
            document.getElementById("town").focus();
            return false;
        }
        if(document.getElementById("county").value=="")
        {
            alert("Enter the required field - County");
            document.getElementById("county").focus();
            return false;
        }
        if(document.getElementById("postcode").value=="")
        {
            alert("Enter the required field - Post Code");
            document.getElementById("postcode").focus();
            return false;
        }
        if(document.getElementById("x_countrycode").value=="")
        {
            alert("Enter the required field - Country");
            document.getElementById("x_countrycode").focus();
            return false;
        }
        if(document.getElementById("email").value=="")
        {
            alert("Enter the required field - email address");
            document.getElementById("email").focus();
            return false;
        }
        if(document.getElementById("phone").value=="")
        {
            alert("Enter the required field - Phone");
            document.getElementById("phone").focus();
            return false;
        }
        
        if(document.getElementById("password").value=="")
        {
            alert("Enter the required field - Password");
            document.getElementById("password").focus();
            return false;
        }
          if(document.getElementById("cpassword").value=="")
        {
            alert("Enter the required field - Confirm Password");
            document.getElementById("cpassword").focus();
            return false;
        }
        
        if(document.getElementById("cpassword").value!=document.getElementById("password").value)
        {
            alert("Password do not match");
            //document.getElementById("cpassword").focus();
            return false;
        }
        
        for(i=1;i<=6;i++) 
        {   
            j=0
            if(document.getElementById("checkbox"+i).checked==true)
            {
                j++;
                return true;
            }
        }
        if(j==0){ alert("Please tick any one of the Industry/Organisation"); return false;}
        
        if(!echeck(document.getElementById("email").value))
        {
            alert("Please enter a valid email address");
            document.getElementById("email").focus();
            return false;   
        }
        return true;
    }
   function echeck(str) {
        
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		var lastdot=str.lastIndexOf(dot);
		if (str.indexOf(at)==-1){
		   
		   return false;
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   
		   return false;
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr || str.substring(lastdot+1)==""){
		   
		    return false;
		}
		 
		 if (str.indexOf(at,(lat+1))!=-1){
		   
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    
		    return false;
		 }
		if(CharsInBag(str)==false){
		    
		    return false;
		 }
		 var arrEmail=str.split("@");
		 var ldot=arrEmail[1].indexOf(".");
		 if(isInteger(arrEmail[1].substring(ldot+1))==false){
	
		    return false;
		 }
 		 return true;					
	}
	
    function diffdel()
	{
	      if (document.frmCart.diffdelchk.checked)
	        {
	        //alert('Delivery address is different');
	         document.getElementById('deladdtbl').style.display='inline';
	        }
	        else
	       {
	       //alert('Delivery address is same');
	       document.getElementById('deladdtbl').style.display='none';
	       }
	}
