
var a_sign = ["$","AU$","C$","&pound;","&euro;"];
var a_type = [1,1,3,2,2,1,3,3,2,2,2,2,2,3];
var a_level = [15,15,12,10,8,6,25,26];

function parseDate(input, format) {
    format = format || 'yyyy-mm-dd'; // default format
    var parts = input.match(/(\d+)/g), 
    i = 0, fmt = {};
    // extract date-part indexes from the format
    format.replace(/(yyyy|dd|mm)/g, function(part) { fmt[part] = i++; });

    return new Date(parts[fmt['yyyy']], parts[fmt['mm']]-1, parts[fmt['dd']]);
}

function getperiod()
{

    var tmp=$('#period  option:selected').val();

    return tmp;
}

function show_current_currency()
{
    $('input[type=radio][name="currency"]').removeAttr('checked');
    $('input[type=radio][name="currency"][value='+$('#current_currency').val()+']').attr('checked', 'checked');
    $("div[class='customRadioChecked']").removeClass("customRadioChecked").addClass("customRadio"); 
    $(":radio[name=currency]").filter(":checked").parent().children(".customRadio").removeClass("customRadio").addClass("customRadioChecked");

    //$('input[type=radio][name="currency"][value='+$('#current_currency').val()+']').change();
    //$('input[type=radio][name="currency"][value='+$('#current_currency').val()+']').attr('checked', 'checked');
}

function changeCurrency()
{
    var currency= $(":radio[name=currency]").filter(":checked").val();
    var pagecost= parseInt($("#pagecost_hid").val());
    //var page_count= parseInt($("#page_count").val());
    var page_count= parseInt($("#words_pages  option:selected").val())==0?parseInt($("#page_count").val()):Math.ceil(parseInt($("#page_count").val())/250.);
    var new_total = Math.round((pagecost*Math.round(a_currency[currency]*1000))/1000)*page_count;

    var grand_total=new_total;
    var idiscount= $("#idiscount").html();
    var  save_total=0;
    if (idiscount>0)
        {
        save_total = Math.round(idiscount*new_total/100);
        $('#save_sign').html(a_sign[currency]);
        $("#save_total").html((save_total/100).toFixed(2));
        grand_total-= save_total;
    }
    var c_balance=  Math.round($("#c_balance_hid").val()*100);
    if (c_balance>0)
        {
        $('#balance_sign').html(a_sign[currency]);
        c_balance= Math.round((c_balance*Math.round(a_currency[currency]*1000))/1000);

        $("#balance").html((c_balance/100).toFixed(2));
        grand_total-= c_balance;
    }



    $('#current_currency').val(currency);
    $('#subtotal').html((new_total/100).toFixed(2));
    $('#subtotal_sign').html(a_sign[currency]);
    pagecost=Math.round((pagecost*Math.round(a_currency[currency]*1000))/1000);
    $('#pagecost').html((pagecost/100).toFixed(2));
    grand_total=(grand_total/100).toFixed(2);
    if (grand_total<0)
        {
        var substracted=new_total-save_total; 
        $("#substracted").html("<strong>"+a_sign[currency]+(substracted/100).toFixed(2)+"</strong>");
        $('#grand_total').html("0");
    }    
    else
        {    
        $('#grand_total').html(grand_total);
        $("#substracted").html("it");
    }
    $('#grand_sign').html(a_sign[currency]);
}



function update_cost()
{
    var period = getperiod();
    var type= parseInt($('#paper_type  option:selected').val());
    var level= parseInt($("#level  option:selected").val());
    
    
    var p_num = 0;
    if (period==60) p_num=6;
    if (period==30) p_num=5;
    if (period==14) p_num=4;
    if (period==7) p_num=3;
    if (period==4) p_num=2;
    if (period==2) p_num=1;
    if (level==-1)
        return false;
    var l_num = level;
    var t_num = a_type[type]-1;
    var page_count=parseInt($("#words_pages  option:selected").val())==0?parseInt($("#page_count").val()):Math.ceil(parseInt($("#page_count").val())/250.);
    
    var total = Math.round(price[t_num][p_num][l_num]*100)*page_count;
    
    $("#pagecost_hid").val(Math.round(price[t_num][p_num][l_num]*100));
    $('#subtotal').html(total);
    $('#usd_total').val(total);
}



function change_total_words()
{
    var pages = parseInt($("#page_count").val());
    var words_pages = parseInt($("#words_pages  option:selected").val());
   
    if (words_pages==1)
        {
        pages=Math.ceil(pages/250.);
        //$("#wordstxt").fadeOut();
        //$("#wordstxt1").fadeOut();
    }
    else
        {
        //$("#wordstxt").fadeIn();
        //$("#wordstxt1").fadeIn();
    }
    $("#pagescnt").html(pages);
    var level= parseInt($("#level  option:selected").val());
    
    
    var period = getperiod();
    var total = period*a_level[level];
    if (total < pages)
        {
        if (level==0) _level = "High-School Level"
        if (level==1) _level = "College Level";
        if (level==2) _level = "University Level, Bachelor's";
        if (level==3) _level = "University Level, Master's";
        if (level==4) _level = "Specialized Level (Nursing)";
        if (level==5) _level = "PhD level";
        if (level==6) _level = "Proofreading";
        if (level==7) _level = "Editing (Rewriting)";
        alert("Unfortunately, we cannot write more than "+a_level[level]+" pages per day at "+_level+", otherwise it will affect the quality of the paper. Please select fewer pages, or extend your deadline.");
        $("#page_count").val(total);


    }
    if (words_pages==0)
        {
        $("#total_words").fadeIn();
        $("#total_words").html(parseInt($("#page_count").val())*250);
        $("#wordstxt1").html(" words ");

    }
    else
        {
        //$("#total_words").fadeOut();
        $("#total_words").html(pages);
        $("#wordstxt1").html(" pages ");
    }
    update_cost();
    changeCurrency();
}



function changeSubject()
{
    var subj = $("#field_of_study option:selected").val();

    if(subj==8 | subj==9 | subj==16 | subj==21  | subj==31 | subj==33)
        {
        $("#level [value='4']").attr("selected", "selected");	
        $("#level").attr("disabled","disabled");
        document.getElementById("level").disabled = true;

    }
    else
        {
        $("#level").removeAttr("disabled");
    }
    //alert($("#level").attr("disabled"));
    window.customForm.updateSelect($("#level"));
    window.customForm.customizeSelects();
    update_cost();
    changeCurrency();
}

function frmSubmit()
{
    document.order_form.level.disabled = false;
    return true;
}

function objToString(o) {
    var s = '{\n';
    for (var p in o)
        s += '    ' + p + ': ' + o[p] + '\n';
    return s + '}';
}




function change_type()
{
    var type= $('#paper_type  option:selected').val();
    var period = getperiod();
    var level= parseInt($("#level  option:selected").text());
    var period = $("#period");
    period.empty();

    if (a_type[type]==1)
        {
        period.append( $('<option value="14">Two Weeks</option>'));
        period.append( $('<option value="7">One Week</option>')); 
        period.append( $('<option value="4">Four Days</option>')); 
        period.append( $('<option value="2">48 Hours</option>')); 
        period.append( $('<option value="1">24 Hours</option>')); 
    }		

    if (a_type[type]==2)
        {
        period.append( $('<option value="60">Two Months</option>'));
        period.append( $('<option value="30">One Month</option>')); 
        period.append( $('<option value="14">Two Weeks</option>'));
        period.append( $('<option value="7">One Week</option>')); 
        period.append( $('<option value="4">Four Days</option>'));
    }		

    if (a_type[type]==3)
        {
        period.append( $('<option value="60">Two Months</option>'));
        period.append( $('<option value="30">One Month</option>')); 
        period.append( $('<option value="14">Two Weeks</option>'));
        period.append( $('<option value="7">One Week</option>')); 
        period.append( $('<option value="4">Four Days</option>'));
        period.append( $('<option value="2">48 Hours</option>')); 
        period.append( $('<option value="1">24 Hours</option>'));
    }			
    change_total_words();
    update_cost();
    changeCurrency();
}



function check_discount()
{
    $('#apply_discount').attr('onClick', '');
    $('#discount_error').empty();
    $('#discount_error').fadeOut();
    var code= $("#discount_code").val();

    $("#discount_code").attr("disabled", "disabled");
    $.post("../ajax_php/check_discount.php", {code: code}, function(answ){

        if (answ[0]=='error')
            {
            $("#discount_code").attr("disabled", "");
            $('#discount_error').append('<li>'+answ[1]+'<li/>');
            $('#discount_error').fadeIn();
            $('#apply_discount').attr('onClick', 'check_discount();');
            return;

        }
        var percent= answ[0];
        var idiscount= $("#idiscount_hid").val();
        if (percent>idiscount)
            {
            $("#apply_discount").css("display", "none"); 
            $("#clear_discount").css("display", ""); 
            $("#discount_text").html("Your discount is");
            $("#idiscount").html(percent);
            update_cost();
            changeCurrency();
            $("#discount_p").fadeIn();
        }
        $('#apply_discount').attr('onClick', 'check_discount();');


    }, 'json');
}

function clear_discount()
{

    $("#discount_code").val("");
    $('#discount_error').empty();
    $('#discount_error').fadeOut();
    $("#discount_code").attr("disabled", "");
    $("#apply_discount").css("display", ""); 
    $("#clear_discount").css("display", "none");
    var idiscount= $("#idiscount_hid").val();
    $("#idiscount").html(idiscount);
    update_cost();
    changeCurrency();
    if (idiscount>0)
        {
        $("#discount_text").html("Your accumulative discount level is");
        $("#discount_p").fadeIn();
        return; 
    }
    $("#discount_p").fadeOut();
}
function Numbers(e)
{
    var keynum;
    var keychar;
    var numcheck;

    if(window.event) // IE
        {
        keynum = e.keyCode;
    }
    else if(e.which) // Netscape/Firefox/Opera
        {
        keynum = e.which;
    }
    keychar = String.fromCharCode(keynum);
    numcheck = /\d/;
    return numcheck.test(keychar)|| keynum<32;
}

function change_tzspan()
{
    $("#tzspan").html($('#timezone  option:selected').val());
}

function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}

function chngcntry()
{
    var country=$('#country  option:selected').val();
    if (country==1) $("#fax").html("+1-800-490-5055");
    else
        if (country==17 || country==161)
        $("#fax").html("+61-28-088-6000");
    else   $("#fax").html("(020) 7117 1110");             
}


function chngcntry2()
{
    var country=$('#country  option:selected').val();
    $("#c_name").html($('#country  option:selected').html());
    if (country==1) $("#fax").html("+1-800-490-5055");
    else
        if (country==17 || country==161)
        $("#fax").html("+61-28-088-6000");
    else   $("#fax").html("(020) 7117 1110"); 
    $.post('../ajax_php/chngcntry.php',{country: country}, 
    function(answ) 
    { 
        if (answ=="error") return;
        $("#c_code").html("+"+answ);
    }
    );            
}

function send_voice()
{
    $.post('../ajax_php/send_voice.php',{test: 1}, 
    function(answ) 
    { 
        if (answ=="error") location.href="https://privatewriting.com/order.html";
        else $("#sendvoiceok_button").click();
    }
    );
}






function show_popup_hint(order, e)
{

    
     var mousex=e.pageY;
     var mousey=e.pageX;
    $.post("../ajax_php/get_order_hint.php", {order: order}, function(answ)
    {


        if (answ[0]=="error") return;
        $("#hint_title").html(answ[0]); 
        $("#hint_text").html(answ[1]);
        
        $("#popup_hint").css("top",mousex); 
        $("#popup_hint").css("left",mousey);
        $("#popup_hint").fadeIn();     
    }, "json");


}

function hide_popup_hint()
{
    $("#popup_hint").fadeOut(); 
}

