/*
* sendToFriend.js
* This javascript file constains all the necessary javascript logic for the 
* included form for sending emails to friends.
*/
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

var stf_preErr = "<li>";
var stf_postErr = "</li>";

//this function displays the email to friend form	
function stf_showForm(){
	document.getElementById("ftf_form").style.display = "block";
	if(document.getElementById("stf_confirm")!=null)
		document.getElementById("stf_confirm").style.display = "none";
}

//this function hides the email to friend form
function stf_hideForm(){
	document.getElementById("ftf_form").style.display = "none";
	document.getElementById("stf_errormsg").style.display = "none";
}

//this function conditionally submits the form after validation
function stf_submitForm(linkObj){
	errormsg = stf_validateForm();
	if(errormsg.length > 0){
		document.getElementById("stf_errormsg").innerHTML = errormsg;
		document.getElementById("stf_errormsg").style.display = "block";
	}else{
		stf_reportToOmniture(linkObj);
		document.stf_form.submit();
	}
}

//this function controls the validation for the email to friend form.
//it returns an error message that is blank if validation is successful
function stf_validateForm(){
	var fromEmailField = document.getElementById("stf_fromEmail");
	var toEmailField = document.getElementById("stf_toEmail");
	var subjectField = document.getElementById("stf_subject");
	var messageField = document.getElementById("stf_message");
	
	fromEmailField.className="";
	toEmailField.className="";
	subjectField.className="";
	messageField.className="";
	
	var errormsg = "";
	var ok = true;
	
	if(!stf_isValidEmail(fromEmailField.value)){
		ok = false;
		errormsg += stf_preErr + "Sender's e-mail is invalid" + stf_postErr;
		fromEmailField.className="error";
	}
	var toEmailsValid = stf_isValidToEmails(toEmailField.value);
	if(toEmailsValid.length>0){
		ok = false;
		errormsg += toEmailsValid; 
		toEmailField.className="error";
	}
	if(subjectField.value.length == 0){
		ok = false;
		errormsg += stf_preErr + "Please enter a subject" + stf_postErr;
		subjectField.className="error";
	}
	if(messageField.value.length == 0){
		ok = false;
		errormsg += stf_preErr + "Please enter a message" + stf_postErr;
		messageField.className="error";
	}
	
	if(!ok){
		errormsg = "<ul>" + errormsg + "</ul>";
		return errormsg;
	}else
		return "";
}

//this function validates a single email address
function stf_isValidEmail(str) {
	str = str.trim();
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	var j;
	for (j=0; j<invalidChars.length; j++) {
   		if (str.indexOf(invalidChars.charAt(j),0) > -1) return false;
   	}
   	var locOfAt = str.indexOf("@");
   	
   	return (str.indexOf(".", locOfAt) > 0) && (locOfAt > 0);
}

//this function validates the comma-separated emails entered into the TO field
function stf_isValidToEmails(emailList){
	var emails = emailList.split(","); 
	var errormsg = "";
	var count=0;
	for(i in emails){
		if(emails[i].trim().length>0){
			count++;
			if(!stf_isValidEmail(emails[i]))
				errormsg += stf_preErr + "Recipient e-mail address '" + emails[i] + "' is invalid" + stf_postErr;
		}
	}
	if(count==0) errormsg += stf_preErr + "Please enter at least one recipient e-mail address" + stf_postErr;
		
	return errormsg;
}

function stf_reportToOmniture(linkObj){
	void(s.t());
	//s=s_gi('luxqa');
	s.linkTrackVars=''; 
	s.linkTrackEvents='';
	if(omnitureSprop == 's.prop3')
		s.prop3=omnitureValue;
	else if(omnitureSprop == 's.prop4')
		s.prop4=omnitureValue;
	else
		s.prop5=omnitureValue;
		
	s.eVar1=''; 
	s.events='';
	s.tl(linkObj,'o','User sent email to friend');
}
