/*
 * 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
 *    24 oct 05: added 'init' for page load hook point and
 *               code to help IE with <abbr>
 *               - dhj
 *    11 nov 05: added code for Cookies
 *               - sap
 */

var current_site_survey = "feb-2006";

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,scrollbars=1,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 );
}

function orderRsrchRptRedirect(){
   document.getElementById("order_research_report_redirect").href="/portal/site/ets/menuitem.c988ba0e5dd572bada20bc47c3921509/?vgnextoid=4b4caf5e44df4010VgnVCM10000022f95190RCRD";
}

// 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;
}

/* added for ics-gateway+ */
function toggleId( id ) {
   if( document.getElementById ) {
      // this is the way the standards work
      var style2 = document.getElementById( id ).style;
      style2.display = style2.display ? "" : "block";
   } else if( document.all ) {
      // this is the way old msie versions work
      var style2 = document.all[ id ].style;
      style2.display = style2.display ? "" : "block";
   } else if( document.layers ) {
      // this is the way nn4 works
      var style2 = document.layers[ id ].style;
      style2.display = style2.display ? "" : "block";
   }
}

// 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 );

/* *************************************************************** */
/* Help IE come to terms with the <abbr> tag */

var isIE = (document.all) ? true:false;

function styleAbbr() {
   var oldBodyText, newBodyText, reg
   if( isIE ) {
      oldBodyText = document.body.innerHTML;
      reg = /<ABBR([^>]*)>([^<]*)<\/ABBR>/g;
      newBodyText = oldBodyText.replace(reg, '<ABBR $1><SPAN class=\"abbr\" $1>$2</SPAN></ABBR>');
      document.body.innerHTML = newBodyText;
   }
}

/* *************************************************************** */
/* a generic 'onload' function that can used to call
   other functions */

function etsInitLoad() {
   // styleAbbr();
   if( document.getElementById( 'etssitesurveybanner' ) ) {
      var site_survey_cookie = getCookie( "ets_site_survey" );
      if( site_survey_cookie ) { if( site_survey_cookie == current_site_survey ) { document.getElementById( 'etssitesurveybanner' ).innerText = "" } }
   } // end if(document.getElementById('etssitesurveybanner'))
}

/* *************************************************************** */
/* 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 );
}

/* *************************************************************** */
/* Cookie 'stuff' */

function getCookie(name){     // retrieves a cooke from a user's machine
  var cname = name + "=";     // if cookie not located, it returns a "null" value.
  var dc = document.cookie;
  if (dc.length > 0) {
    begin = dc.indexOf(cname);
    if (begin != -1) {
      begin += cname.length;
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
        return unescape(dc.substring(begin, end));
    }
  }
  return null;
}

function setCookie(name, value, expires, path, domain, secure) {     // sets a cookie on the user's machine
  document.cookie = name + "=" + escape(value) +
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +  // if exipres == null, coookie will expire at end of session
  ((path == null) ? "" : "; path=" + path) +                         // if path == null, cookie will only be accessbile to the directory the creating file was housed in
  ((domain == null) ? "" : "; domain=" + domain) +                   // if domain == null, cookie will only be accessbile to the server the creating file was hosted on.
  ((secure == null) ? "" : "; secure");                              // if secure == null, cookie could be hacked...cookies ARE NOT secure. just leave this one  blank.
}

function delCookie (name,path,domain) {      // deletes named cookie from user's machine
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +        // if path == null, cookie MUST be deleted by the page/directory that created it
    ((domain == null) ? "" : "; domain=" + domain) +  // if domain == null, cookie MUST be deleted by the directory/server that created it.
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

/* [ fin ] */