// JavaScript Document
function openLocation(div_name, script_name, option) 
{	
	if(option==""){		
		FAjax('empty.php', div_name, '', 'GET');
	}
	else{
		FAjax(script_name, div_name, '', 'GET');
	}
}

function validateStepOneForm() 
{	
	var pickup_location = document.getElementById('pickup_loc').value;
	var pickup_date = document.getElementById('pickup_date').value;
	var pickup_time = document.getElementById('pickup_time').value;	
	var dropoff_location = document.getElementById('dropoff_loc').value;
	var dropoff_date = document.getElementById('dropoff_date').value;
	var dropoff_time = document.getElementById('dropoff_time').value;	
	var car_type = document.getElementById('car_type').value;
	var auto = document.getElementById('car_type_alt').value;
	var hoy = new Date();
	
	/*
	if(isLowerThanToday(pickup_date)==true)
	{
		alert('Pick up date cannot be lower than today');
		return false;
	}	
	if(isLowerThanToday(dropoff_date)==true)
	{
		alert('Drop Off date cannot be lower than today');
		return false;
	}	
*/
	if (pickup_location=="")
	{
		alert('Invalid pickup location');	
		return false;
	}
	if (pickup_date=="")
	{
		alert('Invalid pickup date');	
		return false;
	}
	if (pickup_time==-1)
	{
		alert('Invalid pickup time');	
		return false;
	}
	
	if (dropoff_location=="")
	{
		alert('Invalid drop off location');	
		return false;
	}
	if (dropoff_date=="")
	{
		alert('Invalid drop off date');	
		return false;
	}
	if (dropoff_time==-1)
	{
		alert('Invalid drop off time');	
		return false;
	}	

	if (car_type=="Select Car Type")
	{
		alert('Invalid car type');	
		return false;
	}

	if(CompareDates(pickup_date, dropoff_date)==false) 
	{
		alert("Pick up date cannot be greater than Drop-Off date");
		return false;
	}

	if (auto=="")
	{
		alert('Invalid car type');	
		return false;
	}
	
	return true;
}

function validateStepTwoForm()
{	
	var firstname = document.getElementById('firstname').value;
	var lastname = document.getElementById('lastname').value;
	var email = document.getElementById('email').value;	
	var phone = document.getElementById('phone').value;	
	var city = document.getElementById('city').value;
	var state = document.getElementById('state').value;
	var country = document.getElementById('Country').value;		
	
	if (firstname=="")
	{
		alert('Invalid First Name');	
		return false;
	}
	if (lastname=="")
	{
		alert('Invalid Last name');	
		return false;
	}
	if (email=="")
	{
		alert('Invalid E-Mail Address');	
		return false;
	}
	else if(email!="")
	{
		if (echeck(email)==false) 
		{
			alert('Invalid E-Mail Address');	
			return false;
		}
	}
	if (phone=="")
	{
		alert('Invalid phone number');	
		return false;
	}
	
	if (city=="")
	{
		alert('Invalid City');	
		return false;
	}
	if (state=="")
	{
		alert('Invalid State');	
		return false;
	}	
	if (country=="")
	{
		alert('Invalid Country');	
		return false;
	}	
	
	return true;
}

function CompareDates(fecha_inicio, fecha_final)
{
	var fecha1=fecha_inicio.split("/");
	var fecha2=fecha_final.split("/");
	
	var date1 = new Date(fecha1[2], fecha1[1], fecha1[0], 0, 0, 0);
	var date2 = new Date(fecha2[2], fecha2[1], fecha2[0], 0, 0, 0);
	
	//alert("fecha1="+date1+"    fecha2="+date2);

    if(date2 < date1)
    {        
        return false;
    }
    else
    {
        return true;
    }
}

function isLowerThanToday(date)
{	
	var fecha=date.split("/");	
	var date1 = new Date(parseInt(fecha[2]), parseInt(fecha[1])-1, fecha[0],23, 59, 59, 99);
	
	//alert(fecha[0]+'    ='+parseInt(fecha[0]));
	//alert('anio='+parseInt(fecha[2])+'    mes='+parseInt(fecha[1])+'    dia='+fecha[0]);
	//alert('hoy='+(new Date())+'     fecha='+date1+'       date='+date);

	if((new Date())>=date1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function Plus(total, check, num_days)
{	
	//alert('total='+total+'    check='+check+'    num_days='+num_days);

	var gps_price_per_day=8;
	var bseat_price_per_day=3;
	var requested_bseats=parseFloat(document.getElementById('bseat').value);
	
	var total_bseat=parseFloat((requested_bseats*parseFloat(num_days)*bseat_price_per_day));
	
	var subtotal_gps=parseFloat(gps_price_per_day)*parseFloat(num_days);
	var total_gps=(document.getElementById('gps').checked==true) ? subtotal_gps : 0;
	
	var gran_total=parseFloat(total)+(parseFloat(total_bseat+total_gps));
	
	var xx=gran_total.toFixed(2);
	
	document.getElementById('gran_total').value=formatMoney(xx, "", ",", ".");
}

function isThousands(position) 
{
	if (Math.floor(position/3)*3==position) return true;
	return false;
};

function formatMoney (theNumber,theCurrency,theThousands,theDecimal) 
{
	var theDecimalDigits =	Math.round((theNumber*100)-(Math.floor(theNumber)*100));
	theDecimalDigits= ""+ (theDecimalDigits + "0").substring(0,2);
	theNumber = ""+Math.floor(theNumber);
	var theOutput = theCurrency;
	for (x=0; x<theNumber.length; x++) 
	{
		theOutput += theNumber.substring(x,x+1);
		if (isThousands(theNumber.length-x-1) && (theNumber.length-x-1!=0)) 
		{
			theOutput += theThousands;
		};
	};
	
	theOutput += theDecimal + theDecimalDigits;
	
	return theOutput;
};

function echeck(str) 
{
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}
