﻿var F;
var G;
var hCE;
var PE;
var hCI;
var PI;
var M;
var Q;

var Name;
var Firma;
var Email;
var Phone;

function computePrice()
{
    getValues();
    if (validateValues())
    {
        getServerPrice();
    }
    else
    {
        progress(false);
    }
}

function getValues()
{
    F = $("ddF");
    G = $("ddG"); hCE = $("ddhCE"); PE = $("ddPE"); hCI = $("ddhCI"); PI = $("ddPI");
    M = $("ddM");
    Q = $("ddQ");
    Name = $("txName"); Firma = $("txFirma"); Email = $("txEmail"); Phone = $("txPhone");
}

function validateValues(isContact)
{
    var errors = "";
    if (F.selectedIndex == 0){
        errors += " - Selectati o optiune la \"Format mapa\";\n";
    }
    if (G.selectedIndex == 0){
        errors += " - Selectati o optiune la \"Grosime carton\";\n";
    }
    if (hCE.selectedIndex == 0){
        errors += " - Selectati o optiune la \"Hartie tipar pentru coperta exterioara\";\n";
    }
    if (PE.selectedIndex == 0){
        errors += " - Selectati o optiune la \"Plastifiere coperta exterioara\";\n";
    }
    if (hCI.selectedIndex == 0){
        errors += " - Selectati o optiune la \"Hartie tipar pentru interior\";\n";
    }
    if (PI.selectedIndex == 0){
        errors += " - Selectati o optiune la \"Plastifiere interior\";\n";
    }
    if (M.selectedIndex == 0){
        errors += " - Selectati o optiune la \"Mod de inchidere\";\n";
    }
    if (Q.selectedIndex == 0){
        errors += " - Selectati o optiune la \"Tiraj\";\n";
    }
    
    if (errors.length > 0)
    {
        errors += "\n";
    }
    
    if (trim(Name.value).length == 0){
        errors += " - Completati numele dvs.;\n";
    }
    if (trim(Firma.value).length == 0){
        errors += " - Completati denumire firma;\n";
    }
    if (!isEmail(trim(Email.value))){
        errors += " - Completati adresa dvs. de email;\n";
    }
    if (!isPhone(trim(Phone.value))){
        errors += " - Completati telefonul dvs.;\n";
    }
   
    if (isContact)
    {
        if (errors.length > 0)
        {
            errors += "\n";
        }
        
        if (trim($("txOptions").value).length == 0)
        {
            errors += " - Specificati decoratiunile sau caracteristicile dorite;\n";
        }
    }
   
    
    if (errors.length > 0)
    {
        alert("Va rugam sa remediati urmatoarele probleme:\n"+errors);
        return false;
    }
    else
    {
        return true;
    }    
}

function getAttributesString(unitPrice)
{
    var newLine = "<br />";
     return "FORMAT " + newLine +
            " - format mapa: " + F.options[F.selectedIndex].text + "," + newLine + newLine +
            "MATERIALE FOLOSITE: " + newLine +
            " - grasime carton: " + G.options[G.selectedIndex].text + "," + newLine +
            " - hartie coperta exterioara: " + hCE.options[hCE.selectedIndex].text + "," + newLine +
            " - plastifiere coperta exterioara: " + PE.options[PE.selectedIndex].text + "," + newLine +
            " - hartie interior: " + hCI.options[hCI.selectedIndex].text + "," + newLine +
            " - plastifiere interior: " + PI.options[PI.selectedIndex].text + "," + newLine + newLine +
            "MOD DE INCHIDERE: " + newLine +
            " - mod de inchidere: " + M.options[M.selectedIndex].text + "," + newLine + newLine +
            "CANTITATEA: " + newLine +
            " - tiraj: " + Q.options[Q.selectedIndex].text + ", " + newLine + newLine + newLine +
            "DATE CONTACT: " + newLine +
            " - nume: " + Name.value + "," + newLine +
            " - firma: " + Firma.value + "," + newLine +
            " - email: " + Email.value + "," + newLine +
            " - telefon: " + Phone.value + "," + newLine + newLine + newLine +
            "PRET CALCULAT: " + newLine +
            "   " + unitPrice.toString().replace(".", ",")+ " RON (1 bucata)" + newLine + 
            "   " + (Math.round(parseFloat(unitPrice)*parseInt(Q.value))*100/100).toString().replace(".", ",") + " RON (" + Q.value + " bucati)" + newLine;
}

function sendEmail(unitPrice)
{
    var msg = "";
    var newLine = "<br />";
    msg +=  "S-a cerut calcularea pretului pentru urmatoarea configuratie de mapa:" + newLine + newLine +
            getAttributesString(unitPrice);

    Biblioraft.Web.ConfigurareMapa.SendEmail(msg);            
}

function sendContactEmail(unitPrice)
{
    var msg = "";
    var newLine = "<br />";
    msg += "Ati primit o cerere de contact, cu decoratiuni si caracteristici suplimentare, pentru urmatoarea configuratie de mapa:" + newLine + newLine +
            getAttributesString(unitPrice)+ newLine + newLine + 
            "DECORATIUNI SAU CARACTERISTICI SUPLIMENTARE:" + newLine +
            "   " + $("txOptions").value + newLine + newLine;
    if ($("ckContactMe").checked)
    {
        msg += " [X] Persoana doreste sa fie contactata pentru informatii suplimentare.";
    }
    
    Biblioraft.Web.ConfigurareMapa.SendContactEmail(msg, sendContactEmail_callback);
}

function sendContactEmail_callback(res)
{
    document.location.href = "/confirmare.aspx";
}

function getServerPrice(isContact)
{    
    if (!isContact)
    {
        Biblioraft.Web.ConfigurareMapa.GetServerPrice(
                        F.value,
                        parseFloat(G.value), parseInt(hCE.value), PE.value, parseInt(hCI.value), PI.value,
                        M.value,
                        parseInt(Q.value), getServerPrice_callback);
    }
    else
    {
        Biblioraft.Web.ConfigurareMapa.GetServerPrice(
                        F.value,
                        parseFloat(G.value), parseInt(hCE.value), PE.value, parseInt(hCI.value), PI.value,
                        M.value,
                        parseInt(Q.value), getContactPrice_callback);
    }
}

function getServerPrice_callback(res)
{
    if (res.value == null)
    {
        setTDError();
    }
    else
    {
        sendEmail(res.value);
        setTDPrice(res.value, parseInt(Q.value));                
    }
}

function getContactPrice_callback(res)
{
    if (res.value == null)
    {
        sendContactEmail("0");
    }
    else
    {
        sendContactEmail(res.value);
    }
        
}

function setTDPrice(unitPrice, q)
{
    $("tdPrice").innerHTML = 
        "<div id=\"dvPrice\" style=\"border:1px #9DB8FF solid;padding:15px 15px 0px 15px;width:200px;\">"+
        "    <div style=\"font:bold 12px Verdana,Arial,sans-serif;color:#333333;border-bottom:1px #333333 solid;line-height:160%;\">Pretul calculat este:</div>"+
        "    <div class=\"pt\" style=\"font-size:12px;line-height:160%;padding-top:2px;\">"+
	    "        <strong>"+unitPrice.toString().replace(".", ",")+" RON*</strong> (1 bucata)<br />"+
	    "        <strong>"+(Math.round(parseFloat(unitPrice)*q)*100/100).toString().replace(".", ",")+" RON*</strong> ("+q+" bucati)"+
        "    </div>"+
        "    <div style=\"height:25px;vertical-align:bottom;font:bold 10px Verdana,Arial,sans-serif;color:#666666;padding-top:10px;\">* preturile nu includ TVA</div>"+        
        "</div>";
    
    progress(false);
}

function setTDError()
{
    $("tdPrice").innerHTML = 
        "<div id=\"dvPrice\" style=\"border:1px #9DB8FF solid;padding:15px;width:220px;\">"+
        "    <div style=\"font:bold 12px Verdana,Arial,sans-serif;color:#333333;border-bottom:1px #333333 solid;line-height:160%;\">Ne pare rau</div>"+
        "    <div class=\"pt\" style=\"font-size:12px;line-height:160%;padding-top:2px;\">"+
	    "        Pretul pentru configuratia aleasa nu a putut fi calculat.<br />Va rugam reincercati."+
        "    </div>"+
        "</div>";
    
    progress(false);
}

function resetTDPrice()
{
    $("tdPrice").innerHTML = "&nbsp;";
}

function progress(show)
{
    if (show)
    {
        $("imgProgress").style.display="inline";
    }
    else
    {
        $("imgProgress").style.display="none";
    }
}

function sendOptions()
{
    $("imgProgressOptions").style.display = "inline";
    getValues();
    
    if (validateValues(true))
    {
        getServerPrice(true);
    }
    else
    {
        $("imgProgressOptions").style.display = "none";
    }
}