// Check chars for unauthorized list
function getForbiddenChars(str, forbiddenChars)
{
	var	foundChars = "";
	var i;
	var chi;
	
	for (i = 0; i < forbiddenChars.length ; i++) 
	{
		chi = forbiddenChars.substr(i,1);
		if (str.indexOf(chi) != -1)
		{
			foundChars = foundChars + chi;		
		}
	}
	return foundChars;
}

// auto reload page after refreshRate
function autoReload(refreshRate) 
{
  setTimeout("self.location.reload()", refreshRate);
}

// change the page location
function changePageLocation(toPage)
{
	self.top.scroll(0,0);
	self.location = toPage;	
}

// auto reload page after refreshRate
function loadAfterTimeout(page, timeout) 
{
  setTimeout("changePageLocation('" + page + "')", timeout);  
}


// decode url
function urldecode(str)
{
	return URLDecode(str);
}	
	

// URL encode a text and returns the result
function URLEncode(text)
{
	var SAFECHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.!~*'()";
	var HEX = "0123456789ABCDEF";
	var encoded = "";
	for (var i = 0; i < text.length; i++ ) {
		var c = text.charAt(i);
	    if (c == " ") {
		    encoded += "+";				
		} else if (SAFECHARS.indexOf(c) != -1) {
		    encoded += c;
		} else {
		  var charCode = c.charCodeAt(0);
			if (charCode <= 255) {			    
				encoded = encoded + "%" + HEX.charAt((charCode >> 4) & 0xF) + HEX.charAt(charCode & 0xF);
			}
		}
	} 
	return (encoded)	
}

// URL decode a text and returns the result
function URLDecode(text)
{
   var HEXCHARS = "0123456789ABCDEFabcdef";    
   var decoded = "";
   var i = 0;
   while (i < text.length) {
     var c = text.charAt(i);
	   if (c == "+") {
	   		decoded += " ";
		 		i++; } 
	   else if (c == "%") {
				if (i < (text.length-2) && HEXCHARS.indexOf(text.charAt(i+1)) != -1 && HEXCHARS.indexOf(text.charAt(i+2)) != -1 ) {
					decoded += unescape( text.substr(i,3) );
					i += 3; } else { i++; } } 
			else {
		   decoded += c;
		   i++; }
	}
	return decoded;
}

// parse a query string
function parseQueryString (str) 
{
  str = str ? str : location.search;
  var query = str.charAt(0) == '?' ? str.substring(1) : str;
  var args = new Object();
  if (query) 
  {
    var fields = query.split('&');
    for (var f = 0; f < fields.length; f++) 
    {
      var field = fields[f].split('=');
      args[unescape(field[0].replace(/\+/g, ' '))] = 
			unescape(field[1].replace(/\+/g, ' '));
    }
  }
  return args;
}



// check for invalid chars in str :: Returns false if an invalid char is found in str, true otherwise
function checkChars(str, invalidChars)
{
	var i;
	var chi;

	intdt = invalidChars;
	for (i=0; i < invalidChars.length ; i++) 
	{
		chi = invalidChars.substr(i,1);
		if (-1 != str.indexOf(chi))
			return false;		
	}
	return true;
}

// validate a string email address :: Returns false if not valid, true otherwise
function validateEmail(email)
{
	if (email.length < 5)
		return (false);	
		
	var blankPos = email.indexOf(" ");
	if (blankPos != -1)
		return (false);	
	var aroPos = email.indexOf("@");
	var dotPos = email.lastIndexOf(".");
	if ( (aroPos <= 0) || (dotPos <= 0) || (aroPos + 1 >= dotPos) || (dotPos == email.length) )
		return (false);	
	return (true);	
}

// invalidate a form :: for the input, print the errMsg and returns false
function invalidate(input, errMsg)
{
	alert(errMsg);
	input.focus();
	return (false);	
} 
