﻿var toUpdate;   
var baseUrl;


var Cookie = {
    /*
    * Options:
    * - expires: number of days from now
    * - path: request path, defaults to '/'
    * - domain: domain name
    * - secure: true or false
    */
    set: function(name, value)
    {
        var options = Object.extend($H({
            path: '/'
        }), arguments[2] || {});
        var cookie = escape(name) + '=' + escape(value || '');
        if (options.expires == false)
        {
            cookie += '; expires=' + new Date(2038, 0, 17, 19, 14, 07).toGMTString();
        } else if (options.expires)
        {
            var d = new Date();
            d.setTime(d.getTime() + (86400000 * parseFloat(options.expires)));
            cookie += '; expires=' + d.toGMTString();
        }
        if (options.path) cookie += '; path=' + options.path;
        if (options.domain) cookie += '; domain=' + options.domain;
        if (options.secure) cookie += '; secure';
        return (document.cookie = cookie);
    },
    get: function(name)
    {
        var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
        return (cookie ? unescape(cookie[2]) : null);
    },
    all: function()
    {
        return document.cookie.split(/\s*;\s*/).collect(function(cookie) { return cookie.split('=')[0] });
    },
    erase: function(name)
    {
        var cookie = Cookie.get(name) || true;
        Cookie.set(name, '', { expires: -1 });
        return cookie;
    },
    accept: function()
    {
        if (typeof navigator.cookieEnabled == 'boolean')
        {
            return navigator.cookieEnabled;
        }
        Cookie.set('_test', '1');
        return (Cookie.erase('_test') === '1');
    }
};

function DoLogIn()
{
    if ($("txtUserName").value.length > 1 && $("txtUserPassword").value.length > 3)
    {
        var returnValue = CommonAJAX.LogIn($("txtUserName").value,$("txtUserPassword").value);
        if (returnValue.value != null && returnValue.value != undefined)
        {
            Modalbox.hide();
            BuildUserBar(returnValue);
            eval(toUpdate);
        }
        else
        {
            alert("שם משתמש או סיסמא שגויים");
            $("txtUserPassword").value = "";
            $("txtUserPassword").focus();
        }
    }
    else
    {
        alert("שם משתמש או סיסמא שגויים");
        $("txtUserPassword").value = "";
        $("txtUserPassword").focus();
    }
}

function BuildUserBar(userNick)
{
    var s;

    s = CommonAJAX.GetUserBar().value;

    $("PersonalDiv").innerHTML = s;
}

function CheckAuthentication()
{
    BuildUserBar(CommonAJAX.CheckAuthentication());
}

function LogOut() 
{

    CommonAJAX.LogOut();
    Cookie.set('UserNet', '', { expires: -9 });
    Cookie.erase('UserNet');
    BuildUserBar("");
    eval(toUpdate);
}

function ShowForgotPassword()
{
    var path = baseUrl + "UI/personal/HTML/ForgotPassword.htm";
    Modalbox.show(path, { title: 'שכחתי סיסמא', width: 260 });
}

function DoForgotPassword() 
{
    var userNick = $("txtUserName").value;
    var userEmail = $("txtUserEmail").value;

    if (userNick.length > 1 && userEmail.length > 5)
    {
        CommonAJAX.SendForgotPasswordEmail(userNick, userEmail);
        Modalbox.hide();
        alert("במידה והמייל שלך אומת, תזכורת לסיסמתך נשלחה. אנא בדוק את תיבת האימייל שלך.");
    }
    else
    {
        alert("אנא הזן כינוי וכתובת אי-מייל");
    }
}

function checkEmail(email)
{
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email))
    {
        return false;
    }
    else
    {
        return true;
    }
}

function ShowVerifyEmail()
{
    var path = baseUrl + "UI/personal/HTML/verifyEmail.htm";
    Modalbox.show(path, { title: 'אימות דואר אלקטרוני', width: 260 });
}

function UpdateEmail()
{
    var userEmail = $("txtUserEmail").value;
    if (checkEmail(userEmail))
    {
        CommonAJAX.UpdateUserEmail(userEmail);
        var path = baseUrl + "UI/personal/HTML/verifyEmailCode.htm";
        Modalbox.show(path, { title: 'אימות דואר אלקטרוני', width: 260 });
    }
    else
    {
        alert("אנא הזן כתובת תקינה.");
    }
}


function SetIsVerify()
{
    var code = $("txtCode").value;

    if (code.length > 1 && CommonAJAX.UpdateIsVerified(code).value == true)
    {
        Modalbox.hide();
        eval(toUpdate);
        BuildUserBar();
    }
    else
    {
        alert("קוד האימות אינו תקין");
    }
    
}

function LogInModal()
{
    Modalbox.show( baseUrl + "UI/personal/HTML/LogIn.htm", { title: 'כניסה', width: 260 });
}


function feedback()
{
    var url = "http://www.kampyle.com/feedback_form/ff-feedback-form.php?site_code=8791178&form_id=5877&lang=he"
    var strFeatures = "toolbar=no,status=no,menubar=no,location=no,scrollbars=no,resizable=no"
        strFeatures = strFeatures + ",height=510,width=450"

        newWin = window.open(url, "feedback", strFeatures);

    newWin.opener = top;

}

function BookMark()
{
    var url = location.href;
    var title = document.title;
    
    if (window.sidebar) // Firefox
        window.sidebar.addPanel(title, url, "");
    else if (window.opera && window.print) // Opera
    { 
        var elem = document.createElement('a');
        elem.setAttribute('href', url);
        elem.setAttribute('title', title);
        elem.setAttribute('rel', 'sidebar');
        elem.click();
    }
    else if (document.all) // Ie
        window.external.AddFavorite(url, title);
}

function RegisterPage()
{
    window.location = baseUrl + "UI/Personal/Register.aspx";
}

