var fromName = '';
var fromEmail = '';
var xmlHttp;

//sets up send to a friend
function sendToAFriendInit()
{
	//output to output
	var output = '	<div><h3>Tell a friend about Cheeky Cherub Valentines</h3></div>\n'+
						'<div class="from">\n'+
						'<span>Your details:</span>\n'+
						'<input name="fromName" id="fromName" maxlength="255" />\n'+
						'<input name="fromEmail" id="fromEmail" maxlength="255" />\n'+
					'</div>\n\n'+
					'<div class="cl"></div>\n'+
					
					'<div class="to">'+
						'<span>Friend&rsquo;s details:</span>\n'+
						'<input name="name" id="name" maxlength="255" />\n'+
						'<input name="email" id="email" maxlength="255" />\n'+
						'<input type="submit" name="submit" id="submit" value="Send" onClick="sendToAFriend(); return false;" />\n'+
					'</div>\n\n'+
					'<div class="cl"></div>\n'+
					
					'<ul id="sent_list"></ul>';

	//adds send to a friend boxes
	document.getElementById('send_to_a_friend').innerHTML = output;
}

//updates user details
function detailsUpdated(newFromName, newFromEmail)
{
	//updates from name and email
	fromName = newFromName;
	fromEmail = newFromEmail;
	
	//puts details into fields and disables them
	var fromNameField = document.getElementById('fromName');
	var fromEmailField = document.getElementById('fromEmail');
	fromNameField.value = fromName;
	fromEmailField.value = fromEmail;
}

//sends email to a friend via ajax
function sendToAFriend()
{
	//sets from name and email if they have not been set by Flash yet
	fromName = document.getElementById('fromName').value;
	fromEmail = document.getElementById('fromEmail').value;
	
	//gets friend's details
	var name = document.getElementById('name').value;
	var email = document.getElementById('email').value;
	
	//checks details and sends ajax request
	var emailValid = checkEmail(email);
	if(name != '' && emailValid)
	{
		doAjax('javascript/sendtoafriend.php?from_name=' + fromName + '&from_email=' + fromEmail + '&to_name=' + name + '&to_email=' + email);
		//runbaby();
	}
	else
	{
		alert('Oops! There seems to a problem with what you entered. Please check the details and click send again.');
	}
}


//does ajax
function doAjax(url)
{ 
	xmlHttp = GetXmlHttpObject();
	
	if(xmlHttp == null)
	{
		alert("Browser does not support HTTP Request");
		return;
	}
	//url = 'javascript/sendtoafriend.php?from_name=tony&from_email=tony&to_name=richi&to_email=richi';
	//url = 'javascript/sendtoafriend5.xml';
	//url = 'javascript/test/xml.xml';
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

//runs on state change of http request
function stateChanged() 
{
	//in progress function
	if(xmlHttp.readyState == 1)
	{
		//changes send button
		var submitButton = document.getElementById('submit');
		submitButton.value = 'Sending...';
		submitButton.disabled = true;
	}
	
	//complete function
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		//changes send button back to normal
		var submitButton = document.getElementById('submit');
		submitButton.value = 'Send';
		submitButton.disabled = false;
		
		//gets info from ajax xml response
		var xmlDoc = xmlHttp.responseXML;
		
		var success = xmlDoc.getElementsByTagName("success")[0].childNodes[0].nodeValue;
		var name = xmlDoc.getElementsByTagName("fname")[0].childNodes[0].nodeValue;
		var email = xmlDoc.getElementsByTagName("email")[0].childNodes[0].nodeValue;
		
		//uses correct class, chosen upon if php page failed or passed
		var friendClass = 'failed';
		var statusMessage = 'Failed. This will be automatically attempted again later today.';
		if(success == 'true')
		{
			friendClass = 'sent';
			statusMessage = 'Sent successfully.';
		}
		
		//add message to sent log
		var sentList = document.getElementById('sent_list');
		if(sentList.innerHTML == '')
		{
			sentList.innerHTML = '<li class="first">Sent list:</li>\n';
		}
		sentList.innerHTML = sentList.innerHTML + '<li class="' + friendClass + '">' + name + ' (' + email + ')' + '&hellip; ' + statusMessage + '</li>\n';
	}
} 

//gets new xmlhttp instance
function GetXmlHttpObject()
{ 
	var objXMLHttp = null;
	
	if(window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return objXMLHttp;
}