var main;//this is the transparent overlay
var d;// this is the main div

function passwordReturn(page,element,buttonElement,inputElement,message)
  {
	var overlayXmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    overlayXmlHttp=new XMLHttpRequest();
	}
  catch (e)
    {
    // Internet Explorer
    try
      {
      overlayXmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        overlayXmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    overlayXmlHttp.onreadystatechange=function()
      {
      if(overlayXmlHttp.readyState == 4)
        {
			//element.innerHTML = overlayXmlHttp.responseText;
			if(overlayXmlHttp.responseText == "true")
			{
			element.innerHTML = "Thank you your new password has been sent to your email of record!";
			d.removeChild(buttonElement);
			d.removeChild(inputElement);
			}else{
			element.innerHTML = "";
			element.innerHTML = "We do not have a record of that email address. Please try again or <a href='/Members/Registration.php' title='Register Now'>Register Now!</a>";
			}
		}
      }
    overlayXmlHttp.open("GET",page + "?msg="+ message,true);
    overlayXmlHttp.send(null);
  }

function sendToDynamicPage(page,element,message)
  {
	var overlayXmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    overlayXmlHttp=new XMLHttpRequest();
	}
  catch (e)
    {
    // Internet Explorer
    try
      {
      overlayXmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        overlayXmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    overlayXmlHttp.onreadystatechange=function()
      {
      if(overlayXmlHttp.readyState == 4)
        {
			element.innerHTML = overlayXmlHttp.responseText;
        }
      }
    overlayXmlHttp.open("GET",page + "?msg="+ message,true);
    overlayXmlHttp.send(null);
  }

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   // alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   // alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function hidediv()
{
	main.style.visibility = "hidden";
	main.removeChild(d);
	//alert("hidediv called!");
	}

function closeOverlay()
{
	Spry.Effect.DoFade(main, {duration: 700, from: 100, to: 0, toggle: false,finish:hidediv});
	}

function newsletterRelay(targetElement,ajaxPage,message,html)
{
	 main = document.getElementById(targetElement);
	 main.style.opacity = 0;
     main.style.MozOpacity = 0;
     main.style.KhtmlOpacity = 0;
     main.style.filter = "alpha(opacity=50)";
	 main.style.visibility = "visible";
	  //This creates the the close btn
	 var closediv = document.createElement("div");
	 closediv.setAttribute("id","overlayCloseBtn");
	 var closea = document.createElement("a");
	 closea.setAttribute("href","#");
	 var closeimg = document.createElement("img");
	 closeimg.setAttribute("src","/Includes/Images/closeOverlay.png");
	 closeimg.setAttribute("alt","Close Dialog");
	 closea.appendChild(closeimg);
	 closea.onclick = function(){
	 closeit = closeOverlay(main);
		}
	 closediv.appendChild(closea);
	 //this adds the close div to the div
	 //create div for the message
	 d = document.createElement("div");
	 d.setAttribute("id","overlayMessage"); 
	 d.appendChild(closediv);
	 //creates p tag then adds the text to it
	 var p = document.createElement("p");
	 p.setAttribute("id","pOverlayMessage");
	 d.appendChild(p);
	 //this appends the paragraph to the div
	
	 //this adds text to the div
	 main.appendChild(d);
	 if(!echeck(message))
	{
		p.innerHTML = "Please enter a valid email address";
		}else{
		sendToDynamicPage(ajaxPage,p,message);
	}
	 //appends the div to the target element
	 
	 //this is the fade up
	 Spry.Effect.DoFade(targetElement, {duration: 700, from: 0, to: 100, toggle: false});
	//var modal = document.createNewElement();
	}

function optionOverlay(targetElement,message,html)
{
	 
	 //This element captures the main overlay div
	 main = document.getElementById(targetElement);
	 main.style.opacity = 0;
     main.style.MozOpacity = 0;
     main.style.KhtmlOpacity = 0;
     main.style.filter = "alpha(opacity=0)";
	 main.style.visibility = "visible";
	 var pOptions = "<a href=" + html[1] + ">" + html[0] + "</a>";
  	 pOptions += " | ";
	 pOptions += "<a href=" + html[3] + ">" + html[2] + "</a>";
	 //This creates the the close btn
	 var closediv = document.createElement("div");
	 closediv.setAttribute("id","overlayCloseBtn");
	 var closea = document.createElement("a");
	 closea.setAttribute("href","#");
	 var closeimg = document.createElement("img");
	 closeimg.setAttribute("src","/Includes/Images/closeOverlay.png");
	 closeimg.setAttribute("alt","Close Dialog");
	 closea.appendChild(closeimg);
	 closea.onclick = function(){
		 closeit = closeOverlay(main);
		}
	 closediv.appendChild(closea);
	 //this adds the close div to the div
	  
	 //create div for the message
	 d = document.createElement("div");
	 d.setAttribute("id","overlayMessage"); 
	 d.appendChild(closediv);
	 //creates p tag then adds the text to it
	 var p = document.createElement("p");
	 p.setAttribute("id","pOverlayMessage");
	 var sp = document.createElement("span");
	 sp.setAttribute("class","olayHyperlinks");
	 // sp.setAttribute("class","overlayBtn");
	  sp.setAttribute("id","btnOverlay");
	 sp.innerHTML = "<br />" + pOptions;
	 //this appends the paragraph to the div
	 d.appendChild(p);
	 d.appendChild(sp);
	 //this adds text to the div
	 p.innerHTML = message;
	 //appends the div to the target element
	 main.appendChild(d);
	 //this is the fade up
	 Spry.Effect.DoFade(targetElement, {duration: 700, from: 0, to: 100, toggle: false});
	//var modal = document.createNewElement();
	}
	
function createInputBox(page,message,btntext)
{
	 main = document.getElementById('ajaxOverlay');
	 main.style.opacity = 0;
     main.style.MozOpacity = 0;
     main.style.KhtmlOpacity = 0;
     main.style.filter = "alpha(opacity=0)";
	 main.style.visibility = "visible";
	 
	 var closediv = document.createElement("div");
	 closediv.setAttribute("id","overlayCloseBtn");
	 var closea = document.createElement("a");
	 closea.setAttribute("href","#");
	 var closeimg = document.createElement("img");
	 closeimg.setAttribute("src","/Includes/Images/closeOverlay.png");
	 closeimg.setAttribute("alt","Close Dialog");
	 closea.appendChild(closeimg);
	 closea.onclick = function(){
	 closeit = closeOverlay(main);
		}
	 closediv.appendChild(closea);
	 //this adds the close div to the div
	 //create div for the message
	 d = document.createElement("div");
	 d.setAttribute("id","overlayMessage"); 
	 d.appendChild(closediv);d = document.createElement("div");
	 d.setAttribute("id","overlayMessage"); 
	 d.appendChild(closediv);
	 //creates p tag then adds the text to it
	 var p = document.createElement("p");
	 p.setAttribute("id","pOverlayMessage");
	 d.appendChild(p);
	 //this appends the paragraph to the div
	 p.innerHTML = 	message;
	 var input = document.createElement("input");
	 input.setAttribute("type", "text");
	 input.setAttribute("value","email address");
	 input.onfocus = function(){
		input.value = ""; 
	 }
	 d.appendChild(input);
	 button = document.createElement("div");
	 button.setAttribute("class","overlayBtn");
	 button.innerHTML = "<a href=\"#\" />"+btntext+"</a>";
	 d.appendChild(button);
	 //this adds text to the div
	 main.appendChild(d);
	
	button.onclick=function(){
	if(!echeck(input.value))
	{
		p.innerHTML = "Please enter a valid email address";
		}else{
		 passwordReturn(page,p,button,input,input.value);
	}
	}
	 Spry.Effect.DoFade(main, {duration: 700, from: 0, to: 100, toggle: false});
	}
