var clickedImg="";
var windowLoaded;
window.onload=function(){windowLoaded=true;};
function imgBorder(img){
	if(windowLoaded){
		for(i=1;i<=5;i++) $("img"+i).className="whiteBorder";
		var imgNum=img.replace("img","");
		$(img).className="tanBorder";
		if(img=="img1") {
				$("emailType").value="friends";
				$("tagline").innerHTML="Invite your friends and family over for dinner to share food, wine, and good conversation.";
		}
		if(img=="img2"){
			$("emailType").value="romance";
			$("tagline").innerHTML="Invite your spouse for a surprise night of romance.";
		}
		if(img=="img3") {
			$("emailType").value="ladies";
			$("tagline").innerHTML="Invite your girlfriends over to pamper yourselves with a little girl talk, and some crisp White Zinfandel or Chardonnay with strawberries.";
		}
		if(img=="img4"){
			$("emailType").value="unwind";
			$("tagline").innerHTML="Invite a friend over - nothing fancy, just a shared glass of wine.";
		}
		if(img=="img5"){
			$("emailType").value="outdoor";
			$("tagline").innerHTML="Host a backyard get together and gather your friends around Peter Vella.";
		}
		$("tagline").style.display="block";
		//set which image was clicked now, saves what template to display
		clickedImg=imgNum;
	}
	return false;
	
}

function checkYourEmail(){
	var yourEmail = $("yourEmail").value;
	
	if(yourEmail==""){
		alert("Your Email address is required");
		return false;
	}
	 if(!validEmail(yourEmail)){
		alert(yourEmail + " is not a valid email");
		return false;
	 }
	
		return true;	
}

function doInvitation(){
	var nameTxt=$("name").value;
	var eventName=$("eventName").value;
	var eventDate=$("eventDate").value;
	var eventLocation=$("eventLocation").value;
	var recipients=$("recipients").value;
	var recipArray=new Array();
	var emailTest=true;
	var message=$("message").value;
	if(clickedImg==""){
		alert("Please select an Invitation Layout");
		return false;
	}
	if(nameTxt==""){
		alert("Please tell us your Name");
		$("name").focus();
		return false;
	}
	if(eventName==""){
		alert("Please tell us the Name of your Event");
		$("eventName").focus();
		return false;
	}
	if(eventDate==""){
		alert("Please tell us the Date of your Event");
		$("eventDate").focus();
		return false;
	}
	if(eventLocation==""){
		alert("Please tell us the Location of your Event");
		$("eventLocation").focus();
		return false;
	}
	
	if(recipients==""){
		alert("Please enter a Recipient for your Invitation");
		$("recipients").focus();
		return false;
	}
	else{
		recipArray=recipients.split(",");
		recipArray.each(function(s){
								 if(!validEmail(s)){
									 alert(s+" is not a valid email")
									$("recipients").focus()
									emailTest=false;
									return false;
								 }
		});
		if(!emailTest) return false;
	}
									 
	if(message==""){
		alert("Please enter a message for your Invitation");
		$("message").focus();
		return false;
	}
	Effect.Fade("invitationHdr",{afterFinish:function(){
													 $("loading").style.display="block";
													new Ajax.Updater("invUpdater","resources/enewsletter/template"+clickedImg+".html",{
													method:"get",
													onComplete:function(){
														Effect.Appear("invDisplay");
															$("loading").style.display="none";
															$("recipientsTxt").innerHTML="<strong>"+recipients+"</strong>";
															$("recipientsInput").value=recipients;
															$("nameTxt").innerHTML=nameTxt;
															$("eventNameTxt").innerHTML=eventName;
															$("eventDetailsTxt").innerHTML=eventDate + " - " +eventLocation;
															$("msgTxt").innerHTML=message;

														}
													})
												}
				});
	
}

function edit(){
	Effect.BlindUp("invDisplay");
	Effect.Fade("invDisplay",{afterFinish:function(){
													  Effect.Appear("invitationHdr")
													  }
				});
	return false;
}



function validEmail (email){
	if((email.indexOf("@")>0) && (email.indexOf(".")>0 )){
		return true;
	}
	else{
		return false;
	}
}


function updateMsg(){
	var msg=$("message").value;
	var count=$("message").value.length;
	if(900-count<1){
		$("message").value=msg.substr(0,900);
		count=$("message").value.length;
	}
	$("msgCount").innerHTML="Characters Left: "+(900-count);
}

