// JavaScript Document
function isImageFile (photo)
{
	if (photo != "" )
	{
    	var ext = getExtension(photo);
       	return ( ext.toLowerCase() == "jpg" || ext.toLowerCase() == "gif" || ext.toLowerCase() == "bmp" || ext.toLowerCase() == "jpeg")
	}
   	else
    {
    	if (isImageFile.arguments.length == 1)
        	{ return true; }
       	else
        	{ return (isImageFile.arguments[1] == true); }
	}
}

function getExtension ( filename )
{
	var dotAt = filename.lastIndexOf(".");
   	if ( dotAt != -1 )
    	{ return filename.substring(dotAt+1,filename.length); }
   	else
    	{ return ""; }
}

function getWindowSize(returnWhat) {
	var myWidth = 0, myHeight = 0;
  	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement &&
		( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
  
	if(returnWhat == "w") {
		return myWidth;  
  	}
  	else {
		return myHeight;  
 	}
}


function openWin (URL, winName, width, height)
{
	if(!width && !height) {
		width  = 520;
		height = 500;
	}
	
	var str;
	str  = "height=" + height + ",innerHeight=" + height;
	str += ",width=" + width + ",innerWidth=" + width;
	if (window.screen) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		
		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;
		
		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
		str += "resizable=no,scrollbars=yes,status=no,menubar=no,toolbar=no";
	}
  	window.open (URL, winName, str);
}

// chkBoxAll ( BOOLEAN chk [, OBJECT tg_form, STRING elemName ] )
// chkBoxAll( true, self.document.frmMailList, 'chkMsgID')
function chkBoxAll (chk) {
	if (chkBoxAll.arguments.length >= 2 )
    	{ var tg_form = chkBoxAll.arguments[1]; }
	else
		{ var tg_form = document.forms[0]; }

	for (var i=0; i<tg_form.elements.length; i++ )
	{
		var elem = tg_form.elements[i];
        if ( elem.type != "checkbox" ) { continue; }
		        
		if ( chkBoxAll.arguments.length >= 3 )
        {
        	var elemName = chkBoxAll.arguments[2];
            if ( elem.name == elemName ) { elem.checked = chk; }
		}
        else
        	{ elem.checked = chk; }
	}
}

// chkBoxCount ( [ OBJECT tg_form, STRING elemName ] )
// chkBoxCount ( self.document.frmMailList, 'chkMsgID')
function chkBoxCount()
{
	var chkCount = 0;

	if ( chkBoxCount.arguments.length >= 1 )
    	{ var tg_form = chkBoxCount.arguments[0]; }
	else
    	{ var tg_form = document.forms[0]; }

	for ( var i=0; i<tg_form.elements.length; i++ ) {
		var elem = tg_form.elements[i];
        if ( elem.type != "checkbox" ) { continue; }

        if ( chkBoxCount.arguments.length >= 2 )
        {
        	var elemName = chkBoxCount.arguments[1];
            if ( elem.name == elemName )
            	{ if ( elem.checked ) { chkCount += 1; } }
		}
        else
        	{ if ( elem.checked ) { chkCount += 1; } }
	}

	return chkCount;
}

// chkBoxValue ( [ OBJECT tg_form, STRING elemName ] )
// chkBoxValue ( self.document.frmMailList, 'chkMsgID')
function chkBoxValue()
{
	var chkValue = "";

	if ( chkBoxValue.arguments.length >= 1 )
    	{ var tg_form = chkBoxValue.arguments[0]; }
	else
    	{ var tg_form = document.forms[0]; }

	for ( var i=0; i<tg_form.elements.length; i++ ) {
		var elem = tg_form.elements[i];
        if ( elem.type != "checkbox" ) { continue; }
		
        if ( chkBoxValue.arguments.length >= 2 ) {
        	var elemName = chkBoxValue.arguments[1];
            if ( elem.name == elemName )
            {
            	if ( elem.checked )
                	{ chkValue = (chkValue==null || chkValue.length==0 || chkValue=="")? elem.value:chkValue+","+elem.value; }
			}
		}
        else {
			if ( elem.checked )
            	{ chkValue = (chkValue==null || chkValue.length==0 || chkValue=="")? elem.value:chkValue+","+elem.value; }
		}
	}

	return chkValue;
}


function selectChange(control, controlToPopulate, ItemArray, GroupArray)
{
  var x ;
  // Empty the second drop down box of any choices
  for (var q=controlToPopulate.options.length;q>=0;q--) controlToPopulate.options[q]=null;
/*
  if (control.name == "brand") {
    // Empty the third drop down box of any choices
    for (var q=frmChoose.aid.options.length;q>=0;q--) frmChoose.aid.options[q] = null;
 }
*/
  var count = 0;

  for ( x = 0 ; x < ItemArray.length  ; x++ )
    {
      if ( GroupArray[x] == control.value )
        {
		controlToPopulate.options[count]=null;
		eval("controlToPopulate.options[count]=" + "new Option" + ItemArray[x]);
		count +=1;
        }
    }
}

var errorList 	=  "";
var errorFocus	= "";

function buildError(errorMsg, errorElem) {
	errorList 	= (errorList == "") ? errorMsg : errorList + "\n" + errorMsg;
	
	if(errorFocus == "") {
		errorFocus = errorElem;
	}
}

function checkTextEntry(field) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789()'@-_,./?!&-+=[]|:{}*^\r\n "
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			field.value = field.value.substring(0, field.value.length - 1);
			field.focus();
		}
	}	
} 

function checkMsgEntry(field) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789()'@-_,./?!&-+=[]|;:^<>\r\n "
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			field.value = field.value.substring(0, field.value.length - 1);
			field.focus();
		}
	}	
}

function checkAlphaEmail(field) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@-_."
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			field.value = field.value.substring(0, field.value.length - 1);
			field.focus();
		}
	}	
} 

function checkAlpha(field) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789()'@-_/,.\r\n "
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			field.value = field.value.substring(0, field.value.length - 1);
			field.focus();
		}
	}	
} 

function checkURL(field) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:/-_[]().? "
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			field.value = field.value.substring(0, field.value.length - 1);
			field.focus();
		}
	}		
} 

function checkUsername(field) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789()-_."
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			field.value = field.value.substring(0, field.value.length - 1);
			field.focus();
		}
	}		
} 

function checkNum(field) {
	var valid = "0123456789"
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			field.value = field.value.substring(0, field.value.length - 1);
			field.focus();
		}
	}	
} 

function upperCase(frmObj) {
	frmObj.value = frmObj.value.toUpperCase();
}

function lowerCase(frmObj) {
	frmObj.value = frmObj.value.toLowerCase();
}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	else 
		countfield.value = maxlimit - field.value.length;
}

function isEmail (s)
  {
   if ( s == "" )
      {
       if (isEmail.arguments.length == 1)
          { return false; }
       else
          { return (isEmail.arguments[1] == true); }
      }

   // there must be >= 1 character before @, so we
   // start looking at character position 1 
   // (i.e. second character)
   var i = 1;
   var sLength = s.length;

   // look for @
   while ( i < sLength && s.charAt(i) != "@" )
     { i++; }

   if ( i >= sLength || s.charAt(i) != "@" )
      { return false; }
   else
      { i += 2; }

   // look for .
   while ( i < sLength && s.charAt(i) != "." )
     { i++; }

   // there must be at least one character after the .
   if ( i >= sLength - 1 || s.charAt(i) != "." )
      { return false; }
   else if ( s.indexOf(",") >= 0 )
      { return false; }

   var validEMChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@";
   return true;
  }