

function checkFromAddr(index){

 var formVarString = "document.tellfriend.emailx" + index; var valString = formVarString + ".value";
 var thisValue = eval(valString);
 if ( thisValue.indexOf('@') == -1 || thisValue.indexOf('.') == -1 || thisValue.length <= 7 ) {

 return(0);
 } return(1);
 } 

function verify_form(){

 var fromNameValid = (document.tellfriend.namex6.value == "") ? 0 : 1;
 var fromAddrValid = checkFromAddr(6);
 var firstNameValid = (document.tellfriend.namex1.value == "") ? 0 : 1;
 var firstAddrValid = checkFromAddr(1);
 var probFields = "";
 var someFieldsFilled = 0;
 var focusString = "";
 for(i=1;
i<=6;
i++){

 formVarString = "document.tellfriend.emailx" + i;
 valString = formVarString + ".value";
 nameString = formVarString + ".name";
 thisName = eval(nameString);
 thisValue = eval(valString);
 if(thisValue.length != 0) {

 if ( thisValue.indexOf('@') == -1 || thisValue.indexOf('.') == -1 || thisValue.length <= 7 ) {

 probFields+= "Email " + i + " has an incorrect format\n";
 focusString = formVarString + ".focus()";
 } someFieldsFilled+=1;
 } } if(!fromNameValid){

 alert("Please write your name in the \"Your Name\" field.");
 document.tellfriend.namex6.focus();
 return false;
 } else if(!fromAddrValid){

 alert("Please fill in your email address in the \"Your Email\" field.");
 document.tellfriend.emailx6.focus();
 return false;
 } else if(!firstNameValid){

 alert("Please write your friend's name in the \"Your Friend's Name\" field.");
 document.tellfriend.namex1.focus();
 return false;
 } else if(!firstAddrValid){

 alert("Please fill in your friend's email address in the \"Your Friend's Email\" field.");
 document.tellfriend.emailx1.focus();
 return false;
 } else if(someFieldsFilled == 0){

 alert("Please fill in at least one address before sending");
 document.tellfriend.emailx1.focus();
 return false;
 } else if (probFields != ""){

 alert("One or more email addresses have an incorrect format\n\n" + probFields + "\nThe correct format should be:\nname@adomain.com");
 return false;
 } else {

 document.tellfriend.submit();
 } } 

function setContent(count){

 var top_header ="<div id=\"TellFriendResults\"><span><h1>Thanks for inviting your friends to attend the Q&amp;A Live Celebration!</h1><p>Get your free downloadable materials by clicking the links below.</p><p>We hope that you enjoy the material, and find how it can help you improve your life.</p><p>To download your gifts, right-click on each link below and choose &quot;Save Target As...&quot; or &quot;Save Link As...&quot; Then choose a folder and click &quot;Save.&quot;</p></span><div id=\"MainResultsSection\"><ol>";
 var sec1 ="<li><span><a href=\"http://files.kabbalahmedia.info/arionline/promo/files/gift-1-Moby.zip\" TARGET = \"_blank\"  onclick=\"_gaq.push([\'_trackEvent\', \'Audio Download\',\'MP3-PDF\', \'Package1\']);\">Download The Moby/Michael Laitman Interview (PDF &amp; MP3) &raquo;</a></span></li>";
 var sec2 ="<li><span><a href=\"http://files.kabbalahmedia.info/arionline/promo/files/gift-2-Don-Miguel-Ruiz.zip\" TARGET = \"_blank\"  onclick=\"_gaq.push([\'_trackEvent\', \'Audio Download\',\'MP3-PDF\', \'Package2\']);\">Download The Don Miguel Ruiz/Michael Laitman Interview (PDF &amp; MP3) &raquo;</a></span></li>";
 var sec3 ="<li><span><a href=\"http://files.kabbalahmedia.info/arionline/promo/files/gift-3-Judith-Regan.zip\" TARGET = \"_blank\"  onclick=\"_gaq.push(['_trackEvent', 'Audio Download','MP3-PDF', 'Package3']);\">Download The Judith Regan/Michael Laitman Interview (PDF &amp; MP3) &raquo;</a></span></li>";
 var sec4 ="<li><span><a href=\"http://files.kabbalahmedia.info/arionline/promo/files/gift-4-Amit-Goswami.zip\" TARGET = \"_blank\" onclick=\"_gaq.push(['_trackEvent', 'Audio Download','MP3-PDF', 'Package4']);\" >Download The Amit Goswami/Michael Laitman Interview (PDF &amp; MP3) &raquo;</a></span></li>";
 var sec5 ="<li><span><a href=\"http://files.kabbalahmedia.info/arionline/promo/files/gift-5-Steve-Rubel.zip\"  TARGET = \"_blank\" onclick=\"_gaq.push(['_trackEvent', 'Audio Download','MP3-PDF', 'Package5']);\" >Download The Steve Rubel/Michael Laitman Interview (PDF &amp; MP3) &raquo;</a></span></li>";
 var bottom_sec = "</ol></div></div>";
 if (count == 1){

 return (top_header + sec1 + bottom_sec);
 }else if (count == 2){

 return (top_header + sec1 + sec2 + bottom_sec);
 }else if (count == 3){

 return (top_header + sec1 + sec2 + sec3 + bottom_sec);
 }else if (count == 4){

 return (top_header + sec1 + sec2 + sec3 + sec4 + bottom_sec);
 }else if (count == 5){

 return (top_header + sec1 + sec2 + sec3 + sec4 + sec5 + bottom_sec);
 } else return ("Error Occured!");
 } 

function display_results(){

 var count = getQueryVariable("s");
 var tfcontent = setContent(count);
 if (count){

 document.getElementById('TellTriendContent').innerHTML = tfcontent;
 } else {

 return (true);
 } } 

function getQueryVariable(variable) {

 var query = window.location.search.substring(1);
 var vars = query.split("&");
 for (var i=0;
i<vars.length;
i++) {

 var pair = vars[i].split("=");
 if (pair[0] == variable) {

 return pair[1];
 } }} window.onload = display_results;

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

function validateEmail(email) {
	var at = email.lastIndexOf("@");

	// Make sure the at (@) sybmol exists and  
	// it is not the first or last character
	if (at < 1 || (at + 1) === email.length)
		return false;

	// Make sure there aren't multiple periods together
	if (/(\.{2,})/.test(email))
		return false;

	// Break up the local and domain portions
	var local = email.substring(0, at);
	var domain = email.substring(at + 1);

	// Check lengths
	if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
		return false;

	// Make sure local and domain don't start with or end with a period
	if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
		return false;

	// Check for quoted-string addresses
	// Since almost anything is allowed in a quoted-string address,
	// we're just going to let them go through
	if (!/^"(.+)"$/.test(local)) {
		// It's a dot-string address...check for valid characters
		if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
			return false;
	}

	// Make sure domain contains only valid characters and at least one period
	if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
		return false;	

	return true;
};

$(function() {
	
	display_results();
	$("a[rel]").hover(function(e){
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
	
		var sel = $(this).attr("rel");
		$(sel).css({
		"position": "absolute",
		"top":  posy,
		"left": posx
		});
		$(sel).fadeIn("fast");

	},
	function(){
		var sel = $(this).attr("rel");
		$(sel).fadeOut("fast");
   	});	
	

	$('.error').hide();
	
	function encode(str) {
		str = escape(str);
		return str.replace(/[*+\/@]|%20/g,
			function (s) {
				switch (s) {
					case "*": s = "%2A"; break;
					case "+": s = "%2B"; break;
					case "/": s = "%2F"; break;
					case "@": s = "%40"; break;
					case "%20": s = "+"; break;
				}
				return s;
			}
		);
	}	
	
	$(".button").click(function() {
	
		$('.error').hide();
		
		var name = $("input#entry_0").val();
		if (!name) {
			$("label#name_must").show();
			$("input#entry_0").focus();
			return false;
		}	

		var email = $("input#entry_1").val();	
		if (!email) {
			$("label#email_must").show();
			$("input#entry_1").focus();
			return false;
		}
		if (!validateEmail(email)) {
			$("label#email_invalid").show();
			$("input#entry_1").focus();
			return false;
		}

		var question = $("textarea#entry_2").val();
		if (!question.replace(/^\s+|\s+$/g,"")) {
			$("label#question_must").show();
			$("textarea#entry_2").focus();
			return false;
		}
		post_url = 'http://spreadsheets.google.com/formResponse?formkey=dHpJWGVMcnM5dTVyc1VvdlprUkUxcnc6MQ&amp;ifq';

		post_data = {};
		post_data[0] = post_url;
		post_data[1] = 'entry.0.single';
		post_data[2] = name;
		post_data[3] = 'entry.1.single';
		post_data[4] = email;
		post_data[5] = 'entry.2.single';
		post_data[6] = question;

		$.ajax({
			type: "POST",
			url: "celeb/scripts/post_form.php",
			data: post_data,
			success: function() {
				$("input#entry_0").val('');
				$("input#entry_1").val('');
				$("textarea#entry_2").val('');
			}
		});
	
	
		return true;
	});
});

