//varialbes for regExp
//validate names
var sNameRegExp = "^[a-z]{2,}(-[a-z]{2,})?$";
//validate us zip code
var sZipRegExp = "^\\d{5}$";
//validate address
var sAddrRegExp = "^\\d+(\\s(\\w+)\\.?)+\\s*$";
//validate email
var sEmailRegExp = "^\\w+(\\.\\w+)*@\\w+(\\.\\w+)*\\.\\w{2,3}$";
//validate city
var sCityRegExp = "^[a-z]{1,}\\.?(\\s([a-z]{1,})\\.?)*\\s*$";
//validate us phone (000)000-0000 format
var sPhone1RegExp = "^\\(\\d{3}\\)\\d{3}-\\d{4}$";
// validate us ssn
var sSSNRegExp = "^\\d{9}$";

/*****************************************************************************
	creates a new xml document on the fly
*****************************************************************************/
function createxDoc(){
				
	xDoc = new ActiveXObject("microsoft.xmldom");
	xDoc.async = false;
	xDoc.validateOnParse = false;	
	xDoc.loadXML("<?xml version='1.0' encoding='utf-8' ?><root></root>");		
	xDoc.documentElement.appendChild(xDoc.createElement("function"));	

}

/******************************************************************************
	sends the xDoc
******************************************************************************/
function sendXDoc(sUrl){

	xmlHTTP = new ActiveXObject("MSXML2.xmlHTTP");
	xmlHTTP.open("post", sUrl, false);
	xmlHTTP.send(xDoc.xml);	
	xDoc = xmlHTTP.responseXML;
	//errorSpan.innerHTML = xmlHTTP.responseText;
	//alert(xDoc.xml);

}

/******************************************************************
	loops through the xDoc and sets assigned values to it
	-first checks to see if the id exists
	-id txt(input) value will be used
	-if ta(text area) innerText will be used
	-if td(table data) innerText will be used
	-if content editable spn innerHTML will be used
******************************************************************/
function setXmlValues(xmlNode){

	for(i=0 ; i<xmlNode.childNodes.length ; i++){

		sID = xmlNode.childNodes.item(i).tagName;
		
		sID1 = "txt" + sID;
		sID2 = "ta" + sID;
		sID3 = "td" + sID;
		sID4 = "spn" + sID;
		
		if(document.getElementById(sID1)){
			
			xmlNode.childNodes.item(i).text = document.getElementById(sID1).value;
		
		}else if(document.getElementById(sID2)){
		
			xmlNode.childNodes.item(i).text = document.getElementById(sID2).innerText;
		
		}else if(document.getElementById(sID3)){
		
			xmlNode.childNodes.item(i).text = document.getElementById(sID3).innerText;
		
		}else if(document.getElementById(sID4)){
		
			xmlNode.childNodes.item(i).childNodes.item(0).text = document.getElementById(sID4).innerHTML;
		
		}

	}

}

/******************************************************************
	loops through the xDoc and sets assigned values to the form
	-first checks to see if the id exists
	-id txt(input) value will be used
	-if ta(text area) innerText will be used
	-if td(table data) innerText will be used
	-if content editable spn innerHTML will be used
******************************************************************/
function setFormValues(xmlNode){

	for(i=0 ; i<xmlNode.childNodes.length ; i++){

		sID = xmlNode.childNodes.item(i).tagName;
		
		sID1 = "txt" + sID;
		sID2 = "ta" + sID;
		sID3 = "td" + sID;
		sID4 = "spn" + sID;
		
		if(document.getElementById(sID1)){
			
			document.getElementById(sID1).value = xmlNode.childNodes.item(i).text;
		
		}else if(document.getElementById(sID2)){
		
			document.getElementById(sID2).innerText = xmlNode.childNodes.item(i).text;
		
		}else if(document.getElementById(sID3)){
		
			document.getElementById(sID3).innerText = xmlNode.childNodes.item(i).text;
		
		}else if(document.getElementById(sID4)){
		
			document.getElementById(sID4).innerHTML = xmlNode.childNodes.item(i).childNodes.item(0).text;
		
		}

	}

}

/************************************************************
	takes a piped string and makes xml nodes with it
	-	splits the pipe on a |
	-	first pipe i parent none
	-	all others are child nodes
	make sure you name the ids the same as the xmlNodes
	but with -txt for inputs and selects
			 -ta for textarea
			 -td for td cells
	on the beginning of the id 
	ie <input type="text" id="txtInputID" />	
************************************************************/
function makeXmlNodes(sPipe){
			
	arTemp = sPipe.split("|");
	tempXNode = xDoc.createElement(arTemp[0]);
	xDoc.documentElement.appendChild(tempXNode);
	
	for(i=1 ; i<arTemp.length ; i++){
	
		tempXNode.appendChild(xDoc.createElement(arTemp[i]));
	
	}
	
	

}	

//***************************************************************************************
// This function performs regular expression checks and returns the result to the calling
// routine
//	curFiled = id.value
//	curExp = regExp expression
// this will return true or false
// make sure you have id.value.toLowerCase() if needed
//***************************************************************************************
function valRegExp(curField, curExp)
{
	oTextField = new RegExp(curExp);
	return(oTextField.test(curField));
}

/**********************************************************
	sets the values from the piped string into the xDoc
**********************************************************/	
function setPipeValuesToXml(xmlNode,sPipe){

	arTemp = sPipe.split("*");

	for(i=0 ; i<xmlNode.childNodes.length ; i++){
        
 		xmlNode.childNodes.item(i).text = arTemp[i];

	}

}