var curr=false;
var xmlHttp = createXmlHttpRequestObject();

var f;
var s;

function e(sub){
  var sd=document.getElementById(sub);

  document.getElementById('sharespan').className="tab";
  document.getElementById('rssspan').className="tab";
  document.getElementById('mailspan').className="tab";
  document.getElementById('printspan').className="tab";

  if (!curr) {
    sd.style.display='block';
    document.getElementById(sub+'span').className="taba";
      document.getElementById('sharetop').style.borderBottom='0px';
    curr=sub;
  }
  else { 
    if (curr==sub){
      sd.style.display='none';
      document.getElementById('sharetop').style.borderBottom='1px solid #698FC8';
      curr=false;
    } else {
      document.getElementById(curr).style.display='none';
      sd.style.display='block';
      document.getElementById(sub+'span').className="taba";
      document.getElementById('sharetop').style.borderBottom='0px';
      curr=sub;
    }
  }
}
function isEmail(string) {
  if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
  return true;
  else
  return false;
}

function doSend(formname){
	f=document.getElementById(formname);
	s=document.getElementById(formname+'mailerr');

  if (isEmail(f.fromemail.value) && f.fromname.value.length>0 && f.sendtomyself.checked==true){
    s.innerHTML="";
    dataSubmit();
  }
  else if (isEmail(f.toemail.value) && isEmail(f.fromemail.value) && f.fromname.value.length>0 && f.toname.value.length>0 && f.sendtomyself.checked==false){
    s.innerHTML="";
    dataSubmit();
  }
  else {
    document.getElementById(formname+'mailerr').style.color="red";
    document.getElementById(formname+'mailerr').innerHTML="<b>Please fill all fields</b><br>";
  }
}

function createXmlHttpRequestObject() 
{
  var xmlHttp;
  try
  {
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function dataSubmit(){
  if (xmlHttp)
  {
    try
    {
      xmlHttp.open("POST", "/monitor/email.php", true);
      xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      xmlHttp.onreadystatechange = handleRequestStateChange;
		var values=(f.id?"id="+f.id.value:"")+
		  "&toname="+f.toname.value+
		  "&toemail="+f.toemail.value+
		  "&fromname="+f.fromname.value+
		  "&fromemail="+f.fromemail.value+
		  (f.sendtomyself.checked==true?"&sendtomyself="+f.sendtomyself.value:"")+
		  ((f.sendmeacopy.checked==true||f.sendtomyself.checked!=true)?"&sendmeacopy="+f.sendmeacopy.value:"")+
		  (f.sendtype?"&sendtype="+f.sendtype.value:"")+
		  (f.page?"&page="+f.page.value:"")+
		  (f.gadget?"&gadget="+f.gadget.value:"")+
		  (f.singletestpage?"&singletestpage="+f.singletestpage.value:"")+
		  (f.message?"&message="+f.message.value:"");
	  xmlHttp.send(values);
    }
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}


function handleRequestStateChange() {
  if (xmlHttp.readyState > 0 && xmlHttp.readyState < 4)
  {
    s.innerHTML = "Submitting data...<br/>";
	f.style.display='none';
  }
  else if (xmlHttp.readyState == 4) 
  {
    if (xmlHttp.status == 200) 
    {
      try
      {
        response = xmlHttp.responseText;
		if (response=="OK")
		{
			f.style.display='none';
	        s.style.color = "black";
	        s.innerHTML =  "<b>Thank You.</b><br>The message was sent successfully<br>";
	        s.innerHTML += "<br><a href='javascript:rstemail()'>Send to another address</a><br/>";
		}
		else {
	        s.innerHTML = "<b>Error:</b><br>" + response;
	        s.style.color = "red";
			f.style.display='block';
		}


      }
      catch(e)
      {
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
    }
  }
}

function rstemail(){
s.innerHTML = "";
f.toname.value="";
f.toemail.value="";
f.style.display='block';
f.fromname.focus();
}

function sh(what){
	var sh=document.getElementById(what);
	if (sh.style.display=="none"){
		sh.style.display="block";
	} else {
		sh.style.display="none";	
	}
}

function gr(what){
	var sss=document.getElementById(what+'span');
	var fff=document.getElementById(what);
	if (sss.style.color=="#cecece")
	{
		sss.style.color="black";
		fff.disabled=false;
	}
	else {
		sss.style.color="#cecece";
		fff.disabled=true;
	}
}
