/* Functions in this file
	getObj			22
	colorText		41
	fourNumbers		52
	isInteger		65
	isNumeric		65
	errorMessage	89
	notEmpty		112
	notEmptyRadio
	addError		199
	isemail			210	
*/


/*--------------------------------------------------------------------- 
  getObj() returns the requested object
-----------------------------------------------------------------------*/

var DHTML = (document.getElementById || document.all || document.layers);

function getObj(name)
{
//alert('name = ' + name);
  if (document.getElementById)
  {
 		this.obj = document.getElementById(name);
//alert('this.obj = ' + this.obj);
		this.style = document.getElementById(name).style;
		this.value = document.getElementById(name).value;
//alert('this.id = ' + document.getElementById(name).id);
//alert('this.value = ' + document.getElementById(name).value);
  }
  else if (document.all)
  {
		this.obj = document.all[name];
		this.style = document.all[name].style;
  }
/*  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
  */
}

/*------------------------------------------------------------
 colorText() changes the indicated text to the selected color
--------------------------------------------------------------*/
function colorText(txt, txtcolor)
{
		if (!DHTML) return;
//alert('txt = ' + txt);
		var x = new getObj(txt);
		x.style.color = txtcolor;
//alert('txtcolor = ' + txtcolor);
}
/*---------------------------------------------------------------
 fourNumbers() Limits the number of characters in field1 to 4
  then skips to field2
-----------------------------------------------------------------*/
function fourNumbers(field1, field2)
{
	var x = new getObj(field1);
	var letters=x.value.length + 1;
	if(letters>4)
	{
		document.getElementById(field2).focus();
	}
}

/*---------------------------------------------------------
 isNumber() Verifies that the data in the field is numeric
-----------------------------------------------------------*/
function isInteger(fld)
{
//alert('name = ' + fld);
  var parsed = true;
  var validchars = "0123456789";
    
  for (var i=0; i <fld.length; i++) 
  {
    var letter = fld.charAt(i);
    if (validchars.indexOf(letter) != -1)
    {
      continue;
    }
    else
    {
    	parsed = false;
    	break;
    }
  }
  return parsed;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}

/*---------------------------------------------------------------
  errorMessage opens a window and writes the error messages
-----------------------------------------------------------------*/
function errorMessage(errorField, Error_Msg1, Error_Msg2)
{
	var errorWin=window.open("errorpage.htm",'error_win','height=300,width=300,toolbar=yes,menubar=no,location=no,scrollbar=yes');
    var nbErrs;
	if (errorWin==null || typeof(errorWin)=="undefined") {
		alert("You HAVE A POPUP BLOCKER");
		return false;
	}else {
	
		errorWin.document.write("<html>");
		errorWin.document.write("<b>" + "<font color = 'red'>");
		errorWin.document.write(Error_Msg1);
		errorWin.document.write("<br>");
		errorWin.document.write("</b>" + "</font>");
		for(nbErrs = 1; nbErrs <= errorField[0]; nbErrs++)
		{
			errorWin.document.write("<b>" + "<font color = 'red'>" + "&nbsp;&nbsp;&nbsp;");
			errorWin.document.write(errorField[nbErrs]);
			errorWin.document.write("</font>" + "</b>" + "<br>");
		}
		errorWin.document.write("<br>");
		errorWin.document.write("<A href='javascript://' onclick='javascript:self.close()'>");
		errorWin.document.write(Error_Msg2 + "</A>");
    	errorWin.document.write("</html>");
		errorWin.moveTo(0,0);
		errorWin.focus();
	}
}

/*------------------------------------------------------------
	notEmpty looks at an arr FldName to see if any of the fields
	are empty, if yes, it highlights the associated text,
	FldText, the sets the focus to the field
*/
function notEmpty(FldName, FldText, first, last, errorField)
{
//alert('In notEmpty');
	for(var i=first;i<last+1;i++)
	{
//alert('FldName['+i+'] = ' + FldName[i]);
		var x = new getObj(FldName[i]);
		var val = x.value;
//alert("val = " + val);
//alert("notEmptyval = " + FldName[i]);
		var divID = 'D' + FldName[i];
		colorText(divID, '#000000');
		if(val.length < 1)
		{
			colorText(divID,'#cc0000');
			errorField[0] = errorField[0] + 1;
//errorField[0] is the number of errors
//Add the Field Text to the end of the errorField array
			errorField[errorField[0]] =  FldText[i];
			if(errorField[0] == 1)
			{
				document.getElementById(FldName[i]).focus();
			}
		}
	}
	return errorField;
}

function notEmptyRadio(fld, FldName, FldText, errorField)
{
//alert('In notEmptyRadio');
	var x =fld;
	var divID = 'D' + FldName;
	colorText(divID, '#000000');
	var myOption = -1;
//alert('xlen = ' + x.length);
	for (i=0; i<x.length; i++) 
	{
//alert(x.name + ' = ' + x[1].checked);
		if (x[i].checked) 
		{
			myOption = i;
		}
	}
//alert('myOption  = ' + myOption);
	if (myOption == -1)
	{
		colorText(divID, "#cc0000");
		errorField[0] = errorField[0] + 1;
		errorField[errorField[0]] =  FldText;
		if(errorField[0] == 1)
		{
			document.getElementById(FldName).focus();
		}
	} 
	return errorField;
}

//addError adds an error to the errorField array
function addError(errorField, txt)
{
	var errorFieldSize;
	errorFieldSize = errorField.length;
	errorField[0] = errorField[0] + 1;
	errorField[errorField[0]] = txt;
}

//--------------------------------------------------------------
// isemail verifies that the input follows the rule xxx@xxx.xxx
//--------------------------------------------------------------

function isemail(email)
{
//alert('In isemail');
	var atChar = 0;
	var dotChar = 0;
	
	for (i = 0; i < email.length; i++) 
	{ 
		Char = email.charAt(i); 
		if (Char == '@') 
		{
			atChar = i;
		}
		if(Char == ".")
		{
			dotChar = i;
		}
		if(Char == ",")
		{
			return false;
		}
		if(ascii_value(Char) > 122)
		{
			return false;
		}
		
	}
	if(atChar < 1)
	{
		return false;
	} else if(dotChar < atChar)
	{
		return false;
	} else
	{
		return true;
	}
}
function ascii_value(c)
{
	// restrict input to a single character
	c = c . charAt(0);

	// loop through all possible ASCII values
	var i;
	for(i = 0; i < 256; ++ i)
	{
		// convert i into a 2-digit hex string
		var h = i . toString (16);
		if (h . length == 1)
			h = "0" + h;

		// insert a % character into the string
		h = "%" + h;

		// determine the character represented by the escape code
		h = unescape (h);

		// if the characters match, we've found the ASCII value
		if (h == c)
			break;
	}
	return i;
}

function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}



