


function onchangecategory( value ){
	if( value==0){
		$("#all_extras_item_selector option[index!='0']").remove();	
	}else{
		$("#all_extras_item_selector option[index!='0']").remove();	
		var ajaxurl = "buyextras_step1_ajax.php?action=onchangecaegory&id="+value;
        $.getJSON(
            ajaxurl, function(data){
				$.each(data,function(i){
					//alert( data[i].value  );
					$("#all_extras_item_selector").append("<option value='"+ data[i].value  +"'>" + data[i].text + "</option>"); 
				})
			}
        );
		
	}
}

function onchangecategorypartner( value,datarow_extras ){
	datarow_extras = datarow_extras.replace(/#/g, "_");
	if( value==0){
		$("#all_extras_item_selector option[index!='0']").remove();	
	}else{
		$("#all_extras_item_selector option[index!='0']").remove();	
		var ajaxurl = "buyextras_step1_ajax.php?action=onchangecategorypartner&id="+value+"&datarow_extras="+datarow_extras;
        $.getJSON(
            ajaxurl, function(data){
				$.each(data,function(i){
					//alert( data[i].value  );
					$("#all_extras_item_selector").append("<option value='"+ data[i].value  +"'>" + data[i].text + "</option>"); 
				})
			}
        );
		
	}
}

function onchangecategoryhomepage( value ){
	if( value==0){
		$j("#all_extras_item_selector_homepage option[index!='0']").remove();	
	}else{
		$j("#all_extras_item_selector_homepage option[index!='0']").remove();	
		var ajaxurl = "buyextras_step1_ajax.php?action=onchangecaegory&id="+value;
        $j.getJSON(
            ajaxurl, function(data){
				$j.each(data,function(i){
					//alert( data[i].value  );
					$j("#all_extras_item_selector_homepage").append("<option value='"+ data[i].value  +"'>" + data[i].text + "</option>"); 
				})
			}
        );
		
	}
}

function onchangeitem( value ){

	if( value!=0){
		var itemids = $('#hidden_input_selectitems').val(  );
		if( itemids.indexOf(value+'#')>-1 ){
			//alert( 'You already select this item!' );
			//return;
		}
		
		
		var itemcount = $('#hidden_input_selectitems_count').val(  );
		itemcount = parseInt( itemcount ) + 1;
		$('#hidden_input_selectitems_count').val( itemcount );

		
		
		itemids = itemids+value+"#";
		$('#hidden_input_selectitems').val( itemids );
		$('#hidden_div_selectitems').hide();
	
		
		createHTML( value );
		
		//return default value
		
		$("#all_extras_category_selector option[index='0']").attr("selected" , "selected");  
		$("#all_extras_item_selector option[index!='0']").remove();	
		
		//
		$('#div_youractivetiesheader').show();
		$('#div_totalprice').show();
		//alert(itemids);
	}
}

function onchangeitemnoinfo( value ){

	if( value!=0){
		var itemids = $('#hidden_input_selectitems').val(  );
		if( itemids.indexOf(value+'#')>-1 ){
			//alert( 'You already select this item!' );
			//return;
		}
		
		
		var itemcount = $('#hidden_input_selectitems_count').val(  );
		itemcount = parseInt( itemcount ) + 1;
		$('#hidden_input_selectitems_count').val( itemcount );

		
		
		itemids = itemids+value+"#";
		$('#hidden_input_selectitems').val( itemids );
		$('#hidden_div_selectitems').hide();
	
		
		createHTML2( value );
		
		//return default value
		
		$("#all_extras_category_selector option[index='0']").attr("selected" , "selected");  
		$("#all_extras_item_selector option[index!='0']").remove();	
		
		//
		$('#div_youractivetiesheader').show();
		$('#div_totalprice').show();
		//alert(itemids);
	}
}

function onchangeitemhomepage(  value, facetext ){
	if( value!=0){
		$j('#selected_extrasid').val( value );
		//alert(facetext);
		//get price
		var priceindex = facetext.indexOf( '$');
		var getprice  =  facetext.substring( priceindex+1 );
		document.OrderForm.totalprice.value = getprice ;
	}
}

function onchangeqty( extraid,price,qty ,maxqty){
	//change subprice price
	var newprice = price * qty;
	var subtotal_id = 'subprice' + extraid;
	$('#'+subtotal_id).html(newprice);
	//change guest list
	for( var index=1;index<=qty;index++){
		var id = 'tr_guestlist_title_'+extraid+'_'+index;
		$('#'+id).show();
		id = 'tr_guestlist_body_'+extraid+'_'+index;
		$('#'+id).show();
	}
	var startindex = parseInt( qty ) + 1;
	for( var index2 = startindex;index2<=maxqty;index2++){
		var id2 = 'tr_guestlist_title_'+extraid+'_'+index2;
		$('#'+id2).hide();
		id2 = 'tr_guestlist_body_'+extraid+'_'+index2;
		$('#'+id2).hide();
	}
	//change total price
	changetotalprice();
}

function deleteoneextra( extraid ){
	$("#extra_table_tr_"+extraid).remove();
	$("#extra_table_tr_flight_"+extraid).remove();
	$("#extra_table_tr_pickuplocation_"+extraid).remove();
	$("#extra_table_tr_guestlist_"+extraid).remove();
	$("#extra_table_tr_destinations_"+extraid).remove();
	
	var itemcount = $('#hidden_input_selectitems_count').val(  );
	itemcount = parseInt( itemcount ) - 1;
	$('#hidden_input_selectitems_count').val( itemcount );
	
	var itemids = $('#hidden_input_selectitems').val(  );
	var needtofind = extraid + '#';
	itemids = itemids.replace( needtofind, "" );
	$('#hidden_input_selectitems').val( itemids );
	
	changetotalprice();
}


function changetotalprice(  ){
	var itemids = $('#hidden_input_selectitems').val( );
	var arr_itemid = itemids.split('#');
	var totalprice = 0;
	for( var i=0;i< arr_itemid.length-1 ;i++){
		var one_price =  $('#subprice'+arr_itemid[i]).html();
		totalprice = totalprice + parseInt ( one_price );
	}
	$('#totalprice').html( totalprice );
	$('#totalprice2').val( totalprice );
}

function createHTML( extrasid ){
	var ajaxurl = "buyextras_step1_ajax.php?action=createhtml&id="+extrasid;
	$.ajax({
		type: "post",
		url: ajaxurl,
		async: false,
		success: function(result) {
			//var old_table_html = $('#all_purchased_extras_table').html() ;
			//var new_table_html = result + old_table_html;
			//new_table_html = new_table_html.replace( "<tbody>","" );
			//new_table_html = new_table_html.replace( "</tbody>","" );
			//new_table_html = "<tbody>"+new_table_html+"</tbody>"
			//$('#all_purchased_extras_table').html( new_table_html );
			//alert(result);
			$('#all_purchased_extras_table').prepend(result);
			changetotalprice();
		},

		error: function(result) {
			//alert(result.responseText);
		}
	});
}


function createHTML2( extrasid ){
	var ajaxurl = "buyextras_step1_ajax.php?action=createhtml&id="+extrasid+"&noinfo=1";
	$.ajax({
		type: "post",
		url: ajaxurl,
		async: false,
		success: function(result) {
			//var old_table_html = $('#all_purchased_extras_table').html() ;
			//var new_table_html = result + old_table_html;
			//new_table_html = new_table_html.replace( "<tbody>","" );
			//new_table_html = new_table_html.replace( "</tbody>","" );
			//new_table_html = "<tbody>"+new_table_html+"</tbody>"
			//$('#all_purchased_extras_table').html( new_table_html );
			//alert(result);
			$('#all_purchased_extras_table').prepend(result);
			changetotalprice();
		},

		error: function(result) {
			//alert(result.responseText);
		}
	});
}


function showguestlistinput(number,extraid,guestlistindex){
	number = parseInt(number);
	
	if( number==0 ){
		for( var i=1;i<51;i++){
			var id = "tr_guestname_"+extraid+"_"+guestlistindex+"_"+ i;
			document.getElementById(id).style.display = "none";
		}
		alert('Please select at least one guest!');
	}else{
		for( var i=1;i<number+1;i++){
			var id = "tr_guestname_" +extraid+"_"+guestlistindex+"_"+ i;
			document.getElementById(id).style.display = "";
		}
		for( var i=number+1;i<51;i++){
			var id = "tr_guestname_" +extraid+"_"+guestlistindex+"_"+ i;
			document.getElementById(id).style.display = "none";
		}
	}
}

function submitform(form){
	var checked_datesandtime = true; 
	var itemids = $('#hidden_input_selectitems').val( );
	
	if( itemids=='' && extrasonly_var=='true'  ){
		alert('Please select an Activity or Service before continuing to the next step.');
		return false;
	}
	
	
	var arr_itemid = itemids.split('#');

	for( var i=0;i< arr_itemid.length-1 ;i++){
		var pickuplocationobj = $('#'+ arr_itemid[i]+ 'pickuplocation');
		if(  pickuplocationobj ){
			if( pickuplocationobj.val()=='' ){
			alert('Please indicate your pick up and/or drop off location.');
			return false;
			}
		}
	}
	
	for( var i=0;i< arr_itemid.length-1 ;i++){
		var havedestinationsobjvalue = false;
		var destinationsobj =  $("input:checkbox");
		if(  destinationsobj ){
			for( var i9=0;i9<destinationsobj.length;i9++){
				if( destinationsobj[i9].checked!=false ){
				havedestinationsobjvalue = true;
				break;
			  }
			}
			
			
			if( havedestinationsobjvalue ){
			}else{
				var destinations_other1 = $('#'+ arr_itemid[i]+ 'destinations_other1');
				if( destinations_other1.val()!='' ){
					havedestinationsobjvalue = true;
				
				}
				var destinations_other2 = $('#'+ arr_itemid[i]+ 'destinations_other2');
				if( destinations_other2.val()!='' ){
					havedestinationsobjvalue = true;
		
				}
				
				if( havedestinationsobjvalue ){
				}else{
				alert( 'please select destinations ' );
				return false;
				}
			}
		}
	}
	
	//alert('hi');
	//return false;
	for( var i=0;i< arr_itemid.length-1 ;i++){
		var mmvalue = $('#'+ 'mm' + arr_itemid[i]).val();
		if( mmvalue=='MM' ){
			checked_datesandtime = false;
			break;
		}
		var ddvalue = $('#'+ 'dd' + arr_itemid[i]).val();
		if( ddvalue=='DD' ){
			checked_datesandtime = false;
			break;
		}
		var yyvalue = $('#'+ 'yy' + arr_itemid[i]).val();
		if( yyvalue=='YYYY' ){
			checked_datesandtime = false;
			break;
		}
		var timevalue = $('#'+ 'time' + arr_itemid[i]).val();
		if( timevalue=='Time' ){
			checked_datesandtime = false;
			break;
		}
		if( yyvalue ){
			var d1 = new Date();
			var hours24 = $('#'+ 'hiddenhours24' + arr_itemid[i]).val();
			if( hours24==1 ){
				var d2 = new Date(yyvalue,mmvalue-1,parseInt(ddvalue));
			}else{
				var d2 = new Date(yyvalue,mmvalue-1,parseInt(ddvalue)+1);
			}
			if( d1>d2 ){
				alert( "The date you have selected has already passed. Please choose a new date.");
				return false;
			}
		}
	}
	if( checked_datesandtime ){
		
	}else{
		alert('Please select a valid date and/or time for your item.');
		return false;
	}
	
	
	for( var i=0;i< arr_itemid.length-1 ;i++){
		
		
		var flightinfooptionid = arr_itemid[i];
		var sharedride = $('#'+ 'hiddensharedride' + arr_itemid[i]).val();
		
		
		var transportation1obj =  $( '#' + flightinfooptionid + 'transportation1' );
		var transportation2obj =  $( '#' + flightinfooptionid + 'transportation2' );
		var transportation3obj =  $( '#' + flightinfooptionid + 'transportation3' );
		if(  transportation1obj ){
			if(  transportation1obj.attr("checked")==false && transportation2obj.attr("checked")==false && transportation3obj.attr("checked")==false ){
				alert('Please select Transportation');
				return false;
			}
			
			if(  transportation1obj.attr("checked")==true ||  transportation3obj.attr("checked")==true   ){
			
				
				var flightinfodeparturedatemmobj = document.getElementById(flightinfooptionid+'flightinfodeparturedatemm');
				var index = flightinfodeparturedatemmobj.selectedIndex;
				var flightinfodeparturedatemmvalue = flightinfodeparturedatemmobj.options[index].value; 
				if( flightinfodeparturedatemmvalue=='MM'||flightinfodeparturedatemmvalue=='' ){
					alert('Please input the Arrival date');
					return false;
				}
				
				var flightinfodeparturedateddobj = document.getElementById(flightinfooptionid+'flightinfodeparturedatedd');
				var index = flightinfodeparturedateddobj.selectedIndex;
				var flightinfodeparturedateddvalue = flightinfodeparturedateddobj.options[index].value; 
				if( flightinfodeparturedateddvalue=='DD'||flightinfodeparturedateddvalue=='' ){
					alert('Please input the Arrival date');
					return false;
				}
				
				var flightinfodeparturedateyyobj = document.getElementById(flightinfooptionid+'flightinfodeparturedateyy');
				var index = flightinfodeparturedateyyobj.selectedIndex;
				var flightinfodeparturedateyyvalue = flightinfodeparturedateyyobj.options[index].value; 
				if( flightinfodeparturedateyyvalue=='YYYY'||flightinfodeparturedateyyvalue=='' ){
					alert('Please input the Arrival date');
					return false;
				}
				
				var d1 = new Date();
				var hours24 = $('#'+ 'hiddenhours24' + arr_itemid[i]).val();
				if( hours24==1 ){
					var d2 = new Date(flightinfodeparturedateyyvalue,flightinfodeparturedatemmvalue-1,parseInt(flightinfodeparturedateddvalue));
				}else{
					var d2 = new Date(flightinfodeparturedateyyvalue,flightinfodeparturedatemmvalue-1,parseInt(flightinfodeparturedateddvalue)+1);
				}
				if( d1>d2 ){
					alert( "The date you have selected has already passed. Please choose a new date.");
					return false;
				}
				
				var flightinfoairlinenumber = document.getElementById(flightinfooptionid+'flightinfoairlinenumber').value;
				if( flightinfoairlinenumber=='Other' ){
					var flightinfoairlinenumber_other =  document.getElementById(flightinfooptionid+'flightinfoairlinenumber_other').value;
					if(   flightinfoairlinenumber_other==''   ){
					alert('Please input the Airline Number');
					return false;
					}
				}
				
				var flightinfoflightnumber = document.getElementById(flightinfooptionid+'flightinfoflightnumber').value;
				if( flightinfoflightnumber=='' ){
					alert('Please input the Flight Number');
					return false;
				}
				
				
				var flightinfoarrivaltime1 = document.getElementById(flightinfooptionid+'flightinfoarrivaltime1').value;
				
				if( flightinfoarrivaltime1=='' ){
					alert('Please input the Arrival Time');
					return false;
				}
				
				var flightinfoarrivaltime2 = document.getElementById(flightinfooptionid+'flightinfoarrivaltime2').value;
				if( flightinfoarrivaltime2=='' ){
					alert('Please input the Arrival Time');
					return false;
				}
				
				if( sharedride== 1 ){
					var flightinfoarrivaltime3 = document.getElementById(flightinfooptionid+'flightinfoarrivaltime3').value;
					
					if( flightinfoarrivaltime3=="AM" ){
						if( parseInt( flightinfoarrivaltime1 ) < 10 ){
							alert('Shared ride options are only available for arrivals into Cabo between the hours of 10:00am and 4:30pm. Please choose one of our private transportation options for any arrivals that are outside of this time frame.');
							return false;
						}
					}else if( flightinfoarrivaltime3=="PM" ){
						var requiretime = 4*60 + 30 ;
						var selecttime = parseInt( flightinfoarrivaltime1-0 )*60 + parseInt( flightinfoarrivaltime2-0 ) ;
						
						if( selecttime > requiretime ){
							alert('Shared ride options are only available for arrivals into Cabo between the hours of 10:00am and 4:30pm. Please choose one of our private transportation options for any arrivals that are outside of this time frame.');
							return false;
						}
					}
				}
			}
			
			if( transportation2obj.attr("checked")==true  ||  transportation3obj.attr("checked")==true  ){
				var flightinfoteturndatemmobj = document.getElementById(flightinfooptionid+'flightinfoteturndatemm');
				var index = flightinfoteturndatemmobj.selectedIndex;
				var flightinfoteturndatemmvalue = flightinfoteturndatemmobj.options[index].value; 
				if( flightinfoteturndatemmvalue=='MM'||flightinfoteturndatemmvalue=='' ){
					alert('Please input the Departure date');
					return false;
				}
				
				var flightinfoteturndateddobj = document.getElementById(flightinfooptionid+'flightinfoteturndatedd');
				var index = flightinfoteturndateddobj.selectedIndex;
				var flightinfoteturndateddvalue = flightinfoteturndateddobj.options[index].value; 
				if( flightinfoteturndateddvalue=='DD'||flightinfoteturndateddvalue=='' ){
					alert('Please input the Departure date');
					return false;
				}
				
				var flightinfoteturndateyyobj = document.getElementById(flightinfooptionid+'flightinfoteturndateyy');
				var index = flightinfoteturndateyyobj.selectedIndex;
				var flightinfoteturndateyyvalue = flightinfoteturndateyyobj.options[index].value; 
				if( flightinfoteturndateyyvalue=='YYYY'||flightinfoteturndateyyvalue=='' ){
					alert('Please input the Departure date');
					return false;
				}
				
				
				var d1 = new Date();
				var hours24 = $('#'+ 'hiddenhours24' + arr_itemid[i]).val();
				if( hours24==1 ){
					var d2 = new Date(flightinfoteturndateyyvalue,flightinfoteturndatemmvalue-1,parseInt(flightinfoteturndateddvalue));
				}else{
					var d2 = new Date(flightinfoteturndateyyvalue,flightinfoteturndatemmvalue-1,parseInt(flightinfoteturndateddvalue)+1);
				}
				if( d1>d2 ){
					alert( "The date you have selected has already passed. Please choose a new date.");
					return false;
				}
				
				var flightinforeturnairlinenumber = document.getElementById(flightinfooptionid+'flightinforeturnairlinenumber').value;
				if( flightinforeturnairlinenumber=='Other' ){
					var flightinforeturnairlinenumber_other =  document.getElementById(flightinfooptionid+'flightinforeturnflightnumber_other').value;
					if(   flightinforeturnairlinenumber_other==''   )
					{
					alert('Please input the Return Airline Number');
					return false;
					}
				}
				
				
				var flightinforeturnflightnumber = document.getElementById(flightinfooptionid+'flightinforeturnflightnumber').value;
				if( flightinforeturnflightnumber=='' ){
					alert('Please input the Return Flight Number');
					return false;
				}
				
				
				
				var flightinforeturndeparturetime1 = document.getElementById(flightinfooptionid+'flightinforeturndeparturetime1').value;
				if( flightinforeturndeparturetime1=='' ){
					alert('Please input the Departure Time');
					return false;
				}
				
				var flightinforeturndeparturetime2 = document.getElementById(flightinfooptionid+'flightinforeturndeparturetime2').value;
				if( flightinforeturndeparturetime2=='' ){
					alert('Please input the Departure Time');
					return false;
				}
				
				if( sharedride== 1 ){
					var flightinforeturndeparturetime3 = document.getElementById(flightinfooptionid+'flightinforeturndeparturetime3').value;
					if( flightinforeturndeparturetime3=="AM" ){
						var requiretime = 11*60 + 45 ;
						var selecttime = parseInt( flightinforeturndeparturetime1 )*60 + parseInt( flightinforeturndeparturetime2 ) ;
						if( selecttime < requiretime ){
							alert('Shared ride options are only available for departures from Cabo between the hours of 11:45am and 3:45pm. Please choose one of our private transportation options for any departures that are outside of this time frame.');
							return false;
						}
					}else if( flightinforeturndeparturetime3=="PM" ){
						var requiretime = 3*60 + 45 ;
						var selecttime = parseInt( flightinforeturndeparturetime1-0 )*60 + parseInt( flightinforeturndeparturetime2-0 ) ;
						if( selecttime > requiretime ){
							alert('Shared ride options are only available for departures from Cabo between the hours of 11:45am and 3:45pm. Please choose one of our private transportation options for any departures that are outside of this time frame.');
							return false;
						}
					}
				}
			}
			
			
	
			var hidden_quantity =$('#qty'+flightinfooptionid).val();
			
			for( var hidden_quantity_index=1 ; hidden_quantity_index<=hidden_quantity;hidden_quantity_index++ ){
				var guestlistnumberid = flightinfooptionid + 'guestlistnumber_'+hidden_quantity_index;
				if( document.getElementById(guestlistnumberid) ){
					var selectedgustlistnumber = document.getElementById(guestlistnumberid).value;
					if( document.getElementById(guestlistnumberid) ){
							for( var guestindex=1 ; guestindex<=selectedgustlistnumber; guestindex++ ){
								var guestnameid = flightinfooptionid + 'guestfirstname_' + hidden_quantity_index + '_'+ guestindex;
								var gustnamevalue = document.getElementById(guestnameid).value;
								if( gustnamevalue=='' ){
									alert('Please input the Guest First Name For Guest List ' +hidden_quantity_index );
									return false;
								}
								
								var guestlastnameid = flightinfooptionid + 'guestlastname_' + hidden_quantity_index + '_'+ guestindex;
								var gustlastnamevalue = document.getElementById(guestlastnameid).value;
								if( gustlastnamevalue=='' ){
									alert('Please input the Guest Last Name For Guest List ' +hidden_quantity_index );
									return false;
								}
							}
					}
				}
			}
			
		}
	}
	return true;
	
	//alert('hi');
	//return false;
	
	
}
