/**
 * Read the JavaScript cookies tutorial at:
 *   http://www.netspade.com/articles/javascript/cookies.xml
 */

function setCookie(NameOfCookie, value) 
{

// Three variables are used to set the new cookie. 
// The name of the cookie, the value to be stored,
// and finally the number of days until the cookie expires.
// The first lines in the function convert 
// the number of days to a valid date.

	var expiredays = 7; // set cookie to expire in 7 days
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

	// The next line stores the cookie, simply by assigning 
	// the values to the "document.cookie" object.
	// Note the date is converted to Greenwich Mean time using
	// the "toGMTstring()" function.

	document.cookie = NameOfCookie + "=" + escape(value) + ";path=/" //+ 
	//((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{

    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
//alert("dc = "+dc.substring(begin + prefix.length, end));
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
// Function to write cookies info when search button is clicked
// Currently used by gateway page (index.html) and tplcity.aspx
// source = "0" - from index.html (need to write country, city cookies); 
// source = "1" - from tplcity.aspx
function storeInfo(source) 
{
	if (source == "0")
	{
		setCookie("countryname", cCountry[cCountry.selectedIndex].text);
		setCookie("countryIndex", cCountry.selectedIndex);
		setCookie("cityname", cCity[cCity.selectedIndex].text);
		setCookie("cityIndex", cCity.selectedIndex);
	}
	
	setCookie("arrivalDay", cInDay.selectedIndex);
	setCookie("arrivalMonth", cInMonth.selectedIndex);
	setCookie("arrivalYear", cInYear.selectedIndex);
	setCookie("departureDay", cOutDay.selectedIndex);
	setCookie("departureMonth", cOutMonth.selectedIndex);
	setCookie("departureYear", cOutYear.selectedIndex);
	setCookie("roomNumber", cUnit.selectedIndex);
	setCookie("adultNumber", cAdult.selectedIndex);
	setCookie("childNumber", cChild.selectedIndex);

	if (document.getElementById("Availability1_txtAgeOfChild0") != null)
		setCookie("childAge0", document.getElementById("Availability1_txtAgeOfChild0").value);
	if (document.getElementById("Availability1_txtAgeOfChild1") != null)
		setCookie("childAge1", document.getElementById("Availability1_txtAgeOfChild1").value);
	if (document.getElementById("Availability1_txtAgeOfChild2") != null)
		setCookie("childAge2", document.getElementById("Availability1_txtAgeOfChild2").value);
	if (document.getElementById("Availability1_txtAgeOfChild3") != null)
		setCookie("childAge3", document.getElementById("Availability1_txtAgeOfChild3").value);
	if (document.getElementById("Availability1_txtAgeOfChild4") != null)
		setCookie("childAge4", document.getElementById("Availability1_txtAgeOfChild4").value);
	if (document.getElementById("Availability1_txtAgeOfChild5") != null)
		setCookie("childAge5", document.getElementById("Availability1_txtAgeOfChild5").value);

}
