// JavaScript Document
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;
}
//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();
}
//---------------------------------------------------------------------------------------------------------------------
//CUSTOM DDL FUNCTIONS
//gets postion
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;
	if(testIE6())
		l += 3;
	ddl.style.top = t +"px";
	ddl.style.left = l +"px";
}
//expands ddl - shows options div
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";
	}
}
//selects clicked option, closes options div
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;
}
//updates position for specified ddl by its prefix
function updatePos(itemPrefix)
{
	findPos(document.getElementById('div_'+itemPrefix+'_first'),document.getElementById('div_'+itemPrefix+'_list'));
}
//home ddl - updates destination ddl postion
function updatePositions()
{
	updatePos('dest');
}
//flights ddls - updates positions for destinations, companies, tolerance, day ddls
function updatePositions_flights()
{
	updatePos('dest');
	updatePos('comp');
	updatePos('tol');
	updatePos('day');
}
//----------------------------------------------------------------------------------------------------------------
//news scrolling
var cntNews = 1;
function scrollNews()
{
	if(cntNews<noNews)
	{
		var newTop =  -65 * cntNews;
		cntNews++;
	}
	else
	{
		var newTop =  0;
		cntNews = 0;
	}
	var cmd = "document.getElementById('div_news').style.top = '"+newTop+"px';scrollNews();";
	setTimeout(cmd,3000);
}
//validation of home search form
function validate_search(err_text_criteria,err_text_date)
{
	var valDest = document.getElementById('hidden_select_dest').value;
	var valDate = document.getElementById('input_date').value;
	if((valDate=="")&&(valDest==0))
	{
		document.getElementById('td_err_search').innerHTML=err_text_criteria;
		document.getElementById('div_err_search').style.display='block';
		return false;
	}
	if(!check_date(document.getElementById('input_date').value))
	{
		document.getElementById('td_err_search').innerHTML=err_text_date;
		document.getElementById('div_err_search').style.display='block';
		return false;
	}
	return true;
}
//validation of flights search form
function validate_flights(err_text_criteria,err_text_date)
{
	var valDest = document.getElementById('hidden_select_dest').value;
	var valDate = document.getElementById('input_date').value;
	var valComp = document.getElementById('hidden_select_comp').value;
	var valTol = document.getElementById('hidden_select_tol').value;
	var valDay = document.getElementById('hidden_select_day').value;
	if((valDate=="")&&(valDest==0)&&(valComp==0)&&(valDay==0))
	{
		document.getElementById('td_err_search').innerHTML=err_text_criteria;
		document.getElementById('div_err_search').style.display='block';
		return false;
	}
	if(!check_date(document.getElementById('input_date').value))
	{
		document.getElementById('td_err_search').innerHTML=err_text_date;
		document.getElementById('div_err_search').style.display='block';
		return false;
	}
	return true;
}
//reset error
function resetErr_search()
{
	document.getElementById('td_err_search').innerHTML='';
	document.getElementById('div_err_search').style.display='none';
}
//sets bullet of submenu item
function menuBullet(type,pgURL)
{
	var bullet = document.getElementById('bullet_'+pgURL);
	if(type==1)
		bullet.src = "images/menu_bullet_sel.jpg";
	else
		bullet.src = "images/menu_bullet.jpg";
}
//display swf object in specified parent div
function displayFlash(parentDiv,flashFile,imgFile,width,height)
{
	var so = new SWFObject(flashFile, "", width, height, "8", "#FFFFFF");
	so.write(parentDiv);
	if(document.getElementById(parentDiv).innerHTML=="")
		document.getElementById(parentDiv).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;
}
