
var cnt = 10;
var currentLanguage = "not defined";
var msgNotSigned = true;




function emailFriend(lang){
	
	currentLanguage = lang;
	turnOnLightBox();
	resetEmailFriend();
	document.getElementById('iRecipientAddr').focus();
	
}





function turnOnLightBox() {
	
	document.getElementById('lightBox').style.display = "block";
	document.getElementById('emailFriendBox').style.display = "block";
	
}






function turnOffLightBox() {
	
	document.getElementById('lightBox').style.display = "none";
	document.getElementById('emailFriendBox').style.display = "none";
	
}





function submitEmailFriend(){
	
	
	f = document.emailfriend;
	for (i=0; i<f.elements.length; i++) if (f.elements[i].name.substr(0,1)=='i') f.elements[i].value = allTrim(f.elements[i].value);
	toAddr = document.getElementById('iRecipientAddr').value;
	fromAddr = document.getElementById('iSenderAddr').value;
	fromName = document.getElementById('iSenderName').value;
	msg = document.getElementById('iMsg').value;
	msg = msg.replace(/\r/g,"%0D");
	msg = msg.replace(/\n/g,"%0A");
	validEntry = true;
		
	
	//validate recipient's email address
	if (toAddr.length == 0) {
		document.getElementById("iRecipientAddrError").innerHTML = efLang('NotEntered');
		document.getElementById("iRecipientAddr").style.backgroundColor = "#FEA0A0";
		validEntry = false;
	} else if(! toAddr.match(/^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
		document.getElementById("iRecipientAddrError").innerHTML = efLang('WrongFormat');
		document.getElementById("iRecipientAddr").style.backgroundColor = "#FEA0A0";
		validEntry= false;
	} else {
		document.getElementById("iRecipientAddrError").innerHTML = "";
		document.getElementById("iRecipientAddr").style.backgroundColor = "#FFFFFF";
	}
	
	//validate sender's email address
	if (fromAddr.length == 0) {
		document.getElementById("iSenderAddrError").innerHTML = efLang('NotEntered');
		document.getElementById("iSenderAddr").style.backgroundColor = "#FEA0A0";
		validEntry = false;
	} else if(! fromAddr.match(/^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
		document.getElementById("iSenderAddrError").innerHTML = efLang('WrongFormat');
		document.getElementById("iSenderAddr").style.backgroundColor = "#FEA0A0";
		validEntry= false;
	} else {
		document.getElementById("iSenderAddrError").innerHTML = "";
		document.getElementById("iSenderAddr").style.backgroundColor = "#FFFFFF";
	}
	
	//validate sender's name
	if (fromName.length == 0) {
		document.getElementById("iSenderNameError").innerHTML = efLang('NotEntered');
		document.getElementById("iSenderName").style.backgroundColor = "#FEA0A0";
		validEntry = false;
	} else {
		document.getElementById("iSenderNameError").innerHTML = "";
		document.getElementById("iSenderName").style.backgroundColor = "#FFFFFF";
	}
	
	//validate message
	if (msg.length == 0) {
		document.getElementById("iMsgError").innerHTML = efLang('NotEntered');
		document.getElementById("iMsg").style.backgroundColor = "#FEA0A0";
		validEntry = false;
	} else {
		document.getElementById("iMsgError").innerHTML = "";
		document.getElementById("iMsg").style.backgroundColor = "#FFFFFF";
	}
	
	if (validEntry) {
		
		document.getElementById("formButtons").innerHTML = efLang('Processing');
		
		var xmlHttp;
		try {
			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		}
		catch (e) {
			// Internet Explorer    
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
					alert("Your browser does not support AJAX! \nPlease update your browesr and visit our site again.");
					return false;
				}
			}
		}
	
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				var result = xmlHttp.responseText;
				document.getElementById("emailFriendBody").innerHTML = ''+
				'<p style="background-color:silver; color:white; line-height:30px; text-align:center; font-weight:bold; font-size:14px;">'+efLang("boxTitle")+'</p>'+
				'<div style="margin-top:20%;text-align:center;">'+
				'<p style="margin-bottom:20px;"><span id="emailFriendResultMsg"></span></p>'+
				'<p style="margin-bottom:20px;">'+efLang('WindowCloseP1')+'&nbsp;<span id="cntdwn"></span>&nbsp;'+efLang('WindowCloseP2')+'.</p>'+
				'<p><input type="button" value="'+efLang('Close')+'" onclick="turnOffLightBox();"></p>'+	
				'</div';
				if (result == "success") {
					document.getElementById("emailFriendResultMsg").innerHTML = efLang('SentSuccess');
				} else {
					document.getElementById("emailFriendResultMsg").innerHTML = efLang('SentFailure');
				}
				countdown(cnt);
			}
		}
	
		scriptCall="emailfriend.php?toAddr="+toAddr+"&fromAddr="+fromAddr+"&fromName="+fromName+"&subj="+efLang("Subject")+"&msg="+msg;
		xmlHttp.open("GET",scriptCall,true);
		xmlHttp.send(null);	
	}
	
}





function allTrim (str) {
	while (str.substring(0,1)==' ') {str=str.substring(1,str.length)}
	while (str.substring(str.length-1,str.length)==' ') {str=str.substr(0,str.length-1)}
	return str;
}






function countdown() {
	if (cnt==-1) {
		turnOffLightBox();
		return;
	}
	document.getElementById('cntdwn').innerHTML = cnt;
	cnt--;
	t = setTimeout("countdown()",1000);
}





function resetEmailFriend() {
	
	var emailFriendBody = ''+
'		<p style="background-color:silver; color:white; line-height:30px; text-align:center; font-weight:bold; font-size:14px;">'+efLang("boxTitle")+'</p>'+	
'		<form name="emailfriend">'+
'		<table style="margin: 20px auto;">'+
'			<tr>'+
'				<td style="width:150px;"><label for="iRecipientAddr">'+efLang("RecipientAddrLabel")+':</label></td>'+
'				<td><input type="text" name="iRecipientAddr" id="iRecipientAddr" maxlength="30" value="" />'+
'					<span id="iRecipientAddrError" class="ErrorMsg"></span></td>'+
'			</tr>'+
'			<tr>'+
'				<td><label for="iSenderAddr">'+efLang("SenderAddrLabel")+':</label></td>'+
'				<td><input type="text" name="iSenderAddr" id="iSenderAddr" maxlength="30" value="" />'+
'					<span id="iSenderAddrError" class="ErrorMsg"></span></td>'+
'			</tr>'+
'			<tr>'+
'				<td><label for="iSenderName">'+efLang("SenderNameLabel")+':</label></td>'+
'				<td><input type="text" name="iSenderName" id="iSenderName" maxlength="30" value="" onblur="signMsg(this)" />'+
'					<span id="iSenderNameError" class="ErrorMsg"></span></td>'+
'			</tr>'+
'			<tr>'+
'				<td colspan="2"><label for="iMsg">'+efLang("MsgLabel")+':</label> <span id="iMsgError" class="ErrorMsg"></span></td>'+
'			</tr>'+
'			<tr>'+
'				<td colspan="2"><textarea name="iMsg" id="iMsg" cols="60" rows="10">'+efLang('MsgContent')+'</textarea></td>'+
'			</tr>'+
'			<tr>'+
'				<td colspan="2" style="text-align:center; padding:15px 0px 15px 0px;">'+
'					<span id="formButtons">'+
'					<input type="button" value="'+efLang('ButtonSend')+'" onclick="submitEmailFriend()" style="margin-right:30px;" />'+
'					<input type="button" value="'+efLang('ButtonCancel')+'" onclick="turnOffLightBox()" style="margin-left:30px;" />'+
'					</span>'+
'				</td>'+
'			</tr>'+
'			<tr>'+
'				<td colspan="2"><p>'+efLang('Disclaimer')+'</p></td>'+
'			</tr>'+
'		</table>'+
'		</form>';
	
	document.getElementById("emailFriendBody").innerHTML = emailFriendBody;
	cnt = 10;
	msgNotSigned = true;
	
}






function signMsg(name) {
	if (msgNotSigned) {
		document.getElementById("iMsg").value = document.getElementById("iMsg").value + ",\n\n" + name.value;
		msgNotSigned = false;
	}
		
}






function efLang(txt) {
	
	var ENG = [];
	var POL = [];
	var ref = [];
	
	ref[0] = "boxTitle";
	ENG[0] = "Share this page with your friend";
	POL[0] = "Wyślij linka znajomym";
	ref[1] = "RecipientAddrLabel";
	ENG[1] = "TO address";
	POL[1] = "Adres e-mail odbiorcy";
	ref[2] = "SenderAddrLabel";
	ENG[2] = "FROM address";
	POL[2] = "Adres e-mail nadawcy";
	ref[3] = "SenderNameLabel";
	ENG[3] = "FROM name";
	POL[3] = "Podpis nadawcy";
	ref[4] = "MsgLabel";
	ENG[4] = "Message";
	POL[4] = "Wiadomo&#347;&#263;";
	ref[5] = "MsgContent";
	ENG[5] = "Hi,\r\n\r\nI\'d like to share an interesting website with you:\r\n\r\nhttp://www.prospectdental.net\r\n\r\nCheers";
	POL[5] = "Witaj,\r\n\r\nPolecam Ci poniższą stronę internetową:\r\n\r\nhttp://www.prospectdental.net\r\n\r\nPozdrawiam";
	ref[6] = "ButtonSend";
	ENG[6] = "Send Message";
	POL[6] = "Wy&#347;lij wiadomo&#347;&#263;";
	ref[7] = "ButtonCancel";
	ENG[7] = "Cancel";
	POL[7] = "Zamknij";
	ref[8] = "Disclaimer";
	ENG[8] = "Disclaimer: <br />Email addresses will not be stored or used for any unsolicited contacts.";
	POL[8] = "Informacja:<br />Adresy email wprowadzone powyżej nie będą przechowywane ani używane<br />do jakiejkolwiek komunikacji w przyszłości.";
	ref[9] = "Processing";
	ENG[9] = "Processing";
	POL[9] = "Trwa komunikacja z serverem";
	ref[10] = "WindowCloseP1";

	ENG[10] = "This window will close automatically in";
	POL[10] = "Okno zamknie się automatycznie za";
	ref[11] = "WindowCloseP2";
	ENG[11] = "seconds";
	POL[11] = "sekund";
	ref[12] = "SentSuccess";
	ENG[12] = "Your message has been sent.";
	POL[12] = "Wiadomość wysłana pomyślnie.";
	ref[13] = "SentFailure";
	ENG[13] = "Accept our apologies - your message cannot be sent at this moment due to technical difficulties.";
	POL[13] = "Przepraszamy, wiadomość nie może być wysłana z powodu trudności technicznych.";
	ref[14] = "NotEntered";
	ENG[14] = "Not entered";
	POL[14] = "Pole pominięte";
	ref[15] = "WrongFormat";
	ENG[15] = "Improperly formatted";
	POL[15] = "Adres wpisany błędnie";
	ref[16] = "Subject";
	ENG[16] = "Website recommendation";
	POL[16] = "Rekomendacja strony internetowej";
	ref[17] = "Close";
	ENG[17] = "Close";
	POL[17] = "Zamknij";

	textNum=0;
	while(ref[textNum]!=txt) textNum++;	

	if (currentLanguage == "en") return ENG[textNum];
	if (currentLanguage == "pl") return POL[textNum];
	
}
