﻿var OnMenu = 0;
var lang = 'he';
//////////////////////////////////////////
// Contact Errs:
var ContactNameErr = 'יש להזין שם';
var ContactMailErr = 'יש להזין כתובת אימייל';
var ContactPhoneErr = 'יש להזין מספר טלפון';
var ContactMobileErr = 'יש להזין מספר נייד';
var ContactMsgErr = 'יש לרשום את נושא הפניה';



//////////////////////////////////////////
// Contact Defaults:
var ContactNameDef = 'שם';
var ContactMailDef = 'אימייל';
var ContactPhoneDef = 'טלפון';
var ContactMobileDef = 'נייד';
var ContactMsgDef = 'הודעה';



////////////////////////////////////////////////////////////
// Prototypes and global functions 

String.prototype.Trim = function() {
return this.replace(/^\s+|\s+$/g, "");
}


String.prototype.IsValidMail = function() {
var _email = this;
var emailReg = /^[a-z][a-z-_0-9\.]+@[a-z-_=>0-9\.]+\.[a-z]{2,3}$/i
return emailReg.test(_email);
}


function StringBuilder(value) {
this.strings = new Array("");
this.append(value);
}

// Appends the given value to the end of this instance.
StringBuilder.prototype.append = function(value) {
if (value) {
this.strings.push(value);
}
}

// Clears the string buffer
StringBuilder.prototype.clear = function() {
this.strings.length = 1;
}

// Converts this instance to a String.
StringBuilder.prototype.toString = function() {
return this.strings.join("");
}

String.prototype.ReplaceAll = function(s1, s2) {

if (this.indexOf(s1) > -1) {
return this.split(s1).join(s2);
}
else {
return this;
}
}


String.prototype.CutAfterLimit = function(limit) {
if (this.length <= limit) {
return (this);
}

var i = 0;
var currIndex = limit;
while (this[currIndex] != ' ' && currIndex > 0) {
currIndex--;
}
if (currIndex == 0) {
currIndex = limit;
}
return (this.substring(0, currIndex) + "..");
}


/*
getHttpRequestObj()
Function returns HTTP request object for all browsers.
*/
function getHttpRequestObj() {
    var xmlhttp;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}



function Trim(strText) 
{
    while (strText.substring(0, 1) == ' ')
        strText = strText.substring(1, strText.length);

    while (strText.substring(strText.length - 1, strText.length) == ' ')
        strText = strText.substring(0, strText.length - 1);
    return strText;
}


function ValidateMail(email) 
{
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var address = email;
    return reg.test(address);

}

function WriteStr(InputStr) 
{

    document.write(InputStr);

}


function SubmitContact(ActionPage) 
{
    if (!ActionPage) 
    {
        ActionPage = '/SendContact.aspx'
    }

    document.getElementById("ContactForm").action = ActionPage;

    var _ContactName = document.getElementById("ContactName").value;
    var _ContactPhone = document.getElementById("ContactPhone").value;
    //var _ContactMobile = document.getElementById("ContactMobile").value;
    var _ContactEmail = document.getElementById("ContactEmail").value;
    var _ContactMsg = document.getElementById("ContactMsg").value;


    var hasErrors = 0;

    if (Trim(_ContactName) == '' || Trim(_ContactName) == ContactNameErr || Trim(_ContactName) == ContactNameDef) 
    {
        document.getElementById("ContactName").value = ContactNameErr;
        document.getElementById("ContactName").className += " Err";
        hasErrors = 1;
    }


    
    if (Trim(_ContactEmail) == '' || !ValidateMail(_ContactEmail)) {
        document.getElementById("ContactEmail").value = ContactMailErr;
        document.getElementById("ContactEmail").className += " Err";
        hasErrors = 1;
    }
    

    if (Trim(_ContactPhone) == '' || Trim(_ContactPhone) == ContactPhoneErr || Trim(_ContactPhone) == ContactPhoneDef) 
    {
        document.getElementById("ContactPhone").value = ContactPhoneErr;
        document.getElementById("ContactPhone").className += " Err";
        hasErrors = 1;
    }


    //if (Trim(_ContactMobile) == '' || Trim(_ContactMobile) == ContactMobileErr || Trim(_ContactMobile) == ContactPhoneDef) {
        //document.getElementById("ContactMobile").value = ContactMobileErr;
        //hasErrors = 1;
    //}
    
    if (Trim(_ContactMsg) == '' || Trim(_ContactMsg) == ContactMsgErr || Trim(_ContactMsg) == ContactMsgDef) 
    {
        document.getElementById("ContactMsg").value = ContactMsgErr;
        document.getElementById("ContactMsg").className += " Err";
        hasErrors = 1;
    }


    if (hasErrors) 
    {
        return false;
    }
    else 
    {
        document.getElementById("ContactHidden").value = "dsui6rt58349q568v934q0563p40b5vn";
    }
}


function ClickOnContactText(Obj) {

    Obj.className = "Text";
    var Str = Trim(Obj.value);

    if (Str == ContactNameErr || Str == ContactNameDef) 
    {
        Obj.value = '';
    }
    else if (Str == ContactMailErr || Str == ContactMailDef) 
    {
        Obj.value = '';
    }
    else if (Str == ContactPhoneErr || Str == ContactPhoneDef) 
    {
        Obj.value = '';
    }
    else if (Str == ContactMsgErr || Str == ContactMsgDef) 
    {
        Obj.value = '';
    }
    else if (Str == ContactMobileErr || Str == ContactMobileDef) {
        Obj.value = '';
    }
}


function GoFromContactText(Obj) {
    Obj.className = "Text";
    var Str = Trim(Obj.value);
    if (Obj.id == 'ContactName' && (Str == ContactNameErr || Str == '')) 
    {
        Obj.value = ContactNameDef;
    }
    else if (Obj.id == 'ContactEmail' && (Str == ContactMailErr || Str == '')) 
    {
        Obj.value = ContactMailDef;
    }
    else if (Obj.id == 'ContactPhone' && (Str == ContactPhoneErr || Str == '')) 
    {
        Obj.value = ContactPhoneDef;
    }
    else if (Obj.id == 'ContactMsg' && (Str == ContactMsgErr || Str == '')) 
    {
        Obj.value = ContactMsgDef;
    }
    else if (Obj.id == 'ContactMobile' && (Str == ContactMobileErr || Str == '')) {
        Obj.value = ContactMobileDef;
    }
}


////////////////////////////////////////////////////////////
// Function returns get parameter value from url string
function GetUrlParam(ParamName, UrlStr) 
{
    var reParam = new RegExp('(?:[\?&]|&)' + ParamName + '=([^&]+)', 'i');
    var match = UrlStr.match(reParam);
    return (match && match.length > 1) ? match[1] : '';
}



function OverTopMenu(PageId, PageMenuImageHover) 
{
    SetSubMenuLocation(PageId);
    OnMenu = 1;
    var MenuObj = document.getElementById("TopMenu" + PageId);
    var SubMenuObj = document.getElementById("TopMenuSub" + PageId);
    var MenuImageObj = document.getElementById("TopMenuImg" + PageId);
   
    if (SubMenuObj.innerHTML.Trim() != '' && SubMenuObj.style.display == '') 
    {
        return;
    }
    else if (SubMenuObj.innerHTML.Trim() != '')
    {
        $("#TopMenuSub" + PageId).slideDown(500);
        if (PageMenuImageHover.Trim() != '') 
        {
            MenuImageObj.src = '/uploads/' + PageMenuImageHover;
        }        
        return;
    }
    var pageURL = "/ajax/GetSubPages.aspx?lang=" + lang + "&ParentPageId=" + encodeURI(PageId);
    var xObj = getHttpRequestObj();
    xObj.onreadystatechange = function() {
        if (xObj.readyState == 4) {

            var ResponseString = xObj.responseText;
            var PageNames = GetUrlParam("PageNames", ResponseString).Trim();
            var PageLinks = GetUrlParam("PageLinks", ResponseString).Trim();
            var MenuImages = GetUrlParam("MenuImages", ResponseString).Trim();
            var PageNamesArr = PageNames.split(";");
            var PageLinksArr = PageLinks.split(";");
            var MenuImagesArr = MenuImages.split(";");
            var PageName = '';
            var PageLink = '';
            var MenuImage = '';
            if (PageNames.Trim() == '') {
                /////////////////////////
                // No Sub Pages

            }
            else {
                if (!OnMenu) 
                {
                    return;
                }
                /////////////////////////
                // Creating Sub Menu
               
                var HtmStr = '';
                HtmStr = '<div class="TopSubMenu">';
                HtmStr += '<div class="Top">';
                for (var i = 0; i < PageNamesArr.length; i++) {
                    PageName = PageNamesArr[i].Trim();
                    PageLink = PageLinksArr[i].Trim();
                    MenuImage = MenuImagesArr[i].Trim();
                    if (PageName.Trim() != '') {
                        HtmStr += '<a title="' + PageName + '" href="' + PageLink + '"><img src="/uploads/' + MenuImage + '" /></a>';

                    }
                }
                HtmStr += '</div>';
                HtmStr += '<div class="Bottom"></div>';
                HtmStr += '</div>';
                SubMenuObj.innerHTML = HtmStr;

                $("#TopMenuSub" + PageId).slideDown(500);
          

            }
            if (PageMenuImageHover.Trim() != '') {
                MenuImageObj.src = '/uploads/' + PageMenuImageHover;
            }
        }
    };

    xObj.open("post", pageURL, true);
    xObj.send(null);
}



function OutTopMenu(PageId, PageMenuImage) 
{

    OnMenu = 0;
    //document.getElementById("TopMenuSub" + PageId).style.display = 'none';
    $("#TopMenuSub" + PageId).slideUp('500', function() 
    {
        document.getElementById("TopMenuImg" + PageId).src = '/uploads/' + PageMenuImage;
    });

}

function SetSubMenuLocation(PageId) 
{
    var offsetT = $('#TopMenu' + PageId).offset().top;
    var offsetL = $('#TopMenu' + PageId).offset().left;
    var MenuHeight = $('#TopMenu' + PageId).height();
    offsetT += MenuHeight - 1
   
    document.getElementById("TopMenuSub" + PageId).style.top = offsetT + "px";
    document.getElementById("TopMenuSub" + PageId).style.left = offsetL + "px";

}
