// JavaScript Document
$( document ).ready( function(){
	
	
	var server 	= window.location.hostname;
	var url 	= '';
	var altTags = 'Assist-Card USA Travel Insurance';
	var todaysDate = new Date();			
	
	if(server == 'dev.zcom2.com' ){
		
		url = '/AssistCard/websites/2010-2.0/assist-cardusa_com/';
		
	}else if( server == '63.240.234.136' ){
		
		url = '/';
		
	}else{
		
		url = '/';
	}	
	
	var agencyCodes = new Array( 1 );
	
	
	agencyCodes[ 0 ] = 1000;
	agencyCodes[ 1 ] = 1001;
	agencyCodes[ 2 ] = 1003;
	agencyCodes[ 3 ] = 1004;
	agencyCodes[ 4 ] = 1006;
	agencyCodes[ 5 ] = 1008;
	agencyCodes[ 6 ] = 1010;
	agencyCodes[ 7 ] = 1011;
	agencyCodes[ 8 ] = 1012;
	agencyCodes[ 9 ] = 1013;
	
	
				

//help text

	$( 'body#quote.step-1 div#quote-container label' ).toggle( function( event ){
											 
		$( this ).children( 'span' ).fadeIn( 'slow' );
	
											 
											 
	},function(){
	
		$( this ).children( 'span' ).fadeOut( 'slow' );
	});


//advanced travelers
	
	$( '.dollarformat' ).focus( function( event ){ $( this ).val( '' ) });
	
	$( '#switch-travelers' ).click( function( event ){
			
			var $this = $( this );
			
			event.preventDefault();
			
			
			$( 'div.age-div' ).each( function( index ){
								    
		
				if( !$( this ).hasClass( 'no-show' ) ){
				
					$( this ).delay( 600 ).fadeOut( );
					
					$( this ).addClass( 'no-show' );
					
				}else if( $( this ).hasClass( 'no-show' ) ){
						
					$( this ).delay( 600 ).fadeIn( );
					
					$( this ).removeClass( 'no-show' );

					$( this ).removeAttr( 'style');
				}
			
								    
			}); 
			
			if( !$( '#advanced' ).hasClass( 'no-show' ) ){
				
				$( 'input[ name="advancedtrip" ]' ).val( true );
				

				//$( 'span.simple' ).hide();
				//$( 'span.advanced' ).show();
				
				$( '#totaltripcosttext, #totaltripcostinput' ).fadeOut( );
				
				$( 'input[ name="totaltripcost" ]' ).attr( 'disabled', true ).val( '' );

				$this.text("Click here for total trip cost");
				$this.attr( 'tooltipText' , $this.text()+'|'+altTags);

				
			}else if( $( '#advanced' ).hasClass( 'no-show' ) ){

				//$( 'span.advanced' ).hide();
				//$( 'span.simple' ).show();
				
				$( 'input[ name="advancedtrip" ]' ).val( false );
				
				$( '#totaltripcosttext, #totaltripcostinput' ).removeClass( 'no-show' ).fadeIn( );
				
				$( 'input[ name="totaltripcost" ]' ).attr( 'disabled', false );				

				$this.text("Click here for cost per traveler");
				$this.attr( 'tooltipText' , $this.text()+'|'+altTags);
			
			}
			
			return false;
									
	});


	$( 'select[ name="agent" ]' ).change( function( event ){
								    
		var $this = $( this );
		
		if( $this.val() == 1 ){
			
			$( '#agencycodeblock' ).fadeIn(  );
			
		}else if( $this.val() == 0 ){
			
			$( '#agencycodeblock' ).fadeOut(  );
			
			
		}
	
	});

	//set annual trip cost
	
	$( 'input[ name="annualDates" ]' ).click( function( event ){
									   
		$this = $( this );
		
	
		if( $this.val() == 'yes' ){
			
			$( 'input[ name="totaltripcost" ]' ).val( '0' ).attr( 'disabled', false );
			
			$( '.age-cost' ).each( function(){
								  
				$( this ).val( '0' ).attr( 'disabled', true );
			
			});
		
			
		}else if( $this.val() == 'no' ){
			
			$( 'input[ name="totaltripcost" ]' ).val( '' ).attr( 'disabled', false );
			
			$( '.age-cost' ).each( function(){
								  
				$( this ).val( '' ).attr( 'disabled', false );
			
			});
			
		}
	
	});
	
	//auto check the age-cost field to moke sure all values are under 25000
	$( 'input.age-cost' ).focusout( function( event ){
			
			var $this = $( this );
			
			if( $this.val() > 25000 ){
				
				$this.val( 25000 );
				
				$( '#advanced p.red' )
					.fadeIn( 'fast' )
						.delay( 1000 )
							.fadeOut( 'fast' );
			}							     
							     
	});

	//set all advanced cost to zero
	
	function setToZero(){
		
		
		var inputNumber = 0;
		
		$( '.age-cost' ).each( function( index ){
							  
			$this = $( this );			  
			
			var inputNumber = parseFloat( $this.attr( 'name' ) );
			
			if( $( 'input[ name="' + inputNumber + 'age-adv"]' ).val().length > 0 && $this.val().length < 1 ){
				
				$this.val( '0' );
			}

							  
		});
	
		
		return false;
	}
	//auto tab feature
	$( 'input[ name=totaltripcost ]' ).autotab({ format: 'numeric',  maxlength: 10 });		
	
		
	$( 'input[ name=age1 ]' ).autotab({ target: 'age2', format: 'numeric', maxlength: 2  });
	$( 'input[ name=age2 ]' ).autotab({ target: 'age3', format: 'numeric', previous: 'age1', maxlength: 2 });
	$( 'input[ name=age3 ]' ).autotab({ target: 'age4', format: 'numeric', previous: 'age2', maxlength: 2 });
	$( 'input[ name=age4 ]' ).autotab({ target: 'age5', format: 'numeric', previous: 'age3', maxlength: 2 });
	$( 'input[ name=age5 ]' ).autotab({ target: 'age6', format: 'numeric', previous: 'age4', maxlength: 2 });
	$( 'input[ name=age6 ]' ).autotab({ target: 'age7', format: 'numeric', previous: 'age5', maxlength: 2 });
	$( 'input[ name=age7 ]' ).autotab({ target: 'age8', format: 'numeric', previous: 'age6', maxlength: 2 });
	$( 'input[ name=age8 ]' ).autotab({ target: 'age1', format: 'numeric', previous: 'age7', maxlength: 2 });


		
	$( 'input[ name=1age-adv ]' ).autotab({ target: '1age-cost', format: 'numeric', maxlength: 2  });
	$( 'input[ name=1age-cost ]' ).autotab({ target: '2age-adv', format: 'numeric', maxlength: 10  });

	$( 'input[ name=2age-adv ]' ).autotab({ target: '2age-cost', format: 'numeric', maxlength: 2  });
	$( 'input[ name=2age-cost ]' ).autotab({ target: '3age-adv', format: 'numeric', maxlength: 10  });

	$( 'input[ name=3age-adv ]' ).autotab({ target: '3age-cost', format: 'numeric', maxlength: 2  });
	$( 'input[ name=3age-cost ]' ).autotab({ target: '4age-adv', format: 'numeric', maxlength: 10  });

	$( 'input[ name=4age-adv ]' ).autotab({ target: '4age-cost', format: 'numeric', maxlength: 2  });
	$( 'input[ name=4age-cost ]' ).autotab({ target: '5age-adv', format: 'numeric', maxlength: 10  });

	$( 'input[ name=5age-adv ]' ).autotab({ target: '5age-cost', format: 'numeric', maxlength: 2  });
	$( 'input[ name=5age-cost ]' ).autotab({ target: '6age-adv', format: 'numeric', maxlength: 10  });

	$( 'input[ name=6age-adv ]' ).autotab({ target: '6age-cost', format: 'numeric', maxlength: 2  });
	$( 'input[ name=6age-cost ]' ).autotab({ target: '7age-adv', format: 'numeric', maxlength: 10  });

	$( 'input[ name=7age-adv ]' ).autotab({ target: '7age-cost', format: 'numeric', maxlength: 2  });
	$( 'input[ name=7age-cost ]' ).autotab({ target: '8age-adv', format: 'numeric', maxlength: 10  });

	$( 'input[ name=8age-adv ]' ).autotab({ target: '8age-cost', format: 'numeric', maxlength: 2  });
	$( 'input[ name=8age-cost ]' ).autotab({ target: '1age-adv', format: 'numeric', maxlength: 10  });


	function findInputErrors( errors ){
		
		$( 'input' ).each( function( event ){
						    
		
		
						    
		});			
			
			
			
	
		return false;
	}

	
 	function process(){
 
		$( 'div#page-wrapper' ).hide( 'fast' );
	 
		$( 'body' ).css({ 'background-color' : '#1F5EAE', 'heigtht' : '1000px' });
		
		
		
		
		$( 'html, body' ).animate( { scrollTop: 0 } , 'slow' );
		
		$( 'div.loading-container' ).show();
	 
 
	}


	function setsize( listcount ){
		
		if( listcount <= 3 ){


			$( 'body#quote.step-1 ul#messageBox1' ).attr( 'style' , '');		
			$( 'body#quote.step-1 ul#messageBox1' ).css( 'height' , '10px');
		}
		
		return true;
	}
	
	//step 1 form validation

	$( 'form#step-1' ).validate({
						   
			errorClass: 'error',
			
			onkeyup: false,
	
			onblur: false,
			
			onfocusout: false,

			focusCleanup: true,
			
			focusInvalid: false,

			errorContainer: "#messageBox1, #messageBox2",

			errorLabelContainer: "#messageBox1",
			
			wrapper: 'li',
			
			invalidHandler: function( form , validator ){

				showErrors = validator.numberOfInvalids();
				
				
			},
			showErrors: function( errorMap , errorList ){
				
				$( 'body' ).find( 'span.error' ).remove();
				
				$(  'body#quote.step-1 ul#messageBox1' ).empty();
				
				$(  'body#quote.step-1 div#messageBox2' , 'body#quote.step-1 div#messageBox3' ).hide();
				
				for ( var key in errorMap ) {
			   
					 $( 'input[ name=' + key + '] , select[ name=' + key + '] ' )
					 	.parent( 'p' )
							.append( '<span class="error"></span>' );
					
						
						$( '<li><label class="error">' + errorMap[ key ] + '</label></li>' )
							.appendTo( 'body#quote.step-1 ul#messageBox1' );
						
						$( 'body#quote.step-1 ul#messageBox1' )
							.show( 800 );
							
	
				}
										
				
				
				setToZero();
				
				$(  'body#quote.step-1 div#messageBox2' ).show();

				
				if( $("body#quote.step-1 ul#messageBox1 li").size() <= 3 ){


					$( 'body#quote.step-1 ul#messageBox1' ).addClass( 'shrink' );
				}
				
				$( 'html, body' ).animate( { scrollTop: 200 } , 'slow' );
			
			},
			submitHandler: function( form ) {
			
				
				
				
				process();
			
				form.submit();
			
			},
			rules:{
				
				
				departdate:{
		
					required: true,
					date: true
					
				},
				returndate:{
		
					required: true,
					checkReturndate: true,
					date: true
					
					
				},
				destination:{
		
					required: true				
					
				},

				state:{
		
					required: true,
					stateMsg: true,
                              stateMsg1: true				
					
				},

				
				totaltripcost:{
		
					required: true,
					costamount: true
					
					
				},
				age1:{
					
					number: true,
					checkSimpleAges: true	,
					check85:true				
					
				},
				agencycode:{
					
					isFilled:true,
					checkCodes:true
					
				},
				'1age-adv':{
					
					checkAdvAges: true
					
				},
				tripLength:{
				
					checkLengthNonAnnual: true,
					checkLengthAnnual: true
					
				}
				
			},			   
			
			
			messages:{
				
				departdate:{
					
					required: 'Please enter a valid departure date.',
					date: 'Please enter a valid departure date.'
				
				},
				returndate:{
					
					required: 'Please enter a valid return date.',
					checkReturndate: 'Departure date must fall before the return date.',
					date: 'Please enter a valid return date.'
					
				},
				destination:{
					
					required: 'Please choose a destination.'
						
				},
				totaltripcost:{
					
					required: 'Please enter in your trip cost, only numbers please.',
					number: 'Please enter in your trip cost, only numbers please.'
				},
				state:{
					
					required: 'Please choose your residency state.',
					stateMsg: 'Unfortunately, we are currently unable to offer this program to residents of your state. Please check back with us again in the coming weeks.' ,
					stateMsg1: 'All policies on this website www.assist-cardusa.com are meant strictly for residents of the Unites States. If you are a resident of another country, then please go to http://www.assist-card.com to receive policy options for your country of residence.'
	
				},

				age1:{
				
				checkSimpleAges: 'Please enter the age of each traveler.',
				check85: 'Unfortunately, our current Annual Policies are only available for individuals up to the age of 85. For travelers ages 86 or older, we recommend one of our Per Trip programs. Please call us at (877) 369-2774 if you have any questions.'
					
				},
				agencycode:{
					
				isFilled: 'Please enter an agency code in this format: 5555-555.',
				checkCodes: 'Unfortunately, this agency has not been set up to use this website. Please contact ASSIST-CARD directly for further details.'
				
				},
				'1age-adv':{
					
					checkAdvAges: 'Please enter in the ages and travel cost per traveler.'
				},
				tripLength:{
					
					checkLengthNonAnnual: 'We do not have policies that will accommodate length of travel from 181 days through 364 days. You may be accommodated by an Annual Policy which covers 365 days.  Otherwise, please enter new dates of travel up to a maximum of 180 days.',
					checkLengthAnnual: 'We do not have policies that will accommodate length of travel beyond 365 days.  You will best be accommodated by an Annual Policy which covers 365 days.  Please alter your dates of travel to cover that trip length (ex: Departure Date = 11/30/09 & Return Date = 11/29/10).'
						
					
				}			
			}
			
	
	});//end the validate main function


	// functions added by saritha

	$.validator.addMethod("checkReturndate", function(value) {
        
		if(Date.parse( $( 'input[ name="departdate" ]').val() ) < Date.parse( $( 'input[ name="returndate" ]').val() )){
			return true;
		}else{
			return false;
		}

        });
		

	
	
	$.validator.addMethod( "checkCodes" , function( value ) {

		if( value.length < 1 ){
					
					return true;	
				
				}else{
					
					value = value.split( '-' );
						
					var index =	$.inArray( parseFloat( value[ 0 ] ) , agencyCodes );	
					
					if( index > -1 ){
					
						return false;
						
					}else{
						
						return true;	
					}
		
				}

	});
	

/*
	$.validator.addMethod( "checkAgencyCode" , function( value ) {

		return true;
		
		if( value.length < 1 ){
			
			return true;	
		
		}else{
			
			value = value.split( '-' );
				
			var index =	$.inArray( parseFloat( value[ 0 ] ) , agencyCodes );	
			
			if( index > -1 ){
			
				return false;
				
			}else{
				
				return true;	
			}

		}	});*/




	/*
		if the "use agency code is selected to yes make sure they fill it out

	*/
	
	$.validator.addMethod( "checkLengthNonAnnual" , function( value ) {
		
		value = value++;
		
		if( value >= 181 && value <= 364 && $( 'input[ name="annualDates" ]').val() == 'no' && value != 365 ){
			
			
			if( value >= 181 && value <= 364  && value != 365 ){
				
				return false;
				
			
			}else{
				
				return true;
				
			}

		}	
		return true;
									    
	});


	$.validator.addMethod( "checkLengthAnnual" , function( value ) {
		
        //value = value++;
		
		if( value > 364 ){
			
			return false;
		
		}else{
			
			return true;
			
		}
		
		return true;
									    
	});



	/*
		if the "use agency code is selected to yes make sure they fill it out

	*/
	
	$.validator.addMethod( "checkSimpleAges" , function( value ) {
	
			
		if( $( 'input[ name="advancedtrip" ]' ).val() == 'false' ){
			
			if( $( 'input[ name="age1" ]' ).val().length < 1 ){
			
				return false;
				
			}else{
				
				return true;
			}
			
		}else{
		
			return true;
			
		}
	
	
	});

	$.validator.addMethod( "check85" , function( value ) {
	
		if( $("input[name='annualDates']:checked").val() == 'yes' && ($( 'input[ name="age1" ]' ).val() > 85 || $( 'input[ name="age2" ]' ).val() > 85 || $( 'input[ name="age3" ]' ).val() > 85 
			|| $( 'input[ name="age4" ]' ).val() > 85 || $( 'input[ name="age5" ]' ).val() > 85 || $( 'input[ name="age6" ]' ).val() > 85 || $( 'input[ name="age7" ]' ).val() > 85 || $( 'input[ name="age8" ]' ).val() > 85)){
			
			return false;
				
		}else{
			return true;
		}

		
	});


	/*
		if the "use agency code is selected to yes make sure they fill it out

	*/
	
	$.validator.addMethod( "checkAdvAges" , function( value ) {
	

		
		if( $( 'input[ name="advancedtrip" ]' ).val() == 'true' ){
			
			if( $( 'input[ name="1age-adv" ]' ).val().length < 1 || $( 'input[ name="1age-cost" ]' ).val().length < 1  ){
			
				return false;
				
			}else{
				
				return true;
			}
			
		}else{
			
			return true;	
		}
	
	
	});


	/*
		if the "use agency code is selected to yes make sure they fill it out

	*/
	
	$.validator.addMethod( "isFilled" , function( value ) {

		if( $( 'select[ name="agent" ]' ).val() == 1 ){
			
			if( $( 'input[ name="agencycode" ]' ).val().length < 1 ){
			
				return false;
				
			}else{
				
				return true;
			}
			
		}else{
			
			return true;	
		}
	
	
	});



	$.validator.addMethod( "stateMsg" , function( value ) {

		//alert($( 'select[ name="state" ]' ).val());

		if( $( 'select[ name="state" ]' ).val() == 'California' || $( 'select[ name="state" ]' ).val() == 'New York' || $( 'select[ name="state" ]' ).val() == 'New Hampshire'){

			
							
				return false;
			}
			
		else{
			
			return true;	
		}
	
	
	});

	/*
		check the cost amount divide by the number of travelers

	*/
	
	function filterNum(str) {
		re = /^\$|,/g;
		// remove "$" and ","
		return str.replace(re, "");
	}	
	
	$.validator.addMethod( "costamount" , function( value ) {
		
		var count = 0;
		var tripValue = $( 'input[ name="totaltripcost"] ' ).val();
		
		tripValue = filterNum( tripValue );
		
		var perPersonCost = 0;
		
		
//check to see if the advanced trip is used, if so return true		
		
		if( $( 'input[ name="advancedtrip" ]' ).val() == 'true' ){
			
			return true;	
			
		}
		
// if the cost is greater than 25000 get a count of all filled out travelers
		
		
		if( tripValue > 25000 ){
			
			$( 'input.age-input' ).each( function( index ){
									  
				if( $( this ).val().length > 0 ){
					
					
					count++;		
				}						  
			
		
			})

//if more than 1 traveler is selected get the difference of the tripcost per person

			if( count > 1 ){
				
				perPersonCost = tripValue/count;	
				

//if the cost is greater than 25000, send an error

				if( perPersonCost > 25000 ){
					
					return false;	
					
				}else{
					
					
					return true;	
				}
			
//if only one traveler and the cost is > 25000 send error

			}else if( count = 1 && tripValue > 25000){
					
					return false;
					 
			}
		
		
		}else{
			

			return true;	
		
		}
			
	
	}, 'Total trip cost must be less than $25,000 per traveler.');

	// Make sure the Agency Code field shows up when needed
	var isDealingWithTravelAgents = $('select[name="agent"]').val();

	if (isDealingWithTravelAgents == "1") {
		$("#agencycodeblock").removeClass("no-show");
	} else {
		$("#agencycodeblock").addClass("no-show");
	}

});
