/*
	javascript development
	////////////////////////////////////////////////////////
	website 	: 	www.mykonosblu.com
	date 		: 	29/7/2008		
	author 		: 	mayra metaxa / developer
	company		: 	mozaik creative business solutions
	url			:	http://www.mozaik.com
*/

function show_hide(id)
{
	if (document.getElementById(id).style.display=="none")
	{
		show(id);
		document.getElementById('chk_'+id).checked=true;
	}
	else
	{
		hide(id);
		document.getElementById('chk_'+id).checked=false;
	}
}


function show(id)
{
		document.getElementById(id).style.display = "block";
}

function hide(id)
{
		document.getElementById(id).style.display = "none";
}


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_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)";
		
		//field = str.value.replace(///g,"-");
		var field = str.value;
		//str.value =field;
		
		if (str.value=="")
		{
			//alert(message);
			return(-1);
		}
		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) )
			{
				//alert(message);
				return(-1);
			}
			
			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]);
					}
				}
				//alert(message);
				return(-1);
			}
			if (comp[2].length!=4){
				//alert(message);
				return(-1);
			}
			if ((comp[2]<2008)||(comp[2]>2029)){
				alert("Year is out of range: should be between 2008 and 2029");
				return(-1);
			}
			
			return(comp[0] + "-" + comp[1] + "-" + comp[2] );
			
		}
}

function check_time(elem)
{
		//message="Unacceptable time format (hh:mm - eg 14:30)";

		field = document.getElementById(elem).value;
		comp=field.split(':');

		if ( (field.indexOf(':')<0) || (comp.length!=2) || (isNaN(comp[0])) || (isNaN(comp[1])) || (comp[0]<0)||(comp[0]>24) || (comp[1]<0)||(comp[1]>59))
		{
			//alert(message);
			return(-1);
		}
		
		return(1);	
}

function validate_form(form_id)
{
	clear_errors();
	
	if (document.getElementById('firstname').value=="")
	{
		show_error('firstname');
		alert("Please fill in your First name.");
		return false;
	}
	if (document.getElementById('lastname').value=="")
	{
		show_error('lastname');
		alert("Please fill in your Last name.");
		return false;
	}
	if (document.getElementById('phone').value=="")
	{
		show_error('phone');
		alert("Please fill in your Phone.");
		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;
	}
	if (check_time('besttime') == -1)
	{	
		show_error('besttime');
		alert ("Unacceptable time format for Preffered contact method (hh:mm).");
		return false;
	}
	if (check_date(document.getElementById('temp_start')) == -1)
	{	
		show_error('temp_start');
		alert ("Unacceptable date format for Arrival Date (dd-mm-yyyy).");
		return false;
	}
	if (document.getElementById('resnum').value=="")
	{
		show_error('resnum');
		alert("You must first fill your Reservation Number.");
		return false;
	}
	
	if (check_manadatory_blocks())
		document.getElementById(form_id).submit();
		
	if(document.getElementById('securitycode').value.length<=0) {
		show_error('securitycode');
		alert("Please type the Security Code.");
		return false;
	}
}


function check_manadatory_blocks()
{
	if (document.getElementById('chk_airport').checked)
	{
		if (check_time('arrival_time') == -1)
		{
			show_error('arrival_time');
			alert ("Unacceptable time format for arrival_time in 'Transportation' section (dd-mm-yyyy).");
			return false;
		}
		if (document.getElementById('flight_number').value=="")
		{
			show_error('flight_number');
			alert ("Flight number in 'Transportation' section must be filled.");
			return false;
		}
	}
	
	if (document.getElementById('chk_car_rentalbox').checked)
	{
		if (document.getElementById('car_rental_pref').value=="")
		{	
			show_error('car_rental_pref');
			alert ("Please indicate your car rental preference in 'Transportation' section (dd-mm-yyyy).");
			return false;
		}
		if ( (isNaN(document.getElementById('carrent_numofp').value)) || (document.getElementById('carrent_numofp').value=="") )
		{
			show_error('carrent_numofp');
			alert ("Flight number in 'Transportation' section should be a number.");
			return false;
		}
	}
	
	if (document.getElementById('chk_tour_guide').checked)
	{
		if ( (isNaN(document.getElementById('tourguide_numofp').value)) || (document.getElementById('tourguide_numofp').value=="") )
		{
			show_error('tourguide_numofp');
			alert ("Number of personsr in 'Transportation - Tour guide' section should be a number.");
			return false;
		}
	}
	
	if (document.getElementById('chk_alacarte_rest').checked)
	{
		if (check_date(document.getElementById('diningdate')) == -1)
		{	
			show_error('diningdate');
			alert ("Unacceptable date format for Dining Date in 'Dining' section (dd-mm-yyyy).");
			return false;
		}
		if (check_time('diningtime') == -1)
		{	
			show_error('diningtime');
			alert ("Unacceptable time format for Dining Time in 'Dining' section (dd-mm-yyyy).");
			return false;
		}
		if ( (isNaN(document.getElementById('diningpeople').value)) || (document.getElementById('diningpeople').value=="") )
		{
			show_error('diningpeople');
			alert ("Number of people in 'Hotel's a la carte restaurant' section should be a number.");
			return false;
		}								 
	}
	
	for (i=1;i<7;i++)
	{
		if (document.getElementById('chk_spec'+i).checked)
		{
			if ( (isNaN(document.getElementById('spec'+i).value)) || (document.getElementById('spec'+i).value=="") )
			{
				show_error('spec'+i);
				alert ("Number of items in 'Room/Villa' section should be a number.");
				return false;
			}
			
		}
	}

	for (i=1;i<20;i++)
	{
		if (document.getElementById('chk_mas'+i).checked)
		{
			if ( (isNaN(document.getElementById('masp'+i).value)) || (document.getElementById('masp'+i).value=="") )
			{
				show_error('masp'+i);
				alert ("Number of people in 'Beauty/Spa Treatments' section should be a number.");
				return false;
			}
			if (check_date(document.getElementById('masd'+i)) == -1)
			{	
				show_error('masd'+i);
				alert ("Unacceptable date format for Preferred Date in 'Beauty/Spa Treatments' section (dd-mm-yyyy).");
				return false;
			}
			if (check_time('mast'+i) == -1)
			{	
				show_error('mast'+i);
				alert ("Unacceptable time format in 'Beauty/Spa Treatments' section (dd-mm-yyyy).");
				return false;
			}
			
		}
	}
	
	return true;

}

function show_error(id)
{
	document.getElementById(id).style.background="yellow";
}

function clear_errors()
{
	for (i=0;i<document.forms[0].elements.length;i++)
	{
		document.forms[0].elements[i].style.background="#ffffff";
	}
}
