function CheckLogin(){
    if (logged_in)
        document.location.href = '/order/';
    else {
        $("#login_newform").hide();
        OpenDialog('dialog_login');
    }
    return false;
}

function OpenNewAccount(){
    OpenDialog('dialog_newaccount');
    return false;
}

function OpenRestorePassword(){
    OpenDialog('dialog_restorepsw');
    return false;
}

function RestorePassword(){
    var userid = $('#id_restorepsw_userid').val();
    if (userid) {
        $('#dialog_plzwait').show();
        $.post('/ajax/restorepassword/',{userid:userid},function(data){
            $('#dialog_plzwait').hide();
            if (!data.ok) alert('Invalid E-mail');
            else MessageBox(data.msg);
        },'json');
    } else alert('Please input Your E-mail');
    return false;
}

function NewAccountAccount(){
    var account = $('#id_newaccount_account').val();
    if (account) {
        $('#dialog_plzwait').show();
        $.post('/ajax/companybyaccount/',{account:account},function(data){
            $('#dialog_plzwait').hide();
            $('#id_newaccount_company').val(data.company);
            if (!data.ok) alert('Invalid Account Number');
        },'json');
    } else alert('Please input Your Account Number');
}

function LoginPostalSubmit(){
    var postal = $("#id_login_postal").val();
    if (!ValidatePostalCode(postal)){
        alert('Please enter a valid postal code');
        return false;
    }
    $("#login_newform").show();
    return false;
}

function LoginNewSubmit(){
    var postal = $("#id_login_postal").val();
    if (!ValidatePostalCode(postal)){
        alert('Please enter a valid postal code');
        return false;
    }
    var data = {
        postal: postal,
        name: $("#id_login_name").val(),
        address: $("#id_login_address").val(),
        phone: $("#id_login_phone").val(),
        company: $("#id_login_company").val(),
        daycontact: $("#id_login_daycontact").val(),
        email: $("#id_login_email").val()
    };
    if (data.name==''){
        alert('Please enter your name');
        return false;
    }
    if ((data.email=='') && (data.phone=='')){
        alert('Please enter contact email or phone');
        return false;
    }
    if ((data.email!='') && (!ValidateEmail(data.email))){
        alert('Please enter a valid email');
        return false;
    }
    if ((data.phone!='') && (!ValidatePhone(data.phone))){
        alert('Please enter a valid phone');
        return false;
    }
    $.post(base_ajax_url+"createrequest/",data,function(res){
        if (res.ok)
            MessageBox(res.msg);
        else
            MessageBox("Internal Error");
    },"json");
    return false;
}

function LoginNewAccountSubmit(){
    var data = {
        userid: $("#id_newaccount_userid").val(),
        password: $("#id_newaccount_password").val(),
        securityquestion: $("#id_newaccount_securityquestion").val(),
        userid2: $("#id_newaccount_userid2").val(),
        password2: $("#id_newaccount_password2").val(),
        securityanswer: $("#id_newaccount_securityanswer").val(),
        firstname: $("#id_newaccount_firstname").val(),
        account: $("#id_newaccount_account").val(),
        phone: $("#id_newaccount_phone").val(),
        lastname: $("#id_newaccount_lastname").val(),
        company: $("#id_newaccount_company").val(),
        ship_to: $("#id_newaccount_ship_to").val(),
        ship_city: $("#id_newaccount_ship_city").val(),
        ship_postal: $("#id_newaccount_ship_postal").val(),
        ship_address: $("#id_newaccount_ship_address").val(),
        ship_province: $("#id_newaccount_ship_province").val()
    };
    $(".ast").hide();
    if (
       (data.userid=='') ||
       (data.password=='') ||
       (data.securityquestion=='') ||
       (data.userid2=='') ||
       (data.password2=='') ||
       (data.securityanswer=='') ||
       (data.firstname=='') ||
       (data.account=='') ||
       (data.phone=='') ||
       (data.lastname=='') ||
       (data.company=='')
    ){
        $(".ast").show();        
        alert('Please enter mandatory fields');
        return false;
    }
    if (data.userid != data.userid2) {
        $("#ast_id_newaccount_userid").show();
        $("#ast_id_newaccount_userid2").show();
        alert("The two Email fields didn't match.");
        return false;
    }
    if (data.password != data.password2) {
        $("#ast_id_newaccount_password").show();
        $("#ast_id_newaccount_password2").show();
        alert("The two password fields didn't match.");
        return false;
    }
    if (!ValidateEmail(data.userid)){
        $("#ast_id_newaccount_userid").show();
        alert('Please enter a valid Email');
        return false;
    }
    if (!ValidatePhone(data.phone)){
        $("#ast_id_newaccount_phone").show();
        alert('Please enter a valid phone');
        return false;
    }
    $.post(base_ajax_url+"createcustomer/",data,function(res){
        var msg = res.msg;
        if (res.ok) {
            var proceed = "'"+media_url+"images/proceed.gif'";
            var proceed_hover = "'"+media_url+"images/proceed_black.gif'";
            res.msg += '<br /><br /><img src='+proceed+' onmouseover="this.src='+proceed_hover+'" onmouseout="this.src='+proceed+'" class="hand" onclick="return CloseDialog();" height="31" />'
        }
        MessageBox(res.msg);
    },"json");
    return false;
}

function LoginNew(){
    var postal = $("#id_login_postal").val();
    if (!ValidatePostalCode(postal)){
        alert('Please enter a valid postal code');
        return false;
    }
    var data = {
        postal: postal,
        name: $("#id_login_name").val(),
        address: $("#id_login_address").val(),
        phone: $("#id_login_phone").val(),
        company: $("#id_login_company").val(),
        daycontact: $("#id_login_daycontact").val(),
        email: $("#id_login_email").val()
    };
    if (data.name==''){
        alert('Please enter your name');
        return false;
    }
    if ((data.email=='') && (data.phone=='')){
        alert('Please enter contact email or phone');
        return false;
    }
    if ((data.email!='') && (!ValidateEmail(data.email))){
        alert('Please enter a valid email');
        return false;
    }
    if ((data.phone!='') && (!ValidatePhone(data.phone))){
        alert('Please enter a valid phone');
        return false;
    }
    $.post(base_ajax_url+"createrequest/",data,function(res){
        if (res.ok)
            MessageBox(res.msg);
        else
            MessageBox("Internal Error");
    },"json");
    return false;
}

function LoginSubmit(){
    var data = {
        userid: $("#id_login_userid").val(),
        password: $("#id_login_password").val()
    };
    if ((data.userid=='') || (data.password=='')){
        alert('Please enter a userid and password');
        return false;
    }
    $.post(base_ajax_url+"login/",data,function(res){
        if (res.ok)
            document.location.href='/order/';
        else
            alert(res.msg);
    },"json");
    return false;
}
