
var MasterpageID = "ctl00_cphMaster_";

function SearchOnFocus(ctrl)
{
    if(ctrl.value == document.getElementById('ctl00_cphMaster_LeftNav1_AbtWinesSearch_hdnSearchWines').value)
    {
        ctrl.value = '';
    }
}
function SearchOnBlur(ctrl)
{   	  	  
    if(ctrl.value == "")
        ctrl.value = document.getElementById('ctl00_cphMaster_LeftNav1_AbtWinesSearch_hdnSearchWines').value;	        
  	   
}

function GuaranteeOnFocus(ctrl)
{
    if(ctrl.value == document.getElementById('ctl00_cphMaster_WineGuarantee1_hdnWineGuarantee').value)
    {
        ctrl.value = '';
    }
   
}
function GuaranteeOnBlur(ctrl)
{   	  	  
    if(ctrl.value == "")
        ctrl.value = document.getElementById('ctl00_cphMaster_WineGuarantee1_hdnWineGuarantee').value;
      
}

function DoSearch() {	
	var x = document.getElementById('ctl00_cphMaster_LeftNav1_AbtWinesSearch_keyword').value;
	// search
	if (x != "") {
	    var x1 = x.replace("@","");
	    var x2 = x1.replace("#","");
	     var x3 = x2.replace("&","");
	     var x4 = x3.replace("?","");
		window.location = "../search_results.aspx?keyword=" + x4;
		return false;
	} else {
		alert(GetString("EnterProperSearchItem"));
		return false;
	}		
}
// Set site language on cookie
function SetSiteLanguage(ctrl)
{ 
    setCookie('currentCulture',document.getElementById(ctrl.id).value,30);    
    // reload the page with the selected culture    
    window.location.reload();
    
}

// get cookie
function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}
// set cookie
function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function GetString(keyName)
{
    var localizedKeyName = keyName;
    if(getCookie("currentCulture") == 'fr-ch')
    {
        localizedKeyName = "fr" + keyName;
    }
    if(getCookie("currentCulture") == 'de-ch')
    {
        localizedKeyName = "de" + keyName;
    }    
    return eval(localizedKeyName);
}

//Function to Validate in Login Page
function fnLoginValidation()
{
    document.getElementById(MasterpageID + "txtEmailId").style.background = '#efefef';
    if(trim(document.getElementById(MasterpageID + "txtEmailId").value).length == 0 )
    {
        alert(GetString("EnterEmail"));
        document.getElementById(MasterpageID + "txtEmailId").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtEmailId").focus();
        return false;
    }

    document.getElementById(MasterpageID + "txtEmailId").style.background = '#efefef';
    var e = document.getElementById(MasterpageID + "txtEmailId").value;
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!(filter.test(e))) 
    {       
        alert(GetString("InvalidEmail"));        
        document.getElementById(MasterpageID + "txtEmailId").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtEmailId").focus();
        return false;
    }

    document.getElementById(MasterpageID + "txtPassword").style.background = '#efefef';
    if((document.getElementById(MasterpageID + "rdoOldCustomer").checked == true) && (trim(document.getElementById(MasterpageID + "txtPassword").value).length == 0 ))
    {
        alert(GetString("EnterPassword"));
        document.getElementById(MasterpageID + "txtPassword").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtPassword").focus();
        return false;
    }
    return true;
}

//disable/enable password field based on login option
function fnChangeLogin()
{
    document.getElementById(MasterpageID + "rdoOldCustomer").checked = true;
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

//Validation on New User Registration
function fnValidateUserRegistration()
{
    document.getElementById(MasterpageID +"txtFirstName").style.background = '#efefef';
        if (trim(document.getElementById(MasterpageID + "txtFirstName").value).length == 0) {
        alert(GetString("EnterFirstName"));
        document.getElementById(MasterpageID +"txtFirstName").style.background = '#FFFFcc';
        document.getElementById(MasterpageID +"txtFirstName").focus();
        return false;
    }
    document.getElementById(MasterpageID +"txtLastName").style.background = '#efefef';
        if (trim(document.getElementById(MasterpageID + "txtLastName").value).length == 0) {
        alert(GetString("EnterLastName"));
        document.getElementById(MasterpageID +"txtLastName").style.background = '#FFFFcc';
        document.getElementById(MasterpageID +"txtLastName").focus();
        return false;
    }
    
    document.getElementById(MasterpageID + "txtEmailID").style.background = '#efefef';
    if(trim(document.getElementById(MasterpageID + "txtEmailID").value).length == 0 )
    {
        alert(GetString("EnterEmail"));
        document.getElementById(MasterpageID + "txtEmailID").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtEmailID").focus();
        return false;
    }

    document.getElementById(MasterpageID + "txtEmailID").style.background = '#efefef';
    var e = document.getElementById(MasterpageID + "txtEmailID").value;
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!(filter.test(e))) 
    {
        alert(GetString("InvalidEmail"));
        document.getElementById(MasterpageID + "txtEmailID").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtEmailID").focus();
        return false;
    }
    
    document.getElementById(MasterpageID + "txtConfirmEmailID").style.background = '#efefef';
        if (trim(document.getElementById(MasterpageID + "txtEmailID").value) != trim(document.getElementById(MasterpageID + "txtConfirmEmailID").value)) {
            alert(GetString("EmailDoNotMatch"));
            document.getElementById(MasterpageID + "txtConfirmEmailID").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtConfirmEmailID").focus();
            return false;
        }

    document.getElementById(MasterpageID + "txtPassword").style.background = '#efefef';
        if (trim(document.getElementById(MasterpageID + "txtPassword").value).length < 6) {
            alert(GetString("PasswordAtleast6Char"));
            document.getElementById(MasterpageID + "txtPassword").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtPassword").focus();
            return false;
        }
         
        document.getElementById(MasterpageID + "txtConfirmPassword").style.background = '#efefef';
        if (trim(document.getElementById(MasterpageID + "txtPassword").value) != trim(document.getElementById(MasterpageID + "txtConfirmPassword").value)) {
            alert(GetString("PasswordDoNotMatch"));
            document.getElementById(MasterpageID + "txtConfirmPassword").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtConfirmPassword").focus();
            return false;
        }
        
        document.getElementById(MasterpageID + "txtAddress1").style.background = '#efefef';
        if (trim(document.getElementById(MasterpageID + "txtAddress1").value).length == 0) {
            alert(GetString("EnterAddress"));
            document.getElementById(MasterpageID + "txtAddress1").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtAddress1").focus();
            return false;
        }
         
         document.getElementById(MasterpageID + "txtZipCode").style.background = '#efefef';
        if (trim(document.getElementById(MasterpageID + "txtZipCode").value).length == 0) {
            alert(GetString("EnterZipcode"));
            document.getElementById(MasterpageID + "txtZipCode").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtZipCode").focus();
            return false;
        }
        
        if(document.getElementById("city") == null)
            {
                if ((document.getElementById(MasterpageID + "spnCity") == null) || (document.getElementById("city1") != null))
                {
                    alert(GetString("EnterValidZipcode"));
                    document.getElementById(MasterpageID + "txtZipCode").style.background = '#FFFFcc';
                    document.getElementById(MasterpageID + "txtZipCode").focus();
                    return false;
                }               
            }
            else
            {   
                if(document.getElementById("city").options != null)
                {  
                   if(document.getElementById("city").options.length == 1)
                    {                
                        alert(GetString("EnterValidZipcode"));
                        document.getElementById(MasterpageID + "txtZipCode").style.background = '#FFFFcc';
                        document.getElementById(MasterpageID + "txtZipCode").focus();
                        return false;  
                    }
                    else
                    {                   
                        document.getElementById("city").style.background = '#efefef';
                        if ((document.getElementById("city").value == GetString("ClickToSelectSuburb")) 
                        || (document.getElementById("city").value == GetString("EnterPostCode")))
                        {
                            alert(GetString("SelectValidSuburb"));
                            document.getElementById("city").style.background = '#FFFFcc';
                            document.getElementById("city").focus();
                            return false;           
                        }
                    }
                }
            }
        
         document.getElementById(MasterpageID + "txtPhone").style.background = '#efefef';
        if (trim(document.getElementById(MasterpageID + "txtPhone").value).length == 0) {
            alert(GetString(EnterPhoneNumber));
            document.getElementById(MasterpageID + "txtPhone").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtPhone").focus();
            return false;
        }
        else
	      {
      		var e = document.getElementById(MasterpageID + "txtPhone").value;
            var numericexp = /^[0-9]+$/;
            if (!(e.match(numericexp)))
            {
		        alert(GetString(InvalidPhoneNumber));
		        document.getElementById(MasterpageID + "txtPhone").style.background = '#FFFFcc';
		        document.getElementById(MasterpageID + "txtPhone").focus();
		        return false;
      		}
         }
         
return true;
}

//Modified on 29 Jul 09 by Siva - To validate whether the State is available for the selected zip code.
//For Zipcode, city populate using Ajax
function ZipChange() 
{
    document.getElementById(MasterpageID + "spnCity").innerHTML = '<select name=city id=city><option>' + GetString("EnterZipcode") + '</option></select>';
    if (document.getElementById(MasterpageID + "txtZipCode").value.length == 0) return false;

    var SuburbSelection = ajax_request('processing.aspx?Action=GetSuburbs&zip=' + trim(document.getElementById(MasterpageID + "txtZipCode").value));
    
    if (SuburbSelection == GetString("InvalidPostcode")) {
        alert(GetString("InvalidPostcode"));
        document.getElementById(MasterpageID + "txtZipCode").value="";
        document.getElementById(MasterpageID + "txtZipCode").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtZipCode").focus();
        return false;
    } else {
        document.getElementById(MasterpageID + "spnCity").innerHTML = SuburbSelection;
        
        if (SuburbSelection.indexOf('select') == -1) {
            //SelectState();
            document.getElementById(MasterpageID + "hdnCity").value = SuburbSelection;
            //document.getElementById(MasterpageID + "txtPhone").focus();
        } 
        
    }
    return true;
}

// Selecting the State for the given zipcode using Ajax
function SelectState()
{
    var State = ajax_request('processing.aspx?Action=GetState&zip=' + document.getElementById(MasterpageID + "txtZipCode").value);
    if (State == 'NA')
    {
        alert(GetString("StateNAForZip"));
        document.getElementById(MasterpageID + "spnCity").innerHTML = '<select name=city id=city><option>' + GetString("EnterPostCode") + '</option></select>';
        document.getElementById(MasterpageID + "txtZipCode").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtZipCode").focus();
        return false;
    }
    else
    {
        document.getElementById(MasterpageID + "ddlState").value = State;
        CheckShippingState(State);
        return true;    
    }
}

// Check whether we can Ship to the State using Ajax
function CheckShippingState(State)
{
    var blnShipToState = ajax_request('processing.aspx?Action=CheckShippingState&State=' + State);
    
    if (blnShipToState == 'False')
    {
        var oDDL = document.getElementById(MasterpageID + "ddlState");
        var curText = oDDL.options[oDDL.selectedIndex].text;
        var message = GetString("UnableToShipTo") + curText;
        alert(message);
        document.getElementById(MasterpageID + "spnCity").innerHTML = '<select name=city id=city><option>' + GetString("EnterPostCode") + '</option></select>';
        document.getElementById(MasterpageID + "txtZipCode").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtZipCode").focus();
        return false;
    }
        return true;    
}


function ajax_request(url) {
    if (window.XMLHttpRequest) {
        AJAX = new XMLHttpRequest();
    } else {
        AJAX = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (AJAX) {
        AJAX.open("GET", url, false);
        AJAX.send(null);
        return AJAX.responseText;
    } else {
        alert(GetString("BrowserIncompatible"));
    }
}

//Validate Shipping Address
function fnValidateShippingAddress()
{

    document.getElementById(MasterpageID + "txtFirstName").style.background = '#efefef';
        if (trim(document.getElementById(MasterpageID + "txtFirstName").value).length == 0) {
        alert(GetString("EnterFirstName"));
        document.getElementById(MasterpageID + "txtFirstName").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtFirstName").focus();
        return false;
    }
    document.getElementById(MasterpageID + "txtLastName").style.background = '#efefef';
        if (trim(document.getElementById(MasterpageID + "txtLastName").value).length == 0) {
        alert(GetString("EnterLastName"));
        document.getElementById(MasterpageID + "txtLastName").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtLastName").focus();
        return false;
    }
   
        document.getElementById(MasterpageID + "txtAddress1").style.background = '#efefef';
        if (trim(document.getElementById(MasterpageID + "txtAddress1").value).length == 0) {
            alert(GetString("EnterAddress"));
            document.getElementById(MasterpageID + "txtAddress1").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtAddress1").focus();
            return false;
        }
        document.getElementById(MasterpageID + "txtCity").style.background = '#efefef';
        if (trim(document.getElementById(MasterpageID + "txtCity").value).length == 0) {
            alert(GetString("EnterCity"));
            document.getElementById(MasterpageID + "txtCity").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtCity").focus();
            return false;
        }
         
        document.getElementById(MasterpageID + "txtZipCode").style.background = '#efefef';
        if (trim(document.getElementById(MasterpageID + "txtZipCode").value).length == 0) {
            alert(GetString("EnterZipcode"));
            document.getElementById(MasterpageID + "txtZipCode").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtZipCode").focus();
            return false;
        }
        
        document.getElementById(MasterpageID + "txtZipCode").style.background = '#efefef';
        if (trim(document.getElementById(MasterpageID + "txtZipCode").value).length < 4) {
            alert(GetString("EnterValidZipcode"));
            document.getElementById(MasterpageID + "txtZipCode").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtZipCode").focus();
            return false;
        }
        /*
        if(document.getElementById("city") == null)
            {
                if ((document.getElementById(MasterpageID + "spnCity") == null) || (document.getElementById("city1") != null))
                {
                    alert(GetString("EnterValidZipcode"));
                    document.getElementById(MasterpageID + "txtZipCode").style.background = '#FFFFcc';
                    document.getElementById(MasterpageID + "txtZipCode").focus();
                    return false;
                }               
            }
            else
            {   
                if(document.getElementById("city").options != null)
                {  
                   if(document.getElementById("city").options.length == 1)
                    {                
                        alert(GetString("EnterValidZipcode"));
                        document.getElementById(MasterpageID + "txtZipCode").style.background = '#FFFFcc';
                        document.getElementById(MasterpageID + "txtZipCode").focus();
                        return false;  
                    }
                    else
                    {                   
                        document.getElementById("city").style.background = '#efefef';
                        if ((document.getElementById("city").value == GetString("ClickToSelectSuburb")) 
                        || (document.getElementById("city").value == GetString("EnterPostCode")))
                        {
                            alert(GetString("SelectValidSuburb"));
                            document.getElementById("city").style.background = '#FFFFcc';
                            document.getElementById("city").focus();
                            return false;           
                        }
                    }
                }
            }*/
         
return true;
}

//PaymentOptions, Forgot Password pages script has been added 
//Start Here

function ClientCheck() 
{
    var valid = false;
    //var ctrlID = "ctl00_cphMaster_";
    var grid = document.getElementById(MasterpageID + "gvPaymentDetails");
    var cell;
        
    //loop starts from 1. rows[0] points to the header.
    for (var i = 1; i < grid.rows.length; i++)
    {
        cell = grid.rows[i].cells[0];
        //loop according to the number of childNodes in the cell
        for (var j=0; j<cell.childNodes.length; j++)
        {         
            //if childNode type is CheckBox and its checked       

    
            if (cell != null && cell.childNodes[j].type =="checkbox" && cell.childNodes[j].checked)
            {
                //assign the status of the Select All checkbox to the cell checkbox within the grid
                valid = true;
                break;
            }
        }
    }
    if (!valid) 
    {
        alert(GetString("SelectCreditCardToDel"));
        return false;
    }
    else
    {
	    if (confirm(GetString("ConfirmCreditCardDel"))==true)
		    return true;
	    else
		    return false;
    }
    //return valid;
}

function validate() 
{
    var var1 = new Date();
    var CurrMonth = var1.getMonth();
    var CurrYear = var1.getFullYear();
    //var ctrlID = "ctl00_cphMaster_";
    var ccnum = document.getElementById(MasterpageID + "txtCardNumber").value;
    var seccode = document.getElementById(MasterpageID + "txtSecCode").value;
   
//    if (document.getElementById(MasterpageID + "ddlPayment").value == 'Select') 
//    {
//        alert('Please select the payment type.');
//        document.getElementById(MasterpageID + "ddlPayment").style.background = '#FFFFcc';
//        document.getElementById(MasterpageID + "ddlPayment").focus();
//        return false;
//    }
    document.getElementById(MasterpageID + "txtCardNumber").style.background = '#efefef';
    //alert(ccnum.length);
    if(ccnum.length > 0)
    {
         ccnum = ccnum.replace(/ /g, '').replace(/-/g, '');
         document.getElementById(MasterpageID + "txtCardNumber").style.background = '#efefef';     
         var nDigitCheck = ccnum.substring(0,6);
         var jcbDigitCheck = ccnum.substring(0,4);
         var jcbCheck = ccnum.substring(0,1);
         var cbDigitCheck = ccnum.substring(0,2)
          
         if (((300000 <= nDigitCheck) && (309999 >= nDigitCheck)) || ((601100 <= nDigitCheck) && (601199 >= nDigitCheck)))
        {
            alert(GetString("CreditCardCannotBeUsed"));
            document.getElementById(MasterpageID + "txtCardNumber").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtCardNumber").focus();
            return false;
        } 
        
        if(cbDigitCheck == "38" )
        {
            alert(GetString("CreditCardCannotBeUsed"));
            document.getElementById(MasterpageID + "txtCardNumber").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtCardNumber").focus();
            return false;
        } 
        
         if(((jcbDigitCheck == "1800" || jcbDigitCheck == "2131" || jcbDigitCheck == "2014" || jcbDigitCheck == "2149") && ccnum.length == 15) || (jcbCheck == "3" && ccnum.length == 16))
        {
            alert(GetString("CreditCardCannotBeUsed"));
            document.getElementById(MasterpageID + "txtCardNumber").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtCardNumber").focus();
            return false;
        }
        
        if (!isValidCardNumber(ccnum)) 
        {
            alert(GetString("EnterValidCreditCard"));
            document.getElementById(MasterpageID + "txtCardNumber").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtCardNumber").focus();
            return false;
        }
        document.getElementById(MasterpageID + "txtSecCode").style.background = '#efefef';
        
         if (seccode.length <= 2)
        {
            alert(GetString("EnterValidSecCode"));
            document.getElementById(MasterpageID + "txtSecCode").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtSecCode").focus();
            return false;
        }
        
        if (!isValidSecCode(seccode)) 
        {
            alert(GetString("Enter3or4DigitSecCode"));
            document.getElementById(MasterpageID + "txtSecCode").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "txtSecCode").focus();
            return false;
        }
        
//        if (seccode.length != 3)
//        {
//            alert(GetString("EnterValidSecCode"));
//            document.getElementById(MasterpageID + "txtSecCode").style.background = '#FFFFcc';
//            document.getElementById(MasterpageID + "txtSecCode").focus();
//            return false;
//        }
//        
//        if (!isValidSecCode(seccode)) 
//        {
//            alert(GetString("Enter3DigitSecCode"));
//            document.getElementById(MasterpageID + "txtSecCode").style.background = '#FFFFcc';
//            document.getElementById(MasterpageID + "txtSecCode").focus();
//            return false;
//        }
        
        document.getElementById(MasterpageID + "ddlMonth").style.background = '#efefef';
        if (document.getElementById(MasterpageID + "ddlMonth").selectedIndex == 0)
         {
            alert(GetString("EnterExpMonth"));
            document.getElementById(MasterpageID + "ddlMonth").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "ddlMonth").focus();
            return false;
        }

       document.getElementById(MasterpageID + "ddlYear").style.background = '#efefef';
        if (document.getElementById(MasterpageID + "ddlYear").selectedIndex == 0) 
        {
            alert(GetString("EnterExpyear"));
            document.getElementById(MasterpageID + "ddlYear").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "ddlYear").focus();
            return false;
        }
        
       document.getElementById(MasterpageID + "ddlMonth").style.background = '#efefef';
        if ((document.getElementById(MasterpageID + "ddlMonth").value < CurrMonth+1)&&( document.getElementById(MasterpageID + "ddlYear").value <= CurrYear)) 
        {
            alert(GetString("ExpDateLessThanCurrDate"));
            document.getElementById(MasterpageID + "ddlMonth").style.background = '#FFFFcc';
            document.getElementById(MasterpageID + "ddlMonth").focus();
            return false;
        }
    }
    else
    {
        alert(GetString("EnterValidCreditCard"));
        document.getElementById(MasterpageID + "txtCardNumber").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtCardNumber").focus();
        return false;
    }
}
    
function isValidCardNumber(strNum)
{
    //var nCheck = 0;
    var nDigit = 0;
    //var bEven = false;
    var checksum = 0;
    //var nDigitCheck = strNum.substring(0,6);
    
    //alert("Entered isValidCardNumber");

        for (var n=(2-(strNum.length % 2)); n<=strNum.length; n+=2)
	    {
	        checksum += parseInt(strNum.charAt(n-1));
	    }
	    for (n =(strNum.length % 2) + 1; n<strNum.length; n+=2) 
	    {
	        var cDigit = strNum.charAt(n);
	        if (isDigit(cDigit))
	       {
		    cDigit = parseInt(strNum.charAt(n-1)) * 2;
		    if (cDigit < 10) 
		    {
		     checksum += cDigit; 
		     } 
		     else 
		     { checksum += (cDigit-9); 
		     }
	       }
	        else if (cDigit != ' ' && cDigit != '.' && cDigit != '-') 
	        {
		    return false;
	        }
	    }

		return (checksum % 10) == 0;
}

function isValidSecCode(strNum)
{
    var nCheck = 0;
    var nDigit = 0;
    var bEven = false;
    var result = false;
    for (n = strNum.length - 1; n >= 0; n--) 
    {
        var cDigit = strNum.charAt(n);
        if (isDigit(cDigit)) 
        {
            var nDigit = parseInt(cDigit, 10);
            nCheck += nDigit;
        }
        else 
        {
            return false;
        }
    }
    return true;
}
        
function isDigit(c) 
{
    var strAllowed = "1234567890";
    return (strAllowed.indexOf(c) != -1);
}

function validateEmail() 
{
    document.getElementById(MasterpageID + "txtEmail").style.background = '#efefef';
    var e = document.getElementById(MasterpageID + "txtEmail").value;
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!(filter.test(e))) 
    {
        alert(GetString("InvalidEmail"));
        document.getElementById(MasterpageID + "txtEmail").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtEmail").focus();
        return false;
    }
    return true;
}

function validatePassword()
{
    document.getElementById(MasterpageID + "txtPassword").style.background = '#efefef';
    if (document.getElementById(MasterpageID + "txtPassword").value.length < 8) 
    {
        alert(GetString("PasswordAtleast8Char"));
        document.getElementById(MasterpageID + "txtPassword").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtPassword").focus();
        return false;
    }

    document.getElementById(MasterpageID + "txtPasswordRepeat").style.background = '#efefef';
    if (document.getElementById(MasterpageID + "txtPassword").value != document.getElementById(MasterpageID + "txtPasswordRepeat").value) 
    {
        alert(GetString("PasswordDoNotMatch"));
        document.getElementById(MasterpageID + "txtPasswordRepeat").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtPasswordRepeat").focus();
        return false;
    }
    return true;
}

//End Here


// Function to call the pages by selecting the dropdown in helpful hints pages
//Start
function MM_jumpMenu(targ,selObj,restore)
{ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//End


function fnValidateQuantity()
{ 
    var regExp = /^[0-9]*$/;  
    document.getElementById(MasterpageID + "txtQuantity").style.background = '#efefef';
        if (trim(document.getElementById(MasterpageID + "txtQuantity").value).length == 0) {
        alert(GetString("EnterQuantity"));
        document.getElementById(MasterpageID + "txtQuantity").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtQuantity").focus();
        return false;
    }
    else if (1 > (trim(document.getElementById(MasterpageID + "txtQuantity").value)))
    {
        alert(GetString("QuantityCannotBeZero"));
        document.getElementById(MasterpageID + "txtQuantity").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtQuantity").focus();
        return false;
    }
    else if ((trim(document.getElementById(MasterpageID + "txtQuantity").value)) > 999)
    {
        alert(GetString("QuantityGreaterThan999"));
        document.getElementById(MasterpageID + "txtQuantity").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtQuantity").focus();
        return false;
    }
    else if(regExp.test(trim(document.getElementById(MasterpageID + "txtQuantity").value))==false)
	{
        alert(GetString("EnterValidNumericQuantity"));
	    document.getElementById(MasterpageID + "txtQuantity").style.background = '#FFFFcc';
        document.getElementById(MasterpageID + "txtQuantity").focus();
        return false;
	}	
    return true;
}


 function EditShoppingCart()
 {
   window.location="Shopping_Cart.aspx?action=edit";

 }
 
  function verifyAge() 
  {  
  if (!(document.getElementById("age_verif3").checked))
  { 
	alert(GetString("Certify21YearsOlder"));
	document.getElementById("age_verif3").style.background = '#FFFFcc';
	document.getElementById("age_verif3").focus(); 	
	return false;
  }
  else
  {
    //document.getElementById("checkout").submit();
    return true;
  }
}
   
    function checkOne(rdoIdOriginal)
    {
        var rdoId = rdoIdOriginal;
        var rdoCommonID;
        if(rdoId.indexOf('rdo1') > 0)
        {        
            rdoCommonID = rdoId.replace('rdo1','')
        }
        if(rdoId.indexOf('rdo2') > 0)
        {        
            rdoCommonID = rdoId.replace('rdo2','')
        }
        if(rdoId.indexOf('rdo3') > 0)
        {        
            rdoCommonID = rdoId.replace('rdo3','')
        }
        //var items=document.getElementById("frmSearchResults"); //your form name
        var items = document.aspnetForm;
        for(var i=0;i<items.length;i++)
        {
            if(items.elements[i].type=="radio")//detect radio button
            {
                  if(items.elements[i].id.indexOf("productsList")>-1)//datalist name
                  {
                    if(items.elements[i].id.indexOf(rdoCommonID)>-1) // prefixed client ID for checking
                            items.elements[i].checked=false;
                    if(items.elements[i].id == rdoIdOriginal)
                        items.elements[i].checked=true;
                  } 
            }
        }
    }
   
	function updateQty(xx, x, id, oldQty) 
	{
	    document.getElementById("ctl00_cphMaster_btnContinueShoppingTop").disabled = true;
	    document.getElementById("ctl00_cphMaster_btnContinueShoppingBottom").disabled = true;
	    document.getElementById("ctl00_cphMaster_btnCheckoutTop").disabled = true;
	    document.getElementById("ctl00_cphMaster_btnCheckoutBottom").disabled = true;
	    document.getElementById("ctl00_cphMaster_submitPromo").disabled = true;

		var prodid = parseInt(id.value);
		if (prodid > 0 && prodid < 1000) 
		{
		    if(oldQty != eval(prodid))
		    {		    
			    // update quantity field
			    window.location.href = "shopping_cart.aspx?action=update&id=" + x + "&qty=" + eval(prodid);
			}
			else if(oldQty == eval(prodid))
			{
			    document.getElementById("ctl00_cphMaster_btnContinueShoppingTop").disabled = false;
	            document.getElementById("ctl00_cphMaster_btnContinueShoppingBottom").disabled = false;
	            document.getElementById("ctl00_cphMaster_btnCheckoutTop").disabled = false;
	            document.getElementById("ctl00_cphMaster_btnCheckoutBottom").disabled = false;
	            document.getElementById("ctl00_cphMaster_submitPromo").disabled = false;
			}
		} 
		else if (prodid > 999) 
		{
		    alert(GetString("QuantityGreaterThan999"));
		    id.focus();		
		} 
		else if (prodid < 0 ) 
		{
		    alert(GetString("EnterValidQuantity"));
		    id.focus();		
		}
		else if(prodid == 'NaN')
		{
            alert(GetString("EnterValidNumericQuantity"));
		    id.focus();		
		} 
		else {
			alert(GetString("QuantityCannotBeZero"));
			id.focus();
		}
	}
	function clearCart() 
	{
	    document.getElementById("ctl00_cphMaster_btnContinueShoppingTop").disabled = true;
	    document.getElementById("ctl00_cphMaster_btnContinueShoppingBottom").disabled = true;
	    document.getElementById("ctl00_cphMaster_btnCheckoutTop").disabled = true;
	    document.getElementById("ctl00_cphMaster_btnCheckoutBottom").disabled = true;
	    document.getElementById("ctl00_cphMaster_submitPromo").disabled = true;

		if (confirm(GetString("ConfirmDeleteAllItems"))) {			
			window.location.href = "shopping_cart.aspx?action=deleteall";
		} 
		else 
		{
			document.getElementById("ctl00_cphMaster_btnContinueShoppingTop").disabled = false;
			document.getElementById("ctl00_cphMaster_btnContinueShoppingBottom").disabled = false;
	        document.getElementById("ctl00_cphMaster_btnCheckoutTop").disabled = false;
	        document.getElementById("ctl00_cphMaster_btnCheckoutBottom").disabled = false;
	        document.getElementById("ctl00_cphMaster_submitPromo").disabled = false;
		}
	}
	
	function updateQtyPB(xx, x, id) 
	{
	    document.getElementById("ctl00_cphMaster_btnContinueShoppingTop").disabled = true;
	    document.getElementById("ctl00_cphMaster_btnContinueShoppingBottom").disabled = true;
	    document.getElementById("ctl00_cphMaster_btnCheckoutTop").disabled = true;
	    document.getElementById("ctl00_cphMaster_btnCheckoutBottom").disabled = true;
	    document.getElementById("ctl00_cphMaster_submitPromo").disabled = true;

		var prodid = id.value;
		if (prodid > 0 && (prodid % 6) == 0 ) {
			// update quantity field
			window.location.href = "shopping_cart.aspx?action=update&id=" + x + "&qty=" + eval(prodid);
		} else {
			alert(GetString("EnterQtyInMultiplesOf6"));
			id.focus();
		}
	}
	
	function removeItem(x) 
	{
	    document.getElementById("ctl00_cphMaster_btnContinueShoppingTop").disabled = true;
	    document.getElementById("ctl00_cphMaster_btnContinueShoppingBottom").disabled = true;
	    document.getElementById("ctl00_cphMaster_btnCheckoutTop").disabled = true;
	    document.getElementById("ctl00_cphMaster_btnCheckoutBottom").disabled = true;
	    document.getElementById("ctl00_cphMaster_submitPromo").disabled = true;
		
		if (confirm(GetString("ConfirmDeleteThisItem"))) {
			// update quantity field
			location.href = "shopping_cart.aspx?action=delete&id=" + x;
		} 
		else 
		{
		    document.getElementById("ctl00_cphMaster_btnContinueShoppingTop").disabled = false;
			document.getElementById("ctl00_cphMaster_btnContinueShoppingBottom").disabled = false;
	        document.getElementById("ctl00_cphMaster_btnCheckoutTop").disabled = false;
	        document.getElementById("ctl00_cphMaster_btnCheckoutBottom").disabled = false;
	        document.getElementById("ctl00_cphMaster_submitPromo").disabled = false;
		}
	}
	
	function SubmitPromo()
	{
	    var promoCode = document.getElementById("txtPromo").value;
		if (promoCode == "") 
		{
		    alert(GetString("EnterPromoCode"));
		    document.getElementById("txtPromo").focus();		
		} 
		else 
		{
			location.href = "shopping_cart.aspx?coupon_code=" + promoCode;
		}
	}
	 function IsNumeric(strString)
   //  check for valid numeric strings	
   {
		var strValidChars = "0123456789.";
		var strChar;
		var blnResult = true;

		if (strString.length == 0) return false;

		//  test strString consists of valid characters listed above
		for (i = 0; i < strString.length && blnResult == true; i++)
			{
			strChar = strString.charAt(i);
			if (strValidChars.indexOf(strChar) == -1)
				{
					blnResult = false;
				}
			}
		return blnResult;
   }	
	
	// START - INFOSYS (Ram) - 15-Aprl-2010 - Bug # 1329
	function RedirectForCheckout()
	{
	   var url = window.location;
       var fullhost = window.location.hostname;
	   
	   if (IsNumeric(fullhost) == false && fullhost.indexOf("alpha")==-1 && fullhost.indexOf("beta")==-1 && fullhost.indexOf("staging")==-1) 
       {
            window.location.href = "https://"  + fullhost + "/AddressForm.aspx";
       }
       else 
       {
            window.location.href = "AddressForm.aspx";
       }
	}
   
function SecureRedirect(loginflag)
	{
	   var url = window.location;
       var fullhost = window.location.hostname;
       if (IsNumeric(fullhost) == false && fullhost.indexOf("alpha")==-1 && fullhost.indexOf("beta")==-1 && fullhost.indexOf("staging")==-1) 
       {
        if(loginflag==0)
        {
            window.location.href = "https://"  + fullhost + "/Login.aspx";
        }
        else
        {
            window.location.href = "https://"  + fullhost + "/Login.aspx?LogOut=true";
        }
       }
       else 
       {
       window.location.href = "http://"  + fullhost + "/Login.aspx";
       }
	}
   
   // END - INFOSYS (Ram) - 15-Aprl-2010 - Bug # 1329
   
    
function fnEmailSignupValidation()
{
    document.getElementById("ctl00_cphMaster_WineGuarantee1_emailsignup").style.background = '#efefef';
    if(trim(document.getElementById("ctl00_cphMaster_WineGuarantee1_emailsignup").value).length == 0 )
    {
        alert(GetString("EnterEmail"));
        document.getElementById("ctl00_cphMaster_WineGuarantee1_emailsignup").style.background = '#FFFFcc';
        document.getElementById("ctl00_cphMaster_WineGuarantee1_emailsignup").focus();
        return false;
    }

    document.getElementById("ctl00_cphMaster_WineGuarantee1_emailsignup").style.background = '#efefef';
    var e = document.getElementById("ctl00_cphMaster_WineGuarantee1_emailsignup").value;
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!(filter.test(e))) 
    {       
        alert(GetString("InvalidEmail"));        
        document.getElementById("ctl00_cphMaster_WineGuarantee1_emailsignup").style.background = '#FFFFcc';
        document.getElementById("ctl00_cphMaster_WineGuarantee1_emailsignup").focus();
        return false;
    }
    var EmailSignUp = ajax_request('processing.aspx?Action=EmailSignUp&EmailId=' + trim(document.getElementById("ctl00_cphMaster_WineGuarantee1_emailsignup").value));  
    if(EmailSignUp = true)
    {
        document.getElementById("ctl00_cphMaster_WineGuarantee1_emailsignup").style.visibility = "hidden";
        document.getElementById("emailerror").innerHTML = GetString("ThanksForSubscribing");
    }
//    else
//    {
//        return false;
//    }
    return true;
}
function launchpopup(popup)
{
	var url = popup;

	window.open(url,'popup','height=660,width=640,scrollbars=yes,resizable=no,left = 100,top = 20');
}

function ChangeLoginButton(SelectedRadioButton)
{
    if(SelectedRadioButton == "NewCustomer")
    {
        document.getElementById('ctl00_cphMaster_btnLogin').value = GetString("Register");
    }
    else if(SelectedRadioButton == "OldCustomer")
    {
        document.getElementById('ctl00_cphMaster_btnLogin').value = GetString("Login");
    }
}
function ValidateCaseSelection(strElementType)
{
    var maxNoOfQty = trim(document.getElementById('hdnMaxAllowedQty').value);
    var maxNoOfCase = trim(document.getElementById('hdnMaxAllowedCases').value); 
    var SelectedNoOfCase = 0;
    var SelectedNoOfQty = 0;
    var elementArray = document.forms["form"].elements;
    var blnSelected = false;    
    //loop starts from 1. rows[0] points to the header.
    for (i=1; i<elementArray.length; i++)
    {            
        var element =  elementArray[i];
        
//        if (element.id.indexOf("preference") != -1)
//        {
            if( element.type == strElementType)
            {
                if (element.checked == true)
                {
                    blnSelected = true;
                    SelectedNoOfCase += 1;                            
                }
            }
            if( element.type == "text")
            { 
                if (blnSelected == true)               
                {
                    if (ValidateQty(element) == true)
                    {
                        SelectedNoOfQty += parseInt(trim(element.value));
                    }
                    else
                    {
                        element.select();
                        return false;
                    }
                }                
                blnSelected = false;
            }
        //}                
    }
    if(SelectedNoOfCase == 0)
    {
        alert(GetString("SelectACase"));
        return false;
    }
    if((SelectedNoOfCase > maxNoOfCase))
    {
        alert(GetString("LtdNoPerHousehold").replace("[X]", maxNoOfCase));
        return false;
    }
    if (SelectedNoOfQty > maxNoOfQty) {
        alert(GetString("MaxQtyPerHousehold").replace("[X]", maxNoOfQty));
        return false;
    }
    EnableQtyTextBox(strElementType);
    document.forms["form"].submit();
}
function EnableQtyTextBox(strElementType)
{
    var SelectedACase;
    SelectedACase = false;
    var elementArray = document.forms["form"].elements;
    //loop starts from 1. rows[0] points to the header.
    for (i=1; i<elementArray.length; i++)
    {            
        var element =  elementArray[i];
        if( element.type == strElementType)
        {
            if (element.checked == true)
            {
                SelectedACase = true;                           
            }
            else
            {
                SelectedACase = false;
            }
        }
        if(element.type == "text" && SelectedACase == true)
        {
            SelectedACase = false;
            element.disabled = false;
            if(element.value == "")
            {
                element.value = 1;
                element.select();
            }            
        }
        else if(element.type == "text" && SelectedACase == false)
        {
            element.disabled = true;
            element.value = "";
        }
    }
}
function ValidateQty(id) 
{
    var regExp = /^[0-9]*$/;
    if(regExp.test(trim(id.value))==false || trim(id.value) == "")
	{
        alert(GetString("EnterValidNumericQuantity"));
	    id.select();
	    return false;
	}
	if (parseInt(id.value) == 0) {
		alert(GetString("QuantityCannotBeZero"));
		id.select();
		return false;
	}
	return true;
//	var maxNoOfCase = trim(document.getElementById('maxAllowedCases').value);
//	if(maxNoOfCase !="" && maxNoOfCase>0)
//	{
//	    if (parseInt(id.value) > maxNoOfCase) {
//		    alert(GetString("QuantityGreaterThan999").replace("999", maxNoOfCase));
//		    id.select();
//		    return;
//	    }
//	}
}