var DHTML = (document.getElementById || document.all || document.layers);
// returns a object by id  Browse independent

function IsNumeric(sText)     //Number check function
                              // return true if numeric string
                              // return false if not

{
   var ValidChars = "0123456789.";    // valid digits
   var Char;


   for (i = 0; i < sText.length; i++)                     //>loop thru parameter
      {
      Char = sText.charAt(i);                             // get a char
      if (ValidChars.indexOf(Char) == -1)                 // not 0-9
         {
         return false;                                    // return on first no numeric
         }
      }
   return true;

   }

function getObj(name)
{
  if (document.getElementById)
  {
    
	this.obj = document.getElementById(name);
	
	
	
  }
  else if (document.all)
  {
    this.obj = document.all[name];
  }
  else if (document.layers)
  {
    this.obj = document.layers[name];
  }
}

function SetRowToColor(FieldName,ErrorInField)
// set the text of a row to some color  red if error black if not.
{  // start function

  RowName =  FieldName+"Row";                       // get the relative row name
  var Row = new getObj(RowName).obj;              // get the row object
  if (! Row)                                      // error if missing
  {
    alert("Missing Row Lable "+RowName);
    return false;
  }
  if (ErrorInField)                            // check for missing field
    {
      Row.style.color = "red";                   // set text in row to red
      Row.style.fontWeight = 'bold';
    }
     else
    {
      Row.style.color = "black";                  //reset in case it was error before
      Row.style.fontWeight = 'normal';
    }
  return ErrorInField;
}  // end of function SetRowToRed



function Field_Validator()     //Validate Form

{      //start function
	missing = "";								//missing info string 
	ReturnCode = true;                              // Master Return Code
	ErrorMessage = "";
	TheField = new getObj("PSUFirstName").obj;    // find the field in the document
	LNFNReturnCode = ! SetRowToColor("PSUFirstName",TheField.value == "");  // set field to red if true
	
	TheField = new getObj("PSULastName").obj;    // find the field in the document
	LNFNReturnCode = ! SetRowToColor("PSULastName",TheField.value == "")  && LNFNReturnCode; // set field to red if true
	
	if (! LNFNReturnCode)
	{
	ErrorMessage = 'Both First and Last Names must be specified.\n\r';
	}
	TheField = new getObj("PSUPEMail").obj;    // find the field in the document
	EmailReturnCode = ! SetRowToColor("PSUPEMail",TheField.value == ""); // set field to red if true
	
	if (! EmailReturnCode)
	{
	ErrorMessage = ErrorMessage + 'We require a Email Address to contact you.\n\r';
	}
  
	PSUREMail 		= document.getElementById('PSUREMail');
	PSUPEMail 		= document.getElementById('PSUPEMail');
	//alert(PSUREMail.value);
	//alert(PSUPEMail.value);
	if (PSUREMail.value != PSUPEMail.value)
	{	
		EmailReturnCode = false; 
		ErrorMessage = ErrorMessage + 'Your Email address and Please re-enter Email addresses do not match.\n\r';
	}  
  
  TheField = new getObj("PSUTitle").obj;    // find the field in the document
  TitleReturnCode = ! SetRowToColor("PSUTitle",TheField.value == ""); // set field to red if true
  
  if (! TitleReturnCode)
  {
    ErrorMessage = ErrorMessage + 'We require a Title\n\r';
  }
  
  
  
  var PSUCountry = new getObj("PSUCountry").obj;   // Country
  if (PSUCountry.value == "USA")  // Check only US Zips
  {  // USA only

    var PSUZip = new getObj("PSUZip").obj;   // Get Zip Code
    textzip = PSUZip.value;
    lenzip = textzip.length;


    switch(lenzip)                           //Length check
        {
        case 0:   ZipReturnCode = true;break            // No error checking on length
        case 5:   ZipReturnCode = IsNumeric(textzip.substring(1, 5)); break;  //validate for numerics
        case 10:  // validate zip+4
        {
          ZipReturnCode = IsNumeric(textzip.substring(1, 5)) &&
            IsNumeric(textzip.substring(7, 10)) &&
            textzip.substring(6, 6) == '-';


        }
        break
        default:    ZipReturnCode = false;break
        }           // end of switch
        
    if (! ZipReturnCode)
      ErrorMessage = ErrorMessage + 'You entered an incorrect zip code\r Zip codes need to be 5 digits\r or in the zip+4 format of nnnnn-nnnn where n is a digit\r\r';

  } // end of USA ZIP Checking
  
  //start phone number checking 
  var PSUPhoneobj = new getObj("PSUPhone").obj;
  var PSUPhoneNumber = new getObj("PSUPhoneNumber").obj;
  var PSUPhone = PSUPhoneobj.value;
  if (PSUPhone == "")
  {
  	PSUPhone = '9999999999';   //default phone number
	missing = missing + "Phone Number\n";
  }
  phone_reac = /(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$/;  //reg experssion optional something then 3 digits then optional something then 3 digits then optional something then 4 digits then optional something then n digits  area code 
  
  var phonenumbersegmentsac = phone_reac.exec(PSUPhone);  // if good number sub 1 has area, sub 2 exchange, sub 3 number, sub 4 extention 
  PSUPhonesw = false;
  if (phonenumbersegmentsac  && phonenumbersegmentsac[1] != "" && phonenumbersegmentsac[2] != "" && phonenumbersegmentsac[3] !="")  //true if area code xchg number
	 {
	 	PSUPhoneNumber.value = phonenumbersegmentsac[1]+phonenumbersegmentsac[2]+phonenumbersegmentsac[3]+" "+phonenumbersegmentsac[4];
		PSUPhonesw = true;
	 } 	
  else  // not standard AC phone - test local number 
  	 {
	 phone_renoac = /(\d{3})\D*(\d{4})\D*(\d*)$/;  //reg experssion optional something then 3 digits then optional something then 4 digits then optional something then n digits  no area code	 
  	   
	 var phonenumbersegmentsnoac = phone_renoac.exec(PSUPhone);  // if good number sub 1 exchange, sub 2 number, sub 3 extention 
	 if (phonenumbersegmentsnoac && phonenumbersegmentsnoac[1] != "" && phonenumbersegmentsnoac[2] != "")
	 	{
	 		PSUPhoneNumber.value = phonenumbersegmentsnoac[1]+phonenumbersegmentsnoac[2]+" "+phonenumbersegmentsnoac[3];
	 		PSUPhonesw = true;
			missing = missing + "Missing Phone Area code\n";
		}
		
	 }
  if (! PSUPhonesw)
     {
	    ErrorMessage = ErrorMessage + 'You entered an incorrect Phone Number\r\r';
	 }
  PSUSw1_All = document.getElementsByName("PSUSw1_All")[0];  // check switches
 
  TopicsSws = new Array("PSUSw1_1", "PSUSw1_2", "PSUSw1_3", "PSUSw1_4", "PSUSw1_5", "PSUSw1_6", "PSUSw1_7", "PSUSw1_8", "PSUSw1_9");  // names of all letter switches
  if (PSUSw1_All.checked)                     // if the checkall is checked
  {
    for (i in TopicsSws)       // Set all switches to on
    {
      TheSwName = TopicsSws[i];            // get a switch name
      TheSw = document.getElementsByName(TheSwName)[0];     // get its object
      TheSw.checked = true;                  // force it on
    }
  }
  

  
  ReturnCode = ReturnCode && ZipReturnCode && LNFNReturnCode && EmailReturnCode;
  
  //PSUSstreetLine1     
  var PSUSstreetLine1 = new getObj("PSUSstreetLine1").obj;   // Get the street line  
  PSUSstreetLine1 = PSUSstreetLine1.value

  //PSUCity
  var PSUCity= new getObj("PSUCity").obj;   // Get the City line
  PSUCity = PSUCity.value
  
  //PSUState  
  var PSUState= new getObj("PSUState").obj;   // Get the State line
  PSUState = PSUState.value
 
 //PSUZip  
  var PSUZip = new getObj("PSUZip").obj;   // Get the Zip line
  PSUZip= PSUZip.value

 
  
  if (PSUSstreetLine1.length == 0)
  {
  	missing = missing + "Street Address\n";
  }
    
  if (PSUCity.length == 0)
  {
  	missing = missing + "City\n";
  }
 
   if (PSUState.length == 0)
  {
  	missing = missing + "State\n";
  }	
  
  if (PSUZip.length == 0)
  {
  	
  	missing = missing + "Zip\n";
  }	
  SwSw = false;
  for (i in TopicsSws)       // check to see if any sws are on 
  {
	TheSwName = TopicsSws[i];            // get a switch name
	TheSw = document.getElementsByName(TheSwName)[0]; 
	
	
	if (TheSw.checked == true)             // if on
	{
		SwSw = true;
		break;
	}
  }
  if (!SwSw)
  {
  	missing = missing + "proposed Letter Topics";
  }
  if (missing != "")
	{	 
    	message = "Some optional information is missing\n";
   	 	message = message + "This information is needed to effectively contact your elected officials\n";
    	message = message + missing; 
    	ConfirmSw = true;   //default value for confirm in case of missing info
    	if (ReturnCode)
    		{
    		message = message +"\nPlease click cancel to return to the sign up page";
    		message = message +"\nor Ok to proceed with sign up.";
    	
    		// CONFIRM IS BOOLEAN. THAT MEANS THAT
    		// IT RETURNS TRUE IF 'OK' IS CLICKED
    		// OTHERWISE IT RETURN FALSE
    		var ConfirmSw  = confirm(message);
        	}
    	else
    		{
    		alert(message);
    		}	
	}  //missing info processing end
   
    if (! ReturnCode)
  {
    alert('The following errors were found.\r\r'+ ErrorMessage);
  }
  return ReturnCode && ConfirmSw;

}     //end function


var PSUSalutationChangedSw = false;    //false until a value is entered by the user into the Salutation field

function PSUSalutationChanged()
{ 
	PSUSalutationChangedSw = true;
}


var PSUClosingNameChangedSw = false;    //false until a value is entered by the user into the Closing Name field

function PSUClosingNamenChanged()
{ 
	PSUClosingNameChangedSw = true;
}




function PSUNameChanged()
{ 

	 PSUSalutation = new getObj("PSUSalutation").obj; 
 	 PSUTitle= new getObj("PSUTitle").obj;   // Get Title
 	 PSULastName=  new getObj("PSULastName").obj;  // Get the last name line
     PSUFirstName=  new getObj("PSUFirstName").obj;  // Get the last name line
	 PSUClosingName =  new getObj("PSUClosingName").obj;  // Get closing name box 
	 
	if (!PSUSalutationChangedSw)  //do if the partcipant has not entered something
	{
	   //PSUSalutation.value = PSUTitle.value + " " + PSUFirstName.value + " " + PSULastName.value;   //standared Salute 
	   if (PSUFirstName.value.length > 0)  // case first name exists
	   {
	   		PSUSalutation.value = PSUFirstName.value;   
	   
	   }
	   else if (PSULastName.value.length >  0)
	   {
	   		
			PSUSalutation.value = PSUTitle.value + " " + PSULastName.value; 
	   }
	   		
	}

	if (!PSUClosingNameChangedSw)  //do if the partcipant has not entered something
	{
		PSUClosingName.value = PSUFirstName.value + " " + PSULastName.value;  
	}
}



function PSUStdTitleCheck()   // check for standard title
{
	
PSUTitle= new getObj("PSUTitle").obj; 


ssPSUAltTitleSpan = document.getElementById("PSUAltTitleSpan");
ssPSUAltTitle = document.getElementById("PSUAltTitle");
ssPSUAltTitleSpan.style.visibility = 'hidden';

ssPSUAltTitle.selectedIndex = -1;

switch(PSUTitle.value)
{
case 'Mr.': break;
case 'Mrs.': break;
case 'Ms.': break;
case 'Miss.': break;
case 'Miss': break;
default: alert('Some elected officials allow only titles of Mr. Mrs. Ms. Miss\nFor those officials, which title do you wish to use?\nNew Selections should now be visible.\nYour preferred title will be used whenever possible');
ssPSUAltTitleSpan.style.visibility = 'visible';
}

} //end of function 