//Custom Validator Custom Validation Functions
function AhsRegularExpressionValidation(val) {
    var value = ValidatorGetValue(val.controltovalidate);
    if (ValidatorTrim(value).length == 0)
        return true;
        
    var rx = new RegExp(val.validationexpression);
    var matches = rx.exec(value);
    var isRegularExpressionValid = (matches != null && value == matches[0]);
    HighlightControls(val,isRegularExpressionValid);
    
    return isRegularExpressionValid;
}

function AhsRequiredFieldValidation(val)
{
    var isRequiredFieldValid =  (ValidatorTrim(ValidatorGetValue(val.controltovalidate)) != ValidatorTrim(val.initialvalue));
    HighlightControls(val,isRequiredFieldValid);   
    return isRequiredFieldValid;
}

function AhsCompareValidation(val) {
    var value = ValidatorGetValue(val.controltovalidate);
    if (ValidatorTrim(value).length == 0)
        return true;
    var compareTo = "";
    
    if ((typeof(val.controltocompare) != "string") ||
        (typeof(document.getElementById(val.controltocompare)) == "undefined") ||
        (null == document.getElementById(val.controltocompare))) {
        if (typeof(val.valuetocompare) == "string") {
            compareTo = val.valuetocompare;
        }


    }
    else {
        compareTo = ValidatorGetValue(val.controltocompare);

    }
    var operator = "Equal";
    if (typeof(val.operator) == "string") {
        operator = val.operator;
    }
    var isComparisonValid = ValidatorCompare(value, compareTo, operator, val);
    HighlightControls(val,isComparisonValid);
    return isComparisonValid;
}

//Custom Validator: Validates if the string 'po box' does not exists
function AhsSpecificStringValidation(val) {
    var value = ValidatorGetValue(val.controltovalidate);
    if (ValidatorTrim(value).length == 0)
        return true;
    
    var rx = new RegExp(val.validationexpression);
    value = value.toLowerCase();
    var matches = rx.exec(value);
    var isRegularExpressionValid = (matches != null && matches.length > 0);
    
    if(isRegularExpressionValid)
    {
        isRegularExpressionValid = false;
    }
    else
    {
        isRegularExpressionValid = true;
    }
    
    HighlightControls(val,isRegularExpressionValid);
    
    return isRegularExpressionValid;
}

function AhsRequiredFieldValidation(val)
{
    var isRequiredFieldValid =  (ValidatorTrim(ValidatorGetValue(val.controltovalidate)) != ValidatorTrim(val.initialvalue));
    HighlightControls(val,isRequiredFieldValid);   
    return isRequiredFieldValid;
}
function HighlightControls(val,isValidationSuccess)
{
      var controltovalidate = document.getElementById(val.controltovalidate);
      var controltovalidatelabel = null;
      if(val.getAttribute('controltovalidatelabel').toString != null && val.getAttribute('controltovalidatelabel').toString() != '')
      {
        controltovalidatelabel = document.getElementById(val.getAttribute('controltovalidatelabel').toString());
      }
    
      if(!isValidationSuccess)
      {
        if(val.getAttribute('originalcssclass').toString() == null || val.getAttribute('originalcssclass').toString() == "")
        {
            val.setAttribute('originalcssclass',controltovalidate.className.toString());
        }
        if(val.getAttribute('changecssclass').toString() != null && val.getAttribute('changecssclass').toString() != "")
        {
            controltovalidate.className = val.getAttribute('changecssclass');
        }
        if(controltovalidatelabel != null)
        {
             if(val.getAttribute('originallabelcssclass').toString() == null || val.getAttribute('originallabelcssclass').toString() == "")
             {
                val.setAttribute('originallabelcssclass',controltovalidatelabel.className.toString());
             }
            if(val.getAttribute('changelabelcssclass').toString() != null && val.getAttribute('changelabelcssclass').toString() !='')
            {
                controltovalidatelabel.className = val.getAttribute('changelabelcssclass').toString();  
            }
        }
      }
      else
      {
        if(val.getAttribute('originalcssclass').toString() != null && val.getAttribute('originalcssclass').toString() !='')
        {
            controltovalidate.className = val.getAttribute('originalcssclass').toString();  
        }
        if(val.getAttribute('originallabelcssclass').toString() != null && val.getAttribute('originallabelcssclass').toString() !='')
        {
            if(controltovalidatelabel != null)
            {
                controltovalidatelabel.className = val.getAttribute('originallabelcssclass').toString();  
            }
        }
      }
}

function PopUpCalender(calendarLinkId)
{	
	var url = (document.getElementById(calendarLinkId)).href;
	LeftPosition = (screen.width) ? (screen.width-220)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-200)/2 : 0;
	settings = "width=220px,height=200px,top="+TopPosition+",left="+LeftPosition+",toolbar=0,scrollbars=no,status=0,resizable=yes"

	win = window.open(url,"Calendar",settings);
	if(win.window.focus)
	{
		win.window.focus();
	}
}
var isShoppingCartLoaded = false;

// This method will be called once the shopping cart is loaded
function SetShoppingCartLoaded()
{   
    isShoppingCartLoaded = true;
}

function AddGroupToShoppingCart(checkbox,productid,price) 
{
   // Only process the request if the shopping cart is loaded
   if(isShoppingCartLoaded)
   {
       //Since we will be resubmitting the shopping cart, we need to set the isShoppingCartLoaded to false
       isShoppingCartLoaded = false;
       if(checkbox.checked) 
       {
            AddItemToCart(productid,"Enhanced Package:",price,"Group");
       }
       else 
       {
            RemoveFromCart(productid);
       }
   }
   //If the shopping cart is not loaded, then reset to the check box to its original state
   else
   {    
        checkbox.checked = !checkbox.checked;
   }
}

function AddOptionToShoppingCart(checkbox,productid,price) 
{
    hasFocus = checkbox;
   // Only process the request if the shopping cart is loaded
   if(isShoppingCartLoaded)
   {
       //Since we will be resubmitting the shopping cart, we need to set the isShoppingCartLoaded to false
       isShoppingCartLoaded = false;
       
       if(checkbox.checked) 
       {
            AddItemToCart(productid,"Additional Options:",price,"Optional");
       }
       else 
       {
            RemoveFromCart(productid);
       }
   }
   
   //If the shopping cart is not loaded, then reset to the check box to its original state
   else
   {
        checkbox.checked = !checkbox.checked;
   }
}
function AddOptionToShoppingCartUsingDropDown(checkbox,productid,price) 
{
    hasFocus = checkbox;
   // Only process the request if the shopping cart is loaded
   if(isShoppingCartLoaded)
   {
       //Since we will be resubmitting the shopping cart, we need to set the isShoppingCartLoaded to false
       isShoppingCartLoaded = false;
       
       if(checkbox.checked) 
       {
            RemoveFromCart(productid);
            AddItemToCart(productid,"Additional Options:",price,"Optional");
       }
       else 
       {
            RemoveFromCart(productid);
       }
   }
   
   //If the shopping cart is not loaded, then reset to the check box to its original state
   else
   {
        checkbox.checked = !checkbox.checked;
   }
}

function DisabledValidation(validationControlClientIDList) 
{    
    var requiredFieldValidatorAddressLine1 = document.getElementById
    (validationControlClientIDList[0]);            
    var requiredFieldValidatorCity = document.getElementById
    (validationControlClientIDList[1]);
    var requiredFieldValidatorState = document.getElementById
    (validationControlClientIDList[2]);
    var requiredFieldValidatorZipCode = document.getElementById
    (validationControlClientIDList[3]);
    
       ValidatorEnable(requiredFieldValidatorAddressLine1, false);               
       ValidatorEnable(requiredFieldValidatorCity, false);
       ValidatorEnable(requiredFieldValidatorState, false);
       ValidatorEnable(requiredFieldValidatorZipCode, false);
    
}
 function RFVForAddressListControl(checkboxClientID,requiredFieldClientIDs) {
    var checkbox = document.getElementById(checkboxClientID);
    var requiredFieldValidationStreetName = document.getElementById(requiredFieldClientIDs[0]);
    var requiredFieldValidationStreetNumber = document.getElementById(requiredFieldClientIDs[1]);
    var requiredFieldValidatorRadioButtonList = document.getElementById(requiredFieldClientIDs[2]);
    
    if(!checkbox.checked) {
       ValidatorEnable(requiredFieldValidationStreetName,false);
        ValidatorEnable(requiredFieldValidationStreetNumber,false);
        ValidatorEnable(requiredFieldValidatorRadioButtonList,false); 
    }
   
}
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}

function isItemSelected(source,arguments)
{
    if (arguments.Value == "Distributor Type")
    {
        arguments.IsValid = false;
    }
    else
    {
        arguments.IsValid = true;
    }
}

function CheckValidateStates(regexPhone1,regexPhone2,regexPhone3,customPhone1,customPhone2,customPhone3) 
{
    var regularExpressionValidatorPhone1 = document.getElementById(regexPhone1);
    var regularExpressionValidatorPhone2 = document.getElementById(regexPhone2);
    var regularExpressionValidatorPhone3 = document.getElementById(regexPhone3);
    
    if(!RegularExpressionValidatorEvaluateIsValid(regularExpressionValidatorPhone1)) {
        ValidatorEnable(regularExpressionValidatorPhone2,false);
        ValidatorEnable(regularExpressionValidatorPhone3,false);
        ValidatorEnable(regularExpressionValidatorPhone1,true);            
    }
    else if(!RegularExpressionValidatorEvaluateIsValid(regularExpressionValidatorPhone2)) {
    
        ValidatorEnable(regularExpressionValidatorPhone1,false);
        ValidatorEnable(regularExpressionValidatorPhone3,false);
        ValidatorEnable(regularExpressionValidatorPhone2,true);     
    }
    else if(!RegularExpressionValidatorEvaluateIsValid(regularExpressionValidatorPhone3)) {

        ValidatorEnable(regularExpressionValidatorPhone1,false);
        ValidatorEnable(regularExpressionValidatorPhone2,false);
        ValidatorEnable(regularExpressionValidatorPhone3,true);  
    }
    
    
    var requiredFieldValidatorPreferredContactNo1 = document.getElementById(customPhone1);
    var requiredFieldValidatorPreferredContactNo2 = document.getElementById(customPhone2);
    var requiredFieldValidatorPreferredContactNo3 = document.getElementById(customPhone3);
        
   
    if(!RequiredFieldValidatorEvaluateIsValid(requiredFieldValidatorPreferredContactNo1)) {
        ValidatorEnable(requiredFieldValidatorPreferredContactNo1,true);
        ValidatorEnable(requiredFieldValidatorPreferredContactNo2,false);
        ValidatorEnable(requiredFieldValidatorPreferredContactNo3,false);
        
    }
    else if(!RequiredFieldValidatorEvaluateIsValid(requiredFieldValidatorPreferredContactNo2)) {
        ValidatorEnable(requiredFieldValidatorPreferredContactNo2,true);
        ValidatorEnable(requiredFieldValidatorPreferredContactNo1,false);
        ValidatorEnable(requiredFieldValidatorPreferredContactNo3,false);
    }
    else if(!RequiredFieldValidatorEvaluateIsValid(requiredFieldValidatorPreferredContactNo3)) {
        ValidatorEnable(requiredFieldValidatorPreferredContactNo3,true);
        ValidatorEnable(requiredFieldValidatorPreferredContactNo1,false);
        ValidatorEnable(requiredFieldValidatorPreferredContactNo2,false);
    }
}
    
// This java script function is used to advance the cursor to next text box when 
// user entering the phone number. i.e. Recomended for IE and Firefox browsers.
function onKeyUp(from,to)
{
    from = document.getElementById(from);
    to = document.getElementById(to);
    if(from.value.length == 3)
    {
        to.focus();
    }
}

function ClearPhoneFields(phone1, phone2, phone3)
{
    var phoneControl1 = document.getElementById(phone1);
     var phoneControl2 = document.getElementById(phone2);
      var phoneControl3 = document.getElementById(phone3);
      if(phoneControl1 != null)
      {
        phoneControl1.value = '';
      }
      if(phoneControl2 != null)
      {
        phoneControl2.value = '';
      }
      if(phoneControl3 != null)
      {
        phoneControl3.value = '';
      }
}

