
/**
 * Finds an object
 */
function findObj(n, d)
{
	var p,i,x;
	
	if(!d) d=document;
	
	if((p=n.indexOf("?"))>0&&parent.frames.length)
	{
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}
	
	if(!(x=d[n])&&d.all) x=d.all[n];
	
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
	
	if(!x && d.getElementById) x=d.getElementById(n);
	
	return x;
}

/**
 * Validate the form
 */
function validateForm()
{ //v4.0
	var i,p,q,nm,test,num,min,max,errors='',args=validateForm.arguments;
	
	for (i=0; i<(args.length-2); i+=3)
	{
		test=args[i+2];
		val=findObj(args[i]);
		
		if (val)
		{
			nm=val.name;
			if ((val=val.value)!="")
			{
				if (test.indexOf('isEmail')!=-1)
				{
					p=val.indexOf('@');
					if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
				}
				else if (test!='R')
				{
					num = parseFloat(val);
					
					if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
					
					if (test.indexOf('inRange') != -1)
					{
						p=test.indexOf(':');
						min=test.substring(8,p); max=test.substring(p+1);
						
						if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
					}
				}
			}
			else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; 
		}
	}
	
	if (errors) alert('The following error(s) occurred:\n'+errors);
	
	document.returnValue = (errors == '');
}

/**
 * 
 */
function rot13(str)
{
	var outStr = '';
	var letterTable = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMabcdefghijklmnopqrstuvwxyzabcdefghijklm';
	
	for (var k=0; k<str.length; k++)
	{
		var ch = str.charAt(k);
		var ix = letterTable.indexOf(ch);
		
		outStr += (ix==-1) ? ch : letterTable.charAt(ix+13);
	}
	
	return outStr;
}

/**
 * 
 */
function set_cookie(cookieName, cookieValue, nDays)
{
	var today = new Date();
	var expire = new Date();
	
	if (nDays==null || nDays==0) nDays = 1;
	
	expire.setTime(today.getTime() + 3600000*24*nDays);
	
	document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
}

/**
 * 
 */
function read_cookie(cookieName)
{
	var theCookie = "" + document.cookie;
	var ind = theCookie.indexOf(cookieName);
	
	if (ind==-1 || cookieName=="") return "";
	
	var ind1 = theCookie.indexOf(';',ind);
	
	if (ind1==-1) ind1 = theCookie.length;
	
	return unescape(theCookie.substring(ind+cookieName.length+1, ind1));
}

/**
 * 
 */
function delete_cookie(name)
{
	set_cookie(name, "", -1);
}

/**
 * 
 */
function cookies_enabled()
{
	testValue = Math.floor(1000*Math.random());
	
	set_cookie("AreCookiesEnabled",testValue);
	
	return (testValue==read_cookie("AreCookiesEnabled")) ? 1 : 0;	
}

/**
 * onClick="set_nav_cookies('cwa_nav_header_cookies','nav_home');"
 */
function set_nav_cookies(cookie_name, cookie_value, default_value)
{
	if (cookies_enabled())
	{
		if (default_value!=null)
		{
			cookie_value = default_value;
		}	
		
		set_cookie(cookie_name, rot13(cookie_value,1));
		set_cookie(cookie_name, cookie_value,1);
		
	}
	else
	{
		alert("Cookies are not currently enabled in your browser.");
	}
}

/**
 *
 */
function nav_change_css(nav_name,nav_value)
{
	var class_obj = findObj(nav_name);
	
	class_obj.className = nav_value;
}

