// JavaScript Document

// Open External Links as Blank Targets via Unobtrusive JavaScript
// http://perishablepress.com/press/2007/11/20/open-external-links-as-blank-targets-via-unobtrusive-javascript/

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (
			anchor.getAttribute("href") && ( 
			anchor.getAttribute("rel") == "external" || 
			anchor.getAttribute("rel") == "external nofollow" || 
			anchor.getAttribute("rel") == "nofollow external" )
			)
		anchor.target = "_blank";
	}
}
window.onload = function() {
	externalLinks();
}


function check_form(formid)
{
	var temp;
	
	clear_errors();

	temp = isDate(document.getElementById('Arival_date').value,"Date");
	if(temp == false)
	{
		show_error('Arival_date');
		return false;
	}
	document.getElementById('start').value=temp;
	temp = isDate(document.getElementById('Departure_date').value,"Date");
	if(temp == false)
	{
		show_error('Departure_date');
		return false;
	}		
	document.getElementById('stop').value=temp;
	
	temp = date_comparison(document.getElementById('Arival_date').value, document.getElementById('Departure_date').value);
	if (temp != 2)
	{
		show_error('Departure_date');
		show_error('Arival_date');
		alert("Departure date should be after Arrival date.");
		return false;
	}	
		if (document.getElementById('name').value=="")
	{
		show_error('name');
		alert("Please fill in your name.");
		return false;
	}
	
	if (document.getElementById('surname').value=="")
	{
		show_error('surname');
		alert("Please fill in your surname.");
		return false;
	}
	
/*
	
		if (document.getElementById('telephone').value=="")
	{
		show_error('telephone');
		alert("Please fill in a telephone number.");
		return false;
	}
	*/
	
	
	if (document.getElementById('email').value=="")
	{
		show_error('email');
		alert("Please fill in your E-mail.");
		return false;
	}
	if (!checkEmail('email'))
	{
		show_error('email');
		alert ("Your email address is not valid.");
		return false;
	}
	
	document.getElementById(formid).submit();
}


function check_bookonline(form_id) 
{
                clear_errors(0);
                
                var startdate=check_date(document.getElementById('checkin'));
                if(startdate == false)
                {
                                show_error('checkin');
                                alert("Check - in date should be a date");
                                return false;
                }

                var stopdate=check_date(document.getElementById('checkout'));
                if(stopdate == false) 
                {
                                show_error('checkout');
                                alert("Check - out date should be a date");
                                return false;
                }
                
                //document.getElementById('checkin').value=startdate;
                //document.getElementById('checkout').value=stopdate;
                
                if (date_comparison(startdate, stopdate)==1)   
                {
                                alert("Departure date should be after Arrival day!");
                                return false;
                }
                
                var nights = sub_dates(startdate, stopdate);
                
                document.getElementById('nights').value = nights;

                document.getElementById(form_id).submit();
}


function date_comparison(date1, date2)
{                              
                dt1 = date1.split("-");
                dt2 = date2.split("-");
                
                if ( (dt1[0]==dt2[0]) && (dt1[1]==dt2[1]) && (dt1[2]==dt2[2]) )  //an idies imerominies
                                return 0;              //equal
                if (dt1[2] > dt2[2])
                                return 1;
                else if (dt1[2] < dt2[2])
                                return 2;
                else        //idia eti
                {
                                if (dt1[1] > dt2[1])
                                                return 1;
                                else if (dt1[1] < dt2[1])
                                                return 2;
                                else        //idioi mines (kai idia eti)
                                {
                                                if (dt1[0] > dt2[0])
                                                                return 1;
                                                else
                                                                return 2;
                                }
                }
                
                return -1;
}
                
function sub_dates(date1, date2)
{
                var date1_arr = date1.split("-");
                var date2_arr = date2.split("-");
                
                var dt1 = new Date(date1_arr[2], date1_arr[1]-1, date1_arr[0]);               //Month is 0-11 in JavaScript
                var dt2 = new Date(date2_arr[2], date2_arr[1]-1, date2_arr[0]);               //Month is 0-11 in JavaScript
                
                //Set 1 day in milliseconds
                var one_day=1000*60*60*24
                
                var res = Math.ceil( (dt2.getTime()-dt1.getTime())/(one_day) );
                
                return (res);
}


function check_date(str)
{
                months=new Array();
                months[1]=31;months[2]=28;months[3]=31;months[4]=30;
                months[5]=31;months[6]=30;months[7]=31;months[8]=31;
                months[9]=30;months[10]=31;months[11]=30;months[12]=31;
                
                //message="Unacceptable date format (dd-mm-yyyy)";
                
                var field = str.value;
                
                while (field.indexOf('/')>=0)
                                field = field.replace('/', "-");
                                                                                                
                //var field = str.value;
                //str.value =field;
                
                if (str.value=="")
                                return(false);
                else
                {
                                comp=field.split('-');

                                if ( (field.indexOf('-')<0) || (comp.length!=3) || (isNaN(comp[2])) || (isNaN(comp[1])) || (isNaN(comp[0])) || (comp[1]<1)||(comp[1]>12) )
                                                return(false);
                                
                                if (comp[2].length==2)
                                                comp[2] = "20" + comp[2];
                                                
                                if (months[comp[1]]<comp[0])
                                {
                                                 
                                                if ((comp[1]==2)&&(comp[0]==29))
                                                {  
                                                                if ( (comp[2]%400==0) || ( (comp[2]%4==0) && (comp[2]%100!=0) ) ) //->disekto
                                                                                return(comp[0] + "-" + comp[1] + "-" + comp[2]);
                                                }
                                                return(false);
                                }
                                if (comp[2].length!=4)
                                                return(false);
                
                                if ((comp[2]<2009)||(comp[2]>2040)){
                                                alert("Year is out of range: should be between 2009 and 20240");
                                                return(false);
                                }
                                
                                return(comp[0] + "-" + comp[1] + "-" + comp[2] );
                                
                }
}




function show_error(id)
{
	document.getElementById(id).style.border="solid 2px #ffffff";
}

function clear_errors(which)
{
	for (i=0;i<document.forms[which].elements.length;i++)
	{
		if ( (document.forms[which].elements[i].type != "button") && (document.forms[which].elements[i].type != "reset") && (document.forms[which].elements[i].id!="fieldset") )
			document.forms[which].elements[i].style.border="solid 0px #000000";
	}
}


function checkEmail(elem)
{
   var field = document.getElementById(elem);
   var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
   if (field.value.length ==0)
		return false;

   if(field.value.match(emailExp))
		   return true;
   else 
		   return false;
   
}


function check_form(formid, whichform, mandatory_fields, numericfields, emailid)
{
	clear_errors(whichform);
	
	//check mandatory fields
	var man = mandatory_fields.split(",");
	for (i=0;i<man.length;i++)
	{
		if (document.getElementById(man[i]).value == "")
		{
			show_error(man[i]);
			alert ("You must fill all the mandatory fields (*)");
			return false;
		}
	}		

	if (emailid!="")
	{
		if (!checkEmail(emailid))
		{
			show_error(emailid);
			alert ("Your email address is not valid.");
			return false;
		}
	}
	
	//check numeric fields
	if (numericfields != "")
	{
		var numeric = numericfields.split(",");
		for (i=0;i<numeric.length;i++)
		{
			document.getElementById(numeric[i]).value = document.getElementById(numeric[i]).value.replace(/,/,".");
			
			if (isNaN (document.getElementById(numeric[i]).value))
			{
				show_error(numeric[i]);
				alert (numeric[i]+": should be a number. Plase check and try again!");
				return false;
			}
		}
	}
	
	document.getElementById(formid).submit();
}



/* ===== */




/* ==== */

function click_clear(id, text)
{
	if (document.getElementById(id).value == text)
		document.getElementById(id).value = "";
}

function prefill(id, text)
{
	if (document.getElementById(id).value == "")
		document.getElementById(id).value = text;
}


function login_check()
{
	if ( (document.getElementById('name').value == "Username") || (document.getElementById('login_password').value == "Password") )
	{
		alert("You must fill in your username and password in order to login.");
		return false;
	}

	document.getElementById('form_login').submit();
}


function window_width() 
{
	  var myWidth = 0;
	  if( typeof( window.innerWidth ) == 'number' ) 
	  {
		//Non-IE
		myWidth = window.innerWidth;
	  } 
	  else if ( (document.documentElement) && (document.documentElement.clientWidth) ) 
	  {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	  } 
	  else if( (document.body) && (document.body.clientWidth) ) 
	  {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
	  }	  	  

	  return(myWidth);
}

function window_height()
{
	 var myHeight = 0;
	  if( typeof( window.innerHeight ) == 'number' ) 
	  {
		//Non-IE
		myHeight = window.innerHeight;
	  } 
	  else if ( (document.documentElement) && (document.documentElement.clientHeight) ) 
	  {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	  } 
	  else if( document.body && (document.body.clientHeight) ) 
	  {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	  }
	  
	  return(myHeight);
}




function resize()
{
	var imgid = 'bg_img';
	
	var browserheight = window_height();
	var browserwidth = window_width();
	
	if (parseInt(navigator.appVersion)>3) 
	{
		/*if (navigator.appName=="Netscape") 
		{
			browserwidth = browserwidth-20;
		}*/
		if (navigator.appName.indexOf("Microsoft")!=-1) 
		{
			browserheight = browserheight+14;
		}
	}

	document.getElementById(imgid).style.height = browserheight+"px";
	document.getElementById(imgid).style.width = browserwidth+"px";

}




/* ===== */

function showhide_intro(action) 
{		
	if (action == 1)	//html
	{
		document.getElementById('container_holder').style.display = "block";
		document.getElementById('gallery_prev').style.display = "block";
		document.getElementById('jsintro').style.display = "none";	
		document.location.href = "#nointro";
	}
	/*else if (action == 2)	//intro
	{
		document.getElementById('container').style.display = "none";
		document.getElementById('gallerybg').style.display = "none";
		document.getElementById('fullscreenintro').style.display = "block";
	}*/
}
