/*
 * default.js
 *
 * contains scripting for ets.org
 * this file should be included in every ets.org page
 *
 * history:
 *    15 feb 05: file created.
 *               - dhj
 *    01 jun 05: updated with search validation code and
 *               window popup code from dhj.
 *               - my
 *    13 jul 05: added image rotation coding for ets home page
 *               - dhj
 *    10 aug 05: added form validation functions.
 *               - dhj
 *
 */


function changeImages() {  // the rollover code
   if( document.images ) {
      for( var i=0; i<changeImages.arguments.length; i+=2 ) {
         document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
      }
   }
}

/* pulled from http://www.accessify.com/tutorials/the-perfect-pop-up.asp */
function popUp( strURL, strType, strHeight, strWidth ) {
   var strOptions = "";
   if( strType == "console" ) { strOptions = "resizable,height="+strHeight+",width="+strWidth; }
   if( strType == "fixed" ) {   strOptions = "status,height="+strHeight+",width="+strWidth; }
   if( strType == "elastic" ) { strOptions = "toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth; }
   window.open( strURL, 'newWin', strOptions );
}

// whitespace characters
var whitespace = " ";

function validateSearch(boxName, name) {
   boxValue = boxName.value;
   boxLength = boxName.value.length;
   //alert(boxLength);
   boxSubString = (boxValue.substring(0,boxLength));

   if (boxValue == "" || boxLength < 2 ) {
      alert("Please enter valid " + name);
      return false;
   } else {
      for (j = 0; j < boxLength; j++) {
         eachChar = boxSubString.substring(j,j+1);

         if (whitespace.indexOf(eachChar) ==-1 ) {
            if ( (eachChar < "0" || eachChar >"9") && (eachChar < "a" || eachChar > "z" )
                 &&( eachChar < "A" || eachChar > "Z") && (eachChar != "'" ) &&  (eachChar != "-" ) && (eachChar != "_" ) && (eachChar != "(" ) && (eachChar != ")" ) && (eachChar != "&" )
                 && (eachChar != "," ) && (eachChar != ".")) {
               alert("You may enter only letters (UPPER or lower case), numbers and special characters like ' , _ - & . () in " + name +" field");
               boxName.value = "";
               boxName.focus();
                return false;
            }
         }
      }
   }

   return true;
}

// Image rotation stuff for the home page.
// Should be done in something other than JavaScript
//
var images = 4;   // Number of alternative images
var imgList = new Array( images );

imgList[0] = "/Media/Home/img/main_elementary.jpg";
imgList[1] = "/Media/Home/img/main_college.jpg";
imgList[2] = "/Media/Home/img/main_ell.jpg";
imgList[3] = "/Media/Home/img/main_highschool.jpg";

function pickRandom( range ) {
   if( Math.random ) {
      return Math.round(Math.random() * (range-1));
   } else {
      var now = new Date();
      return (now.getTime() / 1000) % range;
   }
}

var choice = pickRandom( images );


/* *************************************************************** */
/* Form validation 'stuff' */
var errorMsg = "";

//validates fields for letters, numbers and dashes
function validateHasTextOnly( formField ) {
   re = /[a-zA-Z0-9 \-]+/;
   return re.test( formField );
}

//validates field has some content
function validateHasText( formField ) {
   re = /\w+/;
   return re.test( formField.value );
}

function validateHasNumOnly( formField ) {
   re = /[0-9]+/g;
   return re.test( formField );
}

function validateIsSSN ( val1, val2, val3 ) {
   ssn = val1 + "-" + val2 + "-" + val3;
   re = /\d\d\d-\d\d-\d\d\d\d/;
   if( !re.test( ssn ) ) {
      errorMsg += "Social Security Numbers consist of 10 digits, please correct the numbers you entered.\n";
      return false;
   } else {
      return true;
   }
}

// validate zip and zip+4
// return true/false
function validateIsZip( formField ) {
   re = /(^\d{5}$)|(^\d{5}[-| ]{1}\d{4}$)/;
   return re.test( formField );
}

// validate phone/fax numbers
// reformat into (xxx) xxx-xxxx and return
// or return false
function validateIsPhone( formField ) {
   re = /^[\(]{0,1}([1-9]\d{2})[\)]{0,1}[\-| |\.|\/|\\]{0,1}(\d{3})[\-| |\.|\\|\/]{0,1}(\d{4})$/;
   if( re.test( formField ) ) {
      formField = formField.replace( re, "($1) $2-$3");
      return formField;
   } else {
      return false;
   }
}

function validateIsEmail( formField ) {
   re = /^[^@]+@[^@]+.[a-z]{2,}$/i;
   return re.test( formField.value );
}

function validateRequiredRadio( formField ) {
   for( x = 0; x < formField.length; x++ ) {
      if( formField[x].checked == true ) {
         return true;
      }
   }
   return false;
}

function validateRequiredCheckBox( formField ) {
   return formField.checked;
}

// programName is the name of the program being applied for.
// no spaces or punctuation at the end of the program name(s).
function notACitizen( programName) {
   alert( "If you are not a United States citizen, or a legal alien, you are not eligible to score " +
           programName + "." )
}

function validateCreditCard( formField ) {
   re = /(^\d{4}([\-| ]?)\d{4}([\-| ]?)\d{4}([\-| ]?)\d{4}$)/;
   return re.test( formField );
}

function validateCreditCardDate( formField ) {
   re = /(^\d{2}[\/]{1}\d{4}$)/;
   return re.test( formField );
}

// validate date
// reformat into xx/xx/xx and return
// or return false
function validateShortDate( formField ) {
   re = /(^\d{2}[\/]{1}\d{2}[\/]{1}\d{2}$)/;
   return re.test( formField );
}

/* [ fin ] */

