
function WAAddError(formElement,errorMsg,focusIt,stopIt)  {
  if (document.WAFV_Error)  {
	  document.WAFV_Error += "\n" + errorMsg;
  }
  else  {
    document.WAFV_Error = errorMsg;
  }
  if (!document.WAFV_InvalidArray)  {
    document.WAFV_InvalidArray = new Array();
  }
  document.WAFV_InvalidArray[document.WAFV_InvalidArray.length] = formElement;
  if (focusIt && !document.WAFV_Focus)  {
	document.WAFV_Focus = focusIt;
  }

  if (stopIt == 1)  {
	document.WAFV_Stop = true;
  }
  else if (stopIt == 2)  {
	formElement.WAFV_Continue = true;
  }
  else if (stopIt == 3)  {
	formElement.WAFV_Stop = true;
	formElement.WAFV_Continue = false;
  }
}

function WAValidatePN(formElement,errorMsg,areaCode,international,reformat,focusIt,stopIt,required)  {
  var value = formElement.value;
  var isValid = true;
  var allowed = "*() -./_\n\r+";
  var newVal = "";
  if ((!document.WAFV_Stop && !formElement.WAFV_Stop) && !(!required && value==""))  {
    for (var x=0; x<value.length; x++)  {
      var z = value.charAt(x);
      if ((z >= "0") && (z <= "9")) {
	    newVal += z;
	  }
	  else  {
		if (allowed.indexOf(z) < 0)  {
		  isValid = false;
		}
	  }
    }	
	if (international)  {
	  if  (newVal.length < 5)  {
	    isValid = false;
	  }
	}
	else if (newVal.length == 11)  {
	  if (newVal.charAt(0) != "1")	{
		isValid = false;
	  }
	}
	else if ((newVal.length != 10 && newVal.length != 7) || (newVal.length==7 && areaCode)) {
	  isValid = false;
	}
  }
  if (!isValid)  {
    WAAddError(formElement,errorMsg,focusIt,stopIt);
  }
  else  {
    formElement.WAValid = true;
    if (reformat != "" && newVal != "")  {
      for (var x=0; x<newVal.length; x++)  {
	    reformat = reformat.substring(0,reformat.lastIndexOf("x")) + newVal.charAt(newVal.length-(x+1)) + reformat.substring(reformat.lastIndexOf("x")+1);
	  }
	  if (reformat.indexOf("x")>=0)  {
	    reformat = reformat.substring(reformat.lastIndexOf("x")+1);
        z = reformat.charAt(0);
	    while (((z < "0") || (z > "9")) && z != "(")  {
	      reformat = reformat.substring(1);
		  z = reformat.charAt(0);
		}
	  }
      formElement.value = reformat;
	}
  }
}





function setSame(check, tofieldset, fromfieldset) {
	
  if (check.checked)  {
	var theDestinationFieldset = document.getElementById(tofieldset);
	var theDestinationInputs = theDestinationFieldset.getElementsByTagName("input");
	for (var x=0; x<theDestinationInputs.length; x++)  {
	  if (theDestinationInputs[x].type == "text")  {
	    theDestinationInputs[x].value = findFieldValueFromID(stripEnum(theDestinationInputs[x].id,tofieldset), fromfieldset);
	  }
    }
	var theDestinationSelects = theDestinationFieldset.getElementsByTagName("select");
	for (var x=0; x<theDestinationSelects.length; x++)  {
	  theDestinationSelects[x].selectedIndex = findIndexFromID(stripEnum(theDestinationSelects[x].id,tofieldset), fromfieldset);
    }
	
	if (Spry)  {
	      var isValid = true;  
          var isElementValid = true;  
          var q = Spry.Widget.Form.onSubmitWidgetQueue;  
          var qlen = q.length;  
          for (var i = 0; i < qlen; i++)  
               if (!q[i].isDisabled() && theDestinationFieldset.innerHTML.indexOf(q[i].element.innerHTML) >= 0)  
               {
				    hideServerRelated(q[i].element.id);
                    isElementValid = q[i].validate();  
                    isValid = isElementValid && isValid;  
               }  
	}
    check.checked = isValid;
  }
}

function hideServerRelated(theID) {
  var theTestID = theID.replace(/_[^_]*$/,"");
  while (theTestID != "")  {
    if (document.getElementById(theTestID+"_ServerError")) {
	  document.getElementById(theTestID+"_ServerError").style.display='none';
	  return;  
    }
	if (theTestID.search(/_[^_]*$/)<0) theTestID = ""; else theTestID = theTestID.replace(/_[^_]*$/,"");
  }
}

function stripEnum(theID,theStart)  {
	var retID = theID;
	if (theID.indexOf(theStart)==0)  {
		retID = retID.substring(theStart.length);
	}
	if (retID.search(/_\d*$/)>0)  {
	  retID = retID.substring(0,retID.search(/_\d*$/));
	}
	return retID;
}

function findIndexFromID(id, fromfieldset)  {
	var theSourceFieldset = document.getElementById(fromfieldset);
	var theSourceInputs = theSourceFieldset.getElementsByTagName("select");
	for (var y=0; y<theSourceInputs.length; y++)  {
		if (stripEnum(theSourceInputs[y].id,fromfieldset) == id)  {
			return theSourceInputs[y].selectedIndex;
		}
	}
	return 0;
}

function findFieldValueFromID(id, fromfieldset)  {
	var theSourceFieldset = document.getElementById(fromfieldset);
	var theSourceInputs = theSourceFieldset.getElementsByTagName("input");
	for (var y=0; y<theSourceInputs.length; y++)  {
		if (stripEnum(theSourceInputs[y].id,fromfieldset) == id)  {
			return theSourceInputs[y].value;
		}
	}
	return "";
}
