function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rev") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;
$.fn.clearOnFocus = function(){

    return this.focus(function(){
        var v = $(this).val();
        $(this).val( v === this.defaultValue ? '' : v );
    }).blur(function(){
        var v = $(this).val();
        $(this).val( v.match(/^\s+$|^$/) ? this.defaultValue : v );
    });

};
function format_currency(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
     num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
      cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
      num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + num + '.' + cents + ' лв');
}
function get_stamp_total(){
        var obj;
        var val=total=0;
        var selArray=document.getElementsByTagName("select");
        for(var i=0;i<selArray.length;i++){
                obj=selArray[i];
                if(obj.name.indexOf("evalscore")>-1){
                        val=parseFloat(obj.options[obj.selectedIndex].value);
                        total+=val;
                }
        }

        var quantity = document.getElementById("t7").value;
        if (quantity > 0) { total *= quantity; }

        var dofile = parseFloat(document.getElementById("t5").value);
        if (dofile > 0) { total = (total + dofile); }

        var delivery = document.getElementById("address6");
        if (delivery.checked == true) { total = (total + parseFloat(delivery.value)); }

        document.getElementById("running_total").innerHTML=format_currency(total);
        document.getElementById("totalprice").value=format_currency(total);
}
function get_vcard_total(){
        var obj;
        var val=total=0;
        var selArray=document.getElementsByTagName("select");
        for(var i=0;i<selArray.length;i++){
                obj=selArray[i];
                if(obj.name.indexOf("evalscore")>-1){
                        val=parseFloat(obj.options[obj.selectedIndex].value);
                        total+=val;
                }
        }

        var quantity = document.getElementById("t7").value;
        if (quantity > 0) { total *= quantity; }

        var laminat = parseFloat(document.getElementById("t4").value);
        if (laminat == 1 || laminat == 3 || laminat == 5 || laminat == 7) {
            if (quantity == 2) {
                total = total + (quantity * 0.70);
            }
            if (quantity == 4) {
                total = total + (quantity * 0.65);
            }
            if (quantity > 4 && quantity <= 12) {
                total = total + (quantity * 0.58);
            }
            if (quantity > 12) {
                total = total + (quantity * 0.45);
            }
        }
        if (laminat == 2 || laminat == 4 || laminat == 6 || laminat == 8) {
            if (quantity == 2) {
                total = total + (quantity * 1.40);
            }
            if (quantity == 4) {
                total = total + (quantity * 1.30);
            }
            if (quantity > 4 && quantity <= 12) {
                total = total + (quantity * 1.16);
            }
            if (quantity > 12) {
                total = total + (quantity * 0.90);
            }
        }
        if (laminat == 9) {
            if (quantity == 2) {
                total = total + (quantity * 0.90);
            }
            if (quantity == 4) {
                total = total + (quantity * 0.85);
            }
            if (quantity > 4 && quantity <= 12) {
                total = total + (quantity * 0.80);
            }
            if (quantity > 12) {
                total = total + (quantity * 0.75);
            }
        }
        if (laminat == 10) {
            if (quantity == 2) {
                total = total + (quantity * 1.80);
            }
            if (quantity == 4) {
                total = total + (quantity * 1.70);
            }
            if (quantity > 4 && quantity <= 12) {
                total = total + (quantity * 1.60);
            }
            if (quantity > 12) {
                total = total + (quantity * 1.50);
            }
        }
        if (laminat == 11 || laminat == 13) {
            if (quantity == 2) {
                total = total + (quantity * 2.60);
            }
            if (quantity == 4) {
                total = total + (quantity * 2.40);
            }
            if (quantity > 4 && quantity <= 12) {
                total = total + (quantity * 2.20);
            }
            if (quantity > 12) {
                total = total + (quantity * 2.00);
            }
        }
        if (laminat == 12 || laminat == 14) {
            if (quantity == 2) {
                total = total + (quantity * 5.20);
            }
            if (quantity == 4) {
                total = total + (quantity * 4.80);
            }
            if (quantity > 4 && quantity <= 12) {
                total = total + (quantity * 4.40);
            }
            if (quantity > 12) {
                total = total + (quantity * 4.00);
            }
        }
        if (laminat == 15) {
            if (quantity == 2) {
                total = total + (quantity * 1.20);
            }
            if (quantity == 4) {
                total = total + (quantity * 1.10);
            }
            if (quantity > 4 && quantity <= 12) {
                total = total + (quantity * 1.00);
            }
            if (quantity > 12) {
                total = total + (quantity * 0.90);
            }
        }
        if (laminat == 16) {
            if (quantity == 2) {
                total = total + (quantity * 2.40);
            }
            if (quantity == 4) {
                total = total + (quantity * 2.20);
            }
            if (quantity > 4 && quantity <= 12) {
                total = total + (quantity * 2.00);
            }
            if (quantity > 12) {
                total = total + (quantity * 1.80);
            }
        }

        var color = parseFloat(document.getElementById("t2").value);
        if (color > 0) { total = (total + ((quantity * color) / 2)); }

        var oval = parseFloat(document.getElementById("t6").value);
        if (oval > 0) { total = (total + oval); }

        var delivery = document.getElementById("address6");
        if (delivery.checked == true) { total = (total + parseFloat(delivery.value)); }

        document.getElementById("running_total").innerHTML=format_currency(total);
        document.getElementById("totalprice").value=format_currency(total);
}
$(document).ready(function(){
    $("#loginform").click(function () {
        $("#fpw").hide();
        $("#login").toggle();
    });
    $("#lselfile").click(function () {
        $("#browse").toggle();
    });
    $("#fpwform").click(function () {
        $("#login").hide();
        $("#fpw").toggle();
    });
    $("#fpwlogin").click(function () {
        $("#fpw").hide();
        $("#login").toggle();
    });

    $("#invoicecheck").click(function(){
        if($(this).is(":checked") ) {
            $("#invoice").show();
        } else {
            $("#invoice").hide();
        }
    });

    $("#hideaddress1").click(function () {
        $("#deliveryaddress").hide();
    });
    $("#hideaddress2").click(function () {
        $("#deliveryaddress").hide();
    });
    $("#hideaddress3").click(function () {
        $("#deliveryaddress").hide();
    });
    $("#hideaddress4").click(function () {
        $("#deliveryaddress").hide();
    });
    $("#hideaddress5").click(function () {
        $("#deliveryaddress").hide();
    });
    $("#showaddress6").click(function () {
        $("#deliveryaddress").show();
    });

    $("#address1").click(function () {
        $("#deliveryaddress").hide();
    });
    $("#address2").click(function () {
        $("#deliveryaddress").hide();
    });
    $("#address3").click(function () {
        $("#deliveryaddress").hide();
    });
    $("#address4").click(function () {
        $("#deliveryaddress").hide();
    });
    $("#address5").click(function () {
        $("#deliveryaddress").hide();
    });
    $("#address6").click(function () {
        $("#deliveryaddress").show();
    });
});
function ShowHide(elementId, linkId)
{ 
	var browserName=navigator.appName;
	var element = document.getElementById(elementId);
	var viewlink = document.getElementById(linkId);
	if (browserName=="Microsoft Internet Explorer")
 	{
		if(element.style.display != "block")
		{
			element.style.display = "block";
			viewlink.innerHTML="затвори";
		}
		else
		{
			element.style.display = "none";
			viewlink.innerHTML="разгледай";
		}
	}
	else
	{
		if(element.style.display != "table-row")
		{
			element.style.display = "table-row";
			viewlink.innerHTML="затвори";
		}
		else
		{
			element.style.display = "none";
			viewlink.innerHTML="разгледай";
		}
	}
}

