// define functions related to send-to-friend box
function toggleSendToFriend() {
	if (document.getElementById("sendtofriend_link").style.display=="none") {
		document.getElementById("sendtofriend_link").style.display="block";
		document.getElementById("sendtofriend_form").style.display="none";			
	} else {
		document.getElementById("sendtofriend_link").style.display="none";
		document.getElementById("sendtofriend_form").style.display="block";					
	}
}
function hideAllSendToFriendErrors() {
	document.getElementById("stf_error_name").style.display="none";
	document.getElementById("stf_error_from").style.display="none";
	document.getElementById("stf_error_to").style.display="none";
}
function sendToFriend(contestName, contestUrl, emailType) {
	
	// emailType is an optional param; if not defined give it default value
	if (typeof emailType == "undefined") {
		var emailType = "contest";
	}	
	hideAllSendToFriendErrors();
	var name=document.getElementById("sendtofriend_name").value;
	var from=document.getElementById("sendtofriend_from").value;	
	var to=document.getElementById("sendtofriend_to").value;		
	var success=1;
	if (name.length<1 || name.length>25) {
		success=0;
		document.getElementById("stf_error_name").style.display="block";
	}
	if (from.length<1 || from.length>50 || !isValidEmailFormat(from) || from.indexOf(',')!=-1 || from.indexOf(';')!=-1) {
		success=0;
		document.getElementById("stf_error_from").style.display="block";
	}
	if (to.length<1 || to.length>50 || !isValidEmailFormat(to) || to.indexOf(',')!=-1 || to.indexOf(';')!=-1 ) {
		success=0;
		document.getElementById("stf_error_to").style.display="block";
	} 
	if (!success) {
		return false;
	}
	var url = '/contestapp/sendtofriend.php?' + Math.random();
	var postStr = "contest=" + encodeURIComponent(contestName);
	postStr += "&type=" + encodeURIComponent(emailType);
	postStr += "&url=" + encodeURIComponent(contestUrl);
	postStr += "&name=" + encodeURIComponent(name);
	postStr += "&from=" + encodeURIComponent(from);
	postStr += "&to=" + encodeURIComponent(to);	
	makeSendToFriendRequest(url, postStr);
}
function makeSendToFriendRequest(url, param) {
if (STFreceiveReq.readyState == 4 || STFreceiveReq.readyState == 0) {
   STFreceiveReq.open("POST", url, true);
   STFreceiveReq.onreadystatechange = sendToFriendDone; 
   STFreceiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   STFreceiveReq.setRequestHeader("Content-length", param.length);
   STFreceiveReq.setRequestHeader("Connection", "close");
   STFreceiveReq.send(param);
 }   
}
function sendToFriendDone() {
	if (STFreceiveReq.readyState == 4) {
		// successful returns are in the form ok:###" where ### is number of successful sends
		if (STFreceiveReq.responseText.substring(0,2)=="ok") { // if first two letters are ok, continue
			var referredCount=STFreceiveReq.responseText.substring(3); // get the number of sends
			if (typeof document.contestForm != "undefined") {  // make sure this is a contest form page, not a gallery page
				if (typeof document.contestForm.referred_friends != "undefined") { // the field itself may not exist for older contests, so we check first
					document.contestForm.referred_friends.value=referredCount;
				} 
			} 
			document.getElementById("sendtofriend_link").innerHTML='<p><a href="javascript:toggleSendToFriend();">Tell another friend about this contest</a> &raquo;</p><p>Thank you!  Your email was sent successfully.</p>';
			document.getElementById("sendtofriend_to").value="";
			document.getElementById("sendtofriend_link").style.display="block";				
			document.getElementById("sendtofriend_form").style.display="none";	
		} else {
			document.getElementById("sendtofriend_link").style.display="none";	
			document.getElementById("sendtofriend_form").innerHTML='<p><strong>Tell friends about this contest</strong></p><p>Error:  ' + STFreceiveReq.responseText + '</p>';
			document.getElementById("sendtofriend_form").style.display="block";	
		}
	}	
}
var STFreceiveReq = getXmlHttpRequestObject();
