﻿/* Blocker */
function BlockModule(id, msg, timeout) {if (timeout == undefined) timeout = 0;$(id).block({ message: msg, timeout: timeout,css: { backgroundColor: 'transparent', border: '0px', width: '80%', top: '50%' }, overlayCSS: { backgroundColor: '#FFFFFF', opacity: '0.4' }})}
function UnBlockModuleGrowlUI(id, msg, desc) {$(id).unblock();$.growlUI(msg, desc, 4000, { width: '400px', top: '10px', left: '', right: '10px', border: 'none', padding: '5px', opacity: 0.8, cursor: 'wait', color: '#fff', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px' });}
function UnBlockModule(id, msg) {$(id).unblock();BlockModule(id, msg, 4000);}

/* Page Tools*/
function PanelEnterKeyHandler(panel, button) {$('#' + panel).keydown(function(event) {if (event.keyCode == 13) {event.returnValue = false;event.cancel = true;$('#' + button).click();}})}
function GetLangFromUrl() {var pathArray = window.location.pathname.split('/');newPathname = "";return pathArray[1];}
function spliter(text) { var letbin = ''; for (i = 0; i < text.length; i++) { letbin = letbin + str2bin(text.substr(i, 1)); if (i + 1 != text.length) { letbin = letbin + '2' } }; return (letbin) }
function str2bin(txt) { String.prototype.toHex = function() { return this.charCodeAt(0).toString(2) }; return txt.toHex() }
function SetDirByUserInput(a) {k = "[\u0000- !-@[-`{-\u00bf\u00d7\u00f7\u02b9-\u02ff\u2000-\u2bff]";l = new RegExp("^" + k + "*([0-9]" + k + "*$|[A-Za-z\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02b8\u0300-\u0590\u0800-\u1fff\u2c00-\ufb1c\ufdfe-\ufe6f\ufefd-\uffff])");n = new RegExp("^" + k + "*$");var b = a.value;a.dir = l.test(b) ? "ltr" : (n.test(b) ? "" : "rtl");}
function celOver(color, CId) { CId.className = color; }
function textCounter(field, countfield, maxlimit) { if (field.value.length > maxlimit) field.value = field.value.substring(0, maxlimit); else { countfield.value = maxlimit - field.value.length }; }
function BookMark() { var title = document.title; var url = document.location.href; if (window.sidebar) window.sidebar.addPanel(title, url, ""); else if (window.opera && window.print) { var mbm = document.createElement('a'); mbm.setAttribute('rel', 'sidebar'); mbm.setAttribute('href', url); mbm.setAttribute('title', title); mbm.click() } else if (document.all) window.external.AddFavorite(url, title); }
function PrinableView() { var title = "iCMS printable version"; s = "scrollbars=yes,toolbar=yes,width=600,height=400"; icms = window.open("", "popFrameless", s); icms.blur(); window.focus(); var frameString = "<html><head><title>" + title + "</title><body onload='window.print()' dir=rtl>" + document.getElementById("printable").innerHTML + "</body></html>"; icms.document.open(); icms.document.write(frameString); icms.document.close(); icms.focus(); }
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) { if ((!highlightStartTag) || (!highlightEndTag)) { highlightStartTag = "<font style='color:blue; background-color:yellow;'>"; highlightEndTag = "</font>" } var newText = ""; var i = -1; var lcSearchTerm = searchTerm.toLowerCase(); var lcBodyText = bodyText.toLowerCase(); while (bodyText.length > 0) { i = lcBodyText.indexOf(lcSearchTerm, i + 1); if (i < 0) { newText += bodyText; bodyText = "" } else { if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) { if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) { newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag; bodyText = bodyText.substr(i + searchTerm.length); lcBodyText = bodyText.toLowerCase(); i = -1 } } } } return newText } function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag) { if (treatAsPhrase) { searchArray = [searchText] } else { searchArray = searchText.split(" ") } if (!document.body || typeof (document.body.innerHTML) == "undefined") { if (warnOnFailure) { } return false } var bodyText = document.getElementById("printable").innerHTML; for (var i = 0; i < searchArray.length; i++) { bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag) } document.getElementById("printable").innerHTML = bodyText; return true }

/* Login */
function LoginValidator(id) { if ($(id).val() != '' && $(id).val() != null) { $(id).removeClass("QuickLogin_Error"); return true } else { $(id).addClass("QuickLogin_Error"); return false } }

/* Contact */
function Start(page) { OpenWin = this.open(page, 'CtrlWindow', 'width=650,height=500,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes') }
function openWindow(url) { popupWin = window.open(url, 'new_page', 'width=400,height=400') }
function SendQuickContact(lang) {
    var id = $('#QuickContact').children("[@selected]").val();
    ShowQuickContactMailBox(id, lang);
    return false;
}

function ShowQuickContactMailBox(id, lang) {
    $('body').append('<style type="text/css">iframe{border: 0px solid none;}</style><div class="MailBoxBlocker"></div>');
    $('.MailBoxBlocker').css('opacity', '0.7').css('height', $(document).height() + 'px');
    $('body').append('<div class="MailBoxDiv"><div class="ContactForm_Header"></div><div  class="ContactForm_Footer"><iframe id="MailFrame" height="270" width="450"  marginwidth="0" marginheight="0" style="border: 0px solid none;" scrolling="no" border="0" frameborder="0" ></iframe></div></div>');
    $('.MailBoxDiv').css('left', (($(window).width() - 450) / 2) + 'px').css('top', ((($(window).height() - 270) / 2) + $(document).scrollTop()) + 'px');
    $('.MailBoxDiv').css("z-index", parseInt(new Date().getTime() / 1000));
    $('#MailFrame').attr('src', '/incs/SendMailBox.aspx?cid=' + id + '&lang=' + lang);
    $(window).scroll(function() { $('.MailBoxDiv').css('top', ((($(window).height() - 270) / 2) + $(document).scrollTop()) + 'px'); });
}
function ShowContactMailBox(id) {
    $('body').append('<style type="text/css">iframe{border: 0px solid none;}</style><div class="MailBoxBlocker"></div>');
    $('.MailBoxBlocker').css('opacity', '0.7').css('height', $(document).height() + 'px');
    $('body').append('<div class="MailBoxDiv"><div class="ContactForm_Header"></div><div  class="ContactForm_Footer"><iframe id="MailFrame" height="270" width="450"  marginwidth="0" marginheight="0" style="border: 0px solid none;" scrolling="no" border="0" frameborder="0" ></iframe></div></div>');
    $('.MailBoxDiv').css('left', (($(window).width() - 450) / 2) + 'px').css('top', ((($(window).height() - 270) / 2) + $(document).scrollTop()) + 'px');
    $('.MailBoxDiv').css("z-index", parseInt(new Date().getTime() / 1000));
    $('#MailFrame').attr('src', '/incs/SendMailBox.aspx?cid=' + id + '&lang=' + GetLangFromUrl());
    $(window).scroll(function() { $('.MailBoxDiv').css('top', ((($(window).height() - 270) / 2) + $(document).scrollTop()) + 'px'); });
}

function ShowMailBox(id) {
    $('body').append('<style type="text/css">iframe{border: 0px solid none;}</style><div class="MailBoxBlocker"></div>');
    $('.MailBoxBlocker').css('opacity', '0.7').css('height', $(document).height() + 'px');
    $('body').append('<div class="MailBoxDiv"><div class="ContactForm_Header"></div><div  class="ContactForm_Footer"><iframe id="MailFrame" height="270" width="450"  marginwidth="0" marginheight="0" style="border: 0px solid none;" scrolling="no" border="0" frameborder="0" ></iframe></div></div>');
    $('.MailBoxDiv').css('left', (($(window).width() - 450) / 2) + 'px').css('top', ((($(window).height() - 270) / 2) + $(document).scrollTop()) + 'px');
    $('.MailBoxDiv').css("z-index", parseInt(new Date().getTime() / 1000));
    $('#MailFrame').attr('src', '/incs/SendMailBox.aspx?id=' + id + '&lang=' + GetLangFromUrl());
    $(window).scroll(function() { $('.MailBoxDiv').css('top', ((($(window).height() - 270) / 2) + $(document).scrollTop()) + 'px'); });
}

/* Validator */
function GetRegex(type) {
    if (type == 'url') { return /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ }
    if (type == 'email') { return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/ }
    if (type == 'username') { return /^([a-zA-Z])+([a-zA-Z0-9_\.])+([a-zA-Z0-9])$/ }
    if (type == 'password') { return /./ }
    if (type == 'number') { return /^\d+$/ }
    return /./
}
function RequiredValidator(id, len, type) {var value = $(id).val();var retvalue = true;if (value.length < len) {RequiredValidatorMsg(id, 1);return false;}; return RegularValidator(id, type);}
function RegularValidator(id, type) {
    var value = $(id).val();
    var regexStr = new RegExp(GetRegex(type));
    if (value != null && value != '') {
        if (!value.match(regexStr)) {
            RequiredValidatorMsg(id, 1);
            return false;
        }
        RequiredValidatorMsg(id, 0);
        return true;
    }
    RequiredValidatorMsg(id, 2);
    return true;
}

function RegularValidatorByLen(id, type, len) {
    var value = $(id).val();
    if (value != null && value != '') {
        if (value.length >= len) {
            var regexStr = new RegExp(GetRegex(type));
            if (value != null && value != '') {
                if (!value.match(regexStr)) {
                    RequiredValidatorMsg(id, 1);
                    return false;
                }
                RequiredValidatorMsg(id, 0);
                return true;
            }
        }
        else {
            RequiredValidatorMsg(id, 1);
            return false;
        }
    }
    RequiredValidatorMsg(id, 2);
    return true;
}

function CompareValidator(id1, id2) {
    if ($(id1).val() == null || $(id1).val() == '') {
        RequiredValidatorMsg(id1, 1);
        return false;
    }
    if ($(id2).val() == null || $(id2).val() == '') {
        RequiredValidatorMsg(id2, 1);
        return false;
    }
    if ($(id1).val() == $(id2).val()) {
        RequiredValidatorMsg(id2, 0);
        return true;
    }
    else {
        RequiredValidatorMsg(id2, 1);
        return false;
    }
}

function CompareValidatorBlank(id1, id2) {
    if ($(id1).val() == null || $(id1).val() == '')
        RequiredValidatorMsg(id1, 2);
    if ($(id2).val() == null || $(id2).val() == '')
        RequiredValidatorMsg(id2, 2);
    if (($(id1).val() == null || $(id1).val() == '') && ($(id2).val() == null || $(id2).val() == ''))
        return true;
    if ($(id1).val() == $(id2).val()) {
        RequiredValidatorMsg(id2, 0);
        return true;
    }
    else {
        RequiredValidatorMsg(id2, 1);
        return false;
    }
}

function RequiredValidatorMsg(id, what) {
    if (what == 0) /* Accept value */
    {
        $(id).removeClass('Validate_Error');
        $(id).addClass('Validate_Accept');
    }
    if (what == 1) /* Send error */
    {
        $(id).removeClass('Validate_Accept');
        $(id).addClass('Validate_Error');
    }
    if (what == 2) /* Clear field */
    {
        $(id).removeClass('Validate_Error');
        $(id).removeClass('Validate_Accept');
    }
};

$.fn.icmsforms = function() {
    var $this = $(this); var $type = $this.attr("type");
    switch ($type) {
        case '': return '';
        case 'checkbox':
            var myArray = new Array();
            $this.each(function() {
                if ($(this).is(":checked")) myArray[myArray.length] = $(this).val();
            });
            return myArray.join(",");
        case 'radio':
            var myArray;
            $this.each(function() {
                if ($(this).is(":checked")) myArray = $(this).val();
            });
            return myArray;
        case 'select-one':
            var myArray;
            myArray = $(this).children("[@selected]").val();
            return myArray;
        case 'select-multiple':
            var myArray = new Array();
            $(this).children("[@selected]").each(function() {
                myArray[myArray.length] = $(this).val();
            });
            return myArray.join(",");
    }
}; 

