var O3 = window,
T4 = document;
/**Delivery Agent - formvalidate.js**This file contains the form validation js functions used by DA templates with forms*/
function checkState(billOrShip) {
    var isValidState = false;
    if (billOrShip == 'bill') {
        if (T4.billship.billing_country.value == "US" || T4.billship.billing_country.value == "CA") {
            if (T4.billship.billing_state.selectedIndex == 0) {
                isValidState = true;
            }
        }
    } else if (billOrShip == 'ship') {
        if (T4.billship.shipping_country.value == "US" || T4.billship.shipping_country.value == "CA") {
            if (T4.billship.shipping_state.selectedIndex == 0) {
                isValidState = true;
            }
        }
    }
    return isValidState;
}
function checkPhone(billOrShip) {
    var isValidPhone = false;
    if (billOrShip == 'bill') {
        if (T4.billship.billing_country.value == "US" || T4.billship.billing_country.value == "CA") {
            if (StripDigits(T4.billship.billing_phone.value).length < 10) {
                isValidPhone = true;
            }
        }
    } else if (billOrShip == 'ship') {
        if (T4.billship.shipping_country.value == "US" || T4.billship.shipping_country.value == "CA") {
            if (StripDigits(T4.billship.shipping_phone.value).length < 10) {
                isValidPhone = true;
            }
        }
    }
    return isValidPhone;
}

function mobilePhoneCheck(fieldId){
    if($(fieldId)){
        if(StripDigits($(fieldId).value).length < 10){
            return true;
        } 
        return false;
    }
    return false;
}

function checkZip(billOrShip) {
    var zip;
    var cntry;
    if (billOrShip == 'bill') {
        zip = T4.billship.billing_zip.value.toLowerCase();
        cntry = T4.billship.billing_country.value.toLowerCase();
    } else if (billOrShip == 'ship') {
        zip = T4.billship.shipping_zip.value.toLowerCase();
        cntry = T4.billship.shipping_country.value.toLowerCase();
    }
    var isValidZip = false;
    var newZip = "";
    var chr;
    for (i = 0; i < zip.length; i++) {
        chr = zip.charAt(i);
        if (chr != " ") newZip = newZip + chr;
    }
    if (cntry == 'us') {
        if (StripDigits(zip).length < 5) {
            isValidZip = true;
        }
    } else if (cntry == 'ca') {
        if (!isCanadianZip(newZip)) {
            isValidZip = true;
        }
    }
    return isValidZip;
}
function isCanadianZip(zipCode) {
    if ((zipCode == '') || (zipCode.length != 6)) return false;
    else if ((zipCode.charAt(0) < "a" || zipCode.charAt(0) > "z") || (zipCode.charAt(2) < "a" || zipCode.charAt(2) > "z") || (zipCode.charAt(4) < "a" || zipCode.charAt(4) > "z")) return false;
    else if ((zipCode.charAt(1) < "0" || zipCode.charAt(1) > "9") || (zipCode.charAt(3) < "0" || zipCode.charAt(3) > "9") || (zipCode.charAt(5) < "0" || zipCode.charAt(5) > "9")) return false;
    else return true;
}
function StripChrs(s, chrs) {
    var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (chrs.indexOf(c) != -1) returnString += c;
    }
    return returnString;
}
function StripDigits(s) {
    return StripChrs(s, "0123456789");
}
function IsCharInString(c, s) {
    for (i = 0; i < s.length; i++) {
        if (s.charAt(i) == c) return true;
    }
    return false;
}
function EndsWith(s, e) {
    var end = s.substr(s.length - e.length, e.length);
    if (end.toLowerCase() == e.toLowerCase()) return true;
    return false;
}
function getFront(mainStr, searchStr) {
    foundOffset = mainStr.indexOf(searchStr);
    if (foundOffset == -1) {
        return null;
    }
    return mainStr.substring(0, foundOffset);
}
function getEnd(mainStr, searchStr) {
    foundOffset = mainStr.indexOf(searchStr);
    if (foundOffset == -1) {
        return null;
    }
    return mainStr.substring(foundOffset + searchStr.length, mainStr.length);
}
function replaceString(mainStr, searchStr, replaceStr) {
    var front = getFront(mainStr, searchStr);
    var end = getEnd(mainStr, searchStr);
    if (front != null && end != null) {
        return front + replaceStr + end;
    }
    return mainStr;
}
function IsLetter(c) {
    return (((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")));
}
function IsDigit(c) {
    return ((c >= "0") && (c <= "9"));
}
function IsLetterOrDigit(c) {
    return (IsLetter(c) || IsDigit(c));
}
function IsAlpha(str) {
    if (str + "" == "undefined" || str + "" == "null" || str + "" == "") return false;
    str += "";
    for (i = 0; i < str.length; i++) {
        if (!IsLetter(str.charAt(i)) && str.charAt(i) != ' ') return false;
    }
    return true;
}
function IsAlpha2(str) {
    if (str + "" == "undefined" || str + "" == "null" || str + "" == "") return false;
    str += "";
    for (i = 0; i < str.length; i++) {
        if (!IsLetterOrDigit(str.charAt(i)) && str.charAt(i) != ' ') return false;
    }
    return true;
}
function IsMatch(str1, str2) {
    if (str1 + "" == "undefined" || str1 + "" == "null" || str1 + "" == "") return false;
    if (str2 + "" == "undefined" || str2 + "" == "null" || str2 + "" == "") return false;
    if (str1.length != str2.length) return false;
    var isValid = true;
    str1 += "";
    str2 += "";
    for (i = 0; i < str1.length; i++) {
        if (! (str1.charAt(i) == str2.charAt(i))) {
            isValid = false;
            break;
        }
    }
    return isValid;
}
function IsNull(val) {
    if (val + "" == "null") return true;
    return false;
}
function IsUndef(val) {
    if (val + "" == "undefined") return true;
    return false;
}
function IsBlank(str) {
    if (IsNull(str) || IsUndef(str) || (str == "")) return true;
    return false;
}
function IsValidMultEmail(str) {
    str = str.replace(/^\s+|\s+$/g,"");
    if (str + "" == "undefined" || str + "" == "null" || str + "" == "") return false;
    var vArr = str.split(/\s*[,; ]\s*/);
    for (var i = 0; i < vArr.length; i++) {
	if ( !IsValidEmail(vArr[i]) ) {
	   return false;
	}
    }
    return true;
}

function isValidPassword(password){

}

function IsValidEmail(str) {
    if (str + "" == "undefined" || str + "" == "null" || str + "" == "") return false;
    var isValid = true;
    str += "";
    namestr = str.substring(0, str.indexOf("@"));
    domainstr = str.substring(str.indexOf("@") + 1, str.length);
    if (IsBlank(str) || (namestr.length == 0) || (domainstr.indexOf(".") <= 0) || (domainstr.indexOf("@") != -1) || !IsAlpha(str.charAt(str.length - 1))) isValid = false;
    return isValid;
}
function IsChecked(thegroup) {
    if (thegroup.length > 0) {
        for (i = 0; i < thegroup.length; i++) {
            if (thegroup[i].checked) return true;
        }
    } else if (thegroup.checked) return true;
    return false;
}
function IsURL(str) {
    if (str + "" == "undefined" || str + "" == "null" || str + "" == "") return false;
    str += "";
    var validchrs = "./+-?#=%";
    for (x = 0; x < str.length; x++) {
        if (!IsLetterOrDigit(str.charAt(x)) && !IsCharInString(str.charAt(x), validchrs)) return false;
    }
    return true;
}
function IsImage(name) {
    if (IsBlank(name)) return true;
    if (EndsWith(name, '.jpg') || EndsWith(name, '.gif')) return true;
    return false;
}
var errstring;
var errselect;
var doformvalidate = true;
function AddError(errstr, errsel) {
    if (doformvalidate == false) return;
    if (IsBlank(errstring) && !IsNull(errstr)) errstring = errstr;
    else errstring += "\n\n" + errstr;
    if (IsBlank(errselect) && !IsNull(errsel)) errselect = errsel;
}
function FormSubmit(comval, myform) {
    if (IsBlank(myform)) myform = T4.forms[0];
    else if (!myform.submit && myform.length) myform = eval("document." + myform);
    if (!IsBlank(comval)) {
        if (IsCharInString('.', comval)) myform.action = comval;
        else {
            if (IsCharInString('?', myform.action)) myform.action = getFront(myform.action, '?');
            myform.action += '?' + comval;
        }
    }
    myform.submit();
}
function FormReset() {
    T4.forms[0].reset();
}
function SetFormValidate(onoff) {
    doformvalidate = onoff;
}
function RedirectURL(url) {
    O3.location = url;
    return false;
}

