﻿/// <reference path="jquery/jquery-1.2.6.min.js" />
/// <reference path="ajax/MicrosoftAjax.js" />
/// <reference path="ajax/MicrosoftAjaxTimer.js" />
/// <reference path="ajax/MicrosoftAjaxWebForms.js" />
function changeManufacturer(element, modalExtenderId) {
    var modelExtender = $find(modalExtenderId);
    var oldContextKey = modelExtender.get_contextKey();
    var contextElements = oldContextKey.split(';');
    var newContextKey = contextElements[0] + ';' + element.value;
    modelExtender.set_contextKey(newContextKey);
}
function setAdvPanelVisibility(dropDownId, panelId) {
    var dropDown = $get(dropDownId);
    var panel = $get(panelId);
    if (dropDown.value == 'ad') {
        panel.style.display = 'block';
    }
    else if (dropDown.value == 'offer') {
        panel.style.display = 'none';
    }
}

function goToCountryUrl(popupButtonId, preparedUrl) {
    var el = $find(popupButtonId);
    var hiddenValue = $get(el.get_selectedItemIdHiddenFieldId()).value;
    if (hiddenValue == '') {
        window.location.href = preparedUrl;
    }
    else {
        window.location.href = preparedUrl + "&country=" + hiddenValue;
    }
}
function setFocusToEnd(elemId) {
    var elem = $get(elemId);
    elem.focus();
    if (elem.createTextRange) {
        var r = elem.createTextRange();
        r.collapse(false);
        r.select();
    }
}



function getExpDate(days, hours, minutes) {
    var expDate = new Date();
    if (typeof days == "number" && typeof hours == "number" && typeof hours == "number") {
        expDate.setDate(expDate.getDate() + parseInt(days));
        expDate.setHours(expDate.getHours() + parseInt(hours));
        expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
        return expDate.toGMTString();
    }
}
function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}
function getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return getCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}



function isNumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}

function isEmpty(aTextField) {
    if (aTextField.length == 0) {
        return true;
    }
    else { return false; }
}


function isValidEmail(email) {
    return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}

function isOnlyDigitsAndletters(aTextField) {
    return (/^[_a-zA-Z0-9]+$/i).test(aTextField);
}

function isValidIP(ip) {
    return (/^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$/i).test(ip);
}


function inlineFieldLabel(label, inputid) {
    var fieldLabel = label;         // string to put in your text input
    var textInput = $(inputid);  // your text input field

    var form = $($("form")[0]);


    /* add the field label css class to the form field and set the value */
    textInput.addClass("intra-field-label").val(fieldLabel);

    /* remove the placeholder string when field gets focus */
    textInput.focus(function() {
        if (this.value == fieldLabel) {
            //  $(this).removeClass("intra-field-label").val("");
            Sys.UI.DomElement.removeCssClass(this, "intra-field-label");
            this.value = "";
        };
    });

    /* add the field label string when field looses focus */
    textInput.blur(function() {
        if (this.value == "") {
            // $(this).addClass("intra-field-label").val(fieldLabel);
            Sys.UI.DomElement.addCssClass(this, "intra-field-label");
            this.value = fieldLabel;
        };
    });

    /* if the field is set to the fieldLabel on submit, clear the field */
    form.submit(function() {
        if (textInput.val() == fieldLabel) {
            textInput.removeClass("intra-field-label").val("");
        };
    });

}
function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function guid() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

function checkYear(value) {
    var currentYear;
    var nowX = new Date();
    if ($.browser.msie) {
        currentYear = nowX.getYear();
    }
    else if ($.browser.mozilla) {
        currentYear = nowX.getYear() + 1900;
    }
    else if ($.browser.safari) {
        currentYear = nowX.getYear() + 1900;
    }
    else if ($.browser.opera) {
        currentYear = nowX.getYear() + 1900;
    }
    else {
        return true;
    }
    if (value >= currentYear + 1) {

        return false;
    }
    else {
        return true;
    }
}

function checkYearString(yearString) {
    var yearpattern = /^([0-9]{2,4})[ -]*([0-9]{0,4})$/i;
    var match = yearString.match(yearpattern);
    if (match == null) {
        return false;
    }
    var firstyear = match[1];
    var secondyear = match[2];
    if (!isEmpty(firstyear)) {
        if (!isNumeric(firstyear)) {
            return false;
        }
        if (firstyear < 15) {
            firstyear = firstyear + 2000;
        }
        else if (firstyear < 100) {
            firstyear = firstyear + 1900;
        }
        if (!checkYear(firstyear)) {
            return false;
        }
        else if (firstyear < 1900) {
            return false;
        }
    }
    if (!isEmpty(secondyear)) {
        if (!isNumeric(secondyear)) {
            return false;
        }
        if (secondyear < 15) {
            secondyear = secondyear + 2000;
        }
        else if (secondyear < 100) {
            secondyear = secondyear + 1900;
        }
        if (!checkYear(secondyear)) {
            return false;
        }
        else if (secondyear < 1900) {
            return false;
        }
    }
    return true;
}

function getImageSize(id) {
    var oHlpr = document.createElement('IMG');
    var oPic = document.getElementById(id);
    oHlpr.style.visibility = 'hidden';
    oHlpr.style.position = 'absolute';
    oHlpr.top = 0; oHlpr.left = 0;
    oHlpr.src = oPic.src;
    document.body.appendChild(oHlpr);
    var imSize = { 'width': oHlpr.offsetWidth, 'height': oHlpr.offsetHeight }
    document.body.removeChild(oHlpr);
    return imSize;
}
function clearText(text) {
    return text.replace(/\\/g, '\\\\').replace(/\'/g, "\\'");
}

function scrollToElement(theElement) {

    var selectedPosX = 0;
    var selectedPosY = 0;

    while (theElement != null) {
        selectedPosX += theElement.offsetLeft;
        selectedPosY += theElement.offsetTop;
        theElement = theElement.offsetParent;
    }

    window.scrollTo(selectedPosX, selectedPosY);

}
