var COOKIE_NAME_USER = '10pipsusername';
var COOKIE_NAME_PWD = '10pipspwd';

//
// Load login data from cookies into form.
//
function load_login_data_from_cookies()
{
    //
    // Parse cookie data.
    //
    var f = document.forms['FormLogin'];
    
    var username = read_cookie(COOKIE_NAME_USER);
    var pwd = read_cookie(COOKIE_NAME_PWD);
    
    //
    // Load cookie data into form.
    //
    var isCookie = false;
    
    if (username != undefined)
    {
        isCookie = true;
        f.username.value = username;
    }
    
    if (pwd != undefined)
    {
        isCookie = true;
        f.pwd.value = pwd;
    }
    
    //
    // Case cookie exists - 
    //
    // . Check the 'remember' checkbox.
    // . Submit the login form.
    // 
    /*f.remember.checked = false;

    if (isCookie)
    {
        f.remember.checked = true;
        f.submit();
    }*/
}

//
// Login from header form.
//
// NOTE: only hancles the 'remember'
// checkbox. Does NOT validate input
// before submiting the form.
//
function send_form_login_from_header()
{
    var f = document.forms['FormLogin'];

    var username = f.username.value;
    var pwd = f.pwd.value;
    var doRemember = false; //f.remember.checked;
    
    // 
    // Case save login data as a cookie.
    // 
    if (doRemember)
    {
        create_cookie(COOKIE_NAME_USER, username, 365);
        create_cookie(COOKIE_NAME_PWD, pwd, 365);
    }
    
    //
    // Otherwise - delete cookies
    //
    else
    {
        erase_cookie(COOKIE_NAME_USER); 
        erase_cookie(COOKIE_NAME_PWD); 
    }
    
    //
    // Case error - input too short.
    //
    /*
    if (username.length < 4)
    {
        alert('User name should be at least 4 characters long');
        return false;
    }
    */
    
    if (pwd.length < 4)
    {
        alert('Password should be at least 4 characters long');
        return false;
    }
    
    //
    // Send the form.
    //
    f.submit();
    return false;
}

//
// Logout: erases login cookies and
// calls the logout URL.
//
function logout()
{
    erase_cookie(COOKIE_NAME_USER); 
    erase_cookie(COOKIE_NAME_PWD); 

    self.location = JS_ACTIONS_FILE_URL + '?action=logout';
}

                   
//
// Displays a sub page text (in
// a screen with a sub menu).
//
function disp_sub_page(idName)
{
    //
    // Hide all blocks with class 'sub-page-text'.
    //
    $(".sub-page-text").hide();
    $(".sub-menu a").css({fontWeight: 'normal'});
    
    //
    // Display the desired block.
    //
    $("#"+idName).show();
    
    if (document.getElementById("Link"+idName))
    {
        $("#Link"+idName).css({fontWeight: 'bold'});
    }
}

//
// Sets the value of the form field 'orderBy'.
//
// NOTES: 
//
// . Fails on these cases, while alreting
//   an error message:
//
//   . There are more than a single form on 
//     the page
//
//   . The form does not contain a field named
//     'orderBy'.
//
// . Sort direction is toggled.
//
function set_order_by_field(inOrderBy)
{
    //
    // Error cases.
    //
    var numForms = document.forms.length;
     
    if (numForms != 1)
    {
        alert('set_order_by_field() Error: There are '+numForms+' forms on this page - there should be only one');
        return false;
    }
    
    var f = document.forms[0];
    
    if (f.orderBy == undefined)
    {
        alert('set_order_by_field() Error: Form does not contain a field named "orderBy"');
        return false;
    }
    
    if (f.orderDir == undefined)
    {
        alert('set_order_by_field() Error: Form does not contain a field named "orderDir"');
        return false;
    }
    
    //
    // Set values for form fields 'orderBy'
    // and 'orderDir'.
    //
    var curOrderBy = f.orderBy.value;
    var curOrderDir = f.orderDir.value;
    
    if (curOrderBy == inOrderBy)
    {
        f.orderDir.value = (curOrderDir == JS_DIR_ASC) ? JS_DIR_DESC : JS_DIR_ASC;
    }
    
    else
    {
        f.orderBy.value = inOrderBy;
        f.orderDir.value = JS_DIR_DESC;
    }
    
    //
    // Return positive indication.
    //
    return true;
}

//
// Sets the value of the form field 'pageNum'.
//
// NOTES: 
//
// . Fails on these cases, while alreting
//   an error message:
//
//   . There are more than a single form on 
//     the page
//
//   . The form does not contain a field named
//     'pageNum'.
//
function set_page_number_field(inPageNum)
{
    //
    // Error cases.
    //
    var numForms = document.forms.length;
     
    if (numForms != 1)
    {
        alert('set_page_number_field() Error: There are '+numForms+' forms on this page - there should be only one');
        return false;
    }
    
    var f = document.forms[0];
    
    if (f.pageNum == undefined)
    {
        alert('set_page_number_field() Error: Form does not contain a field named "pageNum"');
        return false;
    }
    
    //
    // Set values for form field 'pageNum'.
    //
    f.pageNum.value = inPageNum;
    
    //
    // Return positive indication.
    //
    return true;
}

//
// Sets the value of the form field 'year'.
//
// NOTES: 
//
// . Fails on these cases, while alreting
//   an error message:
//
//   . There are more than a single form on 
//     the page
//
//   . The form does not contain a field named
//     'year'.
//
function set_year_field(inYear)
{
    //
    // Error cases.
    //
    var numForms = document.forms.length;
     
    if (numForms != 1)
    {
        alert('set_year_field() Error: There are '+numForms+' forms on this page - there should be only one');
        return false;
    }
    
    var f = document.forms[0];
    
    if (f.year == undefined)
    {
        alert('set_year_field() Error: Form does not contain a field named "year"');
        return false;
    }
    
    //
    // Set values for form field 'pageNum'.
    //
    f.year.value = inYear;
    
    //
    // Return positive indication.
    //
    return true;
}

//
// Signup
//
function send_form_signup()
{
    var f = document.forms['FormSignup'];
    
    var email = f.email.value;
    var pwd = f.pwd.value;
    var firstName = f.firstname.value;
    var lastName = f.lastname.value;
    var username = f.username.value;
    var cty = f.cty.value;
    var phone = f.phone.value; // optional
    var email = f.email.value;
    var url = f.url.value; // optional
    var plan = f.plan.value;
    var pwd = f.pwd.value;
    var pwd2 = f.pwd2.value;
    var captcha = f.captcha.value;
    var isRead = f.isRead.checked;
    
    //
    // Initialize is-ok variable.
    //
    var isOk = true;
    
    //
    // Hide all form error messages.
    //
    $("#FormSignup .error").hide();
    
    //
    // Error cases.
    //
    if (firstName == '')
    {
        $("#ErrMsgFirstName").html("Name is missing");
        $("#ErrMsgFirstName").show();
        isOk = false;
    }
    
    if (lastName == '')
    {
        $("#ErrMsglastName").html("Name is missing");
        $("#ErrMsglastName").show();
        isOk = false;
    }
    
    if (username == '')
    {
        $("#ErrMsgUsername").html("User name is missing");
        $("#ErrMsgUsername").show();
        isOk = false;
    }
    
    if (cty == '')
    {
        $("#ErrMsgCty").html("Country is missing");
        $("#ErrMsgCty").show();
        isOk = false;
    }
    
    if (email == '')
    {
        $("#ErrMsgEmail").html("Email is missing");
        $("#ErrMsgEmail").show();
        isOk = false;
    }
    
    else if (!is_email_syntax_ok(email))
    {
        $("#ErrMsgEmail").html("Email address is invalid");
        $("#ErrMsgEmail").show();
        isOk = false;
    }

    if (plan == '')
    {
        $("#ErrMsgPlan").html("Plan is missing");
        $("#ErrMsgPlan").show();
        isOk = false;
    }
    
    if (pwd == '')
    {       
        $("#ErrMsgPwd").html("Password is missing");
        $("#ErrMsgPwd").show();
        isOk = false;
    }
    
    if (pwd2 == '')
    {
        $("#ErrMsgPwd2").html("Password is missing");
        $("#ErrMsgPwd2").show();
        isOk = false;
    }
    
    else if (pwd != pwd2)
    {
        $("#ErrMsgPwd2").html("Both passwords should be identical");
        $("#ErrMsgPwd2").show();
        isOk = false;
    }
    
    if (!isRead)
    {
        $("#ErrMsgIsRead").html("You must read and agree to our UserAgreement");
        $("#ErrMsgIsRead").show();
        isOk = false;
    }

    if (captcha == '')
    {
        $("#ErrMsgCaptcha").html("Captcha is missing");
        $("#ErrMsgCaptcha").show();
        isOk = false;
    }
    
    if (!isOk)
    {
        return false;
    }
    
    // Send the form.
    f.submit();
    return false;
}
                   