﻿function TransformCCNumber(value) {
    var number = "";
    for (i = 0; i < value.length; i++)
        if (value.charAt(i) >= "0" && value.charAt(i) <= "9")
        number += value.substr(i, 1);
    return number;
}

function TransformQty(value) {
    var number = "";
    for (i = 0; i < value.length; i++) { if (value.charAt(i) >= "0" && value.charAt(i) <= "9") { number += value.substr(i, 1); } }
    if (number == 0) number = 1;
    return number;
}

function htmlEncode(s) {
    var str = new String(s);
    str = str.replace(/&/g, "&amp;");
    str = str.replace(/</g, "&lt;");
    str = str.replace(/>/g, "&gt;");
    str = str.replace(/"/g, "&quot;");
    str = str.replace(/\(/g, "!***LEFTBRACK***!");
    str = str.replace(/\)/g, "!***RIGHTBRACK***!");
    str = str.replace(/=/g, "!***EQUALS***!");
    return str;
}

function htmlDecode(s) {
    var str = new String(s);
    str = str.replace(/&amp;/g, '&');
    str = str.replace(/&lt;/g, '<');
    str = str.replace(/&gt;/g, '>');
    str = str.replace(/&quot;/g, '"');
    str = str.replace(/!\*\*\*LEFTBRACK\*\*\*!/g, "(");
    str = str.replace(/!\*\*\*RIGHTBRACK\*\*\*!/g, ")");
    str = str.replace(/!\*\*\*EQUALS\*\*\*!/g, "=");
    return str;
}

function getElement(id) {
    if (document.layers)            // Netscape 4
        return eval("document." + id);
    if (document.getElementById)    // Netscape 6
        return eval("document.getElementById('" + id + "')");
    if (document.all)               // IE4+
        return eval("document.all." + id);
}
function createPlayerTest(mediaFile, theFile, placeholder, image, width, height, displayHeight) {
    var s = new SWFObject(mediaFile, "thePlayerId", width, height, "7");
    s.addParam("allowfullscreen", "true");
    s.addVariable("file", theFile);
    s.addVariable("width", width);
    s.addVariable("height", height);
    s.addVariable("wmode", "opaque");
    s.addVariable("autoscroll", "true");
    s.addVariable("allowscriptaccess", "true");
    s.addVariable("wmode", "opaque");
    s.addVariable("bufferlength", "10");
    s.addVariable("displayheight", displayHeight);
    s.addVariable("allowfullscreen", "true");
    s.addVariable("autostart", "false");
    s.addVariable('image', image);
    s.addVariable("shuffle", "false");
    s.addVariable("enablejs", "true");
    s.addVariable("javascriptid", "thePlayerId");
    s.write(placeholder);
}
function getRealTop(element) {
    var position = element.offsetTop;
    var parent = element.offsetParent;
    while (parent != null) {
        position += parent.offsetTop;
        parent = parent.offsetParent
    }
    return position;
}
function getRealLeft(element) {
    var position = element.offsetLeft;
    var parent = element.offsetParent;
    while (parent != null) {
        position += parent.offsetLeft;
        parent = parent.offsetParent
    }
    return position;
}
function FormatPhone(phoneField, Format) {
    if (Format == null) { Format = /^(\D*\d\D*){10}$/ }
    if (Format.test(phoneField.value) == true) {
        var num = phoneField.value.replace(/[^\d]/g, '');
        if (num.length != 10) {
            // alert('Please enter a valid phone number');
        } else {
            phoneField.value = num.substring(0, 3) + "-" + num.substring(3, 6) + "-" + num.substring(6);
        }
    }
}
function fixZipAndPostalCode(zc) {
    var zip;
    zip = zc.replace(/[^0-9A-Za-z]/g, "");
    if (/^\d{5,}$/.test(zip) == true) {
        if (zip.length < 5)
            zip = zc; // Do Nothing
        else
            zip = zip.substring(0, 5);
    }
    else {
        if (zip.length == 6)
            zip = zip.substring(0, 3) + " " + zip.substring(3);
        else if (zip.length >= 3)
            zip = zip.substring(0, 3);
        else
            zip = zc; // Do Nothing
    }
    return zip.toUpperCase();
}


///////////////////////////////////////// CUSTOM VALIDATION AREA /////////////////////////////////////////
// ADDED BY:    MARIA GHAFFAR
// DATE:        7th JULY, 2010
// DESCRIPTION: Show first message with control
//////////////////////////////////////////////////////////////////////////////////////////////////////////

var defaultWidthForErrorDiv = 240;
var minimumWidthForErrorDiv = defaultWidthForErrorDiv;
var maximumWidthForErrorDiv = defaultWidthForErrorDiv;

function ValidateControlsOnSubmit(validationGroup) {

    for (var i = 0; i < Page_Validators.length; i++) {
        //If Not Valid and ValidationGroup is also same '&& typeof (Page_Validators[i].errormessage) == "string"
        var validator = Page_Validators[i];
        if (validator.validationGroup == validationGroup) {
            ValidatorValidate(validator);
            if (validator.isvalid == false) {
                FadeInErrorDiv($("#" + validator.controltovalidate), validator.errormessage);
                return false;
            }
        }
    }

    return true;
}

function ValidateAndShowValidator(validatorID) {

    var validator = $("#" + validatorID);
    ValidatorValidate(validator);
    if (validator.isvalid == false) {
        FadeInErrorDiv($("#" + validator.controltovalidate), validator.errormessage);
        return false;
    }
    return true;
}

function ShowValidatorMessage(controltovalidate, errormessage) {
    FadeInErrorDiv($("[id$='" + controltovalidate + "']"), errormessage);
    return false;
}

function FadeInErrorDiv(inputControl, errorMsg) {

    document.getElementById("spnCustomValidatorMessage").innerHTML = errorMsg;
    ShowCustomDivWithControl(inputControl, $("#divCustomValidatorMessage"));

    var controlWidth = inputControl.width();
    if (controlWidth == null || (controlWidth < defaultWidthForErrorDiv && controlWidth > minimumWidthForErrorDiv))
        $("#divCustomValidatorMessage").width(defaultWidthForErrorDiv);
    else if (controlWidth <= minimumWidthForErrorDiv)
        $("#divCustomValidatorMessage").width(minimumWidthForErrorDiv);
    else if (inputControl.width() > defaultWidthForErrorDiv)
        $("#divCustomValidatorMessage").width(maximumWidthForErrorDiv);
    else //if (controlWidth >= 200)
        $("#divCustomValidatorMessage").width(controlWidth);
    
    GoToPositionOfErrorDiv(inputControl.offset().top - 115);

    //window.location.hash = "#spnCustomValidatorMessage";
    //$("#divCustomValidatorMessage").focus();
    inputControl.addClass("validatorTxt");
    inputControl.focus();
    inputControl.select();    

    inputControl.bind("blur", function() {
        FadeOutErrorDiv(inputControl);
    });

    return false;
}

function FadeOutErrorDiv(inputControl) {

    inputControl.removeClass("validatorTxt");
    HideCustomDivFromControl('divCustomValidatorMessage');

    return false;
}

function GoToPositionOfErrorDiv(pos) {
    $('html, body').animate({ scrollTop: pos }, 'slow');
}

function ShowCustomDivWithControl(control, dv) {
    var Pos = control.offset();
    dv.css({ top: Pos.top + control.outerHeight() + 3, left: Pos.left });
    dv.fadeIn('fast');
    return false;
}
function HideCustomDivFromControl(dv) {
    $("#" + dv).fadeOut('fast');
    return false;
}

///////////////////////////////////////// CUSTOM VALIDATION AREA /////////////////////////////////////////