/*
	Module      	includes/general.js

	Description		General Client Side Utility Functions
	
	Change History
	Date             	  Edit     Author    	Comment
	------------------+---------+------------+---------------------------------------------
	01-Aug-01   [100]     PAC      Created
	------------------+---------+------------+---------------------------------------------
*/

//******************************************************************
//
// "Internal" function to return the decoded value of a cookie
//

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);

  if (endstr == -1)
    endstr = document.cookie.length;

  return unescape(document.cookie.substring(offset, endstr));
}

//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//

function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0

  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}

//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//

function GetCookie (name) 
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;

  while (i < clen) 
  {
    var j = i + alen;

    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);

    i = document.cookie.indexOf(" ", i) + 1;

    if (i == 0) 
		break; 
  }
  
  return null;
}

//
//  Function to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).  
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function SetCookie (name,value,expires,path,domain,secure) 
{
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the 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";
  }
}

	var sLastSection ='';
	var MONTH_NAMES = new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','MAy','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

	function isValidEmail(sEMail)
	{
		/*
			Parameters      

			Description		

			Returns			
											
			Change History
			Date             	  Edit     Author    	Comment
			------------------+---------+------------+---------------------------------------------
			16-May-00   [100]     PAC      Created
			------------------+---------+------------+---------------------------------------------
		*/

	    // var sPattern = "^[\\w\\-\\.]+@[a-z0-9]+[\\-]?[a-z0-9]+((\\.(com|net|org|edu|int|mil|gov))|(\\.(ac|com|net|org|edu|int|mil|gov|co)\\.[a-z]{2})|(\\.[a-z]{2}))$";
	    // var oRegExp = new RegExp(sPattern, 'ig');
	    
		var oRegExp=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

	    return oRegExp.test(sEMail);
	}


	function clickOutlookSection(sName)
	{
		/*
			Parameters      

			Description		

			Returns			
											
			Change History
			Date             	  Edit     Author    	Comment
			------------------+---------+------------+---------------------------------------------
			16-May-00   [100]     PAC      Created
			------------------+---------+------------+---------------------------------------------
		*/

		if(sLastSection != '')
		{
			var oLastSection = document.getElementById(sLastSection);

			if(oLastSection)
				oLastSection.className = 'hiddenoutlooksection';
		}

		var oSection = document.getElementById(sName);

		if(oSection)
			oSection.className = 'visibleoutlooksection';

		sLastSection = sName;
	}

	function formatDTTM(date, format)
	{
		/*
			Parameters      

			Description		

			Returns			
											
			Change History
			Date             	  Edit     Author    	Comment
			------------------+---------+------------+---------------------------------------------
			16-May-00   [100]     PAC      Created
			------------------+---------+------------+---------------------------------------------
		*/

		var result = "";
		var i_format = 0;
		var c = "";
		var token = "";
		var y = date.getYear()+"";
		var M = date.getMonth()+1;
		var d = date.getDate();
		var h = date.getHours();
		var m = date.getMinutes();
		var s = date.getSeconds();
		var yyyy,yy,MMM,MM,dd,hh,mm,ss,ampm,HH,H,KK,K,kk,k;

		format = format+"";

		// Convert real date parts into formatted versions
		// Year

		if (y.length < 4)
			y = y-0+1900;

		y = ""+y;
		yyyy = y;
		yy = y.substring(2,4);

		// Month
		if (M < 10)
			MM = "0"+M;
		else
			MM = M;

		MMM = MONTH_NAMES[M-1];

		// Date
		if (d < 10)
			dd = "0"+d;
		else
			dd = d;

		// Hour
		H=h; K=h; k=h;

		if (h > 12)
			h-=12;

		if (h == 0)
			h=12;

		if (h < 10)
			hh = "0"+h;
		else
			hh = h;

		if (H < 10)
			HH = "0"+K;
		else
			HH = H;

		if (K > 11)
			K-=12;

		if (K < 10)
			KK = "0"+K;
		else
			KK = K;

		if (k == 0)
			k=12;

		if (k < 10)
			kk = "0"+k;
		else
			kk = k;

		// AM/PM
		if (H > 11)
			ampm="PM";
		else
			ampm="AM";

		// Minute
		if (m < 10)
			mm = "0"+m;
		else
			mm = m;

		// Second
		if (s < 10)
			ss = "0"+s;
		else
			ss = s;

		// Now put them all into an object!
		var value = new Object();

		value["yyyy"] = yyyy;
		value["yy"] = yy;
		value["y"] = y;
		value["MMM"] = MMM;
		value["MM"] = MM;
		value["M"] = M;
		value["dd"] = dd;
		value["d"] = d;
		value["hh"] = hh;
		value["h"] = h;
		value["HH"] = HH;
		value["H"] = H;
		value["KK"] = KK;
		value["K"] = K;
		value["kk"] = k;
		value["k"] = k;
		value["mm"] = mm;
		value["m"] = m;
		value["ss"] = ss;
		value["s"] = s;
		value["a"] = ampm;

		while (i_format < format.length)
		{
			// Get next token from format string
			c = format.charAt(i_format);
			token = "";

			while ((format.charAt(i_format) == c) && (i_format < format.length))
			{
				token += format.charAt(i_format);
				i_format++;
			}


			if (value[token] != null)
				result = result + value[token];
			else
				result = result + token;
		}

		return result;
	}

	function getDateFromFormat(val,format)
	{
		/*
			Parameters      

			Description		

			Returns			
											
			Change History
			Date             	  Edit     Author    	Comment
			------------------+---------+------------+---------------------------------------------
			16-May-00   [100]     PAC      Created
			------------------+---------+------------+---------------------------------------------
		*/

		var i_val = 0;
		var i_format = 0;
		var c = "";
		var token = "";
		var token2= "";
		var x,y;
		var now   = new Date();
		var year  = now.getYear();
		var month = now.getMonth()+1;
		var date  = now.getDate();
		var hh    = now.getHours();
		var mm    = now.getMinutes();
		var ss    = now.getSeconds();
		var ampm  = "";

		val = val+"";
		format = format+"";

		while (i_format < format.length)
		{
			// Get next token from format string
			c = format.charAt(i_format);
			token = "";

			while ((format.charAt(i_format) == c) && (i_format < format.length))
			{
				token += format.charAt(i_format);
				i_format++;
			}

			// Extract contents of value based on format token
			if (token=="yyyy" || token=="yy" || token=="y")
			{
				if (token=="yyyy") { x=4;y=4; }// 4-digit year
				if (token=="yy")   { x=2;y=2; }// 2-digit year
				if (token=="y")    { x=2;y=4; }// 2-or-4-digit year

				year = _getInt(val,i_val,x,y);

				if (year == null)
					return 0;

				i_val += year.length;

				if (year.length == 2)
				{
					if (year > 70)
						year = 1900+(year-0);
					else
						year = 2000+(year-0);
				}
			}
			else
			{
				if (token=="MMM")
				{
					// Month name
					month = 0;
					
					for (var i=0; i<MONTH_NAMES.length; i++)
					{
						var month_name = MONTH_NAMES[i];

						if (val.substring(i_val,i_val+month_name.length).toLowerCase() == month_name.toLowerCase())
						{
							month = i+1;

							if (month>12)
								month -= 12;

							i_val += month_name.length;
							break;
						}
					}

					if (month == 0)
						return 0;

					if ((month < 1) || (month>12))
						return 0;

					// TODO: Process Month Name
				}
				else
				{
					if (token=="MM" || token=="M")
					{
						x=token.length;
						y=2;

						month = _getInt(val,i_val,x,y);

						if (month == null)
							return 0;

						if ((month < 1) || (month > 12))
							return 0;

						i_val += month.length;
					}
					else
					{
						if (token=="dd" || token=="d")
						{
							x=token.length;
							y=2;

							date = _getInt(val,i_val,x,y);

							if (date == null)
								return 0;

							if ((date < 1) || (date>31))
								return 0;

							i_val += date.length;
						}
						else
						{
							if (token=="hh" || token=="h")
							{
								x=token.length;
								y=2;

								hh = _getInt(val,i_val,x,y);

								if (hh == null)
									return 0;

								if ((hh < 1) || (hh > 12))
									return 0;

								i_val += hh.length;
							}
							else
							{
								if (token=="HH" || token=="H")
								{
									x=token.length;
									y=2;

									hh = _getInt(val,i_val,x,y);

									if (hh == null)
										return 0;

									if ((hh < 0) || (hh > 23))
										return 0;

									i_val += hh.length;
									hh++;
								}
								else
								{
									if (token=="KK" || token=="K")
									{
										x=token.length;
										y=2;

										hh = _getInt(val,i_val,x,y);

										if (hh == null)
											return 0;

										if ((hh < 0) || (hh > 11))
											return 0;

										i_val += hh.length;
										hh++;
									}
									else
									{
										if (token=="kk" || token=="k")
										{
											x=token.length;
											y=2;

											hh = _getInt(val,i_val,x,y);

											if (hh == null)
												return 0;

											if ((hh < 1) || (hh > 24))
												return 0;

											i_val += hh.length;
										}
										else
										{
											if (token=="mm" || token=="m")
											{
												x=token.length;
												y=2;

												mm = _getInt(val,i_val,x,y);

												if (mm == null)
													return 0;

												if ((mm < 0) || (mm > 59))
													return 0;

												i_val += mm.length;
											}
											else
											{
												if (token=="ss" || token=="s")
												{
													x=token.length;
													y=2;

													ss = _getInt(val,i_val,x,y);

													if (ss == null)
														return 0;

													if ((ss < 0) || (ss > 59))
														return 0;

													i_val += ss.length;
												}
												else
												{
													if (token=="a")
													{
														if (val.substring(i_val,i_val+2).toLowerCase() == "am")
															ampm = "AM";
														else
														{
															if (val.substring(i_val,i_val+2).toLowerCase() == "pm")
																ampm = "PM";
															else
																return 0;
														}
													}
													else
													{
														if (val.substring(i_val,i_val+token.length) != token)
															return 0;
														else
															i_val += token.length;
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}

		// If there are any trailing characters left in the value, it doesn't match
		if (i_val != val.length)
			return 0;

		// Is date valid for month?
		if (month == 2)
		{
			// Check for leap year
			if ( ( (year%4 == 0)&&(year%100 != 0) ) || (year%400 == 0) )
			{
				// leap year
				if (date > 29)
					return false;
			}
			else
			{
				if (date > 28)
					return false;
			}
		}

		if ((month==4)||(month==6)||(month==9)||(month==11))
		{
			if (date > 30)
				return false;
		}

		// Correct hours value
		if (hh<12 && ampm=="PM")
			hh+=12;
		else
		{
			if (hh>11 && ampm=="AM")
				hh-=12;
		}

		//alert(year);
		//alert(month-1);
		//alert(date);
		//alert(hh);
		//alert(mm);
		//alert(ss);
		
		var newdate = new Date(year, month-1, date, hh, mm, ss);

		return newdate;
	}

	function _isInteger(val)
	{
		/*
			Parameters      

			Description		

			Returns			
											
			Change History
			Date             	  Edit     Author    	Comment
			------------------+---------+------------+---------------------------------------------
			16-May-00   [100]     PAC      Created
			------------------+---------+------------+---------------------------------------------
		*/

		var digits = "1234567890";

		for (var i=0; i < val.length; i++)
			if (digits.indexOf(val.charAt(i)) == -1)
				return false;

		return true;
	}

	function _getInt(str,i,minlength,maxlength)
	{
		/*
			Parameters      

			Description		

			Returns			
											
			Change History
			Date             	  Edit     Author    	Comment
			------------------+---------+------------+---------------------------------------------
			16-May-00   [100]     PAC      Created
			------------------+---------+------------+---------------------------------------------
		*/

		for (x=maxlength; x>=minlength; x--)
		{
			var token = str.substring(i,i+x);

			if (_isInteger(token))
				return token;

		}

		return null;
	}
	function setListByValue(select, value, reset)
	{
		/*
			Parameters      

			Description		

			Returns			
											
			Change History
			Date             	  Edit     Author    	Comment
			------------------+---------+------------+---------------------------------------------
			16-May-00   [100]     PAC      Created
			------------------+---------+------------+---------------------------------------------
		*/

		var bFound = false;
			
		for (var i = 0; i < select.options.length; i++) 
		{
			if (select.options[i].value == value)
			{
				bFound=true; 
				break;
			}
		}
		
		if (bFound) 
			select.selectedIndex = i; 
		else
		{
			if(reset)
				select.selectedIndex = -1;
			else
			{
				if(select.options.length > 0)
					select.selectedIndex = 0;
			}
		}
	}

	function formatDate(day, month, year)
	{			
		/*
			Parameters      

			Description		

			Returns			
											
			Change History
			Date             	  Edit     Author    	Comment
			------------------+---------+------------+---------------------------------------------
			16-May-00   [100]     PAC      Created
			------------------+---------+------------+---------------------------------------------
		*/

		var sDate = "";

		sDate += year + "-";
							
		if(month < 10)
			sDate += "0";
					
		sDate += month + "-"
				
		if(day < 10)
			sDate += "0";
				
		sDate += day;

		return sDate;
	}

	function addslashes(sText)
	{
		/*
			Parameters      

			Description		

			Returns			
											
			Change History
			Date             	  Edit     Author    	Comment
			------------------+---------+------------+---------------------------------------------
			16-May-00   [100]     PAC      Created
			------------------+---------+------------+---------------------------------------------
		*/

		var regDBLQuotes = /\"/ig;
		var regQuotes = /\'/ig;
		
		var sNewText = sText.replace(regDBLQuotes, "\\\\'");
		
		return sNewText;
	}

	function addComboOption(cboAdd, sText, nValue)
	{
		/*
			Parameters      

			Description		

			Returns			
											
			Change History
			Date             	  Edit     Author    	Comment
			------------------+---------+------------+---------------------------------------------
			16-May-00   [100]     PAC      Created
			------------------+---------+------------+---------------------------------------------
		*/

		var oOption = document.createElement("option");
		oOption.text = sText;
		oOption.value = nValue;

		cboAdd.add(oOption);
	}

	function clearCombo(cboClear)
	{
		/*
			Parameters      

			Description		

			Returns			
											
			Change History
			Date             	  Edit     Author    	Comment
			------------------+---------+------------+---------------------------------------------
			16-May-00   [100]     PAC      Created
			------------------+---------+------------+---------------------------------------------
		*/

		do
		{
			if(cboClear.options.length > 0)
				cboClear.options.remove(0);

		} while(cboClear.options.length > 0);
	}	
	
	function navigateTo(sPage)
	{
		parent.document.location.href=sPage;
	}
	