var topMenuArray = new Array("subscribe","tellafriend");
var mainMenuArray = new Array("home","promotions","gourmet","select","tips","contact");
var subscribeForm;
var tellafriendForm;
function init(){
	subscribeForm = document.getElementById('subscribecontent').innerHTML;
	tellafriendForm = document.getElementById('tellafriendcontent').innerHTML;
	//new Effect.Scale("promobadge",100,{scaleFromCenter:false,scaleFrom:50})
}
function closeModule(m){
	var e  = document.getElementById("topcontent");
	if(m=="subscribe"){
		new Effect.Move(e,{x:422,y:-110,transition: Effect.Transitions.sinoidal, mode:'absolute'});
	}else if(m=="tellafriend"){
		new Effect.Move(e,{x:422,y:-220,transition: Effect.Transitions.sinoidal, mode:'absolute'});
	}
	updateColor('','top');
}
function displayModule(m){
	updateColor(m,"top");
	var e  = document.getElementById("topcontent");
	if(m=="subscribe" && YAHOO.util.Dom.getY(e)!=0){
		YAHOO.util.Dom.setStyle(e,"height","110px");
		YAHOO.util.Dom.setStyle(e,"top","-110px");
		new Effect.Move(e,{x:422,y:0,transition: Effect.Transitions.sinoidal, mode:'absolute'});
		
	}else if(m=="tellafriend" && YAHOO.util.Dom.getY(e)!=0){
		YAHOO.util.Dom.setStyle(e,"height","220px");
		YAHOO.util.Dom.setStyle(e,"top","-220px");
		new Effect.Move(e,{x:422,y:0,transition: Effect.Transitions.sinoidal, mode:'absolute'});
		
	}else if(m=="tellafriend" && YAHOO.util.Dom.getY(e)==0 && YAHOO.util.Dom.getStyle(e,"height") == "110px"){
		 new Effect.Scale(e, 200, {scaleX:false,scaleFrom:100.0});
	}else if(m=="subscribe" && YAHOO.util.Dom.getY(e)==0 && YAHOO.util.Dom.getStyle(e,"height") == "220px"){
		 new Effect.Scale(e, 50, {scaleX:false,scaleFrom:100.0});
	}
	
	if(m=="subscribe"){ //now display the content
		new Effect.Fade("tellafriendcontent",{duration:0.1});
	}else if(m=="tellafriend"){
		new Effect.Fade("subscribecontent",{duration:0.1});
	}
	c = document.getElementById(m+"content");
	YAHOO.util.Dom.setStyle(c,"margin-top","35px");
	YAHOO.util.Dom.setStyle(c,"padding-left","10px");
	e.appendChild(c);
	setTimeout(function(){ new Effect.Appear(c); },2000);
	
}

function updateColor(to,which){
	if(which == "top"){
		menuArray = topMenuArray;
	}else if(which == "main"){
		menuArray = mainMenuArray;
		init();
	}
	YAHOO.util.Dom.setStyle(to,"color","#c4e5de");
	for(i=0;i<menuArray.length;i++){
		if(menuArray[i]!=to){
			document.getElementById(menuArray[i]).removeAttribute("style"); //for IE and FF
			document.getElementById(menuArray[i]).style.color = null; //for Safari
		}
	}
}

function submitForm(id){ 
	var f = document.getElementById(id);
	if(window.XMLHttpRequest){ 
		var xmlReq = new XMLHttpRequest(); 
	} else if(window.ActiveXObject) { 
		var xmlReq = new ActiveXObject('Microsoft.XMLHTTP'); 
	} 
	var formData = '', elem = ''; 
	for(var s=0; s<f.elements.length; s++){ 
		elem = f.elements[s]; 
		if(formData != ''){ 
			formData += '&'; 
		} 
		formData += elem.name+"="+elem.value; 
	} 
	xmlReq.onreadystatechange = function(){ 
		if(xmlReq.readyState == 4){ 
			if(id=="referForm"){
				document.getElementById('tellafriendcontent').innerHTML = "<span style='font-size:10pt;color:#ffffff;margin-top:40px;'>"+xmlReq.responseText+"</span>";
				setTimeout(function(){new Effect.Move("topcontent",{x:422,y:-220,transition: Effect.Transitions.sinoidal, mode:'absolute',afterFinish:function(){
					document.getElementById('tellafriendcontent').innerHTML = tellafriendForm;updateColor('','top');
				}});},2000);
			}else if(id=="subscribeForm"){
				document.getElementById('subscribecontent').innerHTML = "<span style='font-size:10pt;color:#ffffff;margin-top:40px;'>"+xmlReq.responseText+"</span>";
				setTimeout(function(){new Effect.Move("topcontent",{x:422,y:-110,transition: Effect.Transitions.sinoidal, mode:'absolute',afterFinish:function(){
					document.getElementById('subscribecontent').innerHTML = subscribeForm;updateColor('','top');
				}});},2000);
			}else if(id=="contactForm"){
				document.getElementById('contactXMLResp').innerHTML = xmlReq.responseText;
				new Effect.Appear('contactXMLResp');
			}
		} 
	} 
	xmlReq.open(f.method, f.action, true); 
	xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	xmlReq.send(formData); 
	return false; 
}
		
function checkYouremail (strng) {
	var error="";
	if (strng == "") {
	    error = "Your e-mail address is required.\n";
	}

	/*var emailFilter=/^.+@.+\..{2,3}$/
	if (!(emailFilter.test(strng))) { 
	     error = "Invalid e-mail address.\n";
	}else {
	         var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
	           if (strng.match(illegalChars)) {
	            error = "The email address contains illegal characters.\n";
				}
	}*/
	return error;    
}
function checkFriendemail (strng) {
	var error="";
	if (strng == "") {
	    error = "Your friend's e-mail address is required.\n";
	}

	var emailFilter=/^.+@.+\..{2,3}$/
	if (!(emailFilter.test(strng))) { 
	     error = "Invalid e-mail address.\n";
	}else {
	         var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
	           if (strng.match(illegalChars)) {
	            error = "The email address contains illegal characters.\n";
				}
	}
	return error;    
}
function checkYourname(strng) {
	var error = "";
	if (strng.length == 0) {
	         error = "Your name is required.\n";
	}
	return error;	  
}
function checkFriendname(strng) {
	var error = "";
	if (strng.length == 0) {
	         error = "Friend's name is required.\n";
	}
	return error;	  
}
function checkMessage(strng) {
	var error = "";
	if (strng.length == 0) {
	         error = "Message is required.\n";
	}
	return error;	  
}
function checkWholeForm(id) {
	var theForm = document.getElementById(id);
	var why = "";
	if(id == "subscribeForm"){
		why += checkYouremail(theForm.youremail.value);
	}else if(id=="contactForm"){
		why += checkYourname(theForm.yourname.value);
		why += checkYouremail(theForm.youremail.value);
		why += checkMessage(theForm.message.value);
	}else if(id == "referForm"){
		why += checkYourname(theForm.yourname.value);
		why += checkYouremail(theForm.youremail.value);
		why += checkFriendname(theForm.friendname.value);
		why += checkFriendemail(theForm.friendemail.value);
	}
	      if (why != "") {
	         alert(why);
	         return false;
	      }else{
		submitForm(id);
		return false;
	}
}