﻿  var L;
var H;
var LC;
var G;
var hCE;
var PE;
var hCI;
var PI;
var C;
var IO;
var IRD;
var BE;
var Q;

var Name;
var Firma;
var Email;
var Phone;

function computePrice()
{
    getValues();
    if (validateValues())
    {
        getServerPrice();
    }
    else
    {
        progress(false);
    }
}

function getValues()
{
    L = $("txL"); L.value = L.value.replace(',', '.'); H = $("txH"); H.value = H.value.replace(',','.');
    LC = $("ddLC"); 
    G = $("ddG"); hCE = $("ddhCE"); PE = $("ddPE"); hCI = $("ddhCI"); PI = $("ddPI");
    C = $("ddC"); IO = $("ddIO"); IRD = $("ddIRD"); BE = $("ddBE");
    Q = $("ddQ");
    Name = $("txName"); Firma = $("txFirma"); Email = $("txEmail"); Phone = $("txPhone");
}

function validateValues(isContact)
{
    var errors = "";
    if (trim(L.value).length == 0 || isNaN(L.value) || parseInt(L.value)< 100 || parseInt(L.value) > 500){
        errors += " - Latimea trebuie sa fie o valoare numerica, intre 100 si 500 mm;\n";
    }
    if (trim(H.value).length == 0 || isNaN(H.value) || parseInt(H.value) < 100 || parseInt(H.value) > 700){
        errors += " - Inaltimea trebuie sa fie o valoare numerica, intre 100 si 700 mm;\n";
    }
    if (LC.selectedIndex == 0){
        errors += " - Selectati o optiune la \"Latime cotor\";\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 (C.selectedIndex == 0){
        errors += " - Selectati o optiune la \"Capacitate mecanism\";\n";
    }
    if (IO.selectedIndex == 0){
        errors += " - Selectati o optiune la \"Inele ovale pentru inchidere\";\n";
    }
    if (IRD.selectedIndex == 0){
        errors += " - Selectati o optiune la \"Inel rotund pentru deget\";\n";
    }
    if (BE.selectedIndex == 0){
        errors += " - Selectati o optiune la \"Buzunare PP pentru etichete\";\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 "DIMENSIUNI: " + newLine +
            " - latime: " + L.value + " mm," + newLine +
            " - inaltime: " + H.value + " mm," + newLine +
            " - latime cotor: " + LC.options[LC.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 +
            "MECANISM SI ACCESORII: " + newLine +
            " - capacitate mecanism: " + C.options[C.selectedIndex].text + "," + newLine +
            " - inele ovale pentru inchidere: " + IO.options[IO.selectedIndex].text + "," + newLine +
            " - inel rotund pentru deget: " + IRD.options[IRD.selectedIndex].text + "," + newLine +
            " - buzunare PP pentru etichete: " + BE.options[BE.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 biblioraft:" + newLine + newLine +
            getAttributesString(unitPrice);

    Biblioraft.Web.ConfigurareBiblioraft.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 biblioraft:" + 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.ConfigurareBiblioraft.SendContactEmail(msg, sendContactEmail_callback);
}

function sendContactEmail_callback(res)
{
    document.location.href = "/confirmare.aspx";
}

function getServerPrice(isContact)
{    
    if (!isContact)
    {
        Biblioraft.Web.ConfigurareBiblioraft.GetServerPrice(
                        parseInt(L.value), parseInt(H.value), parseInt(LC.value),
                        parseFloat(G.value), parseInt(hCE.value), PE.value, parseInt(hCI.value), PI.value,
                        parseInt(C.value), IO.value, IRD.value, BE.value,
                        parseInt(Q.value), getServerPrice_callback);
    }
    else
    {
        Biblioraft.Web.ConfigurareBiblioraft.GetServerPrice(
                        parseInt(L.value), parseInt(H.value), parseInt(LC.value),
                        parseFloat(G.value), parseInt(hCE.value), PE.value, parseInt(hCI.value), PI.value,
                        parseInt(C.value), IO.value, IRD.value, BE.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";
    }
}