
/**
 * Sets a cookie for N days
**/
function setCookieForDays(CookieName,CookieValue,expDays)
{
  var exp = new Date();
  exp.setTime(exp.getTime() + (expDays*86400000)); // milliseconds in a day ?
  var domain = document.domain;
//alert(domain);

  setCookie(CookieName, CookieValue,exp,"/", domain , null);
}	

function areCookiesWorking()
{
	var Rval = false;
	setCookieForDays("test_cookie","test_value",1);
	var MyVal = getCookie("test_cookie","invalid_value");
	if (MyVal == "test_value") {
		Rval = true;
	}
	else{
		 //alert("cookie setting failed?");
	}
	return Rval;
}
			
/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
//  alert("SetCookie: " + name + " = " + value);
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
//		alert(document.cookie);
}
	
	
/**
 * 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, defaultValue)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	var Rval = defaultValue;
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return Rval;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    Rval = unescape(dc.substring(begin + prefix.length, end));
	return Rval;
}

function delCookie(CookieName) {
	setCookieForDays(CookieName,"",-1);
}
/**
 * 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,null))
    {
	 var exp = new Date();
	 exp.setTime(exp.getTime() - 10); // expire 10 milliseconds ago
//alert("delete cookie: " + name + " path:" +path);
	 setCookie(name, "", exp, path, domain, null);
// document.cookie = name + "=" + 
//        ((path) ? "; path=" + path : "") +
//        ((domain) ? "; domain=" + domain : "") +
//        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
//alert(document.cookie);
    }
//    else { alert("cookie not found? " + name + " path " + path + " domain=" + domain);}
	
//	alert(document.cookie);
}


function Select_FindIndex(Sel,Val)
{
  var Len = Sel.length;
  var Rval = -1;
  for (var i = 0; i < Len; i++)  {
	if (Sel[i].value == Val) { Rval = i; break;}
  }
  return Rval;
}
  
function Select_SetSelectedByValue(Sel,Val)
{
  var Index = Select_FindIndex(Sel,Val);
  if (Index >= 0)  {
	Sel.selectedIndex = Index;
  }
}
function Select_GetSelectedValue(Sel)
{
 var Index = Sel.selectedIndex ;
 var Value = Sel[Index].value;
 return Value;
}
