
var QSearch = false;
var SpecialOfferImages = new Array(3);
for(var i=0;i<SpecialOfferImages.length;i++){			
	SpecialOfferImages[i]= new Image();
}	

var RecommendedImages = new Array(5);
for(var i=0;i<RecommendedImages.length;i++){			
	RecommendedImages[i]= new Image();
}	

// Variable to store number of big images
var BigImageCount=6;
// Variable to store index of big image.
var BigImageIndex=1;

//var RecImage = new Image();	
var searchtype_options=new Array();
var partysize_options=new Array();		
var acc_ids=new Array();							

// delay in milliseconds between image swaps 1000 = 1 second 
var delay = 1000;
var counter = 0;

var size=0;
var type=0;

// Set slideShowSpeed (milliseconds)
var slideShowSpeed = 3000;
// Duration of crossfade (seconds)
var crossFadeDuration = 1.5;

var AccId=0;
var AccType="";
var AccRegion="";

var KeyDescriptionsHtml="";

function UpdateAccDetails( id, Atype, region){
	AccId=id;
	AccType=Atype;
	AccRegion=region;
}

function RedirectToBookingPage(){
	var str = "Booking.aspx?ID="+AccId+"\&Type="+AccType+"\&Region="+AccRegion; 

	window.location.href=str; 
}

function preloadImgs(){
	for(var i=0;i<SpecialOfferImages.length;i++){		
		document.SpecialOfferImage.src = SpecialOfferImages[i].src;
	}
	//for(var j=0;j<RecommendedImages.length;j++){
	//	document.RecImage.src = RecommendedImages[j].src;
	//}
	//document.RecImage.src = RecImage.src;				
}

function InitSearchPage(){	
	var searchtypeobj=document.getElementById("SearchType");
	
	if(searchtypeobj != null){	
	    for(var i=0;i<searchtype_options.length;++i){
	    	
		    searchtypeobj.options.add(searchtype_options[i]);
		    if(i==0){
		    	searchtypeobj.options[i].selected=true;			
	    	}		
	    }	
	}	
	
	var partysizeobj=document.getElementById("PartySize");
	
	if(partysizeobj != null){
	    for(var i=0;i<partysize_options.length;++i){
		
	    	partysizeobj.options.add(partysize_options[i]);
	    	if(i==0){
	    		partysizeobj.options[i].selected=true;			
	    	}		
	    }	
	}
	
	var AccIdsobj=document.getElementById("AccIds");	
	if(AccIdsobj != null){	   
	    for(var i=0;i<acc_ids.length;++i){		
	      
	    	AccIdsobj.options.add(acc_ids[i]);
	    	if(i==0){
	    		AccIdsobj.options[i].selected=true;			
	    	}		
	    }	
	}
	
	if(document.getElementById("AccommodationID") != null){
	    document.getElementById("AccommodationID").disabled=true;	
	    document.getElementById("AccommodationIDLabel").disabled=true;	   
	    document.getElementById("SearchType").selectedIndex = type;
	    document.getElementById("PartySize").selectedIndex = size;
	    if(document.getElementById("RadioAccommodationId").checked == true){
	    	RBIdClick();
	    }
	}
}

function randomImages(){

	if(counter == (SpecialOfferImages.length)){
		counter = 0;
	}	
	//document.RecImage.src = RecommendedImages[counter].src;	
	document.SpecialOfferImage.src = SpecialOfferImages[counter].src;		
	counter++;
	setTimeout('randomImages()', delay);	
}		

function RBTypeClick(){
	document.getElementById("RadioAccommodationId").checked=false;	
	document.getElementById("AccommodationID").disabled=true;
	document.getElementById("SearchType").disabled=false;		
	document.getElementById("PartySize").disabled=false;
	document.getElementById("AccommodationIDLabel").disabled=true;														
}
function RBIdClick(){
	document.getElementById("RadioAccommodationType").checked=false;
	document.getElementById("AccommodationID").disabled=false;
	document.getElementById("SearchType").disabled=true;		
	document.getElementById("PartySize").disabled=true;
	document.getElementById("AccommodationIDLabel").disabled=false;
}						
				
function IsNumeric(strString)
//  check for valid numeric strings	
{
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (var i = 0; i < strString.length && blnResult == true; i++)
		{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
			{
			blnResult = false;
			}
		}
	return blnResult;
}	

function Validate(){
 var blnResult = true; 
	if(QSearch == true){
	    blnResult =  QuickSearchApply();
	} else {
	    blnResult = SearchApply();
	}
	return blnResult;
}

function Search(){
    QSearch = false;
}


function Enquiry(){    
    var blnResult = true;
	var ErrorMsg ="";
	var name = document.getElementById("txtName").value;
	blnResult = IsNameValid(name);
    if(blnResult == false){
        ErrorMsg = "<small><b>You must provide <br> a valid name</b></small>"; 
        document.getElementById("lblErrorMessage").innerHTML = ErrorMsg;
	    document.getElementById("lblErrorMessage").style.color="Red";	    
        return blnResult;
    }
    
    var email = document.getElementById("txtEmail").value;
    
    blnResult = IsEmailValid(email);
    if(blnResult == false){
        ErrorMsg = "<small><b>You must provide <br> a valid email address</b></small>";	
        document.getElementById("lblErrorMessage").innerHTML = ErrorMsg;
	    document.getElementById("lblErrorMessage").style.color="Red";	 
        return blnResult;
    }
    
    var tel = document.getElementById("txtTelephone").value;
    
    blnResult = IsTelValid(tel);
    if(blnResult == false){
        ErrorMsg = "<small><b>You must provide <br> a valid telephone number</b></small>";	
        document.getElementById("lblErrorMessage").innerHTML = ErrorMsg;
	    document.getElementById("lblErrorMessage").style.color="Red";	 
        return blnResult;
    }   
    document.frmEnquiry.action="Enquiry.aspx?Name="+name+"&Email="+email+"&tel="+tel;
		
	return blnResult;
}

function IsNameValid(name){
    if(name == ""){
        return false;
    }
    return true;
}

function IsEmailValid(email){    
	var filter  = /([a-zA-Z-9_\-\.]+@)((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	if (filter.test(email)) {
	return true;
	}
	return false; 
}

function IsTelValid(tel){
  var re = new RegExp("^[0-9\s\(\)\+\-]+$");
  if (tel.match(re)) {
    return true;
  }
  return false;    
}

function SearchApply(){
    var blnResult = true;
	var ErrorMsg ="";
	if(document.getElementById("RadioAccommodationId").checked == true){
		blnResult = IsNumeric(document.getElementById("AccommodationID").value);
		if(blnResult == false){	
			ErrorMsg = "<small><b>Accommodation ID must  be <br> numeric</b></small>";		
		}		
	} else {
		blnResult = false;	
		if(document.getElementById("SearchType").selectedIndex==4){		
		    blnResult = true;		
		} else {
		    if(document.getElementById("PartySize").selectedIndex==0 || document.getElementById("SearchType").selectedIndex==0){
		    	ErrorMsg = "<small><b>Invalid selection of  type <br> and partysize</b></small>";
		    } else if(document.getElementById("PartySize").selectedIndex==0){
		    	ErrorMsg = "<small><b>Invalid selection of  type <br> and partysize</b></small>";
		    } else if(document.getElementById("SearchType").selectedIndex==0){
		    	ErrorMsg = "<small><b>Invalid selection of  type <br> and partysize</b></small>";
		    } else {
		    	blnResult = true;
		    }
		} 
	}					
	document.getElementById("ErrorMessage").innerHTML = ErrorMsg;
	document.getElementById("ErrorMessage").style.color="Red";	
	if(blnResult==true){
	    if(document.getElementById("SearchType").selectedIndex==4){
	        document.searchform.action="SpaResults.aspx"; 
	    } else {
		    if(document.getElementById("RadioAccommodationId").checked == true){
		       document.searchform.action="AcmdView.aspx?AccId="+document.getElementById("AccommodationID").value;
		    } else {
		       document.searchform.action="AcmdResults.aspx";    
		    }	    
		} 
	}	
	return blnResult;
}


function QuickSearchApply(){
    var blnResult = true;
    //var AccIdsobj=document.getElementById("AccIds");
    var accId=document.getElementById("QuickAccID").value;
    //if(AccIdsobj != NULL){   
        //var selected_index = AccIdsobj.selectedIndex;             
        //var accId = AccIdsobj.options[selected_index].value;
        
        if(IsNumeric(accId))
        {     
             //document.getElementById("searchform").action="AcmdView.aspx?Accid="+accId;   
             document.searchform.action="AcmdView.aspx?Accid="+accId;   
             //window.location.href="AcmdView.aspx?Accid="+accId; 
        } else {
            //document.getElementById("searchform").action=accId;  
            document.searchform.action=accId;  
            //window.location.href=accId; 
        }
	//} 
	return blnResult;
}

function QuickSearch(){
    QSearch = true;   
}

function RecAccLink(){	
	//window.location.href="AcmdView.aspx?id="+RecAccId;   
	window.location.href="SpaResults.aspx";   
}

function SpecialOfferAccLink(){
	window.location.href="SpecialOffers.aspx";	
}

function runSlideShow() {
	if (document.all) {
		document.getElementById("SpecialOfferImage").style.filter="blendTrans(duration=2)";
		document.getElementById("SpecialOfferImage").filter="blendTrans(duration=crossFadeDuration)";
		document.getElementById("SpecialOfferImage").filters.blendTrans.Apply();
		
	    //document.getElementById("RecImage").style.filter="blendTrans(duration=2)";
		//document.getElementById("RecImage").filter="blendTrans(duration=crossFadeDuration)";
		//document.getElementById("RecImage").filters.blendTrans.Apply();
	}
	document.getElementById("SpecialOfferImage").src = SpecialOfferImages[counter].src;
	if (document.all) {
		document.getElementById("SpecialOfferImage").filters.blendTrans.Play();
	}
	
	//document.getElementById("RecImage").src = RecommendedImages[counter].src;
	//if (document.all) {
	//	document.getElementById("RecImage").filters.blendTrans.Play();
	//}
	
	counter  = counter + 1;
	if (counter > (SpecialOfferImages.length - 1)) counter = 0;
	t = setTimeout('runSlideShow()', slideShowSpeed);
}

function ChangeSelection(t,s){
	type=t;
	size=s;	
}

function RedirectPage(){
	if(document.getElementById("RadioAccommodationId").checked == true){
		window.location.href="AcmdView.aspx?Accid="+id;
	} else {
		window.location.href="AcmdResults.aspx";    
	}
}

function OpenBrochureInSeparateWindow(){        
		window.open("Brochure/Databasedesigndocumentation.pdf");
}

function OpenKeyDescriptionsPage(){    
   KeyDescriptionsWindow = window.open ("KeyDescriptions.htm","KeyDescriptionsWindow","location=0,status=1,scrollbars=1,width=100,height=250");   
   KeyDescriptionsWindow.document.write(KeyDescriptionsHtml);
   KeyDescriptionsWindow.document.title="Key Descriptions";
   KeyDescriptionsWindow.document.status="Key Descriptions";
   KeyDescriptionsWindow.document.body.bgColor="maroon";
   KeyDescriptionsWindow.document.body.style.textAlign="center";
}

function delayer(){
	window.location = "default.htm"
}

function bookmarksite(title, url){
     if (document.all)
     window.external.AddFavorite(url, title);
    else if (window.sidebar)
    window.sidebar.addPanel(title, url, "")
}

function ShowNextPicture(){
    if(BigImageCount == BigImageIndex){
        BigImageIndex=1;
    } else {
       ++BigImageIndex;
    }
    SetBigImageSource(BigImageIndex)
}
function ShowPreviousPicture(){
    if(1 == BigImageIndex){
        BigImageIndex=BigImageCount;
    } else {
       --BigImageIndex;
    }
    SetBigImageSource(BigImageIndex)
}

function UpdateBigImageIndex(index){
    BigImageIndex = index;
    var IncrIndexLabel = document.getElementById("BigPictureIndexLabel");
    if(IncrIndexLabel != 0){    
         var str = "( "+BigImageIndex+" of "+BigImageCount+" )";
         IncrIndexLabel.innerHTML = str;
    }      
}

function SetBigImageSource(index){      
    document.imgBigAcc.src = BigImages[index-1].src
    UpdateBigImageIndex(index);   
}

function SearchTypeChange(){
    if(document.getElementById("SearchType").selectedIndex == 4){
        document.getElementById("PartySize").disabled=true;     
    } else {
        document.getElementById("PartySize").disabled=false;
    }
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	var curValue = readCookie("TuscanyOptionsFav");
	if(curValue != null && !cookieValueExists(curValue,value)){
	   if(cookieValueCount(curValue) == 5){
	     curValue = cookieDeleteFirst(curValue);
	   }
	   value= curValue+" "+value;
	}
	document.cookie = name+"="+value+expires+"; path=/";
	
	var sURL = unescape(window.location.pathname);

	//window.location.replace( sURL );

	window.location.reload( false );
}

function cookieValueExists(curValue, value) {	
	var ca = curValue.split(' ');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		if(c == value){
		    return true;
		}		
	}
	return false;
}

function cookieValueCount(curValue) {	
	var ca = curValue.split(' ');
	return ca.length;
}

function cookieDeleteFirst(curValue) {	
    var newVal='';
	var ca = curValue.split(' ');
	for(var i=1;i < ca.length;i++) {
		var c = ca[i];
		newVal+=c;
		if(i!==(ca.length-1)){
		    newVal+=' ';
		}
	}
	return newVal;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);	
}

