// JScript File

// Implements the FriendSignup object.
//
//
function FriendSignup(){

	// Constructor.
    var tbodyEl = document.getElementById('friendData');
    var tableClassName = '';	
	// Create the xml.
	var magList = "";
	var magAry = replaceXmlChars(document.signup.magazines.value).split("\n", 100);
	for(var i = 0; i < magAry.length; i++){
		if(magAry[i] != "")
			magList += "<magazine>" + magAry[i].replace(/\n|\r/,"","g") + "</magazine>";	
	}

	var textData = "<name>" + replaceXmlChars(document.signup.name.value) + "</name>";
	textData += "<email>" + replaceXmlChars(document.signup.email.value) + "</email>";
	textData += "<allMagazines>" + replaceXmlChars(document.signup.magazines.value) + "</allMagazines>";
	textData += magList;
	textData += "<source>" + replaceXmlChars(document.signup.hearAbout.value) + "</source>";
	textData += "<gender>" + replaceXmlChars(document.signup.gender.value) + "</gender>";
	textData += "<ageGroup>" + replaceXmlChars(document.signup.ageGroup.value) + "</ageGroup>";
	textData += "<postalCode>" + replaceXmlChars(document.signup.zip.value) + "</postalCode>";
	textData += "<postalCode2>" + replaceXmlChars(document.signup.zip2.value) + "</postalCode2>";
	//
	var xml = '<?xml version="1.0"?><friend xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
		'<textData>' + textData + '</textData></friend>' 
//alert(xml);		
	//
	// Send the request.
 	window.setTimeout('theFriendSignup.joinSend()',500); 
	// Give some UI feedback
	reset() 
	var currRow = addRow();
   	var currCol = addCol(currRow,"Updating...",true,false);
	

	this.joinSend = function(){
		var aRequest = new Request();
		var parameters = "info=" + escape(xml); 
//alert(aRequest);	
		aRequest.post("/signupjoin", this.joinOnComplete, parameters, this.showError); 
	}

	this.joinOnComplete = function(jsonObj){

		// Give some UI feedback.
		reset() 
		var currRow = addRow();
		var currCol = addCol(currRow,"Thanks for joining our mailing list.",true,false);
    }


 	this.showError = function(jsonObj,txt){
 
//alert(jsonObj.ERROR);
//alert(txt);	
		reset() 
		var currRow = addRow();
		var currCol = addCol(currRow,"Sorry there was an error saving your data.",true,false);
		var currRow = addRow();
		var currCol = addCol(currRow,"Please use the link on the left to email a request to join.",null,false);
 	}



	function replaceXmlChars(st){
		var str = st.replace("&", "&amp;", "g");
		str = str.replace(">", "&gt;", "g");
		str = str.replace("<", "&lt;", "g");
		str = str.replace("'", "&apos;", "g");
		str = str.replace('"', "&quot;", "g");
		return str;
	}
   	function reset() {
   		// As we delete the nodes the length of oNodeList will vary so we
   		// set now how many iterations we want to have.
   		var cnt = tbodyEl.childNodes.length
   		for (var i=0; i<cnt; i++) {
   			tbodyEl.removeChild(tbodyEl.firstChild);
		}
	}
	
    // DOM support.       
	function addRow(){
		var newRow = document.createElement('tr');
		newRow.className = tableClassName;  
		tbodyEl.appendChild(newRow);
		return newRow;
	}// addRow
	function addCol(currRow,txt,colSpan,isTH,alignRight){
		var elName = 'td';
		if(isTH!=null) elName = 'th';
		var newCol = document.createElement(elName);
		newCol.className = tableClassName; 
		if(colSpan!=null) newCol.colSpan=colSpan;
		if(alignRight!=null) newCol.align="right";
		currRow.appendChild(newCol);
		var txtNode = document.createTextNode(txt);
		newCol.appendChild(txtNode);
		return newCol;
	}//addCol	
/*
	// Constructor.
	var request = null;
   	var theCallBackFn = null;
   	var theCallBackErrFn = null;
   	var theImagePath = imagePath;
   	var theImage = image;
   	var theImageBlank = imageBlank;
   	var theImageElLocation = imageElLocation;
   	var theImageElId = imageElId;
   	
	try{
		request = new XMLHttpRequest();
	}catch(trymicrosoft){
		try{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(othermicrosoft){
			try{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(failed){
           		request = null;
			}
		}
	}
	if(request == null) alert("Error creating request object!");
	
	
	this.get = function(url, callBackFn, callBackErrFn){
		if(request != null){
			theCallBackFn = callBackFn
			theCallBackErrFn = callBackErrFn;
			request.open("GET", url, true);
			request.onreadystatechange = this.onComplete;
			request.send(null);
			setImage("ON");
		}
	}

	this.post = function(url, callBackFn, parameters, callBackErrFn){
		if(request != null){
			theCallBackFn = callBackFn
			theCallBackErrFn = callBackErrFn;
			request.open("POST", url, true);
			request.onreadystatechange = this.onComplete;
			request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			request.setRequestHeader("Content-length", parameters.length);
			request.setRequestHeader("Connection", "close");
			request.send(parameters);
			setImage("ON");
		}
	}
	
	this.onComplete = function(){
		if (request.readyState == 4) {	
			setImage("OFF");
			jsonObj = request.responseText.parseJSON();		
			if(!jsonObj){
				if(theCallBackErrFn != null) {
					var json = '{"ERROR":"Bad response from server","responseText":"Y"}'; 
					theCallBackErrFn(json.parseJSON(),request.responseText);
				}
			}else if(jsonObj.ERROR!=null) {
				if(theCallBackErrFn != null) theCallBackErrFn(jsonObj);
			}else{
				theCallBackFn(jsonObj);
			}
		}
	}
	
	function setImage(what){
		if(theImageElLocation == null) return;
		var theImageEl = eval(theImageElLocation + '.getElementById("' + theImageElId + '")');
		if(theImageEl!=null) {
			if(what == 'ON'){
				theImageEl.src = theImagePath + theImage;
			}else{
				theImageEl.src = theImagePath + theImageBlank;
			}
		}
	}
*/	
}