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();
	$(".result_ems").empty().hide();
	var validnext = false;
	var valid = false;	
	var destinationopt = $("input[name='destinationOpt']:checked").val();
	var destinations = "";
	var destinationcountry = "";	
	var importexport ="";		
	var carrier = "";
	var mailtype = "";
	var locationsfrom ="";
	var locationsto ="";
	var sendingto ="";
	var destinationems ="";
	var destinations ="";
	var destinationimportcountry = "";
	var destinationemscountry ="";
	var insuredvalue = 0;
	var insureOpt = 0;
	var EMSWeight = 0;
	var txtweight = 0;
	
	/* Reset values of elements on refresh*/
	$("input[name='txtInsurance']").val() == 'Enter the value of your package';
	$("select").val("");
	$("input[type='checkbox']").each(function() {
		this.checked = false;
	});
	$("input[type='radio']").each(function() {
		this.checked = false;
	});
	
	$('.txtEMSWeight,.txtInsurance').focus(function(){
		if($(this).val() == $(this).attr('title')){
		$(this).val('').css('color', '#333333');
		}
	});

	$('.txtEMSWeight,.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();		
		$('.ems_general').show();		
	}
	else{
		$(".step_1").slideUp("fast");
		$('.progress').show();
		if(destinationopt == 'Local'){
			$('.ems_general').addClass("step_domestic_ems");
			$('.progress_destination').slideDown('slow').append('<b>Local</b>');
			$('.step_2_local').slideDown('slow');			
		}else{
			$('.ems_general').addClass("step_int_ems");
			$('.progress_destination').slideDown('slow').append('<b>International</b>');
			$('.step_2_int').slideDown('slow');						
		}
	}	
	
	$("input[name='destinationOpt']").click(function(){
		destinationopt = $("input[@name='destinationOpt']:checked").val();	
		$(".step_1").slideUp("fast");
		$('.ems_general').removeClass("step_domestic_ems");
		$('.ems_general').removeClass("step_int_ems");
		$('.progress').show();
		if(destinationopt == 'Local'){
			$('.ems_general').addClass("step_domestic_ems");
			$('.progress_destination').slideDown('slow').append('<b>Local</b>');
			$('.step_2_local').slideDown('slow');				
		}
		else{
			$('.ems_general').addClass("step_int_ems");
			$('.progress_destination').slideDown('slow').append('<b>International</b>');
			$('.step_2_int').slideDown('slow');
		}
	});	
	
	$("input[name='importexportOpt']").click(function(){
		importexport = $("input[name='importexportOpt']:checked").val();
		if(importexport == 'import'){
			$('.step_2_int').slideUp().hide();
			$('.progress_importexport').empty().slideDown('slow').append('<b>Import</b> from overseas');
			$('.step_3_int_import').slideDown();				
		}
		else{
			$('.step_2_int').slideUp().hide();		
			$('.progress_importexport').empty().slideDown('slow').append('<b>Export</b> to overseas');
			$('.step_3_int_export').slideDown();				
		}
	});
	
	$(".destinationEMSImp").change(function(){
			destinations = $('.destinationEMSImp').val();
			destinationimportcountry = $('.destinationEMSImp option:selected').text();
			if (destinations == ""){			   			      
			     $(".destinationEMSImp").effect("shake", { times: 3 }, 100);
			     $(".destinationEMSImp").css("border", "1px solid red");
			     valid = false;
			}
			else{			
				$('.step_3_int_import').slideUp();
				$('.step_5').slideDown().children().show();
				$('.progress_country').empty().slideDown('slow').append('Import from <b>'+destinationimportcountry+ '</b>');					
			
			}			
	});
	$(".destinationEMS").change(function(){
			destinations = $('.destinationEMS').val();
			destinationcountry = $('.destinationEMS option:selected').text();
			
			if (destinations == ""){			   			      
			     $(".destinationEMS").effect("shake", { times: 3 }, 100);
			     $(".destinationEMS").css("border", "1px solid red");
			     valid = false;
			}
			else{			
				$('.step_3_int_export').slideUp();
				$('.step_4_int_export').slideDown();
				$('.progress_country').empty().slideDown('slow').append('Export to <b>'+destinationcountry+ '</b>');					
			
			}			
	});	
	
	$("input[name='carrier']").click(function(){
		destinationopt = $("input[name='destinationOpt']:checked").val();
		carrier = $("input[name='carrier']:checked").val();	
		if (carrier == "air") {
			if(destinationopt == 'Local'){
				$('.step_2_local').slideUp().hide();		
				$('.step_3_local_emsair').slideDown();
			}
			$('.progress_carrier').slideDown('slow').append('<b>Air Mail</b>');					
		}
		else if (carrier == "surface") {
			if(destinationopt == 'Local'){
				$('.step_2_local').slideUp().hide();		
				$('.step_3_local_surface').slideDown();	
			}
			$('.progress_carrier').slideDown('slow').append('<b>Surface Mail</b>');					
		}
		else if (carrier == "sea"){
				$('.step_2_local').slideUp().hide();		
				$('.step_5').slideDown().children().show();
		}
	});

	
	$(".locationsfrom").change(function(){
			locationsfrom = $('.locationsfrom').val();
			if (locationsfrom == ""){			   			      
			     $(".locationsfrom").effect("shake", { times: 3 }, 100);
			     $(".locationsfrom").css("border", "1px solid red");
			     $('.step_3_local_air_2').hide();
			     valid = false;
			}
			else{	
				$(".locationsfrom").css("border", "1px solid #7F9DB9");
				/* Remove town selected in step1 */
				$('.locationsto').empty().html($('.locationsfrom').html()).children('option:eq(0)').text('Sending To');
				$('.locationsto option[value='+$('.locationsfrom').val()+']').remove();	
				
				$('.step_3_local_air_2').slideDown('slow');
				$('.progress_locationfrom').empty().slideDown('slow').append('Sending from <b>'+locationsfrom+ '</b>');							
			}
	});
	
	$(".locationsto").change(function(){
			locationsto = $('.locationsto').val();
			
			if (locationsto == ""){			   			      
			     $(".locationsto").effect("shake", { times: 3 }, 100);
			     $(".locationsto").css("border", "1px solid red");
			     valid = false;
			}
			else{
				$('.step_3_local_emsair').slideUp();
				$('.step_5').slideDown().children().show();
				$('.progress_locationto').empty().slideDown('slow').append('Sending to <b>'+locationsto+'</b>');					
				
			
			}
	});	

	$("input[name='mailtype']").click(function(){
		mailtype = $("input[name='mailtype']:checked").val();
		if(mailtype == 'Envelopes' || mailtype == 'Cartons' ){
			$('.step_3_local_surface').slideUp();
			$('.progress_locationto').empty().slideDown('slow').append('Your package is a <b>'+mailtype+'</b>');								
			$('.step_4_local_surface_envelopes_cartons').slideDown();			
		}
		else if(mailtype == 'document' || mailtype == 'merchandise' ){
			$('.step_4_int_export').slideUp();
			$('.progress_locationto').empty().slideDown('slow').append('Your package is a <b>'+mailtype+'</b>');								
			$('.step_5').slideDown().children().show();		
		}
		else{
			$('.progress_locationto').empty().slideDown('slow').append('Your package is a <b>Fragile Good</b>');		
			$('.step_3_local_surface').slideUp();
			$('.step_5').slideDown().children().show();			
		}
		
	});
	$(".sendingto").change(function(){
		sendingto = $('.sendingto').val();
			if (sendingto == ""){			   			      
			     $(".sendingto").effect("shake", { times: 3 }, 100);
			     $(".sendingto").css("border", "1px solid red");
			     valid = false;
			}
			else{
				if(sendingto == 'viti'){
					$('.progress_route').empty().slideDown('slow').append('Send package via <b>Via Queens Road</b>');								
				}else{
					$('.progress_route').empty().slideDown('slow').append('Send package via <b>Via Kings Road</b>');				
				}
				$('.step_4_local_surface_envelopes_cartons').slideUp();
			$('.step_5').slideDown().children().show();			
			}	
	});	

	$("input[name='insureOpt']").click(function(){
		insureOpt = $("input[name=insureOpt]:checked").val();		
		if(insureOpt) {
			$('.insurancebox').show();
 			$('.progress_insurance').slideDown();			
			
		}else{
			$('.insurancebox').hide();
 			$('.progress_insurance').slideUp();			
			
		}			
	});
	
	
	$('#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){
					$('.txtInsurance').css('border', '1px solid #FF0000');
					alert("The insured value cannot exceed $800");
					validnext = false;
				}					
			else{
				var insuredinitial = Math.ceil($("input[name='txtInsurance']").val());
				insuredvalue = (insuredinitial * .025)
				validnext = true;
				$('.txtInsurance').css('border', '1px solid #7F9DB9');	
			}			
		}
		
			if(validnext){
			$('.step_5').slideUp();
			$('.step_6').slideDown().children().show();
			}
			
	}); /* 1st submit*/	




	$('#submitAddOns').click(function(){ 
		if ($('.txtEMSWeight').val() == 'Enter weight in grams') {
			   $('.txtEMSWeight').css('border', '1px solid #FF0000');
			   valid = false;
		}
		else if ($('.txtEMSWeight').val() < 1){
			   $('.txtEMSWeight').css('border', '1px solid #FF0000');
			   alert("Weight cannot be less then  1Kg");		   
			   valid = false;	
		}
		else if ($('.txtEMSWeight').val() > 25000 && importexport != 'import'){
			   $('.txtEMSWeight').css('border', '1px solid #FF0000');
			   alert("Weight cannot exceed 25Kgs");		   
			   valid = false;	
		}	
		else if (carrier == 'air' && $('.txtEMSWeight').val() > 25000){
			   $('.txtEMSWeight').css('border', '1px solid #FF0000');
			   alert("Weight cannot exceed 25Kgs");		   
			   valid = false;	
		}
		else if (carrier == 'surface' && mailtype == 'Cartons' && $('.txtEMSWeight').val() > 25000){
			   $('.txtEMSWeight').css('border', '1px solid #FF0000');
			   alert("Weight cannot exceed 25Kgs per item");		   
			   valid = false;		
		}	
		else if (carrier == 'surface' && mailtype == 'Envelopes' && $('.txtEMSWeight').val() > 1000){
			   $('.txtEMSWeight').css('border', '1px solid #FF0000');
			   alert("Weight cannot exceed 1Kg");		   
			   valid = false;		
		}
		else if (carrier == 'sea' && $('.txtEMSWeight').val() > 25000){
			   $('.txtEMSWeight').css('border', '1px solid #FF0000');
			   alert("Weight cannot exceed 25Kgs");		   
			   valid = false;		
		}

		else { 
			txtweight = $('.txtEMSWeight').val(); /* Get initial weight */
			if(carrier == 'air' || carrier == 'surface' || carrier == 'sea'){
				var IntialWeight = ($('.txtEMSWeight').val()/1000);
				EMSWeight = Math.ceil(IntialWeight);
				valid = true;
			}
			else{
				EMSWeight = $('.txtEMSWeight').val();
				valid = true;
			 }
		}
		if(valid){
		$('.progress_weight').slideUp("fast").empty().slideDown('slow').append('Weight in grams? <b>'+txtweight+ ' grams</b>');
			$.post("/dynamicdata/dynamiccontent.cfm", {type:destinationopt, destination:destinations, country:destinationcountry, importcountry:destinationimportcountry, mailtype:mailtype, carrier:carrier, weight:EMSWeight, initialweight:txtweight, locationfrom:locationsfrom, locationto:locationsto, sendingto:sendingto, selimportexport:importexport, insuredvalue:insuredvalue}, function(data){
			data = $.trim(data.replace('<META NAME="ColdFusionMXEdition" CONTENT="ColdFusion DevNet Edition - Not for Production Use.">', ''));
				if(data.length > 0) {
					$(".result_ems").append(data);
					$(".step_6").slideUp();
					$(".result_ems").slideDown().show();
					$(".disclaimer").slideDown();
					
				}
				if(carrier == 'surface'){
					$('#compare_results').empty().append('Compare results with <b>Sea Freight</b>');
				}else if(carrier == 'sea'){
					$('#compare_results').empty().append('Compare results with <b>Surface Mail</b>');				
				}else{
				$('#compare_results').empty().hide();
				}
			
			});
			
			
			
			$('#compare_results').click(function(){	
			$(".result_postal_compare").empty();/* empty the result div to avoid duplication*/
			
				if(carrier == 'surface'){
					var diffcarrier = 'sea';					

				}else if(carrier == 'sea'){
					var diffcarrier = 'surface';
					if(txtweight > 1000){
						mailtype = "Cartons";
					}else{
						mailtype = "Envelopes";
					}
				}else{
					mailtype = 0;
				}

			$.post("/dynamicdata/compare_ems.cfm", {type:destinationopt, destination:destinations, country:destinationcountry, importcountry:destinationimportcountry, mailtype:mailtype, carrier:diffcarrier, weight:EMSWeight, initialweight:txtweight, locationfrom:locationsfrom, locationto:locationsto, sendingto:sendingto, selimportexport:importexport, insuredvalue:insuredvalue}, function(data){
					data = $.trim(data.replace('<META NAME="ColdFusionMXEdition" CONTENT="ColdFusion DevNet Edition - Not for Production Use.">', ''));
					if(data.length > 0) {
						$(".result_ems_compare").append(data);							
						$(".result_ems_compare").slideDown();
						$('#compare_results').hide();
					}				
				});
				
			});			

		} /* end 1st valid*/	
	});/* 2nd  submit*/


});/* document redy*/

