//  (c) 2002 Duley Hopkins & Associates, Inc. (http://www.dha-us.com)
//  =================================================================
//  File: http://.../global.js
//  Created: 05/10/2002 by L.F.Miller - DHA
//  Last updated: 07/18/2002 by L.F.Miller - DHA
//
//  NOTE: also save on bloodassurancesecure

//--------------------- Globals ---------------------

var base = "";
var path = "";

var IE4 = (document.all && !document.getElementById) ? true : false;
var NS4 = (document.layers) ? true : false; 
var IE5 = (document.all && document.getElementById)  ? true : false; 
var NS6 = (document.getElementById && !document.all) ? true : false; 

var dayArray   = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthArray = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var day2Array  = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var daysArray  = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

var today = new Date();
var thisDay   = today.getDate();
var thisMonth = today.getMonth();
var thisYear  = today.getYear();
thisYear += (thisYear < 1900) ? 1900 : 0;	//fix for Y2K problem in Netscape - 2001 comes in as 101

var todayShort = (thisMonth<10?"0"+thisMonth:thisMonth) + "/" +
                 (thisDay<10?"0"+thisDay:thisDay) + "/" + thisYear;

var dayName   = dayArray[today.getDay()];
var monthName = monthArray[today.getMonth()];

var news;
var message = "";
var thisCalendar;
var thisDate = "none";

//---------------------------------------------------

//===================================================================
  function setBase()
  {
    base = "http://www.bloodassurance.org/";
    path = "/BA/";
//alert("set base: " + base);    
  }

//===================================================================
  function unsetBase()
  {
    base = "";
    path = "";
//alert("unset base: " + base);    
  }

  //===================================================================
  function addBookMark(url, title)
  {
    if (NS6)
      window.sidebar.addPanel(title, url ,"");
    else if (!NS4)
      window.external.AddFavorite(url,title);
  }

//===================================================================
  function createWindow(url, name, width, height)
  {
//alert(url);
    var left = (screen.availWidth - width) / 2;
    var top  = (screen.availHeight - height) / 2;

    if (news)
      if (NS4 || NS6)
      {
        if (news.opener)	//this statement bombs in IE
          news.close();
      }
      else
        news.close();

    news = window.open(url, name, 'resizable,scrollbars,status,width=' + width + ',height=' + height + ',top=' + top + ',left=' + left + ',screenX=' + left + ',screenY=' + top);
  }

  //===================================================================
  function createPopup(url, name, width, height)
  {
//alert(url + " " + name + " " + width + " " + height);
    var left = 100;
    var top  = 60;
//alert(left + " " + top);

    var popup = window.open(url, name, 'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left + ',screenX=' + left + ',screenY=' + top);
 
    popup.focus(); 
    return popup;
  }

  //===================================================================
  function createCalendar(dateField)
  {
    thisMonth = today.getMonth();
    thisDay   = today.getDate();
    thisYear  = today.getYear();
    thisYear += (thisYear < 1900) ? 1900 : 0;		//fix for Y2K problem in Netscape - 2001 comes in as 101
 
    if (eval("document.forms[0]." + dateField + ";"))
    {
      var tempDate = eval("document.forms[0]." + dateField + ".value;");

      if (tempDate != "")
      {
        thisMonth = tempDate.substring(0,2) - 1;
        thisDay   = tempDate.substring(3,5);
        thisYear  = tempDate.substring(6,10);
      }
    }

    if (thisCalendar)
      if (NS4 || NS6)
      {
        if (thisCalendar.opener)	//this statement bombs in IE
          thisCalendar.close();
      }
      else
        thisCalendar.close();

    thisDate = dateField;

    thisCalendar = createPopup("/BA/calendar.html", "Calendar", 350, 260);

//    thisCalendar.focus();
  }

  //===================================================================
  // Format: 9999999999
  //        (999)999-9999 
  //         999-999-9999 
  //===================================================================
  function checkPhone(phone) 
  {
    if (phone.value.length == 0)
      return true;

    var valid = true;
    var totalLength = phone.value.length;

    // check for all numbers
    for (var i=0; i < totalLength; i++)			// valid chars: 0123456789()-
    {
      var thisChar = phone.value.substring(i, i+1);
      if ((thisChar >= "0") && (thisChar <= "9")) continue;
      if (thisChar == "-") continue;
      if (thisChar == "(") continue;
      if (thisChar == ")") continue;
      valid = false;
    }

    if ((totalLength < 10) || (totalLength > 13))
      valid = false;
    else if ((totalLength > 10) && (phone.value.charAt(totalLength-5) != "-"))
      valid = false;
    else if ((totalLength == 12) && (phone.value.charAt(3) != "-" ))
      valid = false;
    else if ((totalLength == 13) && ((phone.value.charAt(0) != "(" ) || (phone.value.charAt(4) != ")" )))
      valid = false;

    if (!valid)
    {
      message  = "ERROR: Phone Number is invalid\n";
      message += "_____________________________________________\n\n";
      message += " - The Phone Number '" + phone.value + "' is invalid\n";
      message += " - A Phone Number must include the area code\n";
      message += "   and have one of the following formats:\n";
      message += " \t 1231231234 \n \t (123)123-1234\n \t 123-123-1234\n";
      alert(message);
      phone.focus();
      return false;
    }
    else if (totalLength == 10)		// reformat phone -> (999)999-9999
    {
      var newPhone = "(" + phone.value.substring(0, 3) + ")" + phone.value.substring(3, 6) + "-" + phone.value.substring(6, 10);
      phone.value = newPhone;
    }
    else if (totalLength == 12)		// reformat phone -> (999)999-9999
    {
      var newPhone = "(" + phone.value.substring(0, 3) + ")" + phone.value.substring(4, 12);
      phone.value = newPhone;
    }

    return true;
  }

  //===================================================================
  // Format: 99999  -or-  99999-9999
  //===================================================================
  function checkZipCode(zipCode) 
  {
    if (zipCode.value.length == 0)
      return true;

    var valid = true;
    var totalLength = zipCode.value.length;

    if ((totalLength != 5) && (totalLength != 10))			// 99999  or  99999-9999
      valid = false;
    else if ((totalLength > 5) && (zipCode.value.charAt(5) != "-"))
      valid = false;
 
    for (var i=0; i < totalLength; i++)					// valid chars: 0123456789-
    {
      var thisChar = zipCode.value.substring(i, i+1);
      if ((thisChar >= "0") && (thisChar <= "9")) continue;
      if (thisChar == "-") continue;
      valid = false;
    }

    if (!valid)
    {
      message  = "ERROR: Zip Code is invalid\n";
      message += "_____________________________________________\n\n";
      message += " - The Zip Code '" + zipCode.value + "' is invalid\n";
      message += " - A Zip Code must have one of the following formats:\n";
      message += " \t 12345 \n \t 12345-1234\n";
      alert(message);
      zipCode.focus();
      return false;
    }
    return true;
  }

  //===================================================================
  // Format: xxxxxx@yyyy.zzz
  //===================================================================
  function checkEmail(email) 
  {
    if (email.value.length == 0)
      return true;

    var locateAt     = email.value.indexOf("@");
    var locatePeriod = email.value.indexOf(".");

    if ((locateAt < 0)  ||                                 	//contains an "@"
        (locateAt == 0) ||                                	//"@" not 1st character
        (locateAt == (email.value.length - 1)) ||          	//"@" not last character
        (email.value.indexOf("@", locateAt + 1) > 0) ||    	//contains only one "@"
        (locatePeriod < 0) ||                               	//contains a "."
        (locatePeriod == (email.value.length - 1)) ||      	//"." not last character
        (locatePeriod < locateAt))                      	//"." is after the "@"
    {
      message  = "ERROR: E-Mail Address is invalid\n";
      message += "_____________________________________________\n\n";
      message += " - The E-Mail Address '" + email.value + "' is invalid\n";
      message += " - An E-Mail Address must have the following format:\n";
      message += " \t name@host.xxx\n";
      alert(message);
      email.focus();
      return false;
    }
    return true;
  }

  //===================================================================
  // Format: 999-99-9999
  //===================================================================
  function checkSSN(ssn) 
  {
    if (ssn.value.length == 0)
      return true;

    var valid = true;
    var totalLength = ssn.value.length;

    if ((totalLength != 9) && (totalLength != 11))  			// 999999999 or 999-99-9999
      valid = false; 
    else if ((totalLength == 11) && 
            ((ssn.value.charAt(3) != "-") || (ssn.value.charAt(6) != "-")))
      valid = false;

    for (var i=0; i < totalLength; i++)					// valid chars: 0123456789-
    {
      var thisChar = ssn.value.substring(i, i+1);
      if ((thisChar >= "0") && (thisChar <= "9")) continue;
      if (thisChar == "-") continue;      
      valid = false;
    }
 
    if (!valid)
    {
      message  = "ERROR: Social Security Number is invalid\n";
      message += "_____________________________________________\n\n";
      message += " - The SSN '" + ssn.value + "' is invalid\n";
      message += " - A SSN must have one of the following formats:\n";
      message += " \t 123121234 \n \t 123-12-1234\n";
      alert(message);
      ssn.focus();
      return false;
    }
    else if (totalLength == 9)		// reformat SSN -> 999-99-9999
    {
      var newSSN = ssn.value.substring(0, 3) + "-" + ssn.value.substring(3, 5) + "-" + ssn.value.substring(5, 9);
      ssn.value = newSSN;
    }
    
    return true;
  }


  //===================================================================
  // Format: MM/DD/YYYY
  //===================================================================
  function checkDate(dateField, futureDateAllowed) 
  {
//alert(dateField.value);

    if (dateField.value.length == 0)
      return false;

    var valid = true;
    var totalLength = dateField.value.length;

    dateField.value = dateField.value.replace("-", "/");
    dateField.value = dateField.value.replace("-", "/");
    dateField.value = dateField.value.replace(" ", "/");
    dateField.value = dateField.value.replace(" ", "/");

    // check for all numbers
    for (var i=0; i < totalLength; i++)
    {
      var thisChar = dateField.value.substring(i, i+1);
      if ((thisChar >= "0") && (thisChar <= "9")) continue;
      if (thisChar == "/") continue;
      valid = false;      
    }

    if ((totalLength < 8) || (totalLength > 10))				// MM/DD/YYYY
      valid = false;

    if (valid)
    {
      // check month
      var testMonth = dateField.value.substring(0, dateField.value.indexOf("/"));
      if ((testMonth < 1) || (testMonth > 12))
        valid = false;

      // check day
      var testDay = dateField.value.substring(dateField.value.indexOf("/")+1, dateField.value.lastIndexOf("/"));
      if ((testDay < 1) || (testDay > 31))
        valid = false;

      if ((testMonth == 4 || testMonth == 6 || testMonth == 9 || testMonth == 11) && (testDay > 30))
        valid = false;
      else if ((testMonth == 2) && (testDay > 29))
        valid = false;

      // check year
      var testYear = dateField.value.substring(dateField.value.lastIndexOf("/")+1, totalLength);
      if ((testYear < 1880) || (testYear > 2100))
        valid = false;
//alert(valid + " > " + testMonth + " " + testDay + " " + testYear);

      if (valid)
      {
        if (testMonth.length == 1)
          testMonth = "0" + testMonth;
        if (testDay.length == 1)
          testDay = "0" + testDay;
        dateField.value = testMonth + "/" + testDay + "/" + testYear;  
      }

    }
    
    if (valid && !futureDateAllowed)
    {
      var testDate = new Date(dateField.value);

      if (testDate > today)
        valid = false;
    }
    
    if (!valid)
    {
      message  = "ERROR: Date is invalid\n";
      message += "_____________________________________________\n\n";
      message += " - The Date '" + dateField.value + "' is invalid\n";
      message += " - A Date must have the following format:\n";
      message += " \t MM/DD/YYYY\n";
      message += " - And cannot be greater than today's date\n";
      alert(message);
      dateField.focus();
      return false;
    }
    
    return true;
  }
