// JavaScript Document

/*
update price for upload art work
@by iCreativeLabs
@copyright 2008
*/
function number_format(number, decimals, dec_point, thousands_sep) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // +     bugfix by: Rival
    // +      input by: Kheang Hok Chin (http://www.distantia.ca/)
    // +   improved by: davook
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Jay Klehr
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Amir Habibi (http://www.residence-mixte.com/)
    // +     bugfix by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Theriault
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'
    // *     example 2: number_format(1234.56, 2, ',', ' ');
    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');
    // *     returns 4: '67,00'
    // *     example 5: number_format(1000);
    // *     returns 5: '1,000'
    // *     example 6: number_format(67.311, 2);
    // *     returns 6: '67.31'
    // *     example 7: number_format(1000.55, 1);
    // *     returns 7: '1,000.6'
    // *     example 8: number_format(67000, 5, ',', '.');
    // *     returns 8: '67.000,00000'
    // *     example 9: number_format(0.9, 0);
    // *     returns 9: '1'
    // *    example 10: number_format('1.20', 2);
    // *    returns 10: '1.20'
    // *    example 11: number_format('1.20', 4);
    // *    returns 11: '1.2000'
    // *    example 12: number_format('1.2000', 3);
    // *    returns 12: '1.200'
    var n = !isFinite(+number) ? 0 : +number, 
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}

function BuyingTotalUpload(){ 
	// this function  is deprecated user updatePrice Instead
	$.post("userprofile.php",{
		 action:"updateBuyingTotalUpload",
		 quantity:$("select[@name='quantity']").val(),
		 side:$("input[@name='side']:checked").val(),
		 names:$("select[@name='names']").val(),
		 paper:$("select[@name=paper]").val(),
		 lamination: $('#lamination').val(),
		 vouchercode: $("input[@name=v-code-id]").val()
	 }, function(xml) {
	  		 statusData = $("status",xml).text();
			 content = $("content",xml).text();
             price_total = $("price_total",xml).text();
			 if (statusData == "ok"){
				if ($("#originalPrice")[0] != null) {
					$("#originalPrice").val(content);
				}
				//$("#updatePrice").html("R" + number_format(parseFloat(content) + parseFloat($("#currentDeliveryCost").val()), 2, '.', ''));
                $("#updatePrice").html("R"+content);
			 }
	});
} 

function updatePrice(){
	var sideValue = '';
    var namesValue = '';
	//if ( $("input[@name='side']").is(':checkbox')){
    if ( $("input[@name='side']").is('.cool')){
		sideValue = $("input[@name='side']:checked").val();	
	}else{
		sideValue = $("#side").val();	
	}
    if ( $("select[@name='names']").is('.tbox')){
        namesValue  = $("select[@name='names']").val();    
    }else{
        namesValue  = $("input[@name='names']").val();   
    }
    
	$.post("ajaxfunction.php",{
		 action:"updatePrice",
         co_title : $("#jobTitle").val(),
		 quantity:$("select[@name='quantity']").val(),
		 side:sideValue,
		 names:namesValue,
		 paper:$("select[@name=paper]").val(),
		 vouchercode: $("input[@name=v-code-id]").val(),
		 lamination: $('#lamination').val()
	 }, function(xml) {
	  		 statusData = $("status",xml).text();
			 content = $("price_total",xml).text();
             cardOrderTitle = $("title",xml).text();
             
			 if (statusData == "ok"){
				$("#updatePrice").html("R"+content);
			 }
             if (cardOrderTitle == "OK"){
                 $("#errorJobTitle").fadeOut('slow');
             }else{
                 $("#errorJobTitle").fadeIn('slow');
             }
	});
} 

function BuyingTotalUploadCustomDesign(){
	$.post("userprofile.php",{
		 action:"updateBuyingTotalProfesional",
		 quantity:$("select[@name='quantity']").val(),
		 side:$("input[@name='side']:checked").val(),
		 names:$("select[@name='names']").val(),
		 paper:$("select[@name=paper]").val(),
		 vouchercode: $("input[@name=v-code-id]").val()
	 }, function(xml) {
	  		 statusData = $("status",xml).text();
			 content = $("content",xml).text();
			 if (statusData == "ok"){
				$("#updatePrice").html("R"+content);
			 }
	});
} 

function BuyingTotalProfesional(){
	$.post("ajaxfunction.php",{
		 action:"updateBuyingTotalProfesional",
		 quantity:$("select[@name='quantity']").val(),
		 side:$("input[@name='side']:checked").val(),
		 names:$("select[@name='names']").val(),
		 paper:$("select[@name=paper]").val(),
		 vouchercode: $("input[@name=v-code-id]").val()
	 }, function(xml) {
	  		 statusData = $("status",xml).text();
			 content = $("content",xml).text();
			 if (statusData == "ok"){
				$("#updatePrice").html("R"+content);
			 }
	});
} 

function BuyingTotalFlash(){ 
	$.post("flash_invoice.php",{
		 action:"updateBuyingTotalUpload",
		 quantity:$("select[@name='quantity']").val(),
		 side:$("input[@name='side']").val(),
		 names:$("input[@name='names']").val(),
		 paper:$("select[@name=paper]").val(),
		 vouchercode: $("input[@name=v-code-id]").val()
	 }, function(xml) {
	  		 statusData = $("status",xml).text();
			 content = $("content",xml).text();
			 if (statusData == "ok"){
				$("#updatePrice").html("R"+content);
			 }
	});
} 

/*
select delivery
highlight tr
and send to ajax
*/ 
function selected(obj) {
    $(".deselected").removeAttr("id");
    $(obj).attr("id","selected");
}
function unselected(obj) {
    $(".deselected").removeAttr("id");
    $(obj).removeAttr("id","selected");
}
function updateDelivery(id, price){
	updateDeliveryResult("updateDelivery", id, price);
	for (i=1; i<=5; i++){
		if (i == id){
			$("#select-"+i).slideDown();
		} else {
			$("#select-"+i).slideUp();
		}
	}
	$("#select-default").hide();
}
function updateDeliveryResult(action, id, price){
	$.post("ajaxfunction.php",{
			 action:action,
			 price:price,
			 deliveryId:id
	 }, function(xml) {
		  	setContentDelivery(xml);
	});
	
}
function setContentDelivery(xml) {		
	 statusData = $("status",xml).text();
	 name = $("name",xml).text();
	 realCost = $("realCost",xml).text();
	 cost = $("cost",xml).text();
	 formName = $("formName",xml).text();
	 formSubmit = $("formSubmit",xml).text();
	 id = $("deliveryId",xml).text();
	 if (statusData == "ok"){
		$("#currentDeliveryCost").val(parseFloat(realCost));
		$("#delivery-name").html(name+" option selected.");
		$("#updatePrice").html("R" + number_format(parseFloat(realCost) + parseFloat($("#originalPrice").val()), 2, '.', ''));
		$(".updateDeliveryId").html("<input type=\"hidden\" name=\"deliveryId\" value=\""+id+"\" />");
		//<a href="#" class="button-black" onclick="document.deliverydetailform.submit()">SUBMIT</a>
		//$("#submitButton").html("<input type=\"image\" name=\"btnSubmit\" id=\"btnSubmit\" src=\"templates/default/images/Submit.gif\" onClick=\"javascript:validate_form_"+formSubmit+"(document."+formName+"); return false;\">");
		$("#submitButton").html("<a href=\"#\" class=\"button-black\" onclick=\"javascript:validate_form_"+formSubmit+"(document."+formName+")\">SUBMIT</a>");
		$("#submitButton2").html("<a href=\"#\" onclick=\"javascript:validate_form_"+formSubmit+"(document."+formName+")></a>");
	 }
}
//update town
function updateTown(id){
	updateTownResult("updateDelivery", "province", id);
}
function updateTownResult(action, mode, id){
	$.post("userprofile.php",{
			 action:action,
			 mode:mode,
			 id: id
	 }, function(xml) {
		  	setContentTown(xml);
	});
	
}
function setContentTown(xml) {
	 statusData = $("status",xml).text();
	 option = $("option",xml).text();
	 if (statusData == "ok"){
		$("#updateTown").html(option);
	 }
}
// update branch
function updateBracnh(id){
	updateBrachResult("updateDelivery", "town", id);
}
function updateBrachResult(action, mode, id){
	$.post("userprofile.php",{
			 action:action,
			 mode:mode,
			 id: id
	 }, function(xml) {
		  	setContentBrach(xml);
	});
	
}
function setContentBrach(xml) {		
	 statusData = $("status",xml).text();
	 option = $("option",xml).text();
	 if (statusData == "ok"){
		$("#updateBranch").html(option);
	 }
}
// update address branch
function updateStore(id){
	updateStoreResult("updateDelivery", "store", id);
}
function updateStoreResult(action, mode, id){
	$.post("userprofile.php",{
			 action:action,
			 mode:mode,
			 id: id
	 }, function(xml) {
		  	setContentStore(xml);
	});
	
}
function setContentStore(xml) {		
	 statusData = $("status",xml).text();
	 address = $("address",xml).text();
	 if (statusData == "ok"){
		$("#updateStore").html(address);
	 }
}


/*
@validation for all fileds form, common function
@by iCreativeLabs
@copyright 2008
*/
function validate_email(field,alerttxt){
	with (field){
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2){
			alert(alerttxt);return false;
		}else{
			return true;
		}
	}
}
function validate_required(field,alerttxt){
	with (field){
		if (value==null||value==""){
			alert(alerttxt);return false;
		}else {
			return true
		}
	}
}
function textCounter(field, countfield, maxlimit) {
   if (field.value.length > maxlimit) {
     field.value = field.value.substring(0, maxlimit);
   } else {
      countfield.value = maxlimit - field.value.length;
   }
}
var toolTipHover;
toolTipHover = false;
function onOut(id){
	setTimeout("onOut2('"+id+"')",200);
}

function onOut2(id){ 
	if (!toolTipHover){
		$('#'+id).hide();
	}
	//document.getElementById(id).style.display="none";
}
function onHover(id){
	toolTipHover = false;
	$('.tooltip').hide();
	$('#'+id).show();
	$('#'+id).hover(function(){
		toolTipHover = true;
		$(this).show();								
	},
	function(){
		//toolTipHover = false;
		$(this).hide();								
	});
	//document.getElementById(id).style.display="block";
}

function payment(obj){
	if (obj == "bank"){
		$("#method").show();
	} else {
		$("#method").hide();
	}	
}
function paymentOption(id,id1){
	if(document.getElementById(id).checked){
		document.getElementById(id1).style.display="";
	}
	else{
		document.getElementById(id1).style.display="none";
	}
}

function paymentOption1(id,id1){
	if(document.getElementById(id).checked){
		document.getElementById(id1).style.display="none";
	}
}

function showSlideDown(object){
	$("#"+object).slideDown();
}
function showSlideUp(object){
	$("#"+object).slideUp();
}
function confirmPopup(link, msg) {
	var is_con = confirm(link, msg);
	return is_con;
}
function subscribePost(){
	$('#successMessage').hide('slow', function(){
	$('#errorMessage').hide('slow', function(){
	$('#formsubscribe').hide('slow', function(){
		$('#loadingIcon').show('slow', function(){
			$.post("subscribe.php",{
				 action:"submited",
				 //subscriber_name:$('#subscriber_name').val(),
                 //subscriber_email:$('#subscriber_email').val(),
                 subscriber_name:$('#the_name').val(),
                 subscriber_email:$('#the_email').val(),
			 }, function(xml) {
					 errorNote = $("status",xml).text();
					 errorMessage = $("errorMessage",xml).text();
					 $('#boxmessage').show();
					 if (errorNote != "ok"){
					 	$('#errorContent').html(errorMessage);
					 	$('#loadingIcon').hide('slow', function(){
							$('#errorMessage').show('slow');
							});
						$('#tryagain').show();
					 }else{
					 	$('#loadingIcon').hide('slow', function(){
							$('#successMessage').show('slow');
							});
					 }
			});
		});
	});
	});
	});
}

/*
function showAddressBilling(){
	if ($('#tBillingAdress').is(":checked")){
		$('#addressBilling').slideDown();
	} else {
		$('#addressBilling').slideUp();
	}
}
*/
