var ERROR_GENERAL = "Error sending email, Please try again later";
var ERROR_FIRST_NAME_INVALID = "Please enter a valid first name\n";
var ERROR_FRIEND_NAME_INVALID = "Please enter a valid friend's name\n";
var ERROR_EMAIL_INVALID = "Please enter a valid email address\n";
var ERROR_EMAIL_SERVER = "There was an error sending your email. Please try again.";
var MSG_EMAIL_SUCCESS = "Thank you, email was sent to your friend";
var errorMsg ="";
var goodWordsCounter =0;
var data = new Object();



function doSend(form) {
	goodWordsCounter =0
    //doLoaderShow();
	data = getData();
	if(!isFormValid(data)) {
		//alert(errorMsg);
		
		return false;
	}
	
	checkBadWord($("#first").attr("value"), "first");
	checkBadWord($("#friend").attr("value"), "friend");
	
	
	return false;
}

function doStfClose() {
	parent.closeTakeover(false);
}

function sendData(){
	var strData = "";
	strData = "senderName=" + data.first;
	strData = strData  + "&friendName=" + data.friend;
	strData = strData  + "&email=" + data.email;
	strData = strData  + "&ad=" + data.ad;
	strData = strData  + "&pageUrl=" + "www.nick.com"+data.path;
	try {
		$.ajax({
			type: "POST",
			url: URL_SEND_EMAIL,
			data: strData,
			success:function(msg){
				doLog("Email sent: close form window");
				parent.closeTakeover(false);
			},
			error:function(msg){
				doLog("send email Error: " + msg);
			}
		});
	} catch (error) {
		doLog("Exception: "+error);
	}


	toggleButton('submit', false);
}




function isFormValid(data) {
	errorMsg ="";
	var isValid = true;
	if(data == null) {
		errorMsg = ERROR_GENERAL
		isValid = false;
	}
	if(isEmptyString(data.first)){
		isValid = false;
		$("#first").attr("value", ERROR_FIRST_NAME_INVALID)
	}
	
	if(isEmptyString(data.friend)) {
		isValid = false;
		$("#friend").attr("value", ERROR_FRIEND_NAME_INVALID)
	}
	if(!validateEmail(data.email)){
		isValid = false;
		errorMsg = errorMsg + ERROR_EMAIL_INVALID;
		$("#email").attr("value", ERROR_EMAIL_INVALID)
	}

	return isValid;
}



function getData() {
	return {
		path: $.jqURL.get('path'),
		first:$("#first").attr("value"),
		friend:$("#friend").attr("value"),
		email:$("#email").attr("value"),
		ad:$("#ad").attr("value")
	}
}

function checkBadWord(word, field){
	try {
		$.ajax({
			type: "GET",
			url: URL_WORD_FILTER+word,
			name: word,
			success: function(msg){
				if(msg.substring(6,7)=="b"){
					if(field=="first"){
						$("#first").attr("value",ERROR_FIRST_NAME_INVALID)
					}else{
						$("#friend").attr("value", ERROR_FRIEND_NAME_INVALID)
					}
				}else{
					goodWordsCounter++;
				}
				if(goodWordsCounter==2){
					sendData();
				}
			},
			error:function(msg){
				doLog("check filter: " + msg);
			}
		});
	} catch (error) {
		doLog("Exception: "+error);
	}
}



function initPage() {

	doLog("initPage: send to friend");
	// enable submission
	toggleButton('sendBtn', true);
}
$(document).ready(initPage);
