//main document ready function for ap application 
/*==============================================================*/
       
        function apMain()
        {
        	     	//dimention suggestion box
		$('#dimWidth,#dimHeight,#qty').change(function() {
			//dimension_suggestion();
			validate_form();
		});
		
		//select border type
		$("input[name='bordermuseumwrap']").click(function() {
			$("input[name='bordergallerywrap']").removeAttr("checked");
			$("input[name='border2inch']").removeAttr("checked");
			$("#border").val('Museum Wrap');
			if($(this).val()==1.5)
				$("#borderSub").val("3/4");
			else if($(this).val()==3)
				$("#borderSub").val("11/2");
			
			//dimension_suggestion();
			validate_form();
		});
		
		$("input[name='bordergallerywrap']").click(function() {
			$("input[name='bordermuseumwrap']").removeAttr("checked");
			$("input[name='border2inch']").removeAttr("checked");
			$("#border").val('Gallery Wrap');
			if($(this).val()==1.5)
				$("#borderSub").val("3/4");
			else if($(this).val()==3)
				$("#borderSub").val("11/2");
			//dimension_suggestion();
			validate_form();
		});
		
		$("input[name='border2inch']").click(function() {
			$("input[name='bordermuseumwrap']").removeAttr("checked");
			$("input[name='bordergallerywrap']").removeAttr("checked");
			$("#border").val('Simple2Inch');
			
			$("#borderSub").val("");
			//dimension_suggestion();
			validate_form();
		});
		
		//zip code check
		$("#ZipCode").keyup(function() {
			
			if(!isNaN(parseInt($(this).val())) && $(this).val().length==5)
				{
					//alert('validate');
					validate_form();
					
				}
				else
				{
					//alert('zero_shipping');
					zero_shipping();	
				}
				
		});
		
		//check form submit
		$("#ap_frm").submit(function() {
			if(!check_frm())
				return false;
		});
		
		close_modal_win();
    }
/*==============================================================*/
//print type check for dimenstion suggestion popup
        function dimension_suggestion()
        {
        	if($("input[name='printOption']:checked").val()=='RolledPrints')
				change_dimension('RolledPrints');
			else if($("input[name='printOption']:checked").val()=='StretcherBarKit')
			{
				change_dimension('StretcherBarKit');
			}
			else if($("input[name='printOption']:checked").val()=='Stretched')
			{
				change_dimension('Stretched');
			}	
			
        }

//popup window for dimenstion suggestion
        function dimension_suggestion_window()
        {
        	//alert($("input[name='printOption']:checked").val());
        	//return false;
        	if($("input[name='printOption']:checked").val()=='Rolled Prints')
				suggestion_dimension('RolledPrints');
			else if($("input[name='printOption']:checked").val()=='Stretcher Bar Kit')
			{
				suggestion_dimension('StretcherBarKit');
			}
			else if($("input[name='printOption']:checked").val()=='Stretched')
			{
				suggestion_dimension('Stretched');
			}	
			
        }
        //rounds the input number to the desired precision
        //and returns the rounded number
        function roundToPrecision(inputNum, desiredPrecision){
        	var precisionGuide = Math.pow(10, desiredPrecision);
        	return( Math.round(inputNum * precisionGuide) / precisionGuide );
        }

        //converts the input number into a string and adds zeroes
        //until the desired precision is reached and then
        //returns the new string
        function addZeroesToPrecision(inputNum, desiredPrecision){
        	var numString = inputNum + "";
        	var decimalPosition=numString.search(/\./);
        	var afterDecimalString = numString.substring(decimalPosition + 1);
        	if(decimalPosition <=0 && inputNum==0)
        	{
        		return('$0.00');	
        	}
        	else if(decimalPosition<=0 && inputNum>0)
        	{
        		return('$'+inputNum+'.00');	
        	}
        	else
        	{
        		numString='$'+numString;
        	while (afterDecimalString.length < desiredPrecision) {
        		afterDecimalString += "0";
        		numString += "0";
        	}
        	return(numString);
        	}
        	
        }
    
   	//Check form submit action
			function validate_form(){
				
				var width=$('#dimWidth').val();
				var height=$('#dimHeight').val();
				var quantity=$('#qty').val();
				var zip_code=$('#ZipCode').val();
				var var_name="";
				var bordermuseum = $("input[name='bordermuseumwrap']:checked").val();
				var bordergallery = $("input[name='bordergallerywrap']:checked").val();
				var border=0;
				var selected_shipping_cost=0;
				var error="";
				
				var_name = $("input[name='printOption']:checked").val();

				//hide 2" border option
				  if($("input[name='printOption']:checked").val()=='Stretched' || $("input[name='printOption']:checked").val()=='Stretcher Bar Kit')
				  {
				  	$("#border2inch_span").hide();
				  }
				  else
				  	$("#border2inch_span").show();	  	



				/*
				if(zip_code=="")
					zip_code=0;
				*/
				if(!isNaN(zip_code) && zip_code.length==5)
				{
					zip_code=zip_code;
				}
				else
					zip_code=0;
				
					//alert(zip_code);
					//alert(var_name);
				if(bordermuseum)
					border=bordermuseum;
				else if(bordergallery)
					border=bordergallery;
				
				//check for convas size error
				error=convas_size_error();
				if(error.length>0)
				{
					zero_fill();
					popupwin(error);
					return false;
				}
				else
				{
					if(var_name=='Rolled Prints')
					{
						if(zip_code.length==5)
						{
							shipping_cost(width,height,quantity,border,zip_code,'RolledPrints');
							selected_shipping_cost=shipping_price();
						}
						else
						{
							zero_shipping();	
						}
						calculate_price(width,height,quantity,border,zip_code,selected_shipping_cost,'RolledPrints');
					}
					else if(var_name=='Stretcher Bar Kit')
					{
						
						if(zip_code.length==5)
						{
							shipping_cost(width,height,quantity,border,zip_code,'StretcherBarKit');
							selected_shipping_cost=shipping_price();
						}
						else
						{
							zero_shipping();	
						}
						calculate_price(width,height,quantity,border,zip_code,selected_shipping_cost,'StretcherBarKit');
						
					}
					else if(var_name=='Stretched')
					{
						if(border!=0)
						{
							if(zip_code.length==5)
							{
								shipping_cost(width,height,quantity,border,zip_code,'Stretched');
								selected_shipping_cost=shipping_price();
							}
							else
							{
								zero_shipping();	
							}
							calculate_price(width,height,quantity,border,zip_code,selected_shipping_cost,'Stretched');
						}
						else
						{
							zero_fill();
							return false;
						}
						
					}
					else
					{
						//if no print option is selected then zero fill all price and shipping fields
						zero_fill();
						return false;	
					}
					/*//check for convas size error
						error=convas_size_error();
						if(error.length>0)
						{
							zero_fill();
							popupwin(error);
							return false;
						}
					*/
				}
			}

			
	
			
	function zero_fill()
	{
		$('#price_per_print').val('$0.00');
		$('#quantity_total').val('$0.00');
		$('#shipping_cost').val('$0.00');
		$('#totalPrice').val('$0.00');
		$('#shipping_ground_cost').val(0);
		$('#shipping_ground_span').text('$0.00');
		$('#shipping_next_day_cost').val(0);
		$('#shipping_next_day_span').text('$0.00');
		$('#shipping_2nd_day_cost').val(0);
		$('#shipping_2nd_day_span').text('$0.00');
		$('#shipping_express_cost').val(0);
		$('#shipping_express_span').text('$0.00');
	}
	function zero_shipping()
	{
		
		$('#shipping_ground_cost').val(0);
		$('#shipping_ground_span').text('$0.00');
		$('#shipping_next_day_cost').val(0);
		$('#shipping_next_day_span').text('$0.00');
		$('#shipping_2nd_day_cost').val(0);
		$('#shipping_2nd_day_span').text('$0.00');
		$('#shipping_express_cost').val(0);
		$('#shipping_express_span').text('$0.00');
	}	
	//function for popup window
	function popupwin(text)
	{
		$('#dimensionboxcontents').html(text);
		$.fn.colorbox({height:"300",width:"500",scrolling:true,inline:true, href:"#dimensionbox",open:true});
	}
	
	//function for popup suggestion
	function show_modal_pupup(text,error)
	 {
	 	if(error=='error')
	 	{
	 		$('.popUpHeading').html("");
	 	}
	 	else
	 	{
	 		$('.popUpHeading').html("Scaled Dimension:");
	 	}
 	 	$('#modal_content').html(text);
 	 var id = '#modal_dialog';	
 	//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(document).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#modal_mask').css({'width':maskWidth,'height':maskHeight,'opacity':0.50});
		
		//transition effect		
		//$('#modal_mask').fadeIn(1000);	
		//$('#modal_mask').fadeTo("slow",0.8);	
		$('#modal_mask').fadeIn('fast');	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
		/*if(parseInt(winH)>200)
        	winH=winH-200;
        */
          
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		//$(id).fadeIn(2000); 
		$(id).fadeIn('fast'); 
 	 	
 	 }
	
	//function to close popup modal window
	function close_modal_win()
	{
		//popup window
		//if close button is clicked
	$('.modal_window .modal_close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		$('#modal_mask').hide();
		$('.modal_window').hide();
	});		
	
	//if mask is clicked
	$('#modal_mask').click(function () {
		$(this).hide();
		$('.modal_window').hide();
	});			
	//popup window end
	
	}

	//shipping_type selected cost
	function shipping_price()
	{
		
		var shipping_type=$("input[name='shippingOption']:checked").val();
		//shipping cost
				if(shipping_type=='ground')
				{
					return $('#shipping_ground_cost').val();
				}
				else if(shipping_type=='nextday')
				{
					return $('#shipping_next_day_cost').val();
				}
				else if(shipping_type=='day2nd')
				{
					return $('#shipping_2nd_day_cost').val();
				}
				else if(shipping_type=='day3rd')
				{
					return $('#shipping_express_cost').val();
				}

	}
	//shipping cost calculate
	function shipping_cost(width,height,quantity,border_option,zip_code,print_opt)
	{
     //	alert("width="+width+"&height="+height+"&quantity="+quantity+"&border_option="+border_option+"&zip_code="+zip_code+"&print_opt="+print_opt+"&func_name=shipping_cost");
		$.ajax({
	   		url: 'ap/ap_ajax.php',
	   		cache:false,
	   		async:false,
	   		data: "width="+width+"&height="+height+"&quantity="+quantity+"&border_option="+border_option+"&zip_code="+zip_code+"&print_opt="+print_opt+"&func_name=shipping_cost",
	   		type: "POST",
	   		dataType: "html",
       		success: function(response) {
       		$shipping_cost_arr=response.split('#*#');
		    if($shipping_cost_arr.length==4)
		    {
			    $('#shipping_ground_cost').val($shipping_cost_arr[0]);
			    if($shipping_cost_arr[0]==0)
			      	$('#shipping_ground_span').text('$0.00');
			    else
			    	$('#shipping_ground_span').text(addZeroesToPrecision($shipping_cost_arr[0],2));
			    $('#shipping_next_day_cost').val($shipping_cost_arr[1]);
			    if($shipping_cost_arr[1]==0)
			      	$('#shipping_next_day_span').text('$0.00');
			    else
			    	$('#shipping_next_day_span').text(addZeroesToPrecision($shipping_cost_arr[1],2));
			    
			    $('#shipping_2nd_day_cost').val($shipping_cost_arr[2]);
			    if($shipping_cost_arr[2]==0)
			      	$('#shipping_2nd_day_span').text('$0.00');
			    else
			    	$('#shipping_2nd_day_span').text(addZeroesToPrecision($shipping_cost_arr[2],2));
			    
			    $('#shipping_express_cost').val($shipping_cost_arr[3]);
			     if($shipping_cost_arr[3]==0)
			      	$('#shipping_express_span').text('$0.00');
			    else
			    	$('#shipping_express_span').text(addZeroesToPrecision($shipping_cost_arr[3],2));
			   
			}
		    else
		    {
		    	$('#shipping_ground_cost').val(0.00);
		    	$('#shipping_ground_span').text('$0.00');
		    	$('#shipping_next_day_cost').val(0.00);
			    $('#shipping_next_day_span').text('$0.00');
			    $('#shipping_2nd_day_cost').val(0.00);
			    $('#shipping_2nd_day_span').text('$0.00');
			    $('#shipping_express_cost').val(0.00);
			    $('#shipping_express_span').text('$0.00');
		    }
		   	//shipping_ground_cost,shipping_ground_span,shipping_next_day_cost,shipping_next_day_span,shipping_2nd_day_cost,shipping_2nd_day_span,shipping_express_cost,shipping_express_span
		     
	     	},
	    	error: function(xhr) {
	        	$('#shipping_ground_cost').val(0.00);
		    	$('#shipping_ground_span').text('$0.00');
		    	$('#shipping_next_day_cost').val(0.00);
			    $('#shipping_next_day_span').text('$0.00');
			    $('#shipping_2nd_day_cost').val(0.00);
			    $('#shipping_2nd_day_span').text('$0.00');
			    $('#shipping_express_cost').val(0.00);
			    $('#shipping_express_span').text('$0.00');
	     	}
	    });
	}
     //Ajax price calculation 
     function calculate_price(width,height,quantity,border_option,zip_code,shipping_type_cost,print_opt) {
     	$.ajax({
	   		url: 'ap/ap_ajax.php',
	   		cache:false,
	   		async:true,
	   		data: "width="+width+"&height="+height+"&quantity="+quantity+"&border_option="+border_option+"&zip_code="+zip_code+"&shipping_opt_cost="+shipping_type_cost+"&print_opt="+print_opt+"&func_name=calculate_price",
	   		type: "POST",
	   		dataType: "html",
       		success: function(response) {
       		 $price_arr=response.split('#*#');
		     if($price_arr.length==4)
		     {
			     
		     	$('#price_per_print').val(addZeroesToPrecision($price_arr[0],2));
			     $('#quantity_total').val(addZeroesToPrecision($price_arr[1],2));
			     $('#shipping_cost').val(addZeroesToPrecision($price_arr[2],2));
			     $('#totalPrice').val(addZeroesToPrecision($price_arr[3],2));
		     }
		     else
		     {
		     	$('#price_per_print').val('$0.00');
		     	$('#quantity_total').val('$0.00');
		     	$('#shipping_cost').val('$0.00');
		     	$('#totalPrice').val('$0.00');
		     }
		     //total_price,shipping_cost,quantity_total,price_per_print	     
	     	},
	    	error: function(xhr) {
	    		$('#price_per_print').val('$0.00');
		     	$('#quantity_total').val('$0.00');
		     	$('#shipping_cost').val('$0.00');
		     	$('#totalPrice').val('$0.00');
	        	//alert('Error!  Status = ' + xhr.status);
	     	}
	    });
	 }
	 //dimension suggestion
	 function change_dimension(p_type) {
	 	var width=$('#dimWidth').val();
	 	var height=$('#dimHeight').val();
	 	var bordermuseum = $("input[name='bordermuseumwrap']:checked").val();
		var bordergallery = $("input[name='bordergallerywrap']:checked").val();
		var border=0;
		var msg="";
		if(bordermuseum)
			border=bordermuseum;
		else if(bordergallery)
			border=bordergallery;
	 	
     	$.ajax({
	   		url: 'ap/ap_ajax.php',
	   		cache:false,
	   		async:true,
	   		data: "width="+width+"&height="+height+"&border_option="+border+"&print_opt="+p_type+"&func_name=change_dimension",
	   		type: "POST",
	   		dataType: "html",
       		success: function(response) {
		    msg=response;
		    /*if(response.search(/Error:/i)>=0)
		    {
		     	msg=response.substring(7);
		    }*/
		    if(msg.search(/Error:/i)<0)
		    {
		     	if(msg.length>0)
		      	 popupwin(msg);		        
		    }	
		    		     
		    },
	    	error: function(xhr) {
	        	alert('Error!  Status = ' + xhr.status);
	     	}
	    });
	    
	    
	    
	    if(msg.search(/Error:/i)>=0)
		{
		     msg=msg.substring(7);
		     return msg;
		}
		else
		{
			msg="";
			return msg;
		}
	 }


//popup windqo dimension suggestion
	 function suggestion_dimension(p_type) {
	 	var width=$('#dimWidth').val();
	 	var height=$('#dimHeight').val();
	 	var bordermuseum = $("input[name='bordermuseumwrap']:checked").val();
		var bordergallery = $("input[name='bordergallerywrap']:checked").val();
		//alert(p_type);
		//return false;
		var border=0;
		var msg="";
		if(bordermuseum)
			border=bordermuseum;
		else if(bordergallery)
			border=bordergallery;
	 	
     	$.ajax({
	   		url: 'ap/ap_ajax.php',
	   		cache:false,
	   		async:true,
	   		data: "width="+width+"&height="+height+"&border_option="+border+"&print_opt="+p_type+"&func_name=suggestion_dimension",
	   		type: "POST",
	   		dataType: "html",
       		success: function(response) {
		    msg=response;
		    /*if(response.search(/Error:/i)>=0)
		    {
		     	msg=response.substring(7);
		    }*/
		    if(msg.search(/Error:/i)<0)
		    {
		     	if(msg.length>0)
		     	{
		     		// popupwin(msg);		        
		     		show_modal_pupup(msg,'');
		     	}
		    }
		    else if(msg.search(/Error:/i)>=0)
			{
		     	msg=msg.substring(7);
		     	if(msg.length>0)
		     	{
		     		show_modal_pupup(msg,'error');
		     	}
			}
		    	
		    		     
		    },
	    	error: function(xhr) {
	        	alert('Error!  Status = ' + xhr.status);
	     	}
	    });
	 }


	 //convas size error check
	 function convas_size_error() {
	 	
	  	var width=$('#dimWidth').val();
	 	var height=$('#dimHeight').val();
	 	var bordermuseum = $("input[name='bordermuseumwrap']:checked").val();
		var bordergallery = $("input[name='bordergallerywrap']:checked").val();
		var zip_code = $('#ZipCode').val();
		var border=0;
		var msg="";
		var p_type="";
		
		if(zip_code.length==5)
		{
			zip_code=zip_code;
		}
		else
			zip_code=0;
		
		if($("input[name='printOption']:checked").val()=='RolledPrints')
				p_type='RolledPrints';
		else if($("input[name='printOption']:checked").val()=='StretcherBarKit')
			{
				p_type='StretcherBarKit';
			}
		else if($("input[name='printOption']:checked").val()=='Stretched')
			{
				p_type='Stretched';
			}
		
		if(bordermuseum)
			border=bordermuseum;
		else if(bordergallery)
			border=bordergallery;
	 	$.ajax({
	   		url: 'ap/ap_ajax.php',
	   		cache:false,
	   		async:false,
	   		data: "width="+width+"&height="+height+"&border_option="+border+"&print_opt="+p_type+"&zip_code=+"+zip_code+"&func_name=change_dimension",
	   		type: "POST",
	   		dataType: "html",
       		success: function(response) {
		    msg=response;
		      
		    },
	    	error: function(xhr) {
	        	alert('Error!  Status = ' + xhr.status);
	     	}
	    });
	    
	    if(msg.search(/Error:/i)>=0)
		{
		     msg=msg.substring(7);
		     return msg;
		}
		else
		{
			msg="";
			return msg;
		}
	 } 
	 
	 
	         //check form submit
       function check_frm()
       {
       		var zip_code=$('#ZipCode').val().length;
			var print_option=$("input[name='printOption']:checked").val();
			var shipping_type=$("input[name='shippingOption']:checked").val();
			var bordermuseum = $("input[name='bordermuseumwrap']:checked").val();
			var bordergallery = $("input[name='bordergallerywrap']:checked").val();
			var border=0;
			//var add_on=$("input[name='add_on']:checked").length;
			//var add_on_agree=$("input[name='add_on_agreement']:checked").val();
			
			var add_on=$("input[name='add_on[]']:checked").length;
   			var add_on_agree=$("input[id='add_on_agreement']:checked").length; 
		//alert(add_on_agree);
		// return false;
			if(bordermuseum)
				border=bordermuseum;
			else if(bordergallery)
				border=bordergallery;
			else
				border="";

			var msg_show="";
			
			
			if(print_option=='undefined' || print_option=='' || print_option==null)
				print_option='';
			if(shipping_type=='undefined' || shipping_type=='' || shipping_type==null)
				shipping_type='';
			  if(!validate_form())
			  {
					
			  	//check for convas size error
				error=convas_size_error();
						
			  		if(print_option== "")
				  	{
				  		msg_show='<br> >> Print Option is Required.<br>';
				  	} 
					else if(print_option!='Rolled Prints' && border =="")
					{
						//msg_show='<br> >> Border Option is Required.<br>';
						msg_show='<br> >> Border Size(either 3/4 or 11/2) is Required.<br>';
					}
					//if(add_on>0 && (add_on_agree =='undefined' || add_on_agree=='' || add_on_agree==null))
					/*alert(add_on);
					alert(add_on_agree);
					return false;
					if(add_on_agree==0)*/
					if(add_on>0 && add_on_agree==0)
					{		
						msg_show=msg_show+'<br> >> Please, check the Add-Ons agreement box.<br>';
					}
					
			  		if(zip_code==0)
				  	{
				  		msg_show=msg_show+'<br> >> ZipCode is Required.<br>';
				  	}
				  	else if(isNaN($('#ZipCode').val()))
				  	{
				  		msg_show=msg_show+'<br> >> ZipCode contain only numeric value.<br>';
				  	}
				  	else if(zip_code<5)
				  	{
				  		msg_show=msg_show+'<br> >> ZipCode must contain 5 digits.<br>';
				  	}
				  	
				  	
				  	if(shipping_type =="")
				  	{
				  		msg_show=msg_show+'<br> >> Shipping Type is Required.<br>';
				  	}
				  	if(msg_show.length>0)
				  	{
				  		popupwin('<div>'+msg_show+'</div>');
				  		return false;
				  	}
				  	else if(error.length>0)
					{
					  	popupwin('<div>First Make Your Product Selection Complete. Then Proceed to Next.</div>');
					  	return false;
				  	}
				  	return true;
			  }
       }
