/* global_function.js yoss3d 2010*/
function val_hotcode(){
var code = $('#hotcode').val();
var car = $('#idcarro').val();
var pick = $('#picklocation').val();
var promotion_n = $('#promotion_n').val();
//alert(code);
if(code!='')
	{
		 $.ajax({
			type:"POST",
			url:"val_hotcode.php",
			data:"code="+code+"&car="+car+"&picklocation=" + pick + "&promotion_n="+promotion_n,
			success: function(msg) {
				$("#error_hotcode").html(msg);
			}		
		});
	}
}

//Validates Rental Info - Pickup location and time, Return location and time, Car Type

function checkRentalInfo() 
{	
	var code = $('#hotcode').val();
	var car = $('#idcarro').val();
	var pick = $('#picklocation').val();
	var promotion_n = $('#promotion_n').val();
	if(code!=''){
		$.ajax({
			type:"POST",
			url:"val_hotcode.php",
			data:"code="+code+"&car="+car+"&picklocation=" + pick + "&promotion_n="+promotion_n,
			success: function(msg) {
				$("#error_hotcode").html(msg);
				if (msg==''){
					SubmitFormReservation();	
				} else {
					alert("The hot code is invalid. Try again");
					return false ;
				}						
			}		
		});
	} else {
		SubmitFormReservation();
	}

}

function SubmitFormReservation(){
	if (document.RentalInfo.picklocation.value == "NONE") 
	{
		alert("Select a Pickup station!") ;
		return false ;
	}

	if (document.getElementById("chek_r").checked)
	{
		document.RentalInfo.returnlocation.value = document.RentalInfo.picklocation.value;
	}	
	else if(RentalInfo.returnlocation.value == "") 
	{
	alert("Select a Return Location") ;
	return false;
	}else{
	document.RentalInfo.charge.value=1;
	}	
	var dateIn= $('#pickupdate').val();
	var dateOut= $('#returndate').val();
	
	pickdate = buildDate(dateIn, 'pick');
	dropdate = buildDate(dateOut, 'return');
	
	if (pickdate < today) 
	{
		alert("The Pick-up Date and Time must be later than today's date and time.");
		return false;
	}
	if (dropdate <= pickdate) 
	{
		alert("The Return Date must be later than the Pick-up date.");
		return false;
	}//*/
	if (document.RentalInfo.hotcode.value=='')
	{document.RentalInfo.action = 'step2.php';}else
	{document.RentalInfo.action = 'step2.php';}
	document.RentalInfo.submit() ;
	return true;
}

function explodeStr(ex, str)
{
	var strEnd=str.split("/");
	return( strEnd);
}
function fechToNumber(fecha){
	var fech_parts = fecha.split("/");
	fech_parts[1] = (fech_parts[1].length==1) ? ("0"+fech_parts[1]) : fech_parts[1];
	fech_parts[0] = (fech_parts[0].length==1) ? ("0"+fech_parts[0]) : fech_parts[0];
	return +(fech_parts[2]+fech_parts[1]+fech_parts[0]);
}
function fechArreglar(fecha){
	var fech_parts = fecha.split("/");
	fech_parts[1] = (fech_parts[1].length==1) ? ("0"+fech_parts[1]) : fech_parts[1];
	fech_parts[0] = (fech_parts[0].length==1) ? ("0"+fech_parts[0]) : fech_parts[0];
	return (fech_parts[0]+'/'+fech_parts[1]+'/'+fech_parts[2]);
}
////comparar las fechas
function compareDate(action)
{
	////si hay promocion SPECIAL restringir dias
	var promotion=$('#promotion_n').val();
	
	var alerTex='';
	var toChange='';//el que cambia (selecciono y Cambiar)
	
	var dateIn= $('#pickupdate').val();
	var dateOut= $('#returndate').val();
	var pickdate = fechToNumber(dateIn);
	var dropdate = fechToNumber(dateOut);
	
	var today = new Date()
	today = today.getDate() + "/" + (today.getMonth()+1) + "/" + today.getFullYear();
	var dayHoy = fechToNumber(today);
   // alert (pickdate+' - '+dropdate +' - '+ dayHoy);
		
	if(pickdate >= dayHoy){ 
			if (pickdate == dropdate) 
			{
				alertTex = 'The Pick-up Date can`t be iquial than Return Date.';
				toChange='returndate';
				if(promotion){
					increm=promotion;
				}else{
					increm=1;
				}
				//haverlo un dia mnor a return
			}else if (pickdate > dropdate) 
			{
				alertTex = 'The Pick-up Date can`t be later than Return Date.';
				toChange='returndate';
				if(promotion){
					increm=promotion;
				}else{
					increm=1;
				}
				///hacer drop day mayor a pick
			}else if(promotion){
				toChange='returndate';
					dateIn_x = fechArreglar(dateIn);
					day_return = addToDate( dateIn_x, promotion);//dia posible de retorno
					day_return = fechToNumber(day_return);
					if(day_return>dropdate){
						increm=promotion;	
					}else{
						toChange='';	
					}
			}
	}else{
		toChange='returndate';
		alertTex = 'The Pick-up Date can`t be affter than to Day.';
		$('#pickupdate').val(today);
		dateIn= today;
		dateIn_x = fechArreglar(today);
		day_return = addToDate( dateIn_x, promotion);//dia posible de retorno
		day_return = fechToNumber(day_return);
		if(day_return>dropdate){
			increm=promotion;	
		}else{
			toChange='';	
		}
		
		//hacer igual a hoy
	}
	if(alerTex){
		//$('#error_'+action).html(alertTex);
		//$('#error_'+action).show();
	}else{
		//$('#error_'+action).hide();
		//$('#error_'+action).html('');
	}
	
	if(toChange!=''){
		dateIn = fechArreglar(dateIn);
		date = addToDate( dateIn, increm);
		$('#'+toChange).val(date);
	}
	
	
	pickupdate= $('#pickupdate').val();
	returndate= $('#returndate').val();
	/*$.ajax({
					type:"POST",
					url:"include/changedate.php",
					data:"pickupdate="+pickupdate+"&returndate="+returndate,
					success: function(msg) {
						$("#dateChange").html(msg);
					}		
				});
	*/
}

function hideDescription(target)
{
	$('#'+target).slideUp('slow');
}
function showDescription(s)
{
	//showDiv(s, total);
	var activo=$("#activo").val();
	if(activo!=s){
		 //oculto
		 hideDescription('car_desc_'+activo);
		 
		$('#select_'+activo).removeClass("select_o");
		$('#select_'+activo).addClass("select_n");
		
		$('#select_'+s).removeClass("select_n");
		$('#select_'+s).addClass("select_o");
		
		$('#car_desc_'+s).slideToggle('slow');
		$("#activo").val(s);
	}
}
function recalculate(action)
{
	var idcarro=$("#idcarro").val();
	var pickupdate=$("#pickupdate").val();
	var returndate=$("#returndate").val();
	var seguros=$("#seguros").val();
	var special_segure=$("#special_segure").val();
	var edit_reserv= $('#edit_reserv').val();
	var hotcode= $('#hotcode').val();
	var hot_valid= $('#hot_valid').val();
	var promotion_n = $('#promotion_n').val();

	var activo=$("#activo").val();
	hideDescription('car_desc_'+activo);
	$("#activo").val('');
	$.ajax({
			type:"POST",
			url:"showcars.php",
			data:"pickupdate="+pickupdate+"&returndate="+returndate+"&action="+action+"&idcarro="+idcarro+"&seguros="+seguros+"&special_segure="+special_segure+"&edit_reserv="+edit_reserv+"&hotcode="+hotcode+"&hot_valid="+hot_valid+"&promotion_n="+promotion_n,
			success: function(msg) {
				$("#showcar").html(msg);
				showDescription(idcarro);
				///si es el mismo que esta selecc
			}		
		});
}
function recalculateSegures(action, valor)
{
	var idcarro=$("#idcarro").val();
	var pickupdate=$("#pickupdate").val();
	var returndate=$("#returndate").val();
	var seguros=$("#seguros").val();
	var special_segure=$("#special_segure").val();
	var hotcode= $('#hotcode').val();
	var hot_valid= $('#hot_valid').val();
	var promotion_n = $('#promotion_n').val();
	var activo=$("#activo").val();
	hideDescription('car_desc_'+activo);
	var edit_reserv= $('#edit_reserv').val();
	$("#activo").val('');
	$.ajax({
			type:"POST",
			url:"showcars.php",
			data:"action="+action+"&valor="+valor+"&pickupdate="+pickupdate+"&returndate="+returndate+"&action="+action+"&idcarro="+idcarro+"&seguros="+seguros+"&special_segure="+special_segure+"&edit_reserv="+edit_reserv+"&hotcode="+hotcode+"&hot_valid="+hot_valid+"&promotion_n="+promotion_n,
			success: function(msg) {
				$("#showcar").html(msg);
				showDescription(idcarro);
				///si es el mismo que esta selecc
			}		
		});
}
function recalculateIndex(action)
{
	var idcarro=$("#idcarro").val();
	var pickupdate=$("#pickupdate").val();
	var returndate=$("#returndate").val();
	
	var hotcode= $('#hotcode').val();
	var hot_valid= $('#hot_valid').val();
	var promotion_n = $('#promotion_n').val();
	
	var activo=$("#activo").val();
	hideDescription('car_desc_'+activo);
	$("#activo").val('');
	$.ajax({
			type:"POST",
			url:"showcarsindex.php",
			data:"pickupdate="+pickupdate+"&returndate="+returndate+"&action="+action+"&idcarro="+idcarro+"&hotcode="+hotcode+"&hot_valid="+hot_valid+"&promotion_n="+promotion_n,
			success: function(msg) {
				$("#showcar").html(msg);
				showDescription(idcarro);
				///si es el mismo que esta selecc
			}		
		});
}
var pickdate = new Date();
var dropdate = new Date();

var today = new Date()
var today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
function MM_reloadPage(init) 
{  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

function buildDate(tDate, target) 
{
	var arry_date = explodeStr('/', tDate);
	
	year  = arry_date[2];
	month = arry_date[1]-1;
	day   = arry_date[0];
	time  = $('#'+target+'time').val();
	timeStrings = time.split(":");
	hour  = timeStrings[0];
	min   = timeStrings[1];


		if(target=='return'){
			// if the PM radio button is checked and the hour is less than 12
			//  add 12 to the hour.
			if ((!document.RentalInfo.returnampm[0].checked) && (hour < 12)) 
			{
				hour = new Number(hour);
				hour = (hour + 12);
			}
		
			//if the AM radio button is checked and the hour is 12, set the hour to 00
			//  So the Date object does not increment the day by one.
			if ((document.RentalInfo.returnampm[0].checked) && (hour == 12)) 
			{
				hour = 00;
			}
		
			//if the AM radio button is checked and the hour is 12 and the min is 30,
			// set the hour to 00 and the min to 30.  So the Date object does not increment the day by one.
			if ((document.RentalInfo.returnampm[0].checked) && (hour == 12) && (min == 30)) 
			{
				hour = 00;
				min = 30;
			}
		}else{
			// if the PM radio button is checked and the hour is less than 12
			//  add 12 to the hour.
			if ((!document.getElementById("pickampm").checked) && (hour < 12)) 
			{
				hour = new Number(hour);
				hour = (hour + 12);
			}
		
			//if the AM radio button is checked and the hour is 12, set the hour to 00
			//  So the Date object does not increment the day by one.
			if ((document.RentalInfo.pickampm[0].checked) && (hour == 12)) 
			{
				hour = 00;
			}
		
			//if the AM radio button is checked and the hour is 12 and the min is 30,
			// set the hour to 00 and the min to 30.  So the Date object does not increment the day by one.
			if ((document.RentalInfo.pickampm[0].checked) && (hour == 12) && (min == 30)) 
			{
				hour = 00;
				min = 30;
			}
			
		}
   	return (new Date(year,month,day,hour,min));
}
function UncheckAll(cant)
{
	
	if (document.getElementById("special_secure").checked)
	{
		
		for (i=0; i<cant; i++)
		{
			document.getElementById("segure_price"+i).checked = false;
			document.getElementById("segure_price"+i).disabled = true;
		}
	}
	else
	{
		for (i=0; i<cant; i++)
		{
			document.getElementById("segure_price"+i).disabled = false;
		}
	}
}
function recogerSeguros(action)
{
var total_seguros = $('#t_seguros').val();
var seguro = new Array();
var selSeg='';
var special='';
UncheckAll(total_seguros);
if (document.getElementById("special_secure").checked)
{
	special='special_segure';
}else{
	var seg_x='';
	var cant='';
		for (i=0; i<total_seguros; i++)
		{
			seg_x = $('#segure_price'+i).val();
			if(seg_x==8){
					document.getElementById("segure_price"+i).checked = true;/// hacer siempre ali select	
					}
			if(document.getElementById("segure_price"+i).checked){
				if(selSeg==''){
					selSeg = seg_x;
				}else{
					selSeg = selSeg + ',' + seg_x;
				}
			}
		}
}
	seguro['seguros']=selSeg;
	seguro['special']=special;
return seguro;	
}
function recogerItens()
{
var total_itens = $('#total_itens').val();
var selIten='';
var unityIten='';
var iten_x='';
var itens = new Array();
var cant='';

	for (i=0; i<total_itens; i++)
	{
		if(document.getElementById("iten_"+i).checked){
			iten_x = $('#iten_'+i).val();
			if(selIten==''){
				selIten = iten_x;
			}else{
				selIten = selIten + ',' + iten_x;
			}
			////veo si tiene cantidad
			cant = $('#iten_unity_'+iten_x).val();
			if(cant){
				if(unityIten==''){
					unityIten = cant;
				}else{
					unityIten = unityIten + ',' + cant;
				}
			}else{//le digo que es una sola unidad
				if(unityIten==''){
					unityIten = 1;
				}else{
					unityIten = unityIten + ',1';
				}
			}
		}
	}
	itens['itens']=selIten;
	itens['unity']=unityIten;
return itens;	
}