// JavaScript Document

/* this file is used only on product customization page */
var productName = '';
var colorName = '';
var frameName = '';

function updateShoppingBag(v){
	var ht = v;
	if(parseFloat(v) > 0){
		 ht = "<a href='"+WebsitePath+"checkout'>"+v+"</a>"
	}
	$("#cartItems").html(ht);	
}

function selectSize(id,price,fprice,w,h){
	$("#selectedSizeID").val(id);
	$(".dimensionHor").html(w);
	$(".dimensionVer").html(h);
	$("#itemPrice").html(fprice);
	$("#selectedProductPrice").show();
	$("#productBasePrice").val(price);
	$("#step_four").val('0');
	$("#step_three").val('0');
	$("#step_two").val('1');
	$("#productAddupPrice").val('0');
	$("#selectedFrameID").val('');
	$("#selectedProductPrice").highlightFade();
	$("#selectedProductName").html("<h4>"+productName+"</h4>");
	colorName = '';
	frameName = '';
}

function selectColor(id,title){
	colorName = title;
	$("#selectedColorID").val(id);
	var img = $("#pimage"+id).attr("alt");
	t = img.split(".");
	$("#productImage").attr("src",WebsitePathProducts+t[0]+"."+t[1]);
	if(frameName != ""){
		$("#selectedProductName").html("<h4>"+productName+" / "+colorName+" / "+frameName+"</h4>");
	}else{
		$("#selectedProductName").html("<h4>"+productName+" / "+colorName+"</h4>");
	}
	$("#step_three").val('1');
	$("#addToCartButton").show();

}

function selectFrame(id, adprice,sign,title){
	frameName = title
	$("#selectedProductName").html("<h4>"+productName+" / "+colorName+" / "+frameName+"</h4>");
	$("#selectedFrameID").val(id);
	var bprice = $("#productBasePrice").val();
	bprice = parseFloat(bprice);
	$("#productAddupPrice").val(adprice);
	var finalPrice = bprice + parseFloat(adprice);
	finalPrice = fmtMoney(finalPrice,0);
	$("#itemPrice").html(sign+finalPrice);
	$("#step_four").val('1');
	$("#selectedProductPrice").highlightFade();
}

function selectProduct(id,tb){
	$("#selectedProductImage").hide();
	var ptab = $("#ptab").val();
	if(ptab != "") $("#"+ptab+"table").html("&nbsp;");
	$("#selectedProductID").val(id);
	$.post(WebsitePath+'ajax/getProductInfo.php',{PID:id, type:"product"}, 
			function(response){
				pr = response.split('|||');
				$("#selectedProductName").html("<h4 id='bName'>"+pr[0]+"</h4>");
				productName = pr[0];
				$("#productImage").attr("src",pr[1]);
				$("#selectedProductImage").slideDown("slow");
				var pCount = parseFloat(pr[2]);
				$("#pCount").val(pCount);
				if(pCount > 1){
					$("#nextDiv").hide();
					pLabels = pr[3].split("%%%");
					phtml ="<table cellpadding='4' cellspacing='0' border='0' width='100%'><tr><Td colspan='2' style='font-size:11px;'>Please list the names of each person you want on the portrait</td></tr>";
					for(k = 0; k< pCount; k++){
						phtml += "<tr><td align='left'><label id='pLabel" + k + "'>"+ pLabels[k] +"</label></td><td><input type='text' id='pValue" + k + "' name='pValue" + k + "' size='54' class='textField1' /></td></tr>";
					}
					phtml += "<tr><td>&nbsp;</td><td align='left'><a href=\"javascript:updateTabs('tab2');\">Enter</a>&nbsp;</td></tr></table>";
					$("#"+tb+"table").html(phtml);
					$("#ptab").val(tb);
					$("#step_one").val('1');
				}else{
					$("#step_one").val('1');
					$("#nextDiv").show();
				}
			});
}
	
	lastTab = "tab1";
	function tabActive(target, option1, option2, option3){
		$("#tabContents").slideUp("slow", function () {
			$("#"+lastTab+"Contents").css("display","none");
			$("#"+target).css("z-index","100");
			$("#"+target+"Image").attr("src",WebsitePathImage+target+"-active.png");
			$("#"+target+"Contents").css("display","block");					
			$("#tabContents").slideDown();

			lastTab = target;
	     });
		
		$("#"+option1).css("z-index","90");
		$("#"+option1+"Image").attr("src",WebsitePathImage+option1+"-inactive.png");

		$("#"+option2).css("z-index","80");
		$("#"+option2+"Image").attr("src",WebsitePathImage+option2+"-inactive.png");

		$("#"+option3).css("z-index","70");
		$("#"+option3+"Image").attr("src",WebsitePathImage+option3+"-inactive.png");
		
	}
	
function updateTabs(target){

	switch(target){
		case "tab1":
			$("#prevDiv").hide();
			$("#nextDiv > a").attr("href","javascript:updateTabs('tab2')");
			$("#nextDiv").slideToggle();
			$("#step_two").val('0');
			$("#step_three").val('0');
			$("#step_four").val('0');
			$("#addToCartButton").hide();
			$("#checkoutButton").hide();
			tabActive(target, 'tab2', 'tab3', 'tab4');
			break;
		case "tab2":
			if($("#step_one").val() == '1'){
				if(pnValidate()){			// validating inputs
					$.scrollTo('#orderNowData',1200);
					$("#nextDiv > a").attr("href","javascript:updateTabs('tab3')");
					$("#prevDiv > a").attr("href","javascript:updateTabs('tab1')");
					$("#nextDiv").slideDown();
					$("#prevDiv").show();
					fetchData('size','tab2Contents');
					tabActive(target, 'tab1', 'tab3', 'tab4');	
				}
			}else{
				showMsg("<center><h1 id='type2'>Please complete step 1</h1></center>",2000);
				}
			break;
		case "tab3":
			if($("#step_two").val() == '1'){
				$("#nextDiv > a ").attr("href","javascript:updateTabs('tab4')");
				$("#prevDiv > a").attr("href","javascript:updateTabs('tab2')");
				$("#nextDiv").slideDown();
				$("#prevDiv").show();
				fetchData('color','tab3Contents');
				tabActive(target, 'tab1', 'tab2', 'tab4');
			}else{
				showMsg("<center><h1 id='type2'>Please complete step 2</h1></center>",2000);
				}
			break;
		case "tab4":
			if($("#step_three").val() == '1'){
				$("#prevDiv > a").attr("href","javascript:updateTabs('tab3')");
				$("#nextDiv").slideUp();
				$("#prevDiv").show();
				fetchData('frame','tab4Contents');
				tabActive(target, 'tab1', 'tab2', 'tab3');
			}else{
				showMsg("<center><h1 id='type2'>Please complete step 3</h1></center>",2000);
				}
			break;
	}

}
function parseConf(conf){
	return conf.split("_");
}

function fetchData(t,tb){
	
	if(tb == 'tab4Contents'){
		var id = $("#selectedSizeID").val();
	}else{
		var id = $("#selectedProductID").val();
	}
	$.post(WebsitePath+'ajax/getProductInfo.php',{PID:id, type:t},
			function(response){
				//alert(response);
				$("#"+tb).html(response);
			}
	);		
}

function addToCart(){
	showMsg("<center><img src='"+WebsitePathImage+"ajax-loader.gif'><br /><br /><h1 id='type2'>Processing request....</h1></center>",5000);
	var pCount = $("#pCount").val();
	pCount = parseFloat(pCount);
	var selectedProductID = $("#selectedProductID").val();
	var selectedSizeID = $("#selectedSizeID").val();
	var selectedColorID = $("#selectedColorID").val();
	var selectedFrameID = $("#selectedFrameID").val();
	var productBasePrice = $("#productBasePrice").val();
	var productAddupPrice = $("#productAddupPrice").val();
	var url = WebsitePath+'ajax/update-cart.php';
	
	var str = "";
	if(pCount > 1){
		for(n = 0; 	n< pCount; n++){
			str += $("#pLabel"+n).html() + "%%%" + $("#pValue"+n).val();
			str += (n < pCount-1)?"|||":"";
		}
	}
	$.post(url,{PID:selectedProductID,SID:selectedSizeID,CID:selectedColorID,FID:selectedFrameID,BP:productBasePrice,AP:productAddupPrice,PSTR:str,DO:"1",type:"add"},
		function(response){
			if(response != "-1"){
				showMsg("<center><h1 id='type2'>Item added in shopping bag</h1><center>",2000);
				updateShoppingBag(response);
			}
		});	

	$("#checkoutButton").slideDown();	
	$("#checkoutButton").highlightFade();	
}

function fmtMoney( n, c, d, t ) {
		var m = ( c = Math.abs( c ) + 1 ? c : 2, d = d || ".", t = t || ",", /(\d+)(?:(\.\d+)|)/.exec( n + "" ) ), x = (m[1].length > 3)?m[1].length % 3:0;
		m[2] = (parseFloat(m[2]) > 0)?m[2]:0.00;
		return ( x ? m[1].substr( 0, x ) + t : "" ) + m[1].substr( x ).replace( /(\d{3})(?=\d)/g, "$1" + t ) + ( c ? d + ( +m[2] ).toFixed( c ).substr( 2 ) : "" );
	 }
	 
function pnValidate(){
	var k = $("#pCount").val();
	k = parseInt(k);
	k = (k > 0)?k:1;
	var errs = 0;
	if(k > 1){
		for(i=0; i<k; i++){
			if($("#pValue"+i).val().length < 1){
				errs += 1;
			}
		}
		if(errs > 0){
			var tb = $("#ptab").val(); 
			showMsg("<center><h1 id='type2'>Please enter persons name</h1><center>",2000);
			$("#"+tb+"table").highlightFade();
		}
	}
	
	return (errs == 0);
}