/*
*********************************************************************
File Name:			form_edits.js
Author:				K. Folsom
Description:		Generic form edits
Includes:			None
Maintenance history:
	11/16/2001		KJF		Original version
*********************************************************************
*/

var g_sNum = "01234567890";
var g_sAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var g_sNumFrmt = g_sNum + ",";
var g_sNumCur = g_sNumFrmt + ".";
var g_sAlphaNum = g_sAlpha + g_sNum;

var g_aErr = new Array();
var g_iErrCnt = -1;
var g_iWinHeight = 350;
var g_iWinWidth = 300;
var g_iWinX = ((screen.width/2) - (g_iWinWidth/2));
var g_iWinY = ((screen.height/2) - (g_iWinHeight/2));
var g_sWinAttr = "height=" + g_iWinHeight + ",width=" + g_iWinWidth + ",alwaysRaised=true,dependent=true,left=" + g_iWinX + ",top=" + g_iWinY + ",menubar=false,statusbar=false,location=false,toolbar=false";
var g_wValidate;		

function isValidChar(sValue, sValidCharacters) 
{
	var bValidChars = false;
	for (var I = 0; I < sValue.length; I++) 
	{
		 var sChr = sValue.substr(I, 1);

		 for (var J = 0; J < sValidCharacters.length; J++) 
		 {
			  var sValidChar = sValidCharacters.substr(J, 1);
			  if (sChr == sValidChar) 
			  {
				  bValidChars = true;
				  break;
			  }
		 }

		 if (!bValidChars)
			 break;
	}
	return bValidChars;
}


function formatNum(oFld)
{
	var sVal = stripCommas(oFld.value);
	
	if (!isNumber(sVal))
	{
		oFld.value = "";
		return;	
	}
	
	var numberTest = new NumberFormat(sVal);

	numberTest.setCommas(true);
	numberTest.setCurrency(false);
	numberTest.setPlaces(0);
	oFld.value = numberTest.toFormatted();
}

function stripCommas(sVal)
{
	var sNewVal = "";
	var aVal = sVal.split(",");
	
	for (var i=0;i < aVal.length;i++)
		sNewVal += aVal[i];
	
	return sNewVal;
}

function numberFldChk(oFld)
{
	if (!isNumber(oFld.value))
		oFld.value = "";
}

function isNumber(inputVal)
{
	oneDecimal = false;
	inputStr = inputVal.toString();
	
	for (var i=0;i<inputStr.length;i++)
	{
		var oneChar = inputStr.charAt(i);
		
		if (i == 0 && oneChar == "-")
			continue;
		
		if (oneChar == "." && !oneDecimal)
		{
			oneDecimal = true;
			continue;
		}
		
		if (oneChar < "0" || oneChar > "9")
			return false;
	}
	return true;
}

function getYear(d) { 
  return (d < 1000) ? d + 1900 : d;
  }

function isDate(sVal) {
	if (sVal.length == 0) return true;
	var aVal = sVal.split("/");
	if (aVal.length != 3) return false;
	var month = aVal[0];
	var day = aVal[1];
	var year = aVal[2];
  // month argument must be in the range 1 - 12
  month = month - 1;  // javascript month range : 0- 11
  var tempDate = new Date(year,month,day);
  if ( (getYear(tempDate.getYear()) == year) &&
     (month == tempDate.getMonth()) &&
     (day == tempDate.getDate()) )
      return true;
  else
     return false
}

function compareStartEndDate(sStartMsg,sEndMsg,oStartFld,oEndFld)
{
	this.sErrMsg = sStartMsg + " must precede " + sEndMsg + ".";
	this.oFocusFld = oStartFld;
	this.bValid = true;
	
	if (isDate(oStartFld.value) && isDate(oEndFld.value))
	{
		if (Date.parse(oStartFld.value) > Date.parse(oEndFld.value))
			this.bValid = false;
	}
}

function emailCheck (emailStr)
{
	if (emailStr.length == 0)
		return true;
		
	/* The following pattern is used to check if the entered e-mail address
	   fits the user@domain format.  It also is used to separate the username
	   from the domain. */
	var emailPat=/^(.+)@(.+)$/
	/* The following string represents the pattern for matching all special
	   characters.  We don't want to allow special characters in the address. 
	   These characters include ( ) < > @ , ; : \ " . [ ]    */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	/* The following string represents the range of characters allowed in a 
	   username or domainname.  It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]"
	/* The following pattern applies if the "user" is a quoted string (in
	   which case, there are no rules about which characters are allowed
	   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	   is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")"
	/* The following pattern applies for domains that are IP addresses,
	   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	   e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* The following string represents an atom (basically a series of
	   non-special characters.) */
	var atom=validChars + '+'
	/* The following string represents one word in the typical username.
	   For example, in john.doe@somewhere.com, john and doe are words.
	   Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	   domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	

	/* Finally, let's start trying to figure out if the supplied address is
	   valid. */
	/* Begin with the coarse pattern to simply break up user@domain into
	   different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
	  /* Too many/few @'s or something; basically, this address doesn't
	     even fit the general mould of a valid e-mail address. */
		//alert("Email address seems incorrect (check @ and .'s)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	// See if "user" is valid 
	if (user.match(userPat)==null) {
	    // user is not valid
	    //alert("The username doesn't seem to be valid.")
	    return false
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	   host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	    // this is an IP address
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
		        //alert("Destination IP address is invalid!")
			return false
		    }
	    }
	    return true
	}
	
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		//alert("The domain name doesn't seem to be valid.")
	    return false
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	   three-letter word (like com, edu, gov) or a two-letter word,
	   representing country (uk, nl), and that there's a hostname preceding 
	   the domain or country. */
	
	/* Now we need to break up the domain to get a count of how many atoms
	   it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
	   // the address must end in a two letter or three letter word.
	   //alert("The address must end in a three-letter domain, or two letter country.")
	   return false
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   //alert(errStr)
	   return false
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}

function validDateFld(sMsg,oFld)
{
	this.sErrMsg = sMsg + " must be a valid date in mm/dd/yyyy format.";
	this.oFocusFld = oFld;
	this.bValid = isDate(oFld.value);
}

function validEmail(sMsg,oFld)
{
	this.sErrMsg = sMsg + " is not a valid E-mail address.";
	this.oFocusFld = oFld;
	this.bValid = emailCheck(oFld.value);
}

function eitherOrText(sMsg1,sMsg2,oFld1,oFld2)
{
	this.sErrMsg = sMsg1 + " or " + sMsg2 + " is required.";
	this.oFocusFld = oFld1;
	var bReturn = false;
	
	if ((oFld1.value.length > 0) || (oFld2.value.length > 0))
		var bReturn = true;
	
	this.bValid = bReturn;
}

function validNumber(sMsg,oFld)
{
	this.sErrMsg = sMsg + " must be numeric.";
	this.oFocusFld = oFld;
	this.bValid = isNumber(oFld.value);
}

function validFrmtNumber(sMsg,oFld)
{
	this.sErrMsg = sMsg + " must be numeric.";
	this.oFocusFld = oFld;
	this.bValid = isNumber(stripCommas(oFld.value));
}

function selectRequired(sMsg,oFld)
{
	this.sErrMsg = sMsg + " is required.";
	this.oFocusFld = oFld;
	
	var sVal = this.oFocusFld.options[this.oFocusFld.selectedIndex].value;
	
	if (sVal == "" || sVal == null)
		this.bValid = false;
	else
		this.bValid = true;
}

function checkboxRequired(sMsg,oFld)
{
	this.sErrMsg = "At least one (1) " + sMsg + " is required.";
	this.oFocusFld = oFld;
	this.bValid = false;

	for (var i=0;i<oFld.length;i++)
	{
		if (oFld[i].checked)
		{
			this.bValid = true;
			break;
		}
	}
}

function textRequired(sMsg,oFld)
{
	this.sErrMsg = sMsg + " is required.";
	this.oFocusFld = oFld;
	
	if (this.oFocusFld.value == "")
		this.bValid = false;
	else
		this.bValid = true;
}

function lengthRequiredNbr(sMsg,oFld,iLen)
{
	this.sErrMsg = sMsg + " must contain " + iLen + " numbers.";
	this.oFocusFld = oFld;
	
	var sVal = oFld.value;
	
	if ((sVal.length > 0) && (sVal.length < iLen) && (isNumber(sVal)))
		this.bValid = false;
	else
		this.bValid = true;
}

function noZeroOrNull(sMsg,oFld)
{
	this.sErrMsg = sMsg + " must not be zero.";
	this.oFocusFld = oFld;
	
	if (this.oFocusFld.value == 0)
		this.bValid = false;
	else
		this.bValid = true;
}

function validate(a_fValidFunctions,oFrm)
{
	var bReturn = true;
	var iFontSize = 8;
	
	for (var i=0;i<a_fValidFunctions.length;i++)
	{
		var oValidation = a_fValidFunctions[i];

		if (!oValidation.bValid)
		{
			g_iErrCnt++;
			g_aErr[g_iErrCnt] = new Array(oValidation.oFocusFld, oValidation.sErrMsg);
		}
	}

	if (g_iErrCnt >= 0)
	{
		bReturn = false;
		if (g_wValidate && !g_wValidate.closed)
			g_wValidate.focus();
		else
		{
			if (document.layers)
				iFontSize = 9;
				
			var sHTML = "";
			sHTML += "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>";
			sHTML += "<HTML>";
			sHTML += "<HEAD>";
			sHTML += "<TITLE>Rixson Specialty Door Controls</TITLE>";
			sHTML += "<STYLE>";
			sHTML += "A { color: #FFFFFF; }";
			sHTML += "TABLE { font-family: Arial,helvetica,sans-serif; font-size: " + iFontSize + "pt; }";
			sHTML += "TD { font-family: Arial,helvetica,sans-serif; font-size: " + iFontSize + "pt; color: #FFFFFF; }";			
			sHTML += "TD.HDR { font-color: #FFD700; font-weight: bold; }";		
			sHTML += "</STYLE>";
			sHTML += "</HEAD>";
			sHTML += "<BODY bgcolor='#000000'>";
			sHTML += "<TABLE border='0' cellpadding='0' cellspacing='0' width='100%'>";
			sHTML += "<TR><TD colspan='2'><font color='#CC9966'><b>The following errors were encountered</b>:</font></TD></TR>";
			sHTML += "<TR><TD colspan='2'>&nbsp;</TD></TR>";
			
			for (var i=0;i<g_iErrCnt+1;i++)
			{
				sHTML += "<TR><TD valign='top'><LI>&nbsp;</LI></TD><TD>";
				sHTML += "<a href='javascript:opener.document." + oFrm.name + "." + g_aErr[i][0].name;
				if (g_aErr[i][0].type.indexOf("text") > -1)
					sHTML +=  ".focus();";
				else
					sHTML +=  ".focus();";
				sHTML += "window.close();'>" + g_aErr[i][1] + "</a><br>";		
				sHTML += "</TD></TR>";
			}
			
			sHTML += "</TABLE>";
			sHTML += "</BODY>";
			sHTML += "</HTML>";
		
			g_wValidate = window.open("","ErrWin","width=400,height=300,scrollbars=yes,screenX=1,screenY=1");
			g_wValidate.document.open();
			g_wValidate.document.write(sHTML);
			g_wValidate.document.close();
			g_wValidate.focus();
		}
	}
	
	g_iErrCnt = -1;
	return bReturn;
}

function validate2(a_fValidFunctions,oFrm)
{
	var bReturn = true;
	var iFontSize = 8;
	
	for (var i=0;i<a_fValidFunctions.length;i++)
	{
		var oValidation = a_fValidFunctions[i];

		if (!oValidation.bValid)
		{
			g_iErrCnt++;
			g_aErr[g_iErrCnt] = new Array(oValidation.oFocusFld, oValidation.sErrMsg);
		}
	}

	if (g_iErrCnt >= 0)
	{
		bReturn = false;
		
		var sHTML = "";

		sHTML += "The following errors were encountered:\n\n";
		
		for (var i=0;i<g_iErrCnt+1;i++)
		{
			sHTML += "-  " + g_aErr[i][1] + "\n";
			if (i > 30) 
			{
				sHTML += "\n...plus " + (g_iErrCnt - 30) + " additional errors.";
				break;
			}
		}
		alert(sHTML);
	}
	
	g_iErrCnt = -1;
	return bReturn;
}

function closeValidWin()
{
	if (g_wValidate && !g_wValidate.closed)
		g_wValidate.close();
}


