/**
changes the color of the selected link from the side navigator
@param itemId: the id of the tag selected.
@return: null;
@caller: Index.php
**/

function showCommentForm()
{
    
    $("#lead_in").click(function(){
    $("#lead_in").fadeOut('fast');
    $("#talk").fadeIn('fast');

});

}

function AnimateInputs()
{

   
    $(".wallform :input").click(function(){
     var original_val = $(this).val();

        if($(this).attr("type")=="text")
        {
            if(original_val=="Your Name" || original_val=="Your Email Address" )
                $(this).val("");
            
        }
        
    });

    $(".wallform :input").blur(function(){
         var original_val = $(this).val();

            if($(this).attr("type")=="text")
            {
                if($(this).val()=="")
                {
                    if($(this).attr("name")=="name" )
                     $(this).val("Your Name");

                    if($(this).attr("name")=="email" )
                     $(this).val("Your Email Address");
                }
            }

        });

}

function showSignUpForm()
{

        $("#lead_in").click(function(){
        $("#lead_in").fadeOut('fast');
        $("#talk").fadeIn('fast');

    });
}

function colorSwap(itemId)
{
	//alert(itemId);
	
	var listItem=document.getElementById(itemId);
	
	
		//FireFox	
		listItem.setAttribute("class", "selected");

		//IE
		if(!window.DOMParser)
		{
			listItem.style.backgroundColor="#810541";
			listItem.style.border="0";
		}		

        var listLength=document.getElementById('sideNavigator').childNodes.length;
        
	for(var i=0; i<=listLength; i++)
	{
		var it=document.getElementById('sideNavigator').getElementsByTagName('li')[i];
		if(it.getAttribute('id')!=itemId )
		{
			if(it.getAttribute('id')=="topOfUl")
			{
				//if dark element, don't change anything.
			}
                        else if(it.getAttribute('id')=="menuHeader")
			{
				//if head element, don't change anything.
			}
			else if(it.getAttribute('id')=="last")
			{
				it.setAttribute("class", "ender"); 
				it.style.backgroundColor="7D0552";
			}
			else
			{
				//FireFox, etc
				it.setAttribute("class", "");
				
				//ie
				if(!window.DOMParser)
				{
				        it.style.border='1px inset black';
					it.style.backgroundColor="#E42217";
					
				}	

			}
		}
	}
	
}


/**
create a generic HTTP Request assign it which php file to call, based on the parameter.
@param request_name: the name of the request to send.
**/
function sendReq( request_name )
{
		var w=document.getElementById('food_window');
		w.innerHTML="";
		

        if (window.XMLHttpRequest)
        {
            //build XML http request objec in newer IE & every other browser.
            //alert('firefox true');
            var xmlhttp=new XMLHttpRequest();
        }
        else
        {
            //build xml http request objec in older versions of IE.
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
	
	//the result funciton of the xmlhttp request.
	xmlhttp.onreadystatechange=function()
	{
		//alert ("change: "+xmlhttp.readyState+" Status: "+xmlhttp.status);
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			var xml=xmlhttp.responseText;
			//alert("xml "+xml);

            //build a Dom Parser to turn XML into a dom tree
			if(window.DOMParser)
			{
                            //this does the job for Firefox & other browsers.
                            parser=new DOMParser();
                            xmlDoc=parser.parseFromString(xml,"text/xml");
			}
                        else
                        {
                            //this does it for IE
                            xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                            xmlDoc.async="false";
                            xmlDoc.loadXML(xml);
                        }
                        
            if(request_name=="get_coup")
				showLinks(xmlDoc);

			else
				changeFoodWindow(xmlDoc);
		}
	}
	
	
	/**this list of if/elses is like a switch.  it determines which file the xml request should send **/	
	if(request_name=='get_pizzas')
		xmlhttp.open("GET","include/xml.php",true);
	
	else if(request_name=='get_pasta')
		xmlhttp.open("GET","include/pastaXML.php",true);
	
	else if(request_name=='get_sand')
		xmlhttp.open("GET","include/sandXML.php",true);
	
	else if(request_name=='get_sides')
		xmlhttp.open("GET","include/sidesXML.php",true);
	
	else if(request_name=='get_greek')
		xmlhttp.open("GET","include/greekXML.php",true);
	
	else if(request_name=='get_combos')
		xmlhttp.open("GET","include/comboXML.php",true);
	
	else if(request_name=='get_can')
		xmlhttp.open("GET","include/entreeXML.php",true);
	
	else if(request_name=='get_lunch')
		xmlhttp.open("GET","include/lunchXML.php",true);
		
	else if(request_name=='get_monthly')
		xmlhttp.open("GET","include/monthlySpecialXML.php",true);

	else if(request_name=='get_coup')
		xmlhttp.open("GET","include/coupXML.php",true);

       

	
	//whatever the request was, send it.
	xmlhttp.send("request_name");	
}

/**
This function fills the food window in index.php with data pulled from the database
@param inXML: incoming XML data.
**/
function changeFoodWindow( inXML )
{
	var xmlDoc=inXML;
	var w=document.getElementById('food_window');
	w.innerHTML=" <p> <a class=\"BackToMain\" href='http://venicehousepizza.com/wil2n/'> Back to main </a> </p> ";
	var i=0;
	while(xmlDoc.getElementsByTagName("item_name")[i].childNodes[0].nodeValue !=null)
	{
                var name=xmlDoc.getElementsByTagName("item_name")[i].childNodes[0].nodeValue;
		var toppings=xmlDoc.getElementsByTagName("desc")[i].childNodes[0].nodeValue;
		var pic="<img src="+xmlDoc.getElementsByTagName("pic")[i].childNodes[0].nodeValue+" class=\"small_view\"> </img>";
		
		if(name==null)
		{
			alert("Don't exist");
		}	
		else
		{
			w.innerHTML+=pic+"<h2 class=\"menu_h2\">"+name+"</h2>" +"<h3 class=\"menu_h3\">"+toppings+"</h3>";
		}
		i++;
	}
	//w.innerHTML+="<a class=\"next_food\" href=# OnClick=\"sendReq()\";> <b>  More Pizzas > </b> </a>"
} 

function showLinks( inXML )
{
	//alert('here');
	var xmlDoc=inXML;
	var w=document.getElementById('food_window');
	w.innerHTML="";
	var i=0;
	while(xmlDoc.getElementsByTagName("item_name")[i].childNodes[0].nodeValue !=null)
	{
		var name=xmlDoc.getElementsByTagName("item_name")[i].childNodes[0].nodeValue;
		var toppings=xmlDoc.getElementsByTagName("desc")[i].childNodes[0].nodeValue;
		var pic="<img src="+xmlDoc.getElementsByTagName("pic")[i].childNodes[0].nodeValue+" class=\"small_view\"> </img>";
		
		if(name==null)
		{
			alert("Don't exist");
		}	
		else
		{
			w.innerHTML+=pic+"<h2 class=\"menu_h2\">"+name+"</h2>" +"<a href="+toppings+"> click here </a>";
		}
		i++;
	}
	//w.innerHTML+="<a class=\"next_food\" href=# OnClick=\"sendReq()\";> <b>  More Pizzas > </b> </a>"
} 

function showFeedBackForm(){


	var w=document.getElementById('food_window');
	w.innerHTML="";
	w.innerHTML="<form name='commentClick' class='postComment' id='feedBackForm' > email address <br><input id=\"email\"> </input> <br> Name <br><input id=\"sender_name\"> </input> <br><br> Comment <br><textarea rows=15 cols=40 id=\"sender_msg\"> </textArea> <br> <input type=\"button\" onClick=\"sendForm('feedBackForm');\" class='submitButton'  value=\"Done\"> </input> <br> </form>";


}


/**
 * Sends the Form it is sent.
 * @param: THe id of the form
 * @param: the thing to do with the form.
 */
 var k=0;
function sendForm(form_id){
	
	var w=document.getElementById('food_window');
			
        if (window.XMLHttpRequest)
        {
            //build XML http request objec in newer IE & every other browser.
            //alert('firefox true');
            var xhReq=new XMLHttpRequest();
        }
        else
        {
            //build xml http request objec in older versions of IE.
            xhReq=new ActiveXObject("Microsoft.XMLHTTP");
        }

        var msg=document.getElementById("sender_msg").value;
        var name=document.getElementById("sender_name").value;
        var email=document.getElementById("email").value;
        var cap=document.getElementById("recaptcha_response_field").value;
        var url="tools/submitMessage.php?name="+name+"&msg="+msg+"&email="+email+"&cap="+cap;
     
        

        //Send the header information along with the request
        xhReq.open("GET", url , true);
	
	xhReq.onreadystatechange = function()
        {
            if(xhReq.readyState == 4 && xhReq.status == 200)
            {
                 document.getElementById(form_id).innerHTML=xhReq.responseText;
            }
        }

        xhReq.send(null);
}


/**
 * Sends the Form it is sent.
 * @param: THe id of the form
 */

function sendJoinEmailListForm(form_id)
{
	
	var w=document.getElementById('food_window');
			
        if (window.XMLHttpRequest)
        {
            //build XML http request objec in newer IE & every other browser.
            //alert('firefox true');
            var xhReq=new XMLHttpRequest();
        }
        else
        {
            //build xml http request objec in older versions of IE.
            xhReq=new ActiveXObject("Microsoft.XMLHTTP");
        }

        var cap=document.getElementById("cap").value;
         var name =document.getElementById("name").value;
        var email=document.getElementById("email").value;
        var town=document.getElementById("town").value;
       
        var url="submitEmail.php?email="+email+"&cap="+cap+"&town="+town+"&name="+name;
     
        

        //request settings
        xhReq.open("GET", url , false);
        xhReq.send();
	
        
if (xhReq.status == 200) { 
  document.getElementById(form_id).innerHTML=xhReq.responseText;
}

       
}

/**
 *  to do :  add the id to PHP Page who calls this function.
 *           add reply to DB id to the PHP Function call also.
 */
function showReplyBox(id, cap_key, cap_pic)
{
    
    var hashId="#"+id;
    var reply_button_id="wall_reply_"+id;
    //alert(recapCode);


    $('#'+reply_button_id).click(function(e) {

      
        var y=e.pageY-220+"px";
        var x=(e.pageX/2)+"px";
        var j=document.getElementById('overlay_'+id);
        var form_id="talk"+id;
        
        j.innerHTML=" ";
        $(".overlay").fadeOut('fast');
        $(j).css("top",y);
        $(j).css("left",x);
       
        
        var form=j.innerHTML;
        form+="<h1>&nbsp; Reply <a class='closer'> X </a>  </h1> <br/> <br/>  \n\
        <form class=\"reply_form\" id=\""+form_id+"\" action=\"sync_requests/submitReply.php\" method=\"POST\"> \n\
        <p><input name=\"name\" id=\"sender_name\" value=\"Your Name\" type=\"text\"/> </p>\n\
        <p> <input name=\"email\" id=\"email\" value=\"Your Email Address\" type=\"text\"/> </p> \n\
        <img src =\""+cap_pic+"\" /> \n\
        <p> <input name=\"cap\" value=\"type the letters\" type=\"text\"/> </p> \n\
        <p><input name=\"cap_key\"  value=\""+cap_key+"\" type=\"hidden\"/> </p>  \n\
        <p><input name=\"reply_to\" id=\"reply_to\" value=\""+id+"\" type=\"hidden\"/> </p>  \n\
        <p> <textarea rows=\"13\" cols=\"13\" name=\"msg\" id=\"sender_msg\" > Message..</textarea></p><p><input type=\"submit\" class=\"submit\"  value=\"Post!\"/> </p> ";
        j.innerHTML=form;
        
        
     // bind form using ajaxForm
            $('#'+form_id).ajaxForm({
                // target identifies the element(s) to update with the server response
                target: '#overlay_'+id,

                // success identifies the function to invoke when the server response
                // has been received; here we apply a fade-in effect to the new content
                success: function() {
                    $('#'+form_id).fadeIn('slow');
                }
            });

            $(".closer").click(function() {
               $(".overlay").fadeOut("fast");
            });
        
        $(j).fadeIn("slow");
       

    });  
        
}

 function external_overlay()
 {
       $(function() {

	// if the function argument is given to overlay,
	// it is assumed to be the onBeforeLoad event listener
	$("a[rel]").overlay({

		mask: 'white',
		//effect: 'apple',

		onBeforeLoad: function() {

			// grab wrapper element inside content
			var wrap = this.getOverlay().find(".contentWrap");

			// load the page specified in the trigger
			wrap.load(this.getTrigger().attr("href"));
		}

	});
});
}
