// when the costumer visits the site, sets the cookie with the details of the last search
// if there is a cookie already, fills the form

// get the subsite relevan cookie
function getCookie(name) 
{
	var start = document.cookie.indexOf(name+"=");
	var len = start+name.length+1;
	
	if ((!start) && (name != document.cookie.substring(0,name.length)))
	{
	return null;
	}
	
	if (start == -1)
	{
	return null;
	}
	
	var end = document.cookie.indexOf(";",len);
	if (end == -1)
	{
	end = document.cookie.length;
	}
	
	return unescape(document.cookie.substring(len,end));
}
function setCookie(name,value,expires,path,domain,secure)
{
    document.cookie = name + "=" +escape(value) +
        	     ( (expires) ? ";expires=" + expires.toGMTString() : "") +
		     ( (path) ? ";path=" + path : "") + 
		     ( (domain) ? ";domain=" + domain : "") +
		     ( (secure) ? ";secure" : "");
}

function fillFormGeneral() 
{
	//console.log ('fillFormGeneral');
	if (userLastSearchGeneral) getvaluesFromForm(userLastSearchGeneral, holidayType);
}

function fillForm() 
{
	//console.log ('fillForm');
	if (userLastSearch) getvaluesFromForm(userLastSearch, holidayType);
}

function getvaluesFromForm(string, holidayType)
{
	var siteType = getUrlVars('packagetype');
	
	if(holidayType == 'ski')
	{	
		// ski form
		if(siteType == 'DP')
		{
			getValueTypeSelect(string,"airportcombinedfromid", document.srhfrm.airportcombinedfromid, "select");
		}
		
		getValueTypeSelect(string,"resort_group", document.forms.srhfrm.resort_group, "select");
		getValueTypeSelect(string,"resortid", document.forms.srhfrm.resortid, "select");
		getValueTypeSelect(string,"departure", document.forms.srhfrm.departure, "select");
		getValueTypeSelect(string,"duration", document.forms.srhfrm.duration, "select");
		
		if(siteType != 'FO')
		{
			getValueTypeSelect(string,"rating", document.forms.srhfrm.rating, "select");
			getValueTypeSelect(string,"boardbasis", document.forms.srhfrm.boardbasis, "select");
			getValueTypeSelect(string,"numrooms", document.forms.srhfrm.numrooms, "select");
		}
		getValueTypeSelect(string,"adults", document.forms.srhfrm.adults, "select");
		getValueTypeSelect(string,"children", document.forms.srhfrm.children, "select");
		getValueTypeSelect(string,"infants", document.forms.srhfrm.infants, "select");
	}
	else
	{
		if(siteType == 'DP')
		{
			getValueTypeSelect(string,"airportcombinedfromid", document.srhfrm.airportcombinedfromid, "select");
		}
		
		getValueTypeSelect(string,"airportgrouptoid", document.forms.srhfrm.airportgrouptoid, "select");    
		getValueTypeSelect(string,"airporttoid", document.forms.srhfrm.airporttoid, "select");    
		getValueTypeSelect(string,"departure", document.forms.srhfrm.departure, "select");
		getValueTypeSelect(string,"duration", document.forms.srhfrm.duration, "select");
		
		if(siteType != 'FO')
		{
			getValueTypeSelect(string,"rating", document.forms.srhfrm.rating, "select");
			getValueTypeSelect(string,"boardbasis", document.forms.srhfrm.boardbasis, "select");
			getValueTypeSelect(string,"property_id", document.forms.srhfrm.property_id, "select");
			getValueTypeSelect(string,"numrooms", document.forms.srhfrm.numrooms, "select");
		}
		getValueTypeSelect(string,"adults", document.forms.srhfrm.adults, "select");
		getValueTypeSelect(string,"children", document.forms.srhfrm.children, "select");
		getValueTypeSelect(string,"infants", document.forms.srhfrm.infants, "select");
	}
}

function getValueTypeSelect(string,elementName,object,elementType) 
{
    var startPos = string.indexOf(elementName + "=")
    
    if (startPos > -1) 
    {
        startPos = startPos + elementName.length + 1;
        var endPos = string.indexOf("&",startPos);
        if (endPos == -1)
	{
		endPos = string.length;
	}
	
        var elementValue = unescape(string.substring(startPos,endPos)); 
        if (elementType == "select" && elementValue)
	{
		object.value = elementValue;
	}
    }
}

// beach, cities, ski or long haul
function getHolidayType()
{
	var vars = [], hash;
	if(window.location.href.indexOf(':'))
	{
		var hashes = window.location.href.slice(window.location.href.indexOf(':') + 3).split('.');
	}
	else
	{
		var hashes = window.location.href.slice(window.location.href).split('.');
	}
	vars = hashes[0];
return vars;
}

// DP, AO, FO or nothing
function getUrlVars(string)
{		
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

var packageTypeInUrl = window.location.href.indexOf('=');

	if(packageTypeInUrl != -1)
	{
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			if(hash[0] == string)
			{
				vars = hash[1];
			}
		}
	}
return vars;
}

/*function getSecure()
{		
var hashes = window.location.href.slice(window.location.href).split('/');

return hashes;
}*/

function makeEnableDropDown(formFieldId)
{
    var x=document.getElementById('formFieldId')
    x.disabled=false
}

function extraPage()
{
	var indexpage = window.location.href.indexOf('?');
	
	return indexpage;
}

function setCookieGeneral(name, value, expires, path, domain, secure)
{
document.cookie = name + "=" +escape(value) +
        	     ( (expires) ? ";expires=" + expires.toGMTString() : "") +
		     ( (path) ? ";path=" + path : "") + 
		     ( (domain) ? ";domain=" + domain : "") +
		     ( (secure) ? ";secure" : "");
}

function splitString(string, splitCharacterFirst, splitCharacterSecond, valueOf)
{
	var vars = [];
	var pieces = [];
	var part = [];
	pieces = string.split(splitCharacterFirst);
	
	if(!splitCharacterSecond)
	{
		return pieces;
	}

	for(var i = 0; i < pieces.length; i++)
	{
		part[i] = pieces[i].split(splitCharacterSecond);
		if(valueOf)
		{
			if(part[i][0] == valueOf)
			{
				vars[valueOf] = part[i][1];
			}
		}
	}
	return vars[valueOf];
}
