// JavaScript Document

function dateAdd($interval,$number,$dateTime) {
      
    $dateTime = (strtotime($dateTime) != -1) ? strtotime($dateTime) : $dateTime;       
    $dateTimeArr=getdate($dateTime);
                
    $yr=$dateTimeArr[year];
    $mon=$dateTimeArr[mon];
    $day=$dateTimeArr[mday];
    $hr=$dateTimeArr[hours];
    $min=$dateTimeArr[minutes];
    $sec=$dateTimeArr[seconds];

    switch($interval) {
        case "s"://seconds
            $sec += $number; 
            break;

        case "n"://minutes
            $min += $number; 
            break;

        case "h"://hours
            $hr += $number; 
            break;

        case "d"://days
            $day += $number; 
            break;

        case "ww"://Week
            $day += ($number * 7); 
            break;

        case "m": //similar result "m" dateDiff Microsoft
            $mon += $number; 
            break;

        case "yyyy": //similar result "yyyy" dateDiff Microsoft
            $yr += $number; 
            break;

        default:
            $day += $number; 
         }       
                
        $dateTime = mktime($hr,$min,$sec,$mon,$day,$yr);
        $dateTimeArr=getdate($dateTime);
        
        $nosecmin = 0;
        $min=$dateTimeArr[minutes];
        $sec=$dateTimeArr[seconds];

        if ($hr==0){$nosecmin += 1;}
        if ($min==0){$nosecmin += 1;}
        if ($sec==0){$nosecmin += 1;}
        
        if ($nosecmin>2){return(date("Y-m-d",$dateTime));} else {return(date("Y-m-d G:i:s",$dateTime));}
} 

function ValidCarForm(form)
{
	if ((form.date_dep0.value == '') || (form.date_dep0.value == 'yyyy-mm-dd'))
	{
		alert(document.getElementById('msg_enter_checkin_date').value);
		form.date_dep0.focus();
		return false;
	}		

	if ((form.date_dep1.value=='') || (form.date_dep1.value == 'yyyy-mm-dd'))
	{
		alert(document.getElementById('msg_enter_checkout_date').value);
		form.date_dep1.focus();
		return false;
	}	

	// Validar las fechas
	var date_dat = new Array();
	date_dat = form.date_dep0.value.split('-');
	var current1 = new Date(date_dat[0], date_dat[1]-1, date_dat[2]); // a new instance	
	//fixDate(current1);
	Date1 = current1.getTime();
	// Validar las fechas
	date_dat = form.date_dep1.value.split('-');
	var current2 = new Date(date_dat[0], date_dat[1]-1, date_dat[2]); // a new instance	
	//fixDate(current2);
	Date2 = current2.getTime();
	form.duration.value = (Date2 - Date1)/86400000;

	if (form.duration.value < 3)
	{
		alert(document.getElementById('msg_acept_cars_request_3_days').value);
		form.date_dep1.focus();
		return false;
	}
	// Today
	var current3 = new Date(); // a new instance	
	//fixDate(current3);	
	var current4 = new Date(current3.getFullYear(), current3.getMonth(), current3.getDate())
	Today = current4.getTime();
	
	
 	if(Date1 <= Today) // Today
	{
		alert(document.getElementById('msg_checkin_after_today').value);
		form.date_dep0.focus();
		return false;
	}
 	if(((Date1 - Today)/86400000) < 6) 
	{
		alert(document.getElementById('msg_acept_cars_request').value);
		form.date_dep0.focus();
		return false;
	}
	if(Date1 > Date2)
	{
		alert(document.getElementById('msg_checkout_after_checkin').value);
		form.date_dep1.focus();
		return false;
	}
	return true;
}

function SelectPickUpDropOff(dropdown)
{
	if (dropdown.name == 'gateway_dep')
	{
		dropdown.form.dest_dep.selectedIndex = dropdown.form.gateway_dep.selectedIndex;
	}
	else
	{
		if (dropdown.selectedIndex==0)
		{
			dropdown.form.gateway_dep.selectedIndex = dropdown.selectedIndex;
		}
	}
}

function SubmitCurrentCar(carindex, codtrans, codcat)
{
	pickupcombo = document.getElementById('pickuplocation_'+carindex);
	dropoffcombo = document.getElementById('dropofflocation_'+carindex);

        pickuptimecombo = document.getElementById('time_dep_'+carindex);
	dropofftimecombo = document.getElementById('time_ret_'+carindex);
          
	if (pickupcombo.options[pickupcombo.selectedIndex].value == '0')
	{		
		alert(document.getElementById('msg_please_select_pickup_place').value);
		pickupcombo.focus();
		return false;
	}
	if (dropoffcombo.options[dropoffcombo.selectedIndex].value == '0')
	{		
		alert(document.getElementById('msg_please_select_dropoff_place').value);
		dropoffcombo.focus();
		return false;
	}

        paramList = pickupcombo.options[pickupcombo.selectedIndex].value.split('-', 4);
        document.getElementById('pickuplocation').value =  paramList[0] + '-' + paramList[3];
        paramList = dropoffcombo.options[dropoffcombo.selectedIndex].value.split('-', 4);
	document.getElementById('dropofflocation').value = paramList[0] + '-' + paramList[3];

        if (parseInt(pickuptimecombo.options[pickuptimecombo.selectedIndex].value) < parseInt(dropofftimecombo.options[dropofftimecombo.selectedIndex].value))
        {
                alert(document.getElementById('msg_dropofftime_less_than_pickuptime').value);
		dropofftimecombo.focus();
		return false;
        }

        document.getElementById('time_dep').value = pickuptimecombo.options[pickuptimecombo.selectedIndex].text;
        document.getElementById('time_ret').value = dropofftimecombo.options[dropofftimecombo.selectedIndex].text;
        document.getElementById('pickupdate').value = document.getElementById('pickupdate_' + carindex).value;
	document.getElementById('dropoffdate').value = document.getElementById('dropoffdate_' + carindex).value;
	document.getElementById('codtrans').value = codtrans;
	document.getElementById('codcat').value = codcat;
	document.CarsForm.submit();
        return true;
}

function FillOptions(locationcombo, timecombo)
{
    currentValue = locationcombo.options[locationcombo.selectedIndex].value;
    if (currentValue != '0')
    {
        timeList = currentValue.split('-', 4);
        timecombo.options.length = 0;       
        from = parseInt(timeList[1]);
        to = parseInt(timeList[2]);
        for (i=from; i<=to; i++)
        {
            var currentOption = document.createElement("OPTION");            
            currentOption.text = (i<10) ? '0'+i+':00' : i+':00';          
            currentOption.value = i;            
            timecombo.options[timecombo.options.length] = currentOption;
        }
    }

}