// Original:  Tom Khoury (twaks@yahoo.com)
// places focus in first text input field
function placeFocus() {
if (document.forms.length > 0) {
var field = document.forms[0];
for (i = 0; i < field.length; i++) {
if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
document.forms[0].elements[i].focus();
break;
         }
      }
   }
}



function trimString(sInString) {
   sInString = sInString.replace( /^\s+/g, "" );// strip leading
   return sInString.replace( /\s+$/g, "" );// strip trailing
}



function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}




function removeSpaces(v){
	// remove all spaces
	while( v.indexOf(" ") > -1 ) v = v.substring( 0, v.indexOf(" ") ) + v.substring( v.indexOf(" ")+1 );
	return v;
}




function limitText(limitField, limitNum) {
	if (limitField.value.length > limitNum) {
		alert("A maximum of " + limitNum + " characters are allowed in this field.");
		limitField.value = limitField.value.substring(0, limitNum);
	}
}



function doreferer(url) {
	if (self.opener && !self.opener.closed)
  		self.opener.document.location = url;
		self.close();
}




function getRandom(min,max)
{
   return (Math.round(Math.random()*(max-min)))+min;
}





function changeImages() {
  if (document.images) {
    for (var i=0; i<changeImages.arguments.length; i+=2) {
      document[changeImages.arguments[i]].src = eval(changeImages.arguments[i+1] + ".src");
    }
  }
}






function openModalWindow(surl, sname, w, h) {
	if (window.showModalDialog) {
		window.showModalDialog(surl, sname, "dialogWidth:" + w + "px;dialogHeight:" + h + "px");
	} else {
		window.open(surl, sname, 'height=' + h + ',width=' + w + ',toolbar=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=no,modal=yes');
	}
}



//--------------------------------------------------------
//from CNET








function openAnyWindow(url, name) {
  var l = openAnyWindow.arguments.length;
  var w = "";
  var h = "";
  var features = "";

  for (i=2; i<l; i++) {
    var param = openAnyWindow.arguments[i];
    if ( (parseInt(param) == 0) ||
      (isNaN(parseInt(param))) ) {
      features += param + ',';
    } else {
      (w == "") ? w = "width=" + param + "," :
        h = "height=" + param;
    }
  }

  features += w + h;
  var code = "popupWin = window.open(url, name";
  if (l > 2) code += ", '" + features; 
  code += "')";
  eval(code);
   
}
//--------------------------------------------------------




// ADAPTED FROM NETSCAPE
      
	var whitespace = " \t\n\r";
	var digits = "0123456789";
	var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
	var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	
      /****************************************************************/

      // Check whether string s is empty.
      function isEmpty(s)
      { return ((s == null) || (s.length == 0)) }

      /****************************************************************/

      function isWhitespace (s)
      {
           var i;

           // Is s empty?
           if (isEmpty(s)) return true;

           // Search through string's characters one by one
           // until we find a non-whitespace character.
           // When we do, return false; if we don't, return true.

           for (i = 0; i < s.length; i++)
           {
                // Check that current character isn't whitespace.
                var c = s.charAt(i);

                if (whitespace.indexOf(c) == -1) return false;
           }

           // All characters are whitespace.
           return true;
      }

      /****************************************************************/

      function ForceEntry(val, str) {
           var strInput = new String(val.value);

           if (isWhitespace(strInput)) {
                alert(str);
				val.focus();
                return false;
           } else
                return true;

      }

      /****************************************************************/


// Returns true if character c is an English letter 
// (A .. Z, a..z).
function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}


function isSym (c)
{	return ( (c == "'" ) || (c == "-") || (c == "_") || (c == " ") || (c == '"') || (c == '.'))
}


function isPeriod(c)
{   return (c == '.') 
}


function isSlash(c)
{   return (c == '/') 
}


// Returns true if character c is a digit 
// (0 .. 9).
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}










// Returns true if character c is a letter or digit.
function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}


function isLetterOrSym(c)
{   return (isLetter(c) || isSym(c))
}


function isLetterOrDigitorSym(c)
{   return (isLetter(c) || isDigit(c) || isSym(c))
}

// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}





function isDecimal(s)  {
	for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or period.
        var c = s.charAt(i);

        if (!(isDigit(c) || isPeriod(c))) return false;
    }

    // All characters are numbers or periods.
    return true;
}





	  
// isAlphabetic (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z, ') only.
function isAlphabetic (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphabetic character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is letter.
        var c = s.charAt(i);

		
		// in SMASH we're allowing apostrophes and dashes
        //if (!isLetter(c))
		if (!isLetterOrSym(c))
        return false;
    }

    // All characters are letters.
    return true;
}




// isAlphanumeric (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) and numbers only.

function isAlphanumeric (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);
		
		
		// in SMASH we're allowing apostrophes and dashes
        //if (! (isLetter(c) || isDigit(c) ) )
		if (! (isLetterOrDigitorSym(c)))
        return false;
    }

    // All characters are numbers or letters.
    return true;
}
	  
	  
	 
	 
	 
	  
//--------------------------------------------------------

function RequireLength(val,len,str) {
	var strInput = new String(val.value);
	
	if (strInput.length < len) {
		alert(str);
		return false;
	} else
		return true;
}






function check_date(field){
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "/";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = DateField.value;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(0,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(2,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 is entered, empty date field, alert user */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 100; day = ""; month = ""; year = ""; seperator = "";
	  //alert("Date is a required field.");
	  //DateField.focus();
	  //return false;
	  return true;
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      DateField.value = month + seperator + day + seperator + year;
	  return true;
   }
   /* Error-message if err != 0 */
   else {
      alert("Date is not formatted correctly (mm/dd/yyyy).");
      DateField.select();
	  DateField.focus();
	  return false;
   }
}



function isDate(s) {
	if (s.search(/^(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)\d\d/) != -1)
    	return true;
    else
        return false;
}   
	
	

//checks if we have a string formatted nn/nn/nnnn where n is a digit
function IsDateString(s) {
	if (s.length != 10) return false;
	for (i=0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!isDigit(c) && !isSlash(c)) return false;
	}
	return true;
}


function IsFutureDate( d ) {
	if (d1=="") return false;
	
	re = /(\d{1,2})\/(\d{1,2})\/(\d{4})/ // Parse the string in MM/DD/YYYY format
	var arr = re.exec( d );
	var dt1 = new Date( parseInt(arr[3]), parseInt(arr[1], 10)-1, parseInt(arr[2], 10));
	return dt1 > new Date();
}


// returns true if date d2 is after or equal to date d1
function IsAfterDate( d1, d2 ) {
	//returns false if both values are not properly formatted strings
	if (!IsDateString(d1)) return false;
	if (!IsDateString(d2)) return false;
	
	
	re = /(\d{1,2})\/(\d{1,2})\/(\d{4})/ // Parse the string in MM/DD/YYYY format
	var arr1 = re.exec( d1 );
	var arr2 = re.exec( d2 );
	var dt1 = new Date( parseInt(arr1[3]), parseInt(arr1[1], 10)-1, parseInt(arr1[2], 10));
	var dt2 = new Date( parseInt(arr2[3]), parseInt(arr2[1], 10)-1, parseInt(arr2[2], 10));
	return dt2 >= dt1;
}



function isEmail(string) {
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}


function isIE() {
	var agt=navigator.userAgent.toLowerCase();
	return((agt.indexOf("msie") != -1));
}

