/***************************************************************************
 [cookie.js]
 
 Copyright (C) 2004 Next IT Corporation, Inc. Spokane, WA. All Rights Reserved. 
 This document is confidential work and intellectual property of Next IT 
 Corporation. Permission to copy, distribute or use any portion of this file 
 is prohibited without the express written consent of Next IT Corporation.

*****************************************************************************/

function getCookie(N)
{
	var E=N+"=";
	if(document.cookie.length>0)
	{
		F=document.cookie.indexOf(E);
		if(F!=-1)
		{
			F+=E.length;
			end=document.cookie.indexOf(";",F);
			if(end==-1)end=document.cookie.length;
			return unescape(document.cookie.substring(F,end));
		}
	}
};

function setCookie(N,V)
{
	var argv=setCookie.arguments;
	var argc=setCookie.arguments.length;
	var X=(argc>2)?argv[2]:null;
	var P=(argc>3)?argv[3]:null;
	var D=(argc>4)?argv[4]:getDomain();//"onecall.com";
	var E=(argc>5)?argv[5]:false;
	var name =N+"="+escape(V)+((X==null)?"":(";expires="+X.toGMTString()))+((P==null)?";path=/":(";path="+P))+((D==null)?"":(";domain="+D))+((E==true)?";secure":"");
	document.cookie = name;
};

function getDomain()
{
	var url = location.href;
	var s = url.indexOf('.')+1;
	if (s==-1) s = url.indexOf('://')+3;
	var e = url.indexOf('/',s);
	if (e > -1)
		return url.substring(s,e);
	else
		return url.substring(s);
};

function formResetCookies()
{
	var i,s;
	for(i=0;(s=formResetCookies.arguments[i]);i++)
	{
		setCookie(s,'',null,null);
	}
};

function formSetCookie(f,c,rst)
{
	if (f.type=='select-one')
		setCookie(c,rst?0:f.selectedIndex,null,null);
	else
		setCookie(c,rst?'':f.value,null,null);
};

function formLoadCookie(f,c)
{
	var i=getCookie(c),e=document.forms[0].elements[f];
	if (i)
	{
		if (e.type=='select-one')
			e.selectedIndex=i;
		else
			e.value=i;
	}
};

function PrintAllCookies() 
{
	if (document.cookie != "") 
	{
		thisCookie= document.cookie.split("; ");
		for (i=0; i< thisCookie.length; i++) 
		{
			document.write(thisCookie[i].split("=")[0] + "=" + thisCookie[i].split("=")[1] + "<br>");
		}
	}
};

function DeleteCookie(sName)
{
  //document.cookie = DeleteCookieString( sName,  );
  var d = new Date( 1970, 0, 2 ); // "Fri, 02-Jan-1970 00:00:00 GMT" );
  //setCookie( _brainID, "", d );
  setCookie( sName, "", d );
};

// this deletes the cookie when called
function Delete_Cookie( name, path, domain )
{
	if ( Get_Cookie( name ) )
		document.cookie = name + "=" +( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// this function gets the cookie, if it exists
function Get_Cookie( 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 DeleteCookieString( sName, sPath, sDomain ) 
{
  var cookie = sName+'=';
  if( sPath )   
	cookie+='; path='+sPath;
  if( sDomain )
	cookie+='; domain='+sDomain;
  cookie+='; expires=Fri, 02-Jan-1970 00:00:00 GMT'; // MAKE IT EXPIRE!
  return cookie;
};
