//Custom select functions
function findPos(obj,ddl)
{
	var initialObj = obj;
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	var t = curtop + 20;
	var l = curleft;
	ddl.style.top = t +"px";
	ddl.style.left = l +"px";
}

function expandDDL(itemPrefix)
{
	if(document.getElementById('div_'+itemPrefix+'_list').style.visibility=="visible")
	{
		document.getElementById('div_'+itemPrefix+'_list').style.visibility="hidden";
		document.getElementById('div_'+itemPrefix+'_list').style.display="none";
	}
	else
	{
		document.getElementById('div_'+itemPrefix+'_list').style.visibility="visible";
		document.getElementById('div_'+itemPrefix+'_list').style.display="block";
	}
}

function ascundeDDL(itemPrefix)
{
	if(document.getElementById('div_'+itemPrefix+'_list').style.visibility=="visible")
	{
		document.getElementById('div_'+itemPrefix+'_list').style.visibility="hidden";
		document.getElementById('div_'+itemPrefix+'_list').style.display="none";
	}
	
}

function selectDDL(opt,opt_text,itemPrefix)
{
	document.getElementById('div_'+itemPrefix+'_first_text').innerHTML = opt_text;
	document.getElementById('div_'+itemPrefix+'_list').style.visibility="hidden";
	document.getElementById('div_'+itemPrefix+'_list').style.display="none";
	document.getElementById('hidden_select_'+itemPrefix).value = opt;
	document.getElementById('hidden_select_'+itemPrefix+'_opt').value = opt_text;
}
function updatePos(itemPrefix)
{
	findPos(document.getElementById('div_'+itemPrefix+'_first'),document.getElementById('div_'+itemPrefix+'_list'));
}

function updatePositions_flights()
{
	updatePos('tara');
	updatePos('conv_din');
	updatePos('conv_in');
	updatePos('transport');
	updatePos('camera');
	updatePos('categorie');
	updatePos('tip');
	updatePos('regiune');
	updatePos('statiune');
	
	
	
}

//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 != "PNG" && 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<3)
				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;
}

//submit form for admin adding
function submitAddForm()
{
	if(validate_form()==true)
		document.getElementById('add_form').submit();
}

function conf(strAlert)
{
	return confirm(strAlert);
}
//Create js date object from date,time string
function formatDateJS(strInput)
{
	var myDate = new Date;
	myDate = Date.UTC(strInput.split("/")[2]*1,strInput.split("/")[1]*1,strInput.split("/")[0]*1,0,0,0);
	return myDate;
}

function checkSearch()
{
if ((document.getElementById("hidden_select_tara").value==0) && (document.getElementById("hidden_select_regiune").value==0) && (document.getElementById("hidden_select_statiune").value==0) && (document.getElementById("hidden_select_tip").value==0) && (document.getElementById("hidden_select_categorie").value==0) && (document.getElementById("date_search").value=="") && (document.getElementById("hidden_select_camera").value==0) && (document.getElementById("hidden_select_transport").value==0)) 
	{
	alert("Va rugam sa selectati cel putin un criteriu de cautare");
	return false;
	}

if (!check_date(document.getElementById("date_search")))
	{
	alert("Va rugam sa introduceti o data valida");
	return false;
	}
document.getElementById("form_cautare").submit();
return true;
}

function convertor(eur,usd)
{
var val1=0;var val2=0;
eur=eur*1;
usd=usd*1;
document.getElementById("suma").value=document.getElementById("suma").value*1;

if ((document.getElementById("div_conv_din_first_text").innerHTML!="Alege") && (document.getElementById("div_conv_in_first_text").innerHTML!="Alege"))
	{
	if (document.getElementById("div_conv_din_first_text").innerHTML.indexOf("Lei")!=-1) val1=1;
	else if (document.getElementById("div_conv_din_first_text").innerHTML.indexOf("Euro")!=-1) val1=eur;
	else if (document.getElementById("div_conv_din_first_text").innerHTML.indexOf("Dolari")!=-1) val1=usd;
	
	if (document.getElementById("div_conv_in_first_text").innerHTML.indexOf("Lei")!=-1) val2=1;
	else if (document.getElementById("div_conv_in_first_text").innerHTML.indexOf("Euro")!=-1) val2=eur;
	else if (document.getElementById("div_conv_in_first_text").innerHTML.indexOf("Dolari")!=-1) val2=usd;
	if (document.getElementById("suma").value=="") { document.getElementById("suma").focus();alert("Va rugam sa introduceti suma de convertit"); }
		else if (!IsNumeric(document.getElementById("suma").value))	alert("Valoarea introdusa nu este valida");
			else document.getElementById("suma").value=Math.round(document.getElementById("suma").value*val1/val2*1000)/1000;

	}
else alert("Va rugam sa alegeti valuta");
}
	
function validate_contact()
	{
	document.getElementById('input_nume').value=trim(document.getElementById('input_nume').value);
	document.getElementById('input_prenume').value=trim(document.getElementById('input_prenume').value);
	document.getElementById('input_email').value=trim(document.getElementById('input_email').value);
	document.getElementById('input_message').value=trim(document.getElementById('input_message').value);
	//REQUIRED FIELDS
		if((document.getElementById('input_nume').value=="")||(document.getElementById('input_prenume').value=="")||(document.getElementById('input_email').value=="")||(document.getElementById('input_message').value==""))
		{
			document.getElementById('div_error').style.display = "";
			document.getElementById('div_error1').style.display = "";
			document.getElementById('div_error').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/exclamation.gif' alt=''/>&nbsp;</td><td>V&#259; rug&#259;m completa&#355;i toate c&acirc;mpurile marcate cu * !</td></tr></table>";
			return false;
		}
	//VALID FIELDS	
		if(check_mail(document.getElementById('input_email').value)==false)
		{
			document.getElementById('div_error').style.display = "";
			document.getElementById('div_error1').style.display = "";
			document.getElementById('div_error').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/exclamation.gif' alt=''/>&nbsp;</td><td>E-mail nu con&#355;ine un text valid !</td></tr></table>";
			document.getElementById('input_email').focus();
			return false;
		}
	return true;
	}

function display_error(div_id, imag, post_error)
{
document.getElementById(div_id).innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/"+imag+"' alt=''/>&nbsp;</td><td>"+post_error+"</td></tr></table>";
}

function validate_rezervare()
	{
	document.getElementById('input_nume').value=trim(document.getElementById('input_nume').value);
	document.getElementById('input_prenume').value=trim(document.getElementById('input_prenume').value);
	document.getElementById('input_email').value=trim(document.getElementById('input_email').value);
	document.getElementById('input_message').value=trim(document.getElementById('input_message').value);
	document.getElementById('input_phone').value=trim(document.getElementById('input_phone').value);
	//REQUIRED FIELDS
		if((document.getElementById('input_nume').value=="")||(document.getElementById('input_prenume').value=="")||(document.getElementById('input_phone').value=="")||(document.getElementById('input_email').value=="")||(document.getElementById('input_message').value==""))
		{
			document.getElementById('div_error').style.display = "";
			document.getElementById('div_error1').style.display = "";
			document.getElementById('div_error').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/exclamation.gif' alt=''/>&nbsp;</td><td>V&#259; rug&#259;m completa&#355;i toate c&acirc;mpurile marcate cu * !</td></tr></table>";
			return false;
		}
	//VALID FIELDS	
		if(check_mail(document.getElementById('input_email').value)==false)
		{
			document.getElementById('div_error').style.display = "";
			document.getElementById('div_error1').style.display = "";
			document.getElementById('div_error').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/exclamation.gif' alt=''/>&nbsp;</td><td>E-mail nu con&#355;ine un text valid !</td></tr></table>";
			document.getElementById('input_email').focus();
			return false;
		}
		if(check_phoneNo(document.getElementById('input_phone').value)==false)
		{
			document.getElementById('div_error').style.display = "";
			document.getElementById('div_error1').style.display = "";
			document.getElementById('div_error').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/exclamation.gif' alt=''/>&nbsp;</td><td>Telefon nu con&#355;ine un text valid</td></tr></table>";
			document.getElementById('input_phone').focus();
			return false;
		}
	return true;
}

function validate_bilete()
	{
	document.getElementById('input_nume').value=trim(document.getElementById('input_nume').value);
	document.getElementById('input_prenume').value=trim(document.getElementById('input_prenume').value);
	document.getElementById('input_email').value=trim(document.getElementById('input_email').value);
	document.getElementById('input_message').value=trim(document.getElementById('input_message').value);
	document.getElementById('input_tel').value=trim(document.getElementById('input_tel').value);
	document.getElementById('date1').value=trim(document.getElementById('date1').value);
	document.getElementById('date2').value=trim(document.getElementById('date2').value);
	document.getElementById('input_from').value=trim(document.getElementById('input_from').value);
	document.getElementById('input_to').value=trim(document.getElementById('input_to').value);
	//REQUIRED FIELDS
		if((document.getElementById('input_nume').value=="")||(document.getElementById('input_prenume').value=="")||(document.getElementById('date1').value=="")||(document.getElementById('date2').value=="")||(document.getElementById('input_tel').value=="")||(document.getElementById('input_email').value=="")||(document.getElementById('input_message').value==""))
		{
			document.getElementById('div_error').style.display = "";
			document.getElementById('div_error1').style.display = "";
			document.getElementById('div_error').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/exclamation.gif' alt=''/>&nbsp;</td><td>V&#259; rug&#259;m completa&#355;i toate c&acirc;mpurile marcate cu * !</td></tr></table>";
			return false;
		}
	//VALID FIELDS	
		if(check_mail(document.getElementById('input_email').value)==false)
		{
			document.getElementById('div_error').style.display = "";
			document.getElementById('div_error1').style.display = "";
			document.getElementById('div_error').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/exclamation.gif' alt=''/>&nbsp;</td><td>E-mail nu con&#355;ine un text valid !</td></tr></table>";
			document.getElementById('input_email').focus();
			return false;
		}

		if(check_phoneNo(document.getElementById('input_tel').value)==false)
		{
			document.getElementById('div_error').style.display = "";
			document.getElementById('div_error1').style.display = "";
			document.getElementById('div_error').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/exclamation.gif' alt=''/>&nbsp;</td><td>Telefon nu con&#355;ine un text valid</td></tr></table>";
			document.getElementById('input_tel').focus();
			return false;
		}
		if (!check_date(document.getElementById("date1")))
		{
		document.getElementById('div_error').style.display = "";
			document.getElementById('div_error1').style.display = "";
			document.getElementById('div_error').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/exclamation.gif' alt=''/>&nbsp;</td><td>Va rugam sa introduceti o data valida</td></tr></table>";
			document.getElementById('date1').focus();
			return false;
		}
		if (!check_date(document.getElementById("date2")))
		{
		document.getElementById('div_error').style.display = "";
			document.getElementById('div_error1').style.display = "";
			document.getElementById('div_error').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/exclamation.gif' alt=''/>&nbsp;</td><td>Va rugam sa introduceti o data valida</td></tr></table>";
			document.getElementById('date2').focus();
			return false;
		}
		if (document.getElementById("input_valabil").value==0)
		{
		document.getElementById('div_error').style.display = "";
			document.getElementById('div_error1').style.display = "";
			document.getElementById('div_error').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/exclamation.gif' alt=''/>&nbsp;</td><td>Va rugam sa alegeti o perioada de valabilitate</td></tr></table>";
			document.getElementById('input_valabil').focus();
			return false;
		}
		if (document.getElementById("input_tip").value==0)
		{
		document.getElementById('div_error').style.display = "";
			document.getElementById('div_error1').style.display = "";
			document.getElementById('div_error').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/exclamation.gif' alt=''/>&nbsp;</td><td>Va rugam sa alegeti tipul calatoriei</td></tr></table>";
			document.getElementById('input_tip').focus();
			return false;
		}
		if ((document.getElementById("input_senior").value=="") && (document.getElementById("input_senior").value=="") && (document.getElementById("input_senior").value=="") && (document.getElementById("input_senior").value==""))
		{
		document.getElementById('div_error').style.display = "";
			document.getElementById('div_error1').style.display = "";
			document.getElementById('div_error').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/exclamation.gif' alt=''/>&nbsp;</td><td>Va rugam sa alegeti cel putin o persoana pentru care se face rezervarea biletului</td></tr></table>";
			return false;
		}
return true;
}

function displayFlash(parentDiv,flashFile,imgFile,width,height)
{
var so = new SWFObject(flashFile, "", width, height, "8", "#FFFFFF");
so.addParam("wmode","transparent");

so.write(parentDiv);
if(document.getElementById(parentDiv).innerHTML=="")
document.getElementById(parentDiv).innerHTML = "<img src='"+imgFile+"' alt=''/>";
}
