// 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()
{
	var det_date = new Array;
	var det_date1 = new Array;
	
	
	
	if(document.searchCar.dest_dep.value==0)
	{
		alert(document.getElementById('msg_enter_pickup_place').value);
		document.searchCar.gateway_dep.focus();
		return false;
	}		

	if ((document.searchCar.date_dep0.value == '') || (document.searchCar.date_dep0.value == 'yyyy-mm-dd'))
	{
		alert(document.getElementById('msg_enter_checkin_date').value);
		document.searchCar.date_dep0.focus();
		return false;
	}		

	if(document.searchCar.gateway_dep.value==0)
	{
		alert('Select a Drop up location -- Seleccione lugar de Entrega');
		document.searchCar.gateway_dep.focus();
		return false;
	}				
	
	if ((document.searchCar.date_dep1.value=='') || (document.searchCar.date_dep1.value == 'yyyy-mm-dd'))
	{
		alert(document.getElementById('msg_enter_checkout_date').value);
		document.searchCar.date_dep1.focus();
		return false;
	}	

	det_date = document.searchCar.date_dep0.value.split('-');			
	det_date1 = document.searchCar.date_dep1.value.split('-');	
	document.searchCar.date_dep.value = det_date[0] + det_date[1] + det_date[2];
	document.searchCar.date_ret.value = det_date1[0] + det_date1[1] + det_date1[2];
	
	// Validar las fechas
	var date_dat = new Array();
	date_dat = document.searchCar.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 = document.searchCar.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();
	document.searchCar.duration.value = (Date2 - Date1)/86400000;	
	// 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);
		document.searchCar.date_dep0.focus();
		return false;
	}
 	if(((Date1 - Today)/86400000) < 3) 
	{
		alert(document.getElementById('msg_acept_cars_request').value);
		document.searchCar.date_dep0.focus();
		return false;
	}
	if(Date1 > Date2)
	{
		alert(document.getElementById('msg_checkout_after_checkin').value);
		document.searchCar.date_dep1.focus();
		return false;
	}
	return true;
}

function SelectPickUpDropOff(dropdown)
{
	if (dropdown.name == 'gateway_dep')
	{
		document.searchCar.dest_dep.selectedIndex = document.searchCar.gateway_dep.selectedIndex;		
	}
	else
	{
		if (dropdown.selectedIndex==0)
		{
			document.searchCar.gateway_dep.selectedIndex = dropdown.selectedIndex;
		}
	}
}

function SubmitCurrentCar(carindex, codtrans, codcat, codauto)
{
	pickupcombo = document.getElementById('pickuplocation_'+carindex);
	dropoffcombo = document.getElementById('dropofflocation_'+carindex);
	
	document.getElementById('pickuplocation').value = pickupcombo.options[pickupcombo.selectedIndex].value ;
	document.getElementById('dropofflocation').value = dropoffcombo.options[dropoffcombo.selectedIndex].value ;
	if (document.getElementById('pickuplocation').value == 0)
	{		
		alert(document.getElementById('msg_please_select_pickup_place').value);
		pickupcombo.focus();
		return false;
	}
	if (document.getElementById('dropofflocation').value == 0)
	{		
		alert(document.getElementById('msg_please_select_dropoff_place').value);
		dropoffcombo.focus();
		return false;
	}
	document.getElementById('codtrans').value = codtrans;
	document.getElementById('codcat').value = codcat;
	document.getElementById('codauto').value = codauto;
	document.getElementById('purchase_car_form').submit();
}
