/*================================================================================================================================
Company: AppleTreeSoft
Application: javascript utility
Author: Jacob Park
Description: javascript utility
Dates: 4/20/2006
==================================================================================================================================*/

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  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=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


//**************************************************************************************************************************
function changeCase(frmObj) {
	var index;
	var tmpStr;
	var tmpChar;
	var preString;
	var postString;
	var strlen;
	tmpStr = frmObj.value.toLowerCase();
	strLen = tmpStr.length;
	if (strLen > 0)  {
	for (index = 0; index < strLen; index++)  {
	if (index == 0)  {
	tmpChar = tmpStr.substring(0,1).toUpperCase();
	postString = tmpStr.substring(1,strLen);
	tmpStr = tmpChar + postString;
	}
	else {
	tmpChar = tmpStr.substring(index, index+1);
	if (tmpChar == " " && index < (strLen-1))  {
	tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
	preString = tmpStr.substring(0, index+1);
	postString = tmpStr.substring(index+2,strLen);
	tmpStr = preString + tmpChar + postString;
	         }
	      }
	   }
	}
	frmObj.value = tmpStr;
}

//**************************************************************************************************************************
function ignoreSpaces(string,dlmtr) {
	var temp = "";
	string = '' + string;
	splitstring = string.split(dlmtr);
	for(i = 0; i < splitstring.length; i++)
	temp += splitstring[i];
	return temp;
}

//**************************************************************************************************************************
function trimLeadingSpaces(field){
	while(''+field.value.charAt(0)==' ') 		
		field.value=field.value.substring(1,field.value.length);
}



//**************************************************************************************************************************
function checkBrowser(NSvers,NSpass,NSnoPass,IEvers,IEpass,IEnoPass,OBpass,URL,altURL) { //v4.0
  var newURL='', verStr=navigator.appVersion, app=navigator.appName, version = parseFloat(verStr);
  if (app.indexOf('Netscape') != -1) {
    if (version >= NSvers) {if (NSpass>0) newURL=(NSpass==1)?URL:altURL;}
    else {if (NSnoPass>0) newURL=(NSnoPass==1)?URL:altURL;}
  } else if (app.indexOf('Microsoft') != -1) {
    if (version >= IEvers || verStr.indexOf(IEvers) != -1)
     {if (IEpass>0) newURL=(IEpass==1)?URL:altURL;}
    else {if (IEnoPass>0) newURL=(IEnoPass==1)?URL:altURL;}
  } else if (OBpass>0) newURL=(OBpass==1)?URL:altURL;
  if (newURL) { alert(newURL); document.MM_returnValue=false; }
}




//**************************************************************************************************************************
function CharCheck(field) {
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  var checkStr = field.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    return (false);
  }
  
  return true;
}



//-----------------------------------------------------------------------------------------
function CheckEmail(email) {

		var first = email.indexOf("@")
		var last = email.indexOf(".")
		var email_num = email.length;
		
		if (first != -1) {                // check @ duplicate
			mail_str = email;
			mail_str = mail_str.substr(first+1);
			second = mail_str.indexOf("@");
			if (second != -1)	{	
				alert("Unrecognized e-mail address format. Please check your e-mail and enter a valid e-mail address.");
				
				return false;
			}
		}

		for (var l = 0; l <= (email_num - 1); l++) {
			if (email.indexOf(" ") >= 0 ) {
				alert ("\"Space\" is not valid in e-mail address.");
		        
		        return false;
		    } 
		}

		if ((email.indexOf("/")) != -1)	{	
			alert ("\" / \" is not valid in e-mail address.");
			
			return false;
		}

		if ((email.indexOf(";")) != -1)	{	
			alert ("\" ; \" is not valid in e-mail address.");
			
			return false;
		}

		if ((email.indexOf(",")) != -1)	{	
			alert ("\" , \" is not valid in e-mail address.");
			
			return false;
		}

		if (email.search(/(\S+)@(\S+)\.(\S+)/) == -1) {
				alert("Unrecognized e-mail address format. Please check your e-mail and enter a valid e-mail address.");
			
			return false;
		}
	
		if (email.substring(0, 5) == "http:") {
			alert ("\" http: \" is not valid in e-mail address.");
			
			return false;
		} 

		if (email.lastIndexOf(".") == email_num-1) {
			alert ("\" . \" is not valid at the end of e-mail address.");
			
			return false;
		} 
	
	return true;
} // checkEMail ()



//**************************************************************************************************************************
function emailCheck (emailStr) {
	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("Ths username in email address contains invalid characters.");
	return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
	if (domain.charCodeAt(i)>127) {
	//alert("Ths domain name in email address contains invalid characters.");
	return false;
	   }
	}
	if (user.match(userPat)==null) {
	//alert("The username in email address 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("Destination IP address in email 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("The domain name in email address does not seem to be valid.");
	return false;
	   }
	}
		
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
	//alert("The email address must end in a well-known domain or two letter " + "country.");
	return false;
	}
		
		
	if (len<2) {
	//alert("The email address is missing a hostname!");
	return false;
	}
		
	return true;
}


//**************************************************************************************************************************
function changeText(bt,over) {
	if (over) 
		bt.style.color="#ff0000";
	else
		bt.style.color="#000080";
}


//**************************************************************************************************************************
function showDlg(winDlg, DlgName, H, W) {
	// Dialog URL
	var strPage = winDlg;
	strAttr = "status:no;dialogWidth:"+ W +"px;dialogHeight:"+ H + "px;help:no";' '

	// run the dialog that implements this type of element
	var strURL = showModalDialog(strPage, DlgName, strAttr);
	strURL.focus();
 
}

//**************************************************************************************************************************
function showPopup(winDlg, DlgName, H, W, Scroll) {
  var mW = (window.screen.width - W)/2;
  var mH = (window.screen.height - H)/2;
  if (DlgName == 'SO') {
	mH = mH-100;
	}
  
  fs2 = window.open('', DlgName, 'width='+W+',height='+H+',scrollbars='+Scroll+',location=0,status=0,toolbar=0,resizable=1,titlebar=0,left='+mW+',top='+mH);
  if (fs2 != null) {
    if (fs2.opener == null) {
      fs2.opener = self;
      self.location.href=document.location;
   }
   
   fs2.location.href = winDlg;
   fs2.focus();
  
  }
	fs2.creator=self; 
}


//**************************************************************************************************************************
function button_over(eButton)	{
	eButton.style.backgroundColor = "#B5BDD6";
	eButton.style.border = "darkblue 1px solid";
}

//**************************************************************************************************************************
function button_out(eButton) {
	eButton.style.backgroundColor = "buttonface";
	eButton.style.border = "buttonface 1px solid";
}

//**************************************************************************************************************************
function button_down(eButton) {
	eButton.style.backgroundColor = "#8494B5";
	eButton.style.border = "darkblue 1px solid";
}

//**************************************************************************************************************************
function button_up(eButton) {
	eButton.style.backgroundColor = "#B5BDD6";
	eButton.style.border = "darkblue 1px solid";
	eButton = null; 
}

//**************************************************************************************************************************
function winopen(URL){
	var targeturl=URL;
	newwin=window.open("","MAIN","scrollbars=0, resizable=0,toolbar=0,top=0,left=0")
	if (document.all)	{
		newwin.resizeTo(screen.width,screen.height-27)
	//	newwin.resizeTo(1024, 764)
	}
	newwin.location=targeturl
}

//**************************************************************************************************************************
function FormatPhone (obj) {
	var str = obj.value;
	var strOut;
	if (str.length == 10) {
		if (isNaN(str)) {
		alert('Please enter numeric characters.');
		obj.select();
		obj.focus();
		}
		else {
			strOut = '('+str.substring(0,3)+')'+str.substring(3,6)+'-'+str.substring(6,10);
			obj.value = strOut;
		}
	}
}

//**************************************************************************************************************************
function CheckField(obj) {
	if (obj.value == '') {
		alert(obj.name+' is missing, enter ' + obj.name + '.');
		obj.focus();
		return false;
	}

	return true;
}

//**************************************************************************************************************************
function FormatCurrency(number) {
    var x = (Math.floor(number*100+0.50000000001))/100;
    return (x == Math.floor(x)) ? x + '.00' : ((x*10 == Math.floor(x*10)) ? x + '0' : x);
}

//**************************************************************************************************************************
function round(number,X) {
// rounds number to X decimal places, defaults to 2
    X = (!X ? 2 : X);
    return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

//**************************************************************************************************************************
function replace(originalString,searchText,replaceText) { 
	var strLength = originalString.length; 
	var txtLength = searchText.length; 
	if ((strLength == 0) || (txtLength == 0)) 
	{ return originalString; } 
	var i = originalString.indexOf(searchText); 
	if ((!i) && (searchText != originalString.substring(0,txtLength))) 
	{ return originalString; } 
	if (i == -1) 
	{ return originalString; } 
	var newstr = originalString.substring(0,i) + replaceText; 
	if (i+txtLength < strLength) 
	{ newstr += replace(originalString.substring(i+txtLength,strLength),searchText,replaceText); } 
	return newstr;
} 

//**************************************************************************************************************************
function isDate(dateStr) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
alert("Please enter date as either mm/dd/yy or mm-dd-yy.");
return false;
}

month = matchArray[1]; // p@rse date into variables
day = matchArray[3];
year = matchArray[5];

if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}

if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn`t have 31 days!")
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("February " + year + " doesn`t have " + day + " days!");
return false;
}
}
return true; // date is valid
}

//**************************************************************************************************************************
function NumCheck(field) {
  var checkOK = "0123456789";
  var checkStr = field.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    return (false);
  }
  
  return true;
}

function isnumeric(n) {
  var checkOK = "0123456789";
  var checkStr = n;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    return (false);
  }
  
  return true;
}

function GetESTTime() {
    // create Date object for current location
    d = new Date();
    
    // convert to msec
    // add local time zone offset 
    // get UTC time in msec
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    
    // create new Date object for EST using offset
    nd = new Date(utc + (3600000*(-4)));
    
	return nd;
}

function FireEvent(obj,evt){
	var fireOnThis = obj;
	if( document.createEvent ) {
	  var evObj = document.createEvent('MouseEvents');
	  evObj.initEvent( evt, true, false );
	  fireOnThis.dispatchEvent(evObj);
	} else if( document.createEventObject ) {
	  fireOnThis.fireEvent('on'+evt);
	}
}