// JavaScript Document
var airport_locations = new Array();
airport_locations[1]=0; 
airport_locations[2]=1;
airport_locations[3]=0;
airport_locations[4]=0;
airport_locations[5]=1;
airport_locations[6]=0;
airport_locations[7]=0;
airport_locations[8]=1;
airport_locations[9]=1;
airport_locations[10]=0;
airport_locations[11]=0;
airport_locations[12]=0;
airport_locations[13]=0;
airport_locations[14]=0;
airport_locations[15]=0;
airport_locations[16]=0;
airport_locations[17]=0;
airport_locations[18]=0; 
airport_locations[19]=1;
airport_locations[20]=0;
airport_locations[21]=0;
airport_locations[22]=0;
airport_locations[23]=0;
airport_locations[24]=0;
airport_locations[25]=0;
airport_locations[26]=0;
airport_locations[27]=1;
airport_locations[28]=0;
airport_locations[29]=0;
//eliminates white spaces from a string
function trim(str)
{  
	while(str.charAt(0) == (" ") )
		str = str.substring(1);
  	while(str.charAt(str.length-1) == " " )
		str = str.substring(0,str.length-1);
  return str;
}

//validates an email address
function check_mail(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
	   return false
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	   return false
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false
	 if (str.indexOf(at,(lat+1))!=-1)
		return false
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false
	 if (str.indexOf(dot,(lat+2))==-1)
		return false
	 if (str.indexOf(" ")!=-1)
		return false
	 return true					
}

//validate a date
function check_date(input)
{
	ok = 1;
   	if(input.value != "")
	{
		var testString = input.value.split("/");
  		var dayfield=input.value.split("/")[0];
		var monthfield= input.value.split("/")[1];   
	 	var yearfield= input.value.split("/")[2];
		if(testString.length > 1)
		{
			if (monthfield.length <1 || monthfield.length >2 || dayfield.length < 1 || dayfield.length >2 || yearfield.length > 4  || yearfield.length < 2)
			{
			   ok = 2;
			   input.select();
			}
			else
			{ 
			 	//Detailed check for valid date ranges
				var dayobj = new Date(yearfield, monthfield-1, dayfield)
				if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getYear()!=yearfield && dayobj.getFullYear() !=yearfield))
				{
				   ok = 2;
				   input.select();
				}
			}
		}
		else
		   ok = 2;
   }
	if (ok ==2) 
		validation=false;
	else
		validation = true;
	return validation;
}

//checks if file extention is valid as photo
function check_photoExtension(con)
{
    if(con.length >0)
    {
        var fileExtension = con.substring(con.lastIndexOf('.')+1,con.length);
        extensionValid =true;
        fileExtension = fileExtension.toUpperCase();
        if( fileExtension != "BMP" && fileExtension != "GIF" && fileExtension != "JPEG" && fileExtension != "JPG" )
            return false;	         				
   	}
	return true;
}

//checks if file extention is valid as file:doc,docx,xls,xlsx,ppt,pptx,txt,jpg,jpeg,pdf
function check_fileExtension(con)
{
    if(con.length >0)
    {
        var fileExtension = con.substring(con.lastIndexOf('.')+1,con.length);
        extensionValid =true;
        fileExtension = fileExtension.toUpperCase();
        if( fileExtension != "DOC" && fileExtension != "DOCX" && fileExtension != "PDF" )
            return false;	         				
   	}
	return true;
}

//checks if URL is valid
function check_url(s) 
{
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}

//checks if phone number is valid
function check_phoneNo(str)
{
	str=str.replace("/","");
	str=str.replace("+","");
	str=str.replace(".","");
	if (!IsNumeric(str))
		return false;
	else
		if(str=="")
			return false;
		else
			if(str.length<6)
				return false;
			else
				return true;
}

//checks if string is number
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

//validate CNP
function check_CNP(str)
{
	/*
	if(str.lenght<13)	
		return false;
	if((str[0]!=1)&&(str[0]!=2))
		return false;
	if(!IsNumeric(str))
		return false;
	var date = str[5]+str[6]+"/"+str[3]+str[4]+"/"+str[1]+str[2];
	if(!check_date_str(date))
		return false;
	*/
	return true;
}

//submit form for admin adding
function submitAddForm()
{
	if(validate_form()==true)
		document.getElementById('add_form').submit();
}

//Alert confirmation
function conf(strAlert)
{
	return confirm(strAlert);
}

//Create js date object from date,time string
function formatDateJS(strInput,strTime)
{
	var myDate = new Date;
	myDate = Date.UTC(strInput.split("/")[2]*1,strInput.split("/")[1]*1,strInput.split("/")[0]*1,strTime*1,0,0);
	return myDate;
}

//Check if date is valid - input as string
function check_date_str(inputStr)
{
	ok = 1;
   	if(inputStr != "")
	{
		var testString = inputStr.split("/");
  		var dayfield=inputStr.split("/")[0];
		var monthfield= inputStr.split("/")[1];   
	 	var yearfield= inputStr.split("/")[2];
		if(testString.length > 1)
		{
			if (monthfield.length <1 || monthfield.length >2 || dayfield.length < 1 || dayfield.length >2 || yearfield.length > 4  || yearfield.length < 2)
			{
			   ok = 2;
			}
			else
			{ 
			 	//Detailed check for valid date ranges
				var dayobj = new Date(yearfield, monthfield-1, dayfield)
				if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getYear()!=yearfield && dayobj.getFullYear() !=yearfield))
				{
				   ok = 2;
				}
			}
		}
		else
		   ok = 2;
   }
	if (ok ==2) 
		validation=false;
	else
		validation = true;
	return validation;
}

//Check if browser is IE -> no shadow(png)
function testIE6()
{
	var browserName=navigator.appName; 
	var agt=navigator.userAgent.toLowerCase();
	if (browserName=="Microsoft Internet Explorer")
	{
	  if((agt.indexOf("msie 6.")!=-1))
	  {
	  	return true;
	  }
	}
	return false;
}

//Car details page - change main photo when clicking thumbnail
function changePhoto(filename)
{
	document.getElementById('img_large').src="upload/photos/"+filename;
}

//Car details page - Calculate price for selected days
function calculate(sel)
{
	var totalDiv = document.getElementById('div_total');
	if((sel>=1)&&(sel<=2))	
		totalDiv.innerHTML = prices[1]*sel;
	if((sel>=3)&&(sel<=5))	
		totalDiv.innerHTML = prices[2]*sel;
	if((sel>=6)&&(sel<=10))	
		totalDiv.innerHTML = prices[3]*sel;
	if((sel>=11)&&(sel<=15))	
		totalDiv.innerHTML = prices[4]*sel;
	if((sel>=16)&&(sel<=30))	
		totalDiv.innerHTML = prices[5]*sel;
	if(sel==0)
		totalDiv.innerHTML = "---";
}

//Reservation form - display price list when car type is selected
function displayPrice(carId)
{
	if(carId!=0)
	{
		price_values = prices[carId].split(";");
		document.getElementById('div_price1').innerHTML = '<strong>'+price_values[0]+'</strong>';
		document.getElementById('div_price2').innerHTML = '<strong>'+price_values[1]+'</strong>';
		document.getElementById('div_price3').innerHTML = '<strong>'+price_values[2]+'</strong>';
		document.getElementById('div_price4').innerHTML = '<strong>'+price_values[3]+'</strong>';
		document.getElementById('div_price5').innerHTML = '<strong>'+price_values[4]+'</strong>';
		var divError = document.getElementById('div_error');
		var divErrorHolder = document.getElementById('div_error_holder');
		var imgCar = document.getElementById('img_car');
		divError.innerHTML = '';
		divErrorHolder.style.display = 'none';
		imgCar.src = "upload/photos/thumbs/"+photos[carId];
		document.getElementById('div_price').style.display = 'block';
	}
	else
	{
		document.getElementById('div_price').style.display = 'none';
	}
}

//Reservation form - Show Airport CB if present
function location_change(type,dumb)
{
	
	if(type==0)
	{
		//pickup
		var dv = document.getElementById('div_cb_airport_pickup');
		var L = document.getElementById('select_location_pickup');
		var cb = document.getElementById('cb_airport_pickup');
		var A = document.getElementById('input_address_pickup');
		var city = document.getElementById('div_city_pickup');
		var city_input = document.getElementById('input_city_pickup');
	}
	else
	{
		//dropoff
		var dv = document.getElementById('div_cb_airport_dropoff');
		var L = document.getElementById('select_location_dropoff');
		var cb = document.getElementById('cb_airport_dropoff');
		var A = document.getElementById('input_address_dropoff');
		var city = document.getElementById('div_city_dropoff');
		var city_input = document.getElementById('input_city_dropoff');
	}
	if(airport_locations[L.selectedIndex]==1)
	{
		dv.style.display='block';
		cb.checked=false;
	}
	else
	{
		dv.style.display='none';
		cb.checked=false;
	}
	if(L.selectedIndex!=27)
		A.value="";
	else
		A.value = adrTM;
	if(L.selectedIndex!=85)
	{
		city_input.value="";
		city.style.display="none";
	}
	else
	{
		city.style.display="block";
		city_input.value="";
	}
}

//Reservation form - Clear address when airport is checked
function airport_checked(type)
{
	
	if(type==0)
	{
		//pickup
		var A = document.getElementById('input_address_pickup');
		var cb = document.getElementById('cb_airport_pickup');
	}
	else
	{
		//dropoff
		var A = document.getElementById('input_address_dropoff');
		var cb = document.getElementById('cb_airport_dropoff');
	}
	if(cb.checked==true)
		A.value="";
}

//Reservation form - Validation for price calculation (without alerts)
function validate_reservation_simple()
{
	//Car type
	if(document.getElementById('select_car').value=="0")
		return false;
	//Dates
	var date_pickup;
	date_pickup=document.getElementById("select_day_pickup").options[document.getElementById("select_day_pickup").selectedIndex].value;
	date_pickup+="/";
	date_pickup+=document.getElementById("select_month_pickup").options[document.getElementById("select_month_pickup").selectedIndex].value;
	date_pickup+="/";
	date_pickup+=document.getElementById("select_year_pickup").options[document.getElementById("select_year_pickup").selectedIndex].value;
	if(check_date_str(date_pickup)==false)
		return false;
	var date_dropoff;
	date_dropoff=document.getElementById("select_day_dropoff").options[document.getElementById("select_day_dropoff").selectedIndex].value;
	date_dropoff+="/";
	date_dropoff+=document.getElementById("select_month_dropoff").options[document.getElementById("select_month_dropoff").selectedIndex].value;
	date_dropoff+="/";
	date_dropoff+=document.getElementById("select_year_dropoff").options[document.getElementById("select_year_dropoff").selectedIndex].value;
	if(!check_date_str(date_dropoff))
		return false;
	var l_dt = new Date();
	
	var today = Date.UTC( l_dt.getFullYear(), l_dt.getMonth()+1, l_dt.getDate(),l_dt.getHours(),l_dt.getMinutes(),0);
	time_pickup=document.getElementById("select_time_pickup").options[document.getElementById("select_time_pickup").selectedIndex].value;
	time_dropoff=document.getElementById("select_time_dropoff").options[document.getElementById("select_time_dropoff").selectedIndex].value;
	/*
	if(today>=formatDateJS(date_pickup,time_pickup))
		return false;
	if(today>=formatDateJS(date_dropoff,time_dropoff))
		return false;
	*/
	if(formatDateJS(date_pickup,time_pickup)>=formatDateJS(date_dropoff,time_dropoff))
		return false;
	//Location
	other_location_pickup=document.getElementById("select_location_pickup").options[document.getElementById("select_location_pickup").selectedIndex].value;
	other_location_dropoff=document.getElementById("select_location_dropoff").options[document.getElementById("select_location_dropoff").selectedIndex].value;
	city_location_pickup = document.getElementById("input_city_pickup").value;
	city_location_dropoff = document.getElementById("input_city_dropoff").value;
	if((other_location_pickup==0)||((other_location_pickup==85)&&(city_location_pickup=="")))
		return false;
	if((other_location_dropoff==0)||((other_location_dropoff==85)&&(city_location_dropoff=="")))
		return false;
	return true;
}

//Check/uncheck checkbox for an extra when clicking label + recalculate total price
function check_extras(no)
{
	if(document.getElementById("cb_extras"+no).checked  == true)
		document.getElementById("cb_extras"+no).checked  = false;
	else
		document.getElementById("cb_extras"+no).checked  = true;
	calculate_price();
}
//Hide CNP for countries diferent from Romania
function change_country(ind)
{
	if(ind!=201)
		document.getElementById('tr_cnp').style.display = 'none';
	else
	{
		if(testIE6())
			document.getElementById('tr_cnp').style.display = 'block';
		else
			document.getElementById('tr_cnp').style.display = 'table-row';
	}
}
//Submit login form
function submit_login()
{
	var ok=1;
	var dvError = document.getElementById('div_error_login');
	var iUser = document.getElementById('input_user');
	var iPass = document.getElementById('input_pass');
	iUser.value=trim(iUser.value);
	iPass.value=trim(iPass.value);
	if(iUser.value=="")
	{
		ok=0;
		dvError.style.display = "block";
		dvError.innerHTML = requiredUser;
	}
	else
	{
		if(iPass.value=="")
		{
			ok=0;
			dvError.style.display = "block";
			dvError.innerHTML = requiredPass;
		}
	}
	if(ok==1)
		document.getElementById('form_login').submit();
}
function menuOver(obj,no,crt,lg)
{
	if(no!=crt)
		obj.src = "images/"+lg+"/menu_sel_0"+no+".jpg";
}
function menuOut(obj,no,crt,lg)
{
	if(no!=crt)
		obj.src = "images/"+lg+"/menu_0"+no+".jpg";
}