// JavaScript Document
//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 != "")
	{
		var testString = input.split("/");
  		var dayfield=input.split("/")[0];
		var monthfield= input.split("/")[1];   
	 	var yearfield= input.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 != "XLS" && fileExtension != "XLSX" && fileExtension != "PPT" && fileExtension != "PPTX" && fileExtension != "TXT" && fileExtension != "PDF" && fileExtension != "JPEG" && fileExtension != "JPG" )
            return false;	         				
   	}
	return true;
}
//checks if import extention is valid as an import file:csv
function check_importExtension(con)
{
    if(con.length >0)
    {
        var fileExtension = con.substring(con.lastIndexOf('.')+1,con.length);
        extensionValid =true;
        fileExtension = fileExtension.toUpperCase();
        if(fileExtension != "CSV")
            return false;	         				
   	}
	return true;
}
//checks if file extention is valid as video
function check_movieExtension(con)
{
    if(con.length >0)
    {
        var fileExtension = con.substring(con.lastIndexOf('.')+1,con.length);
        extensionValid =true;
        fileExtension = fileExtension.toUpperCase();
        if( fileExtension != "SWF")
            return false;	         				
   	}
	return true;
}
//checks if file extention is valid as video
function check_videoExtension(con)
{
    if(con.length >0)
    {
        var fileExtension = con.substring(con.lastIndexOf('.')+1,con.length);
        extensionValid =true;
        fileExtension = fileExtension.toUpperCase();
        if( fileExtension != "AVI" && fileExtension != "MPEG" && fileExtension != "MPG" && fileExtension != "WMV" )
            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(" ","");
	if (!IsNumeric(str))
		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;
}
//checks if string is alfa
function IsAlpha(sText)
{
   var IsAlpha=true;
   var Char;
   for (i = 0; i < sText.length && IsAlpha == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (((Char<'a') && (Char<'A'))||((Char>'z')&&(Char>'Z'))) 
      {
         IsAlpha = false;
      }
   }
   return IsAlpha;
}
//submit form for admin adding
function submitAddForm()
{
	if(validate_form()==true)
		document.getElementById('add_form').submit();
}
//display confirmation with given alert
function conf(strAlert)
{
	return confirm(strAlert);
}
//tests if a given date is the required weekday
function check_weekday(strInput,reqWeekday)
{
	var d=new Date(strInput.split("/")[2]*1,strInput.split("/")[1]*1-1,strInput.split("/")[0]*1,0,0,0);
	if(d.getDay()!=reqWeekday)
		return false;
	return true;
}
//checks if a field is not empty
function field_required(input,err_msg,isFocus)
{
	if(isFocus==1)
		document.getElementById(input).value=trim(document.getElementById(input).value);	
	if(document.getElementById(input).value=="")
	{
		alert(err_msg);
		if(isFocus==1)
			document.getElementById(input).focus();
		return false;
	}
	return true;
}
//checks if a field is valid calling the specific function
function field_validate(input,err_msg,isFocus,checkFunction)
{
	if(isFocus==1)
		document.getElementById(input).value=trim(document.getElementById(input).value);	
	if(document.getElementById(input).value!="")
	{
		checkValue = eval(checkFunction+'(document.getElementById("'+input+'").value)');
		if(checkValue==false)
		{
			alert(err_msg);
			if(isFocus==1)
				document.getElementById(input).focus();
			return false;
		}
	}
	return true;
}
//checks if flight type is set
function type_required(err_msg)
{
	cb1=document.getElementById('cb_type1').checked;
	cb2=document.getElementById('cb_type2').checked;
	cb3=document.getElementById('cb_type3').checked;
	if((!cb1)&&(!cb2)&&(!cb3))
	{
		alert(err_msg);
		return false;
	}
	return true;	
}
//creates 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;
}
//checks if start date is sooner than finish date
function compare_dates(val_start,val_finish,err_msg)
{
	if(formatDateJS(document.getElementById(val_start).value)>=formatDateJS(document.getElementById(val_finish).value))
	{
		alert(err_msg);
		document.getElementById(val_start).focus();
		return false;
	}	
	return true;
}
//sets composed phone value 
function composePhone(input)
{
	document.getElementById(input).value = document.getElementById(input+'_prefix').value+" "+trim(document.getElementById(input+'_provider').value)+" "+trim(document.getElementById(input+'_number').value);
}
//mainatins flights search criteria for each page number
function changePage(newURL)
{
	document.getElementById('search_form').action=newURL;
	document.getElementById('search_form').submit();
}
//news scrolling
var crtNewsPos = 0;
function scrollNews()
{
	var divScroll = document.getElementById('div_news_scroll');
	if(divScroll!=null)
	{
		if(crtNewsPos!=divScroll.offsetHeight)
			crtNewsPos ++;
		else
			crtNewsPos=0;
		divScroll.style.marginTop="-"+crtNewsPos+"px";
		setTimeout('scrollNews()',40);
	}
}
//sets style for mouse over on submenu item
function submenuOver(obj,no,isSel)
{
	if(isSel=="")
	{
		obj.style.color = "#005E83";
		var imgSub = document.getElementById('bullet_submenu'+no);
		if(no!=1)
			imgSub.src = "images/bullet_submenu_sel.jpg";
		else
			imgSub.src = "images/bullet_submenu_sel_first.jpg";
	}
}
function submenuOut(obj,no,isSel)
{
	if(isSel=="")
	{
		obj.style.color = "#005E83";
		var imgSub = document.getElementById('bullet_submenu'+no);
		if(no!=1)
			imgSub.src = "images/bullet_submenu.jpg";
		else
			imgSub.src = "images/bullet_submenu_first.jpg";
	}
}
//display swf object in specified parent div
function displayFlash(parentDiv,flashFile,imgFile,width,height)
{
	var divP = document.getElementById(parentDiv);
	if(divP!=null)
	{
		var so = new SWFObject(flashFile, "", width, height, "8", "#FFFFFF");
		so.addParam("wmode", "transparent");
		so.write(parentDiv);
		if(divP.innerHTML=="")
			divP.innerHTML = "<img src='"+imgFile+"' alt=''/>";
	}
}
//display image
function putImg(path,dim,links)
{
	if(path!="")
	{
		aImg = path.split(";");
		aLinks = links.split(";");
		return "<a href='"+aLinks[0]+"' target='_blank'><img src='upload/banners/"+aImg[0]+"' alt=''/></a>";
	}
	else
		return "<img src='images/flash/banner_empty"+dim+".jpg' alt='' />";
}
//submit common form
function submitForm(id)
{
	document.getElementById(id).submit();
}
function validate_suggestions(err_req_fields,mail_field,err_invalid_field)
{
	//ELMINATE WHITE SPACES
	document.getElementById('input_message').value=trim(document.getElementById('input_message').value);
	document.getElementById('input_name').value=trim(document.getElementById('input_name').value);
	document.getElementById('input_mail').value=trim(document.getElementById('input_mail').value);
	document.getElementById('input_code').value=trim(document.getElementById('input_code').value);
	//REQUIRED FIELDS
	if((document.getElementById('input_message').value=="")||(document.getElementById('input_name').value=="")||(document.getElementById('input_mail').value=="")||(document.getElementById('input_code').value==""))
	{
		document.getElementById('div_error').style.display = "block";
		document.getElementById('div_error').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/exclamation.gif' alt=''/>&nbsp;</td><td>"+err_req_fields+"</td></tr></table>";
		return false;
	}
	//VALIDATE FIELDS
	//valid mail
	if(check_mail(document.getElementById('input_mail').value)==false)
	{
		document.getElementById('div_error').style.display = "block";
		document.getElementById('div_error').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td><img src='images/exclamation.gif' alt=''/>&nbsp;</td><td>"+mail_field+" "+err_invalid_field+"</td></tr></table>";
		document.getElementById('input_mail').focus();
		return false;
	}
	return true;
}
//ajax
function switchFlights(dir)
   {
    $('div_flights_result').update('<img src="images/loading.gif" align="left" alt="Loading" style="padding-top:100px; padding-left:190px;" />');
				new Ajax.Request('front/ajax/switchFlights.php?dir='+dir, {
					  method: 'post',
					  onComplete: function(transport) {
						$('div_flights_result').update(unescape(transport.responseText));
					  }
					  
					});
   }
//ajax
function displayFlights(dir)
   {
	 
	 if(document.getElementById('cb_flight_type1').checked==true)
	 	cbReg = "1";
	else
		cbReg="0";
	 if(document.getElementById('cb_flight_type2').checked==true)
	 	cbCha = "1";
	else
		cbCha="0";
    $('div_flights_list_result').update('<img src="images/loading.gif" align="left" alt="Loading" style="padding-top:100px;padding-bottom:100px; padding-left:240px;" />');
				new Ajax.Request('front/ajax/displayFlights.php?dir='+dir+'&cb1='+cbReg+'&cb2='+cbCha, {
					  method: 'post',
					  onComplete: function(transport) {
						$('div_flights_list_result').update(unescape(transport.responseText));
					  }
					  
					});
   }
function adjustPageHeight()
{
	if(document.getElementById('div_elements')!=null)
	{
		var bannerH = 285;
		var submenuH = document.getElementById('div_submenu').offsetHeight-77;
		if(bannerH>=submenuH)
			maxH = bannerH;
		else
			maxH = submenuH;
		if(document.getElementById('div_elements').offsetHeight<maxH)
			document.getElementById('div_elements').style.height = maxH+"px";
	}
	
}
