 /**This file holds For Validation js For all the templates
 * @author Anish Malik
 * @version 1.0.0.5
 * @created 5/14/2009
 */

//var sName		= '';
//var eMailEmpty	= '';
//var eMailWrong	= '';
//var sComment	= '';
//var sThank		= '';
var sFirstName  = '';
var sLanguage	= '';
var sCountry	= '';
var sNThank		= '';
var sHThank		= '';
var sPhone		= '';
var sCity		= '';
var sActivity	= '';
var sNameFirm	= '';
var eLanguage	= '';
var recEmailEmpty = '';
var recEmailWrong = '';
var emailThank = '';
//-------------------------------Contact Page------------------------------
//This js function is written for the Contact Page Validation check
function checkCForm()
{
	if($F("sent") == "1")
		return;
	//Name validation
	if ($F("name").search(/^[A-Za-z0-9][A-Za-z0-9 ]{2,50}$/) == -1)
	{
		alert(sName);
		$("name").focus();
		//return false;
	}
	else if ($F("email") == "")		//Enter your Email-id validation
	{
		alert(eMailEmpty);
		$("email").focus();
	}
	else if ($F("email").search(/^[_a-zA-Z0-9-\']+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9]+)*\.[a-zA-Z]{2,5}$/) == -1)	//Enter your valid Email-id validation
	{
		alert(eMailWrong);
		$("email").focus();
	}
	else if ($F("comment") == "")											//Enter comments validation
	{
		alert(sComment);
		$("comment").focus();
	}
	sendMail("comment");
}
//-------------------------------Contact Page Ends----------------------------------
//-------------------------------Email Page------------------------------
//This js function is written for the Contact Page Validation check
function checkEmailForm()
{
	if($F("sent") == "1")
		return;
	//Name validation
	if ($F("name").search(/^[A-Za-z0-9][A-Za-z0-9 ]{2,50}$/) == -1)
	{
		alert(sName);
		$("name").focus();
		return false;
	}
	else if ($F("recemail") == "")		//Enter your Email-id validation
	{
		alert(recEmailEmpty);
		$("recemail").focus();
		return false;
	}
	else if ($F("recemail").search(/^[_a-zA-Z0-9-\']+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9]+)*\.[a-zA-Z]{2,5}$/) == -1)	//Enter your valid Email-id validation
	{
		alert(recEmailWrong);
		$("recemail").focus();
		return false;
	}
	if(document.getElementById('agree').checked==true)
	{
		if ($F("email") == "")		//Enter your Email-id validation
		{
			alert(eMailEmpty);
			$("email").focus();
			return false;
		}
		else if ($F("email").search(/^[_a-zA-Z0-9-\']+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9]+)*\.[a-zA-Z]{2,5}$/) == -1)	//Enter your valid Email-id validation
		{
			alert(eMailWrong);
			$("email").focus();
			return false;
		}
	}
	sendMail("email");
}
//-------------------------------Email Page Ends----------------------------------
//---------------------------------NewsLetter Page----------------------------------
//This function is used  to apply validations on fields and used for news-letter page
function checkForm()
{
	if($F("sent") == "1")
		return;
	
	if ($F("name") == "")							//Check Whether Name is Entered
	{
		alert(sName);
		$("name").focus();
		return false;
	}
	else if ($F("firstname") == "")					//Check Whether First Name is Entered
	{
		alert(sFirstName);
		$("firstname").focus();
		return false;
	}
	else if ($F("country") == "")					//Check Whether Country is Selected
	{
		alert(sCountry);
		$("country").focus();
		return false;
	}
	else if ($F("language") == "")					//Check Whether Language is Selected
	{
		alert(sLanguage);
		$("language").focus();
		return false;
	}
	sendMail("newsletter");
}
//---------------------------------NewsLetter Page Ends-----------------------------
//---------------------------------Hotelier Page -----------------------------------
function checkForm2()
{
	if ($F("firm").search(/^[A-Za-z0-9][A-Za-z0-9 ]{2,50}$/) == -1 || $F("firm")=='')
        {
                alert(sNameFirm);
                $("firm").focus();
                return false;
        }

	if ($F("inf_name").search(/^[A-Za-z0-9][A-Za-z0-9 ]{2,50}$/) == -1 || $F("inf_name")=='')
	{
		alert(sName);
		$("inf_name").focus();
		return false;
	}

	if ($F("inf_email").search(/^[_a-zA-Z0-9-\']+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9]+)*\.[a-zA-Z]{2,5}$/) == -1 || $F("inf_email")=='')
	{
		alert(eMailWrong);
		$("inf_email").focus();
		return false;
	}

	if ($F("inf_comment") == '')
	{
		alert(sComment);
		$("inf_comment").focus();
		return false;
	}

	sendMail("hotelier");
	return true;
}
//This function is used to send mail
function sendMail(formType)
{
	var ibe;
	var params = "page=_sendmail&site=collection";
	//If this is set then only show thank message
	sent_id = "inf_sent";
	if(formType == 'hotelier')
	{
		params+= "&formType="+formType+"&inf_name="+$F("inf_name")+"&inf_comment="+$F("inf_comment")+"&inf_email="+$F("inf_email");
		params+= "&fname="+$F("surname")+"&firm="+$F("firm")+"&activity="+$F("position");
		params+= "&country="+$F("country");
		params+= "&phone="+$F("phone")+"&website="+$F("website");
	}
	else if(formType == 'newsletter')
	{
		params+= "&formType="+formType+"&name="+$F("name")+"&firstname="+$F("firstname")+"&language="+$F("language")+"&email="+$F("email")+"&country="+$F("country")+"&luxury="+$F("luxury")+"&design="+$F("design")+"&tradition="+$F("tradition")+"&castle="+$F("castle")+"&spa="+$F("spa")+"&gastro="+$F("gastro")+"&golf="+$F("golf");
	}
	else if(formType == 'comment')
	{
		params+= "&formType="+formType+"&name="+$F("name")+"&fname="+$F("firstname")+"&comment="+$F("comment")+"&email="+$F("email")+"&userType1="+$F("youare1")+"&userType2="+$F("youare2")+"&userType3="+$F("youare3");
	}
	else if(formType == 'email')
	{
		params+= "&formType="+formType+"&name="+$F("name")+"&fname="+$F("fname")+"&comment="+$F("comment")+"&recemail="+$F("recemail")+"&urlContent="+$F("URL")+"&lg="+$F("LG");
		if(document.getElementById('agree').checked==true)
			params+="&email="+$F("email");
	}
//this Ajax request send it first to anyscript and from there to sendmail file in controller
	new Ajax.Request("../PHP/anyscript.php",
	{
		method: 'post',
		parameters: params,
		asynchronous: false,
		onComplete: showResponse,
		contentType:'application/x-www-form-urlencoded',
		encoding : "UTF-8"
	});
	function showResponse(originalRequest)		//This function will fetch the result from mailing server and display it in popup
	{
		var result = originalRequest.responseText;
		//if want to echo something in sendmail file to check then alert response from here first.
		if(result == "true")
		{
			if(formType == 'hotelier')
			{
				alert(sHThank);
				document.getElementById('button').disabled = true;
			}
			else if(formType == 'newsletter')
					alert(sNThank);
			else if(formType == 'comment')
					alert(sThank);
			else if(formType == 'email')
					alert(emailThank);
		}
	}
	if(formType == 'newsletter')
			window.close();			//This will close the PopUp as soon as mail is sent
}
//---------------------------------Hotelier Page Ends-------------------------------
//---------------------------------Find n book Luxury Hotel Page--------------------
//Function is used to check at least one value before clicking on the view all hotel button
function checkfindForm(showrate)
{
	var bValidate	= false;
	var bDest		= false;
	var bStyle		= false;
	var bTheme		= false;
	$('find_book').value = showrate;
	if($F('destinations') != '0')
	{
		bValidate	= true;
		bDest		= true;
	}
	// check if radio button selected
	var objRadioLuxe		= document.getElementById('style_filter_Luxe');
	var objRadioEl_Tr		= document.getElementById('style_filter_El_Tr');
	var objRadiodsg_Cont	= document.getElementById('style_filter_dsg_Cont');
	var objRadioHou_Cast	= document.getElementById('style_filter_Hou_Cast');
	if(objRadioLuxe.checked || objRadioEl_Tr.checked || objRadiodsg_Cont.checked || objRadioHou_Cast.checked)
	{
			checked		= true;
			bValidate	= true;
			bStyle		= true;
	}
	var objSpa = document.getElementById('theme_filter_Spa');
	var objGolf = document.getElementById('theme_filter_Golf');
	var objGas = document.getElementById('theme_filter_Gastronomy');
	if(objSpa.checked || objGolf.checked || objGas.checked)
	{
		bValidate	= true;
		bTheme		= true;
	}

	if(bValidate == false)
	{
		alert(sSelValidation);
		return false;
	}
	else if(bDest	== true)
	{
		document.forms['ViewAllHotels'].action ='location/hotels_'+$F("destinations")+'.htm';
		document.ViewAllHotels.submit();
		document.ViewAllHotels.submit();
		return true;
	}
	else if(bStyle	== true)
	{
		if(objRadioLuxe.checked)
		document.forms['ViewAllHotels'].action ='hotels_pure_luxury.htm';
		else if(objRadioEl_Tr.checked)
		document.forms['ViewAllHotels'].action ='luxury_hotels_elegance_tradition.htm';
		else if(objRadiodsg_Cont.checked)
		document.forms['ViewAllHotels'].action ='luxury_hotels_design_contemporary.htm';
		else if(objRadioHou_Cast.checked)
		document.forms['ViewAllHotels'].action ='luxury_hotels_mansions_castles.htm';
		document.ViewAllHotels.submit();
		document.ViewAllHotels.submit();
		return true;
	}
	else
	{
		if(objSpa.checked)
		document.forms['ViewAllHotels'].action ='luxury_hotels_spa.htm';
		else if(objGolf.checked)
		document.forms['ViewAllHotels'].action ='luxury_hotels_golf.htm';
		else if(objGas.checked)
		document.forms['ViewAllHotels'].action ='luxury_hotels_gastronomy.htm';

		document.ViewAllHotels.submit();
		document.ViewAllHotels.submit();
		return true;
	}
}
//-------------------------------Find n book Luxury Hotel Page Ends-----------------
//-------------------------------Special Offer Page --------------------------------
//This is the function used to open google map image iframe and close it
function loadIframe()
{
	var myiframe = document.getElementById('google');
	if(myiframe.style.visibility == '')
	{
		myiframe.style.height = 320 + "px";
		myiframe.style.width = 730 + "px";
		myiframe.style.visibility = "visible";
		document.GoogleMapsForm.submit();
	}
	else if(myiframe.style.visibility == 'visible')
	{
		myiframe.style.height = 0 + "px";
		myiframe.style.width = 0 + "px";
		myiframe.style.visibility = "";
	}
}
//-------------------------------Special Offer Page Ends -----------------------------
//-------------------------------Header page E-News-Page opening ---------------------
//This function is called on clicking of the OK Button against E-News
function WindowDetails(sLang)
{
	//This will fetch mailing address Entered if any
	mailVal = document.getElementById('mailingadd');
	mail = mailVal.value;
	if(mail == "Enter your email")
	{
		alert(eMailEmpty);
		mailVal.focus();
		return false;
	}
	//This function verify if mailing address entered is correct
	if (mail.search(/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9]+)*\.[a-zA-Z]{2,5}$/) == -1)
	{
		alert(eMailWrong);
		mailVal.focus();
		return false;
	}
	//Set the Popup URL
	waction = "/"+sLang+"/newsletter.htm?mailid="+mail;
	//Open the NewsLetter Popup
	window.open(waction,"newsletter","width=600,height=550,scrollbars=no");
}
//-------------------------------Header page E-News-Page Ends -----------------------
function replaceInString(src)
{
var newString='';
var retString='';
        for (i=0;i<=src.length;i++)
        {
                if (src.charAt(i)=='/')
                        newString += '-';
                else
                        newString += src.charAt(i);
        }

firstSlash = strpos(newString, '-');
if (firstSlash==2)
{
        retString = newString.substring(6,10)+'-'+newString.substring(3,5)+'-'+newString.substring(0,2);
}
else
        retString = newString;

return retString;
}

function strpos (haystack, needle, offset) {
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}


function moreRooms(TableId, allrooms, lg)
{
        //change URL when rewritting rule is in place
        var attr = "&lg="+lg;
                attr += "&page=_search";
                attr += "&site=collection";
                attr += "&moreRoom=1";
                attr += "&allrooms="+allrooms;
                attr += "&TableId="+TableId;

        var myAjax = new Ajax.Request(
                    '/PHP/anyscript.php',
                    {
                        method: 'post',
                        parameters: attr,
                        asynchronous: true,
                        onComplete: showResponse,
                        contentType:'application/x-www-form-urlencoded',
                        encoding: 'UTF-8'
                    });

        function showResponse(originalRequest)
        {
                var result = originalRequest.responseText;
                $(TableId).update(result);
        }

        var DivID = 'btn_rooms'+TableId;
        if(allrooms)
                $(DivID).innerHTML = "<div id='btn_rooms'><span id='crs_plus-more-rooms'>- </span> <a href='javascript:moreRooms("+'"'+TableId+'"'+", 0,"+ '"' + lg+ '"' +");' title='"+closeLink+"' class='color4'>"+closeLink+"</a></div>";
        else
                $(DivID).innerHTML = "<div id='btn_rooms'<span id='crs_plus-more-rooms'>+ </span><a href='javascript:moreRooms("+'"'+TableId+'"'+", 1,"+ '"' + lg+ '"' +");' title='"+MoreRoomsLink+"' class='color4'>"+MoreRoomsLink+"</a></div>";
}




function updateAutoForm()
{ 
	updString = '?';
	updString += "nbadults="+document.getElementById("nbadults").value;
	updString += "&nbchild="+document.getElementById("nbchild").value;
	updString += "&currency="+document.getElementById("currency").value;
	updString += "&arr_date="+document.getElementById("arrdate2_txt").value;
	updString += "&dep_date="+document.getElementById("depdate2_txt").value;
	updString += "&nbdays="+document.getElementById("nonights").innerHTML;
	document.location.href = updString;
}
