//--------------------- Last Modified --> 2006.11.10 ---------------------//
//------------------------------------------------------------------------------------//
function cityClick(args) {                                               
	argar = args.split('=='); 
		if (argar[2]=="Single") {
			document.location.href = "http://www.helpforvisionloss.com/centers/directory/clinic?id="+argar[1];
		} else {
			document.location.href = "http://www.helpforvisionloss.com/centers/directory/?city=" + argar[0] + "&map_state=" + argar[1];
		} 
}

function attachPhoneHideEvents () {
	// need to add error checking
	var numberRegExp = new RegExp('(^| )number( |$)');
	var noneRegExp = new RegExp('(^| )none( |$)');
	
	if (document.getElementById) {
		if(document.getElementById('chkbx')){
			document.getElementById('chkbx').onclick = function (){
				var phoneLabel = document.getElementById('phone_number');
				var fphone1 = document.getElementById('fphone1');
				var fphone2 = document.getElementById('fphone2');
				var fphone3 = document.getElementById('fphone3');
			
				phoneLabel.className=phoneLabel.className.replace(noneRegExp, "");
				if(!numberRegExp.test(fphone1.className))
					fphone1.className += " number";
				if(!numberRegExp.test(fphone2.className))
					fphone2.className += " number";
				if(!numberRegExp.test(fphone3.className))
					fphone3.className += " number";
				};
		}
		if(document.getElementById('chkbx1')){
			document.getElementById('chkbx1').onclick = function (){
				var phoneLabel = document.getElementById('phone_number');
				var fphone1 = document.getElementById('fphone1');
				var fphone2 = document.getElementById('fphone2');
				var fphone3 = document.getElementById('fphone3');
			
				if(!noneRegExp.test(phoneLabel.className))
					phoneLabel.className += "none";
				fphone1.className=fphone1.className.replace(numberRegExp, "");
				fphone2.className=fphone2.className.replace(numberRegExp, "");
				fphone3.className=fphone3.className.replace(numberRegExp, "");
				};
		}
	}
}
addLoadEvent(attachPhoneHideEvents);
								
				

function yesCall() { 
	var phoneLabel = document.getElementById('phone_number');
	var fphone1 = document.getElementById('fphone1');
	var fphone2 = document.getElementById('fphone2');
	var fphone3 = document.getElementById('fphone3');

	phoneLabel.className=phoneLabel.className.replace(new RegExp("none"), "");
	fphone1.className += " number";
	fphone2.className += " number";
	fphone3.className += " number";
}

function noCall() { 
	var phoneLabel = document.getElementById('phone_number');
	var fphone1 = document.getElementById('fphone1');
	var fphone2 = document.getElementById('fphone2');
	var fphone3 = document.getElementById('fphone3');

	phoneLabel.className = "none"; 
	fphone1.className=fphone1.className.replace(new RegExp("(\s)*number"), "");
	fphone2.className=fphone2.className.replace(new RegExp("(\s)*number"), "");
	fphone3.className=fphone3.className.replace(new RegExp("(\s)*number"), "");
}

//------------------- Son-Of-Sucker-Fish IE Hack -------------------//
sfHover = function() {
	if (document.getElementById("nav")) {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		// for each list item in the menu...
		for (var i=0; i < sfEls.length; i++) {
			// Is this IE7?  If so, use onmouseleave to fix the fact that onmouseout won't fire
			is_IE7 = navigator.appVersion.indexOf("MSIE 7.0") != -1;
			
			sfEls[i].onmouseover = function() {
				this.className+=" sfHover";
				// is this a top-level menu item?
				var child_ul = this.getElementsByTagName('ul')[0];
				if (child_ul && is_IE7){
					// fix for IE7
					child_ul.style.position = 'static';
				}
			}
			
			sfEls[i].onmouseleave = function() {
			// is this a top-level menu item?
				var child_ul = this.getElementsByTagName('ul')[0];
				if (child_ul && is_IE7){
					// fix for IE7
					child_ul.style.position = 'absolute';
					child_ul.style.left = '-9000px';
				}
			}
			
			sfEls[i].onmouseout = function() {
				this.className=this.className.replace(new RegExp(" sfHover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


function clearText(thefield) {
  if (thefield.defaultValue==thefield.value) { thefield.value = "" }
} 
function replaceText(thefield) {
  if (thefield.value=="") { thefield.value = thefield.defaultValue }
}


addLoadEvent(attachFormHandlers);

function attachFormHandlers()
{
  // Ensure we're working with a 'relatively' standards 
  // compliant browser
  if (document.getElementsByTagName)
  {
    var objForm = document.getElementsByTagName('form');

    for (var iCounter=0; iCounter<objForm.length; iCounter++)
      objForm[iCounter].onsubmit = function(){return checkForm(this);}
  }
}

function checkForm(objForm)
{
  var arClass, bValid;
  var objField = objForm.getElementsByTagName('*');
	var errorMessage = '';

  for (var iFieldCounter=0; iFieldCounter<objField.length; iFieldCounter++)
  {
    // Allow for multiple values being assigned to the class attribute
    arClass = objField[iFieldCounter].className.split(' ');
    for (var iClassCounter=0; iClassCounter<arClass.length; iClassCounter++)
    {
      switch (arClass[iClassCounter])
      {
        case 'string':
           bValid = isString(objField[iFieldCounter].value.replace(/^\s*|\s*$/g, ''));
           break;
        case 'number' :
           bValid = isNumber(objField[iFieldCounter].value);
           break;
        case 'email' :
           bValid = isEmail(objField[iFieldCounter].value);
					 break;
        case 'select' :
           bValid = isSelected(objField[iFieldCounter].value);
					 break;
        default:
           bValid = true;
      }

      if (bValid == false)
      {
        // If this field is invalid, leave the testing early,
        // and alert the visitor to this error
				var errorMessageArray=new Array();
				errorMessageArray['contact_first_name']="Please enter your First name";
				errorMessageArray['contact_last_name']="Please enter your Last name";
				errorMessageArray['contact_email']="Please enter a valid Email address";
				errorMessageArray['i_am_a']="Please select an option from 'I am a'";
				errorMessageArray['how_did_you_hear_about_us']="Please select an option from 'How did you hear about us?'";
				errorMessageArray['contact_comment']="Please enter a Question/Comment";
				errorMessageArray['contact_phone_area']="Please enter the area code of your phone number";
				errorMessageArray['contact_phone_three']="Please enter the first three digits of your phone number";
				errorMessageArray['contact_phone_four']="Please enter the last four digits of your phone number";

       // alert(errorMessageArray[objField[iFieldCounter].name]);
        //objField[iFieldCounter].select();
        objField[iFieldCounter].focus();
        return false;
      }
    }
  }
  return true;
}

function isString(strValue)
{
  return (typeof strValue == 'string' && strValue != '' && isNaN(strValue));
}

function isNumber(strValue)
{
  return (!isNaN(strValue) && strValue != '');
}

function isEmail(strValue)
{
  var objRE = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;

  return (strValue != '' && objRE.test(strValue));
}
function isSelected(strValue)
{
  return (strValue != '');
}
