// JavaScript Document

var xmlHttp;
var fieldset;
var str = '';

function verifyForm() {
	str = '';	
	var errors = new Array();
	var count = 0;
	
	var senderEmail = document.sendToFriend.senderEmail.value;
	var recipientEmail = document.sendToFriend.recipientEmail.value;
	fieldset = document.getElementById('formFieldset');
	
	var sendReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	var receiveReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4}, ?)*([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	
	//Check to make sure the sending email is not empty or invalid
	if(senderEmail == '') {
		errors[count] = 'You forgot to enter your email address.';
		count++;
	} else {
		if(!sendReg.test(senderEmail)) {
			errors[count] = 'Your email address is not valid.';
			count++;
		}
	}
	
	//Check to make sure the recipient email(s) is not empty or invalid
	if(recipientEmail == '') {
		errors[count] = 'You forgot to enter your friend\'s email address.';
		count++;
	} else {
		if(!receiveReg.test(recipientEmail)) {
			errors[count] = 'Your friend\'s email address is not valid.';
			count++;
		}
	}
	
	if(errors.length == 0) { //If there are no errors, process form and show success
		
		for(var y = 0; y < document.sendToFriend.length; y++) {
			if(document.sendToFriend[y].name == 'sendURL') {
				var tempUrl = document.sendToFriend[y].value.replaceAll('&','*');
				//tempUrl = document.sendToFriend[y].value.replaceAll('=','*');
				//alert(tempUrl);
				str += document.sendToFriend[y].name;
				str += '=' + tempUrl + '&';
			} else {
				str += document.sendToFriend[y].name;
				str += '=' + document.sendToFriend[y].value + '&';
			}
		}
		str = str.substr(0,(str.length - 1));
		processForm();
		return false;
		
	} else { //Display the errors
		var dl = document.getElementsByTagName('DL')[0];
		
		if(document.getElementById('errors')) {
			var formObj = document.getElementById('sendToFriend');
  			var errorObj = document.getElementById('errors');
  			formObj.removeChild(errorObj);
		}
		
		var ul1=document.createElement('UL');
		ul1.id='errors';
		
		for(var i=0; i<errors.length; i++) {
			var li1=document.createElement('LI');
			ul1.appendChild(li1);
			var txt1 = document.createTextNode(errors[i]);
			li1.appendChild(txt1);
		}
		dl.parentNode.insertBefore(ul1,dl);
		return false;
	}
}
	
	function processForm() {
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null) {
			alert ("Browser does not support HTTP Request");
			return;
		} 
		var url="/sendToFriend/?fuseaction=ajaxSubmission";
		url=url+"&sid="+Math.random();
		xmlHttp.open("POST",url,true);
		//alert(url);
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		//alert(str);
		xmlHttp.send(str);
	} 
	
	function stateChanged() { 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 

			document.getElementById('sendToFriend').style.display= 'none';

			var h11=document.createElement('H1');
			var txt1=document.createTextNode('Thank you!');
			h11.appendChild(txt1);
			
			var p1=document.createElement('P');
			var txt2=document.createTextNode('The page has been sent to the following recipient(s):');
			p1.appendChild(txt2);
			
			var p2=document.createElement('P');
			var txt3=document.createTextNode(document.sendToFriend.recipientEmail.value);
			p2.appendChild(txt3);
			
			fieldset.appendChild(h11);
			fieldset.appendChild(p1);
			fieldset.appendChild(p2);
		} 
	} 
	
	function GetXmlHttpObject() { 
		var objXMLHttp=null;
		if (window.XMLHttpRequest) {
			objXMLHttp=new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		return objXMLHttp;
	}
	
	 String.prototype.replaceAll = function(
	 strTarget, // The substring you want to replace
	 strSubString // The string you want to replace in.
	 ){
	 var strText = this;
	 var intIndexOfMatch = strText.indexOf( strTarget );
	  
	 // Keep looping while an instance of the target string
	 // still exists in the string.
	 while (intIndexOfMatch != -1){
	 // Relace out the current instance.
	 strText = strText.replace( strTarget, strSubString )
	  
	 // Get the index of any next matching substring.
	 intIndexOfMatch = strText.indexOf( strTarget );
	 }
	  
	 // Return the updated string with ALL the target strings
	 // replaced out with the new substring.
	 return( strText );
	 }