var urlBase = 'http://dogood.aish.com/'

var compileQueryStringCb =function(reply) {
	 document.getElementById("formToBeReplaced").innerHTML = reply;
}
function limitMoreEmails()
{
	var moreEmailsArray = (document.getElementById("moreEmail").value).split(",");
	if(moreEmailsArray.length>10) 
	{
		alert("You can only refer 10 more friends at a time.");
		return false;
	}
	return true;
		
}
function fixSpaces(toFix)
{
	return (toFix.replace(/ /g,'+'));
}
function compileQueryString(petId)
{
	var url,processingText;
	if(petId==1 || petId ==2 || petId == 7 || petId == 8)
		processingText = "Processing";
	else if(petId==3 || petId ==6 || petId ==9)
		processingText = "Procesando";
	url = urlBase + "processPetition.php"
	queryString = new String("email1=" + document.getElementById("email1").value);
	queryString += "&city=" + document.getElementById("city").value
			+ "&Name=" + document.getElementById("Name").value
			+ "&joinaish=" + document.getElementById("joinaish").checked
			+ "&textSderot=" + document.getElementById("textSderot").value
			+ "&petitionId="+petId 
	//		+ "&phone=" + document.getElementById('phone').value + "&state=" + document.getElementById('state').value
	queryString = fixSpaces(queryString);
	joinAish = document.getElementById("joinaish").checked;
	if(isValidEmail(document.getElementById("email1").value,"Please enter a valid email address"))
	{
		executePageWithText(url,queryString,"formToBeReplaced","<br><br><br><br><br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + processingText + "...",compileQueryStringCb);
	}
}
var compileReferQueryStringCb =function(reply) {
	  parent.document.getElementById("formToBeReplaced").innerHTML = reply;
}

function compileReferQueryString(petitionIdToPass)
{
	var atLeastOneValidEmail = false, tooManyMoreEmails = false,processingText;
	var queryString = "";
	if(petitionIdToPass==1 || petitionIdToPass ==2 || petitionIdToPass ==7 || petitionIdToPass ==8)
		processingText = "Processing";
	else if(petitionIdToPass==3 || petitionIdToPass ==6 || petitionIdToPass ==9)
		processingText = "Procesando";	
	url = urlBase + "referPetition.php";
	if(isValidEmail(document.getElementById("yourEmail").value,"Your email address is invalid. Please correct it."))
	{
		queryString += "yourEmail=" + document.getElementById("yourEmail").value;
	}
	queryString += "&petitionId=" + petitionIdToPass;
	if(document.getElementById("friend1").value!="" && isValidEmail(document.getElementById("friend1").value,"Friend #1 email address is invalid. Please correct it."))			
	{
		queryString += "&friend1=" + document.getElementById("friend1").value;
		atLeastOneValidEmail = true;
	}
	else
	{
		queryString += "&badFriend1=" + document.getElementById("friend1").value;
	}
	if(document.getElementById("friend2").value!="" && isValidEmail(document.getElementById("friend2").value,"Friend #2 email address is invalid. Please correct it."))			
	{
		queryString += "&friend2=" + document.getElementById("friend2").value;
		atLeastOneValidEmail = true;
	}
	else
	{
		queryString += "&badFriend2=" + document.getElementById("friend2").value;
	}
	if(document.getElementById("friend3").value!="" && isValidEmail(document.getElementById("friend3").value,"Friend #3 email address is invalid. Please correct it."))			
	{
		queryString += "&friend3=" + document.getElementById("friend3").value;
		atLeastOneValidEmail = true;
	}
	else
	{
		queryString += "&badFriend3=" + document.getElementById("friend3").value;
	}

	moreEmailsAllValid = true;
	var moreEmails, invalidMoreEmails="",moreEmailString="";
	moreEmails = document.getElementById("moreEmail").value;
	moreEmails = moreEmails.replace(/ /g,"");
	var emailList = moreEmails.split(",");
	if(limitMoreEmails())
	{
		for(var i=0;i<emailList.length;i++)
		{
			if(emailList[i] != "" && isValidEmail(emailList[i],emailList[i] + " is not a valid email address. Please correct it and resubmit."))
			{
				moreEmailString += emailList[i] + ",";
				atLeastOneValidEmail = true;
			}
			else
			{
				invalidMoreEmails += emailList[i] + ",";
				moreEmailsAllValid = false;
			}
		}
	}
	else
	{
		tooManyMoreEmails = true;
		moreEmailsAllValid = false;
		for(var i=0;i<emailList.length;i++)
		{
			invalidMoreEmails += emailList[i] + ",";
		}
	}
	//get rid of trailing commas
	if(invalidMoreEmails.length>0)
		invalidMoreEmails = invalidMoreEmails.substr(0,invalidMoreEmails.length-1);
	if(moreEmailString.length>0)
		moreEmailString = moreEmailString.substr(0,moreEmailString.length-1);
	if(moreEmailsAllValid)
		queryString += "&moreEmail=" + moreEmailString;
	if(invalidMoreEmails.length > 0)
		queryString += "&badMoreEmail=" + invalidMoreEmails;
//	document.write(url + "?" + queryString);
	if(atLeastOneValidEmail )
		executePageWithText(url,queryString,"formToBeReplaced","<br><br><br><br><br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + processingText + "...",compileReferQueryStringCb);	
	else if(!tooManyMoreEmails)
		alert("None of the email addresses you have entered for referral are valid. Please correct them and resubmit.");		
}