﻿function ajaxValidatorEvaluate(validator)
{
var value = ValidatorGetValue(validator.controltovalidate);
var ajaxResponse = ajaxSyncCallback(validator.id.replace(/_/g, "\$"), value);
if (ajaxResponse == "True")
    return true;
else
    return false;
}

function checkIfTrue(validator)
{
var checkBox = GetElement(validator.othercontrol1);
var textBox = GetElement(validator.othercontrol2);
 
if (checkBox.checked)
{
    if (textBox.value == validator.customstring1 || isWhitespace(textBox.value))
        return false;
    else
        return true;
}
else
    return true;
}

function validateAddressType(validator) {
var rbBusiness = GetElement(validator.controltovalidate);
var rbResidential = GetElement(validator.othercontrol1);
if (!rbResidential.checked && !rbBusiness.checked) 
    return false;
else 
    return true; 
}

function validateStateCountry(validator)
{
changeValidatorCalloutDynamic();
var state = GetElement(validator.controltovalidate);
var country = GetElement(validator.othercontrol1);
changeState(state,country);
if (validateState(state,country))
    return true;
else
{
    validator.errormessage = "'" + selectText(state) + "' is not a valid State/Province for the Country '" + selectText(country) + "'.";
    if (validator.customstring1 != undefined)
        validator.errormessage = validator.customstring1 + validator.errormessage;
    return false; 
}
}


function validateState(FState, FCountry)
{
    stateValue = selectValue(FState);
    stateFirstSemicolonPos = stateValue.indexOf(";");
    stateLastSemicolonPos = stateValue.lastIndexOf(";");
    countryValue = selectValue(FCountry);
    countryFirstSemicolonPos = countryValue.indexOf(";");
    countryLastSemicolonPos = countryValue.lastIndexOf(";");

    if (stateFirstSemicolonPos == -1)
        stateCountry = "";
    {
        if (stateFirstSemicolonPos == stateLastSemicolonPos)
            stateCountry = stateValue.slice(stateFirstSemicolonPos + 1);
        else
            stateCountry = stateValue.slice(stateFirstSemicolonPos + 1, stateLastSemicolonPos);
    }

    if (countryFirstSemicolonPos == -1)
        countryState = "";
    {
        if (countryFirstSemicolonPos == countryLastSemicolonPos)
            countryState = countryValue.slice(countryFirstSemicolonPos + 1);
        else
            countryState = countryValue.slice(countryFirstSemicolonPos + 1, countryLastSemicolonPos);
    }

    if (stateCountry == "" && countryState == "")
        return false;
    else if (stateCountry == selectText(FCountry))
        return true;
    else if (countryState == selectText(FState))
        return true;
    else
        return false;
}

function conditionalValidateStateCountry(validator)
{
var checkbox = GetElement(validator.othercontrol2);
if (checkbox.checked)
    return validateStateCountry(validator);
else
    return true;
}

function companyNameRequired(validator) 
{
var businessRequired = validator.customstring1;
var companyName = GetElement(validator.controltovalidate);
switch (businessRequired) 
{
case "required":
    if (isWhitespace(companyName.value))
      return false;
    break;
case "requiredb":
    var rbBusiness = GetElement(validator.othercontrol1); 
    if (rbBusiness.checked && isWhitespace(companyName.value))
      return false;
    break;
}                               
return true;
}

function validatePOBox(validator) {
var addressLine1 = GetElement(validator.controlcontroltovalidate);
var addressLine2 = GetElement(validator.othercontrol1);
if (isPOBox(addressLine1, addressLine2))
    return false;
else
    return true;
}

function validateCheckbox(validator)
{
return GetElement(validator.controltovalidate).checked;
}

function otherControl1HasValue(validator)
{
var otherControl1 = GetElement(validator.othercontrol1);
return !isWhitespace(otherControl1.value);
}

function requiredIfChecked(validator) 
{
var requiredField = GetElement(validator.controltovalidate);
var checkbox = GetElement(validator.othercontrol1);
if (checkbox.checked && isWhitespace(requiredField.value))
    return false;
else
    return true;
}

function cardExpired(validator)
{
    curDate = new Date();
    var selectedMonth = GetElement(validator.othercontrol1);                                     
    var selectedYear = GetElement(validator.controltovalidate);                         
    if (selectedMonth.selectedIndex < curDate.getMonth() && selectedYear.selectedIndex == 0)
       return false;
    else
       return true;

}


function requirePasswordRetype(validator)
{
    var password = nextQuery('#' + validator.customstring1).val();
    var passwordRetype = nextQuery('#' + validator.customstring2).val();
    
    if (isWhitespace(password))
        return true;
    else
    {
        if (isWhitespace(passwordRetype))
            return false;
        else
            return true;
    }
}

function checkPOVisibility(validator) {
    var poNumberValue = nextQuery('#' + validator.controltovalidate).val();
    var ponumberVisible = nextQuery('#' + validator.controltovalidate).is(":visible");
    var optionalVisible = nextQuery('#' + validator.othercontrol1).is(":visible");
    if (ponumberVisible) {
        if (optionalVisible) {
            return true;
        }
        else {
            if (isWhitespace(poNumberValue))
               return false;
            else
               return true;
        }
    }
    else {
        return true;
    }
}

function checkForPayment(validator) {
    if (nextQuery("input[name$='payment']:checked").val()) {
        return true;
    }
    else {
        return false;
    }
}

function checkValidationValue(validator) {
    changeValidatorCalloutDynamic();
    var validationValue = nextQuery('#' + validator.controltovalidate).val();
         
    if (nextQuery('#' + validator.othercontrol1).is(":checked")) {
        if (isWhitespace(validationValue))
        {
            validator.errormessage = "Card Validation Value is required if you are using a credit card.";
            return false;
        }
        else 
        {
            var ccType = nextQuery('#' + validator.othercontrol2).val();
            var ccTypes = ccType.split(";");
            var ccTypeName = ccTypes[1];
            
            switch (ccTypeName) {
                case "American Express":
                    if (ccTypeName.length != 4) {
                        validator.errormessage = "Validation Values for American Express cards must have 4 digits.\n";
                        return false;
                    }
                default:
                    if (ccTypeName.length != 3) {
                        validator.errormessage = "Validation Values for " + ccTypeName + " cards must have 3 digits.\n";
                        return false;
                    }
            }
           return true; 
        } 
    }
    else
    {
        return true;
    }
}

function changeValidatorCalloutDynamic() {
    if (!AjaxControlToolkit.ValidatorCalloutBehavior.prototype.oldShow) {
        AjaxControlToolkit.ValidatorCalloutBehavior.prototype.oldShow = AjaxControlToolkit.ValidatorCalloutBehavior.prototype.show;
        AjaxControlToolkit.ValidatorCalloutBehavior.prototype.show = function(force) {
            if (force || !this.get_isOpen()) {
                nextQuery(this._errorMessageCell).html(this._getErrorMessage());
                this.oldShow(force);
            }
        }
    }
}

function requireAffiliateCategory(validator)
{
     var validationValue = nextQuery('#' + validator.controltovalidate + ' option:selected').text();
     if (isWhitespace(validationValue))
        return false;
    else
        return true;
}




