//-----------------------------------------------------
function WriteCookie (cookieName, cookieValue, expiry) 
{
var expDate = new Date();

if(expiry)
{
expDate.setTime (expDate.getTime() + expiry);
    document.cookie = cookieName + "=" + escape (cookieValue) + "; expires=" + expDate.toGMTString();
    }
    else
    {
        document.cookie = cookieName + "=" + escape (cookieValue);
	}
}

//-----------------------------------------------------
function ReadCookie (CookieName) {
  var CookieString = document.cookie;
  var CookieSet = CookieString.split (';');
  var SetSize = CookieSet.length;
  var CookiePieces
  var ReturnValue = "";
  var x = 0;

  for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++) {

    CookiePieces = CookieSet[x].split ('=');

    if (CookiePieces[0].substring (0,1) == ' ') {
      CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length);
    }

    if (CookiePieces[0] == CookieName) {
      ReturnValue = CookiePieces[1];
    }

  }
return ReturnValue;
}

//-----------------------------------------------------
function Right(str, n)
{
if (n <= 0) return "";
else if (n > String(str).length) return str;
else {
	var iLen = String(str).length;
	return String(str).substring(iLen, iLen - n);
}
}

//-----------------------------------------------------
function Left(str, n)
{
if (n <= 0) return "";
else if (n > String(str).length) return str;
else return String(str).substring(0,n);
}

//-----------------------------------------------------
function Trim(str)
{
return RTrim(LTrim(str));
}

//-----------------------------------------------------
function RTrim(str)
{
var whitespace = new String(" \t\n\r");
var s = new String(str);

if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
	var i = s.length - 1;
	while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
		i--;
		s = s.substring(0, i+1);
}
return s;
}

//-----------------------------------------------------
function LTrim(str)
{
var whitespace = new String(" \t\n\r");
var s = new String(str);
if (whitespace.indexOf(s.charAt(0)) != -1) {
	var j=0, i = s.length;
	while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
		j++;

	s = s.substring(j, i);
}
return s;
}

//-----------------------------------------------------
function toProper(inpstr)
{
	var str1;
	var str2;
	str1 = inpstr.substring(0, 1);
	str2 = inpstr.substring(1, inpstr.length+1);
	str1 = str1.toUpperCase(); 
	str2 = str2.toLowerCase(); 
	return (str1 + str2);
}

//-----------------------------------------------------
function strtran(inpstr, fromstr, tostr)
{
	var str1;
	var str2;
	var pos;
	while ( (pos=inpstr.indexOf(fromstr)) != -1 )
	{
		str1 = inpstr.substring(0, pos);
		str2 = inpstr.substring(pos + fromstr.length, inpstr.length);
		inpstr=str1 + tostr + str2;

	}
	return inpstr;
}

//-----------------------------------------------------
function emailCheck (emailStr) {
	if (emailStr == "")
	{
		alert("Please enter Email Address.")
		return false;
	}
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {

		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}

	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Email username contains invalid characters.");
			return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Email domain name contains invalid characters.");
			return false;
		 }
	}

	if (user.match(userPat)==null) {
		alert("Email username doesn't seem to be valid.");
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Email Destination IP address is invalid!");
				return false;
		   }
		}
		return true;
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("Email domain name does not seem to be valid.");
			return false;
	   }
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {

		alert("Email address must end in a well-known domain or two letter " + "country.");
		return false;
	}

	if (len<2) {
		alert("Email address is missing a hostname!");
		return false;
	}

	return true;
}

//---------------------------------------------------------------
function move(fbox, tbox) {
if (fbox.options.length == 0) return;
var tmpVal = fbox.options[fbox.selectedIndex].value;
if (tmpVal=="-" || tmpVal=="") return;
var arrFbox = new Array();
var arrTbox = new Array();
var arrLookup = new Array();
var i, ii;
var j, exists_flag;
ii = 0;

for (i = 0; i < tbox.options.length; i++) {
	if(tbox.options[i].value !="-") {
		arrLookup[tbox.options[ii].text] = tbox.options[i].value;
		arrTbox[ii] = tbox.options[i].text;
		ii++;
	}
}
var fLength = 0;
var tLength = arrTbox.length;

for(i = 0; i < fbox.options.length; i++) {

	exists_flag = false;
	for (j=0; j< arrTbox.length; j++) {
		if (arrLookup[arrTbox[j]]==fbox.options[i].value) {
			exists_flag = true;
			break;
		}
	}

	if (! fbox.options[i].selected) {
		arrLookup[fbox.options[i].text] = fbox.options[i].value;
		arrFbox[fLength] = fbox.options[i].text;
		fLength++;
	}
	if (!exists_flag && fbox.options[i].selected && fbox.options[i].value != "" && fbox.options[i].value != "-") {
		arrTbox[tLength] = fbox.options[i].text;
		arrLookup[arrTbox[tLength]] = fbox.options[i].value;
		tLength++;
	}
}
arrFbox.sort();
arrTbox.sort();
fbox.length = 0;
tbox.length = 0;
var c;
for(c = 0; c < arrFbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrFbox[c]];
no.text = arrFbox[c];
fbox[c] = no;
fbox.selectedIndex = 0;
}
for(c = 0; c < arrTbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrTbox[c]];
no.text = arrTbox[c];
tbox[c] = no;
tbox.selectedIndex = 0;
   }
}

//---------------------------------------------------------------
function FormatDateTime(datetime, FormatType)
/*
	 FomatType takes the following values
		1 - General Date = Friday, October 30, 1998
		2 - Typical Date = 10/30/98
		3 - Standard Time = 6:31 PM
		4 - Military Time = 18:31
*/
{
	var strDate = new String(datetime);

	if (strDate.toUpperCase() == "NOW") {
		var myDate = new Date();
		strDate = String(myDate);
	} else {
		var myDate = new Date(datetime);
		strDate = String(myDate);
	}


	// Get the date variable parts
	var Day = new String(strDate.substring(0,3));
	if (Day == "Sun") Day = "Sunday";
	if (Day == "Mon") Day = "Monday";
	if (Day == "Tue") Day = "Tuesday";
	if (Day == "Wed") Day = "Wednesday";
	if (Day == "Thu") Day = "Thursday";
	if (Day == "Fri") Day = "Friday";
	if (Day == "Sat") Day = "Saturday";	
	
	var Month = new String(strDate.substring(4,7)), MonthNumber = 0;
	if (Month == "Jan") { Month = "January"; MonthNumber = 1; }
	if (Month == "Feb") { Month = "February"; MonthNumber = 2; }
	if (Month == "Mar") { Month = "March"; MonthNumber = 3; }
	if (Month == "Apr") { Month = "April"; MonthNumber = 4; }
	if (Month == "May") { Month = "May"; MonthNumber = 5; }
	if (Month == "Jun") { Month = "June"; MonthNumber = 6; }
	if (Month == "Jul") { Month = "July"; MonthNumber = 7; }
	if (Month == "Aug") { Month = "August"; MonthNumber = 8; }
	if (Month == "Sep") { Month = "September"; MonthNumber = 9; }
	if (Month == "Oct") { Month = "October"; MonthNumber = 10; }
	if (Month == "Nov") { Month = "November"; MonthNumber = 11; }
	if (Month == "Dec") { Month = "December"; MonthNumber = 12; }
	
	var curPos = 11;
	var MonthDay = new String(strDate.substring(8,10));
	if (MonthDay.charAt(1) == " ") {
		MonthDay = "0" + MonthDay.charAt(0);
		curPos--;
	}	
	
	var MilitaryTime = new String(strDate.substring(curPos,curPos + 5));
	
	var Year = new String(strDate.substring(strDate.length - 4, strDate.length));	
	
	document.write(strDate + "");	

	// Format Type decision time!
	if (FormatType == 1)
		strDate = Day + ", " + Month + " " + MonthDay + ", " + Year;
	else if (FormatType == 2)
		strDate = MonthNumber + "/" + MonthDay + "/" + Year.substring(2,4);
	else if (FormatType == 3) {
		var AMPM = MilitaryTime.substring(0,2) >= 12 && MilitaryTime.substring(0,2) != "24" ? " PM" : " AM";
		if (MilitaryTime.substring(0,2) > 12)
			strDate = (MilitaryTime.substring(0,2) - 12) + ":" + MilitaryTime.substring(3,MilitaryTime.length) + AMPM;
		else {
			if (MilitaryTime.substring(0,2) < 10)
				strDate = MilitaryTime.substring(1,MilitaryTime.length) + AMPM;
			else
				strDate = MilitaryTime + AMPM;
		}
	}	
	else if (FormatType == 4)
		strDate = MilitaryTime;


	return strDate;
}

//---------------------------------------------------------------
function isNum(str) {
 for (var i = 0; i < str.length; i++) {
  var ch = str.substring(i, i + 1)
  if ((ch < "0" || "9" < ch) && ch != '.') {
   return false
  }
 }
 return true
}
