var NS4 = (document.layers) ? true : false;
var IE4 = (document.all) ? true : false;
var NS4MAC = NS4 && (navigator.appVersion.indexOf("Macintosh") > -1);
var size = 8;
var active = false;
var date;
//

if (NS4) document.captureEvents(Event.KEYUP);
document.onkeyup = checkKey;
var style = 2;

function checkKey(e)
{
   if (!active)
   {
                // active is turned on when the search text box recieves focus
                // it is turned off when the search text box loses focus
                return true;
                //alert('active = false');
   }
}


         function check_reg_form(frm){
                // check the form for errors

                var bChecked=false;
                var blank= new String();
                var error= new String();
                bChecked=false;

                // f name
                if(frm.f_name.value.length == 0){
                        blank+='First Name\n';
                }

                // l name
                if(frm.l_name.value.length == 0){
                        blank+='Last Name\n';
                }
                
                //credentials
                //Title
                //if(frm.credentials.value.length == 0){
                //        blank+='Credentials\n';
                //}
                
                //Title
		//if(frm.title.value.length == 0){
                //        blank+='Title\n';
                //}

                //Organization_1
                if(frm.org1.value.length == 0){
                        blank+='Organization 1\n';
                }
                
                // address
                if(frm.address1.value.length == 0){
                        blank+='Address 1\n';
                }

                // contry
                if(frm.country.value.length == 0){
                        blank+='Country \n';
                }
                // city
                if(frm.city.value.length == 0){
                        blank+='City\n';
                }

                // state
                //alert(frm.state.options[frm.state.selectedIndex].value.length);
                if(frm.state.options[frm.state.selectedIndex].value.length ==0){
                        blank +='State\n';
                }

                // zip
                if(frm.zip.value.length == 0){
                        blank+='Zip Code\n';
                }
                
                // phone
                if(frm.phone.value.length == 0){
                        blank+='Telephone\n';
                }
                
                //// phone  validation
                //if(frm.phone.value.length > 0){
                //      if(frm.phone.value.length >0 &&  frm.hid_phone.value == 'n/a'){
                //        error+='Telephone (10-digits number)\n';
                //        temp=frm.phone.value;
                //        temp = replace(temp,'(','');
                //        temp = replace(temp,')','');
                //        temp = replace(temp,'-','');
                //        frm.phone.value = temp;
                //     }
                //}

                //// fax validation
                //if(frm.fax.value.length != 0){
                //      if(frm.fax.value.length >0 &&  frm.hid_fax.value == 'n/a'){
                //        error+='Fax (10-digits number)\n';
                //        temp=frm.fax.value;
                //        temp = replace(temp,'(','');
                //        temp = replace(temp,')','');
                 //       temp = replace(temp,'-','');
                 //       frm.fax.value = temp;
                 //    }
                //}

                // email
                if(frm.email.value.length == 0){
                        blank+='Email Address\n';
                }
                //alert(error.length);
                if(blank.length>0){
                    alert('This form has several required fields. Your data has been checked and the following fields are blank:\n\n'+blank+'\n\nPlease fill in the fields listed and re-submit the form');
                    return false;
                }else{
                    if(error.length>0){
                            alert('This form has one or more invalid fields. Your data have been checked, and the following fields are invalid:\n\n'+error+'\n\nPlease check the fields listed and re-submit the form');
                            return false;
                    }else{
                    
                            if(verify_email(frm.email.value.trim())){
                                    return true;
                            } else {
                                    alert ('The email address you entered does not appear to be valid. Please double check it.');
                                    frm.email.focus();
                                    return false;
                            }
                    }
                }
        }

        function verify_email(str) {
          //field.value.match(/\b(^(\S+@).+( (\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)| (\..{2,2}) )$)\b/gi);
                // ends with: +( (\.com)|(\.biz)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)| (\..{2,2}) )$
                if(!str.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,3}$/i)) {
                        return false;
                } else {
                        return true;
                }
        }
                //  <!-- Begin
                        function formatCurrency(num) {
                        num = num.toString().replace(/\$|\,/g,'');
                        if(isNaN(num))
                        num = "0";
                        sign = (num == (num = Math.abs(num)));
                        num = Math.floor(num*100+0.50000000001);
                        cents = num%100;
                        num = Math.floor(num/100).toString();
                        if(cents<10)
                        cents = "0" + cents;
                        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
                        num = num.substring(0,num.length-(4*i+3))+','+
                        num.substring(num.length-(4*i+3));
                        return (((sign)?'':'-') + num + '.' + cents);
                        //return (((sign)?'':'-') + '$' + num + '.' + cents);
                        //return (((sign)?'':'-') + num);
                        }
                 //  End -->
                function replaceChars(entry) {
                       out = ","; // replace this
                       add = ""; // with this
                       temp = "" + entry; // temporary holder

                       while (temp.indexOf(out)>-1) {
                       pos= temp.indexOf(out);
                       temp = "" + (temp.substring(0, pos) + add +
                       temp.substring((pos + out.length), temp.length));
                       }
                       return temp;
                }



                      function window_open(http)
                      {
                           //alert(http);
                           //window.open (http, 'newwindow', config='height=300,width=400, toolbar=1, menubar=1, scrollbars=1, resizable=1,location=1, directories=1, status=1')
                           myRef = window.open(http+self.location,'mywin','left=20,top=20,width=600,height=300,toolbar=1,resizable=0');
                      }





                 //-- This function opens the new, empty window named mission.

        function openWindow()
        {
                winStats='toolbar=no,location=no,directories=no,menubar=no,'
                winStats+='scrollbars=no,width=510,height=350'
                if (navigator.appName.indexOf("Microsoft")>=0)
                {
                        winStats+=',left=10,top=25'
                }
                else
                {
                        winStats+=',screenX=10,screenY=25'
                }
                mission=window.open("","",winStats)
                mission.document.writeln ("<title>SHIP MISSION</title>")
                mission.document.writeln ("<body background='pillslarge.jpg'>")
                mission.document.writeln ("<br><center><H2>SHIP MISSION</H2></center><br>")
                mission.document.writeln ("<br><center>To insure that healthcare consumers have a competent, committed,<br><br> compassionate consumer-focused network of staff and volunteers who<br><br> provide accurate and objective information through innovative<br><br> community programs at the state and local level. We promote fairness<br><br> and quality, and empower consumers by facilitating solutions to<br><br> individual and systemic health benefits problems.</center> ")
                mission.document.writeln ("</body>")
                mission.document.close()
                mission.focus()
        }

        function numbersonly(myfield, e, dec)
                               {
                               var key;
                               var keychar;

                               if (window.event)
                                  key = window.event.keyCode;
                               else if (e)
                                  key = e.which;
                               else
                                  return true;
                               keychar = String.fromCharCode(key);
                               // control keys
                               if ((key==null) || (key==0) || (key==8) ||
                                   (key==9) || (key==13) || (key==27) )
                                  return true;
                               // numbers
                               else if (((",-0123456789").indexOf(keychar) > -1))
                                  return true;

                               // decimal point jump
                               else if (dec && (keychar == "."))
                                  {
                                  myfield.form.elements[dec].focus();
                                  return false;
                                  }
                               else
                                  return false;
                               }



            function telePhone(m,n){
              str = m.value;
              if(!validateUSPhone(m.value)){
                 num=m.value;
            
                 n.value=m.value;
            
                 if (m.value.length == 10 && IsNumeric(m.value,false)) {
                   num = "(" + str.substring(0,3) + ")" + str.substring(3,6) + "-" + str.substring(6,10);
                 }else{
                    if(m.value.length>0){
                       n.value="n/a";
                    }
                 }
                 m.value="";
                 m.value=num;
              }
            }

                function IsNumeric(strString)
   //  check for valid numeric strings
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

function trim(s){
s = s.replace(/^\s*/,'').replace(/\s*$/, '');
return s;
}
   function validateUSPhone( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains valid
  US phone pattern.
  Ex. (999) 999-9999 or (999)999-9999

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/
  var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

  //check for valid us phone with or without space between
  //area code
  return objRegExp.test(strValue);
}

function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

