function isNumberKey(evt)	{
	 var charCode = (evt.which) ? evt.which : event.keyCode
	 if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	 return true;
}

$(document).ready(function(){
 $('#print').hide();

/* Printer Friendly BETA*/
$('.print_link').click(function(){
   var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
   sOption+="scrollbars=yes,width=750,height=600,left=100,top=25"; 
   $('#print').show();   
   var sWinHTML = $('.maincontent').html();
   var winprint=window.open("","",sOption); 
       winprint.document.open(); 
       winprint.document.write('<html><LINK href=/_resources/stylesheets/generic.css rel=Stylesheet><body>'); 
       winprint.document.write(sWinHTML);          
       winprint.document.write('</body></html>'); 
       winprint.document.close(); 
       $('#print').hide();  
       winprint.focus();
});


	$(".result_postal").empty().hide(); /* empty the divs which will show result*/
	$(".result_ems").empty().hide();
	var validnext = false;
	var valid = false;
	var destinationopt = $("input[name='destinationOpt']:checked").val();
	var destinations = "";
	var destinationcountry = "";	
	var carrier = $("input[name='carrier']:checked").val();
	var mailtype = $("input[name='mailtype']:checked").val();
	var acknowledgedelivery = 0;
	var intreplycoupons = 0;
	var registration = 0;
	var expressdelivery = 0;
	var insuredvalue = 0;
	var insureOpt = 0;
	var diffcarrier = "";
	var fastpost = 0;
	var Weight = 0;
	var txtweight = 0;
	
	/* Reset values of elements on refresh*/
	$("select").val("");
	$("input[type='checkbox']").each(function() {
		this.checked = false;
	});
	$("input[type='radio']").each(function() {
		this.checked = false;
	});	
	$(".txtInsurance").val() == 'Enter the value of your package';
	$('.txtWeight,.txtInsurance').focus(function(){
		if($(this).val() == $(this).attr('title'))
		$(this).val('').css('color', '#333333');
	});

	$('.txtWeight,.txtInsurance').blur(function(){
		if($.trim($(this).val()).length == 0)
		$(this).val($(this).attr('title')).css('color', '#333333');
	});
	
	if(!$("input[name='destinationOpt']:checked").val()) {
		$('.destinationOpt').show();		
		$('.postal_general').show();		
	}
	else{
		$(".step_1").slideUp("fast");
		$('.progress').show();
		if(destinationopt == 'localpostal'){
			$('.postal_general').addClass("step_domestic_local");
			$('.progress_destination').slideDown('slow').append('<b>within Fiji</b>');		
		}else{
			$('.postal_general').addClass("step_int_local");
			$('.progress_destination').slideDown('slow').append('<b>Overseas</b>');				
		}
		$('.step_2').slideDown('slow');
	}
	
	$("input[name='destinationOpt']").click(function(){
		destinationopt = $("input[@name='destinationOpt']:checked").val();	
		$(".step_1").slideUp("fast");
		$('.step_2').slideDown('slow');	
		$('.postal_general').removeClass("step_domestic_local");
		$('.postal_general').removeClass("step_int_local");
		$('.progress').show();
		if(destinationopt == 'localpostal'){
			$('.postal_general').addClass("step_domestic_local");
			$('.progress_destination').slideDown('slow').append('<b>within Fiji</b>');				
		}
		else{
			$('.postal_general').addClass("step_int_local");
			$('.progress_destination').slideDown('slow').append('<b>Overseas</b>');
		}
	});

	$("input[name='carrier']").click(function(){
		destinationopt = $("input[@name='destinationOpt']:checked").val();
		carrier = $("input[name='carrier']:checked").val();	
		if (carrier == "air") {
			if(destinationopt == 'localpostal'){
				$('.step_1').hide();			
				$('.step_2').hide();		
				$('.step_3_local_air').fadeIn();
			}else{
				$('.step_1').hide();			
				$('.step_2').hide();				
				$('.step_3_int_air').fadeIn();			
			}
			$('.progress_carrier').slideDown('slow').append('<b>Air Mail</b>');					
		}
		else if (carrier == "surface") {
			if(destinationopt == 'localpostal'){
				$('.step_1').hide();			
				$('.step_2').hide();
				$('.step_3_local_surface').fadeIn();	
			}else{
				$('.step_1').hide();			
				$('.step_2').hide();				
				$('.step_3_int_surface').fadeIn();			
			}
			$('.progress_carrier').slideDown('slow').append('<b>Surface Mail</b>');					
		}
		else {
			$('.localsurfacemail').hide();
			$('.localairmail').hide();			
			$('.intsurfacemail').hide();			
			$('.intairmail').hide();
		}
	});
	
	
	$(".destinations").change(function(){
			destinations = $('.destinations').val();
			destinationcountry = $('.destinations option:selected').text();
			if (destinations == ""){			   			      
			     $(".destinations").effect("shake", { times: 3 }, 100);
			     $(".destinations").css("border", "1px solid red");
			     valid = false;
			}
			else{
				$('.step_4').slideUp();
				$('.step_5').slideDown('slow');
				$('.progress_country').empty().slideDown('slow').append('Country sending to is <b>'+destinationcountry+ '</b>');					
				
			
			}
			/* else{
				$(".progress").hide("slide", {direction : "right"} , 1000, function(){
				    $(".progress1").effect("slide", {direction : "right"} , 1000);
  				  });
							
			} */
	});
	
	$(".intsurfaceparcelCountries").change(function(){
			destinations = $('.intsurfaceparcelCountries').val();
			destinationcountry = $('.intsurfaceparcelCountries option:selected').text();
			if (destinations == ""){			   			      
			     $(".destinations").effect("shake", { times: 3 }, 100);
			     $(".destinations").css("border", "1px solid red");
			     valid = false;
			}
			else{
				$('.step_4').slideUp();			
				$('.step_5').slideDown('slow');
				$('.progress_country').empty().slideDown('slow').append('Country sending to is <b>'+destinationcountry+ '</b>');					
			
			}			
	});
	
	$(".intairparcelCountries").change(function(){
			destinations = $('.intairparcelCountries').val();
			destinationcountry = $('.intairparcelCountries option:selected').text();
			if (destinations == ""){			   			      
			     $(".destinations").effect("shake", { times: 3 }, 100);
			     $(".destinations").css("border", "1px solid red");
			     valid = false;
			}
			else{
				$('.step_4').slideUp();
				$('.step_5').slideDown('slow').prev('div').slideUp();
				$('.progress_country').empty().slideDown('slow').append('Country sending to is <b>'+destinationcountry+ '</b>');					
			
			}			
	});
	
	$("input[name='mailtype']").click(function(){
		mailtype = $("input[name='mailtype']:checked").val();
		destinationopt = $("input[name='destinationOpt']:checked").val();
		carrier = $("input[name='carrier']:checked").val();
		
		if(mailtype == 'parcels'){
			$('.progress_mailtype').slideUp("fast").empty().slideDown('slow').append('Package is a <b>Parcel</b>');						
			if(destinationopt == 'localpostalint'){
				if(carrier == 'air'){
					$('.step_4_countries').hide();
					$('.step_4_surface_countries').hide();						
					$('.step_4_air_countries').fadeIn();
				}else{
					$('.step_4_countries').hide();
					$('.step_4_air_countries').hide();
					$('.step_4_surface_countries').fadeIn();								
				}
			}
			else{
				$('.step_5').slideDown('slow');
			}
		}else{
			if(destinationopt == 'localpostalint'){ /* only show dis if int */
			$('.step_4_countries').fadeIn();
			$('.step_4_air_countries').hide();
			$('.step_4_surface_countries').hide();
			}else{
				$('.step_3').children().slideUp('fast');
				$('.step_5').slideDown();
			}			
			if(mailtype == 'letter'){
				$('.progress_mailtype').slideUp("fast").empty().slideDown('slow').append('Package is a <b>Letter</b>');				
			}
			else if(mailtype == 'aerogrammes'){
				$('.progress_mailtype').slideUp("fast").empty().slideDown('slow').append('Package is an <b>Aerogramme</b>');						
			}
			else if(mailtype == 'postcard'){
				$('.progress_mailtype').slideUp("fast").empty().slideDown('slow').append('Package is a <b>Postcard</b>');						
			}
			else if(mailtype == 'literature'){
				$('.progress_mailtype').slideUp("fast").empty().slideDown('slow').append('Package is a <b>Literature for the blind</b>');									
			}			
			else if(mailtype == 'printedpapers'){
				$('.progress_mailtype').slideUp("fast").empty().slideDown('slow').append('Package is a <b>Printed Paper</b>');
			}			
			else if(mailtype == 'pamphlets'){
				$('.progress_mailtype').slideUp("fast").empty().slideDown('slow').append('Package is a <b>Calender,Book or Pamphlet</b>');
			}
			else if(mailtype == 'smallpackets'){
				$('.progress_mailtype').slideUp("fast").empty().slideDown('slow').append('Package is a <b>Small Packet</b>');
			}
			else if(mailtype == 'newspapers'){
				$('.progress_mailtype').slideUp("fast").empty().slideDown('slow').append('Package is a <b>Newspaper</b>');
			}			
			else{
				$('.progress_mailtype').empty().slideUp().append('Package Type?').slideDown('slow');			
			}			

		}
		$('.step_3').children().slideUp('fast');
	});	


	$("input[name='intfastpost']").click(function(){
		fastpost = $("input[name='intfastpost']:checked").val();
		mailtype = $("input[name='mailtype']:checked").val();
		
		if(fastpost){
		
			$('#fastpost').show();			
		}else{
			$('#fastpost').slideUp().hide();		
		}
	
	
	});
	$("input[name='expressdelivery']").click(function(){
		expressdelivery = $("input[name='expressdelivery']:checked").val();
		if(expressdelivery){
			$('#expressdelivery').show();			
		}else{
			$('#expressdelivery').slideUp().hide();	
		}
	
	
	});	
	$("input[name='intreplycoupons']").click(function(){
		intreplycoupons = $("input[name='intreplycoupons']:checked").val();
		if(intreplycoupons){
			$('#intreplycoupons').show();			
		}else{
			$('#intreplycoupons').slideUp().hide();		
		}
	
	
	});	
	$("input[name='registration']").click(function(){
		registration = $("input[name='registration']:checked").val();
		if(registration){
		
			if(destinationopt == 'localpostal'){
				registration = 1.1;
			}else{
				registration = 2.5;
			}
			$('.acknowledgedelivery').show();		
			$('#registration').show();			
		}else{
			$('.acknowledgedelivery').hide();				
			$('#registration').slideUp().hide();		
		}
	
	
	});
	$("input[name='acknowledgedelivery']").click(function(){
		acknowledgedelivery = $("input[name='acknowledgedelivery']:checked").val();
		if(acknowledgedelivery){
		acknowledgedelivery = 0.72;
			$('#acknowledgedelivery').show();			
		}else{
			$('#acknowledgedelivery').slideUp().hide();		
		}
	
	
	});	
	$("input[name='insureOpt']").click(function(){
		insureOpt = $("input[name=insureOpt]:checked").val();		
		if(insureOpt) {
			$('.insurancebox').show();
			$("input[name=registration]").attr('checked',true);	/* If insured then auto register*/
			$('#insurance').show();	
			$('#registration').show();	
			$('.acknowledgedelivery').show();					
			
		}else{
			$("input[name=registration]").attr('checked',false);		
			$('.insurancebox').hide();
			$('.acknowledgedelivery').hide();
			$('#registration').hide();				
			$('#insurance').slideUp().hide();			
		}			
	});
	
	$('#submitPostal').click(function(){
		validnext = true ;
		insureOpt = $("input[name=insureOpt]:checked").val();
		mailtype = $("input[name='mailtype']:checked").val();

		if(insureOpt == 'on') {	/*If insured then validate value textbox*/
			if ($("input[name='txtInsurance']").val() == 'Enter the value of your package') {
				$('.txtInsurance').css('border', '1px solid #FF0000');
				validnext = false;
			}
			/*If insurance value > $800 */
			 else if ($("input[name='txtInsurance']").val() > 800 && mailtype == 'letter'){
					$('.txtInsurance').css('border', '1px solid #FF0000');
					alert("The insured value cannot exceed $800");
					validnext = false;
				}
				
			else{	
				if($("input[name='registration']:checked").val() == 'on'){
					var insuredinitial = Math.ceil(($("input[name='txtInsurance']").val()/2));
					insuredvalue = (insuredinitial * 0.11)
					validnext = true;
					$('.txtInsurance').css('border', '1px solid #7F9DB9');	
				}else{
					alert("Please register your product in order to insure it");
					validnext = false;
				}
			}
		}
		
			if(validnext){
			$('.step_5').slideUp();
			$('.step_6').slideDown().children().show();
			}
			
	}); /* 1st submit*/	


	$('#submitAddOns').click(function(){
		if ($("input[name='txtWeight']").val()  == 'Enter weight in grams') {
			$('.txtWeight').css('border', '1px solid #FF0000');
			valid = false;
		}
		else if($("input[name='txtWeight']").val()  < 1 ){
			alert("Please enter a valid weight greater then zero");
			$('.txtWeight').css('border', '1px solid #FF0000');
			valid = false;		}
		/*If weight > 2000*/
		else if ($('.txtWeight').val() > 2000 && mailtype != 'literature' && mailtype != 'pamphlets' && mailtype != 'parcels'){
		
			$('.txtWeight').css('border', '1px solid #FF0000');
			alert("Weight cannot exceed 2Kgs");
			valid = false;
		}
		else if ($("input[name='txtWeight']").val() > 5000 && mailtype == 'pamphlets'){
			$('.txtWeight').css('border', '1px solid #FF0000');
			alert("Weight cannot exceed 5Kgs");
			valid = false;
		}
		else if ($("input[name='txtWeight']").val() > 7000 && mailtype == 'literature'){
			$('.txtWeight').css('border', '1px solid #FF0000');
			alert("Weight cannot exceed 7Kgs");
			valid = false;
		}
		 else if ($("input[name='txtWeight']").val() > 20000 && mailtype == 'parcels'){
			carrier = $("input[name='carrier']:checked").val();	
			if(carrier == 'air'){
				destinationcountry = $('.intairparcelCountries option:selected').text();	 	
			}else{
				destinationcountry = $('.intsurfaceparcelCountries option:selected').text();		 	
			}

			if(destinationcountry == 'AUSTRALIA' || destinationcountry == 'NEW ZEALAND' || destinationcountry == 'CANADA' || destinationcountry == 'GREAT BRITAIN AND NORTHERN IRELAND' || destinationcountry == 'UNITED STATES OF AMERICA'){
				if($("input[name='txtWeight']").val() > 30000){
					$('.txtWeight').css('border', '1px solid #FF0000');
					alert("Weight cannot exceed 30Kgs");
					valid = false;				
				}else{
					txtweight = $("input[name='txtWeight']").val();
					var IntialWeight = ($("input[name='txtWeight']").val()/500);
					Weight = Math.ceil(IntialWeight);
					valid = true;
					
				}
			}else{
				$('.txtWeight').css('border', '1px solid #FF0000');
				alert("Weight cannot exceed 20Kgs");
				valid = false;
			}
		}	
		else if ($("input[name='txtWeight']").val() > 30000 && mailtype == 'parcels'){
			$('.txtWeight').css('border', '1px solid #FF0000');
			alert("Weight cannot exceed 30Kgs");
			valid = false;
		}
		else {
			txtweight = $("input[name='txtWeight']").val();
			carrier = $("input[name='carrier']:checked").val();	
			destinationopt = $("input[name='destinationOpt']:checked").val();
			mailtype = $("input[name='mailtype']:checked").val();

			if (carrier != 'surface' && destinationopt == 'localpostalint') {
				if (mailtype == 'parcels'){
					var IntialWeight = ($("input[name='txtWeight']").val()/500);
					Weight = Math.ceil(IntialWeight);
					valid = true;
				}
				else{
					var IntialWeight = ($("input[name='txtWeight']").val()/15);
					Weight = Math.ceil(IntialWeight);
					valid = true;
				}
			}
			else if (destinationopt == 'localpostal'){
				if(mailtype == 'printedpapers' || mailtype == 'smallpackets'){
					if(carrier == 'surface'){
						var IntialWeight = ($("input[name='txtWeight']").val()/50);
						Weight = Math.ceil(IntialWeight);
						valid = true;
					}
					else{
						var IntialWeight = ($("input[name='txtWeight']").val()/30);
						Weight = Math.ceil(IntialWeight);
						valid = true;
					}
				}
				else if (mailtype == 'parcels') { /* if air parcel charged by 250g, if surface charged on Kgs */				
					if(carrier == 'air'){
						var IntialWeight = ($("input[name='txtWeight']").val()/250);
						 Weight = Math.ceil(IntialWeight);
						valid = true;
					}
					else{
						var IntialWeight = ($("input[name='txtWeight']").val()/1000);
						Weight = Math.ceil(IntialWeight);
						valid = true;
					}
				}
				else {
					var IntialWeight = ($("input[name='txtWeight']").val()/30);
					Weight = Math.ceil(IntialWeight);
					valid = true;
				}
			}
			else {
				Weight = $("input[name='txtWeight']").val();
				valid = true;
			}
		}
		if(valid){
		$('.progress_weight').slideUp("fast").empty().slideDown('slow').append('Weight in grams?<b> '+txtweight+ ' grams</b>');

			carrier = $("input[name='carrier']:checked").val();
			mailtype = $("input[name='mailtype']:checked").val();
			txtweight = $("input[name='txtWeight']").val();
			var fastpost = $("input[name='intfastpost']:checked").val();
			if(fastpost){
				if(mailtype == 'letter'){fastpost = 0.6;} /*If letter then fastpost rate is $.60 else $0.60*/
				else {fastpost = 0.6;}
			}else{
				fastpost = 0;
			}
			var expressdelivery = $("input[name='expressdelivery']:checked").val();
			if(expressdelivery){
				expressdelivery = 2.55;
			}else{
				expressdelivery = 0;			
			}
			var intreplycoupons = $("input[name='intreplycoupons']:checked").val();
			if(intreplycoupons){
				intreplycoupons = 2.35;
			}else{
				intreplycoupons = 0;
			}
			var deliveryacknopt = $("input[name=acknowledgedelivery]:checked").val();				
			if(deliveryacknopt) {
				acknowledgedelivery = 0.72;
			}else{
				acknowledgedelivery = 0;
			}
			var registrationopt = $("input[name=registration]:checked").val();				
			if(registrationopt) {
				if(destinationopt == 'localpostal'){
					registration = 1.1;
				}else{
					registration = 2.5;
				}

 			}else{
				registration = 0;
			}

			/* determine which country list to follow*/
			if(destinationopt == 'localpostalint' && carrier == 'air' && mailtype == 'parcels'){
				destinations = $('.intairparcelCountries').val();
				destinationcountry = $('.intairparcelCountries option:selected').text();			
			}
			else if(destinationopt == 'localpostalint' && carrier == 'surface' && mailtype == 'parcels')
			{
				destinations = $('.intsurfaceparcelCountries').val();
				destinationcountry = $('.intsurfaceparcelCountries option:selected').text();			
			}
			else{
				destinations = $('.destinations').val();
				destinationcountry = $('.destinations option:selected').text();			
			}
			$.post("/dynamicdata/dynamiccontent1.cfm", {type:destinationopt, destination:destinations, country:destinationcountry, carrier:carrier, mailtype:mailtype, weight:Weight, initialweight:txtweight, registration:registration, acknowledgedelivery:acknowledgedelivery, intreplycoupons:intreplycoupons, expressdelivery:expressdelivery, fastpost:fastpost, insuredvalue:insuredvalue }, function(data){
				data = $.trim(data.replace('<META NAME="ColdFusionMXEdition" CONTENT="ColdFusion DevNet Edition - Not for Production Use.">', ''));
				if(data.length > 0) {
					$(".result_postal").append(data);
					$(".step_6").slideUp();
					$(".result_postal").slideDown();
					$(".disclaimer").slideDown();
				}
				if(carrier == 'air'){
					if(mailtype == 'aerogrammes' && destinationopt == 'localpostalint'){
						$('#compare_results').hide();
					}else{
						$('#compare_results').empty().append('Compare results with <b>Surface Mail</b>');
					}
				}else{
					if(mailtype == 'smallpackets' && destinationopt == 'localpostalint'){
						$('#compare_results').hide();
					}else{				
						$('#compare_results').empty().append('Compare results with <b>Air Mail</b>');
					}
				}
				

			});
			$('#compare_results').click(function(){	
			
			$(".result_postal_compare").empty();/* empty the result div to avoid duplication*/
			if (destinationopt == 'localpostalint'){
				if(carrier == 'air'){
					diffcarrier = 'surface';
					
					/* choose air parcel countries since we using same options selected*/
					if(mailtype == 'parcels'){
						var IntialWeight = ($("input[name='txtWeight']").val());
						Weight = Math.ceil(IntialWeight);
						destinations = $("select[name=intairparcelCountries] option:selected").val();
						destinationcountry = $("select[name=intairparcelCountries] option:selected").text();				
					}else{
						var IntialWeight = ($("input[name='txtWeight']").val());
						Weight = Math.ceil(IntialWeight);					
						destinations = $("select[name=intCountries] option:selected").val();
						destinationcountry = $("select[name=intCountries] option:selected").text();								
					}
					
				}else{
					diffcarrier = 'air';
					
				/* choose surface parcel countries since we using same options selected*/
					if(mailtype == 'parcels'){
						var IntialWeight = ($("input[name='txtWeight']").val()/500);
						 Weight = Math.ceil(IntialWeight);
						destinations = $("select[name=intsurfaceparcelCountries] option:selected").val();
						destinationcountry = $("select[name=intsurfaceparcelCountries] option:selected").text();					
					}else{
						var IntialWeight = ($("input[name='txtWeight']").val()/15);
						Weight = Math.ceil(IntialWeight);					
						destinations = $("select[name=intCountries] option:selected").val();
						destinationcountry = $("select[name=intCountries] option:selected").text();
					}
				}
			}
			else if (destinationopt == 'localpostal'){
				if(carrier == 'air'){
					diffcarrier = 'surface';
				}else {
					diffcarrier = 'air';
				}
				if(mailtype == 'printedpapers' || mailtype == 'smallpackets'){
					if(diffcarrier == 'surface'){
						var IntialWeight = ($("input[name='txtWeight']").val()/50);
						Weight = Math.ceil(IntialWeight);
						valid = true;
					}
					else{
						var IntialWeight = ($("input[name='txtWeight']").val()/30);
						Weight = Math.ceil(IntialWeight);
						valid = true;
					}
				}
				else if (mailtype == 'parcels') { /* if air parcel charged by 250g, if surface charged on Kgs */				
					if(diffcarrier == 'air'){
						var IntialWeight = ($("input[name='txtWeight']").val()/250);
						 Weight = Math.ceil(IntialWeight);
						valid = true;
					}
					else{
						var IntialWeight = ($("input[name='txtWeight']").val()/1000);
						Weight = Math.ceil(IntialWeight);
						valid = true;
					}
				}
				else {
					var IntialWeight = ($("input[name='txtWeight']").val()/30);
					Weight = Math.ceil(IntialWeight);
					valid = true;
				}
			}
			else {
				Weight = $("input[name='txtWeight']").val();
				valid = true;
			}				

			$.post("/dynamicdata/compare_postal.cfm", {type:destinationopt, destination:destinations, country:destinationcountry, carrier:diffcarrier, mailtype:mailtype, weight:Weight, initialweight:txtweight, registration:registration, acknowledgedelivery:acknowledgedelivery, intreplycoupons:intreplycoupons, expressdelivery:expressdelivery, fastpost:fastpost, insuredvalue:insuredvalue }, function(data){
				data = $.trim(data.replace('<META NAME="ColdFusionMXEdition" CONTENT="ColdFusion DevNet Edition - Not for Production Use.">', ''));
				if(data.length > 0) {
					$(".result_postal_compare").append(data);							
					$(".result_postal_compare").slideDown();
					$('#compare_results').hide();						
				}				
			});
		});			

		}		
	});

	
});
