/// main.js ///

function PrintMe(object)
{
	string = '';
 var NewItem = object;
	counter = 0;
 var item;
	for (item in NewItem)
	{
		counter++;
		string += item + '\n';
		if (counter == 30)
		{
			alert(string);
			counter = 0;
			string = '';
		}
	}
	alert(string)
}


function LoadPage()
{
 var ref = document.URL;
 if (ref.search("submitted") > 0)
{
 NavClick('submitted');	
}
else 
{
NavClick('home');	
}
  
}

function GetClientURL(id, url)
{
  var element = document.getElementById(id);
  if (!element) 
  {
     return;
  }

  var xmlHttpReq = false;
  var msxmlhttp = new Array('MSXML2.XMLHTTP.3.0','Microsoft.XMLHTTP','Msxml2.XMLHTTP.7.0','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP'); 
  for (i = 0; i < msxmlhttp.length; i++) 
  { 
    try 
    { 
      xmlHttpReq = new ActiveXObject(msxmlhttp[i]); 
      break;
    } 
    catch (e) 
    { 
      xmlHttpReq = null; 
    } 
  } 

  if (!xmlHttpReq && (typeof XMLHttpRequest != "undefined") )
  { 
    xmlHttpReq = new XMLHttpRequest(); 
  } 

  if (!xmlHttpReq)
  {
    alert("Browser unsupported.");
    return;
  }

  try
  {
    xmlHttpReq.open("GET", url, false);
    xmlHttpReq.send("");
    element.innerHTML = xmlHttpReq.responseText;
  }
  catch(e)
  {
    alert('Error: ' + e)
  }
}

function NavBarChange()
{
  var table = document.getElementById("links");
  cells = table.rows[0].cells;
  var length = cells.length;
  for (i = 0; i<length; i++)
  {
    cells[i].style.backgroundColor = "transparent";
  }
  
  
  
   
}

function NavClick(pageid)
{
  url = "site/"+pageid + ".html";
  GetClientURL("mainPageText", url);
  NavBarChange();
  var element = document.getElementById("td" + pageid);
  if (element) 
  {
    element.style.backgroundColor = "#CA5228";
  }
}

function checkForm() 

{   
	var eName = document.getElementById("errorName");
	var eEmail = document.getElementById("errorEmail");
	var eMessage = document.getElementById("errorMessage");
	
	if (!checkName(document.getElementById('username')))
	{
		eName.style.display = "";
		eEmail.style.display = "none";
		eMessage.style.display = "none";
		return false;
	}
	if (!checkEmail(document.getElementById('email')))
	{
		eName.style.display = "none";
		eEmail.style.display = "";
		eMessage.style.display = "none";
		return false;
	}
	if (!checkName(document.getElementById('message')))
	{
		eName.style.display = "none";
		eEmail.style.display = "none";
		eMessage.style.display = "";
		return false;
	}	
	document.getElementById('myform').submit();
	return true;
}

function checkName(nm)
{   if ( nm.value )      
        return true;
    else 
        return false;
} 

function checkEmail(em)
{ var str = em.value;
  var twoPart=/^\s*([^ @][^@]*)@([^@]+[^@\. ]+)\.*\s*$/;
  var atom ="[^\\s \\( \\) < > @ , \" \\\\" +
            "\\/ : ; \\. \\[ \\] ]+";
  var dotPt=new RegExp("^" + "(" + atom + 
                     ")(\\." + atom + ")*$");
  var ipPt=
  /^\[(\d+)\.(\d+)\.(\d+)\.(\d+)\]$/;

  var part=str.match(twoPart);
  if (part==null)
     return false;
  var user=part[1];
  var host=part[2];
  em.value = user+"@"+host;

// See if "user" is valid 
  if (user.match(dotPt)==null)
  {   if (! user.match(/^"[^"]+"$/))
      return false;
  }

  var ipArr=host.match(ipPt)
  if (ipArr!=null)
  { for (var i=1;i<=4;i++)
     { if (ipArr[i]>255)
	  return false;
     }
     return true;
  }

// Domain is symbolic name
  var domainArray=host.match(dotPt);
  if (domainArray==null) 
      return false;
  var atomPt=new RegExp(atom,"g");
  var dArr=host.match(atomPt);
  var len=dArr.length;
  if (len<2) return false;
  var tld =dArr[len-1];
  if (tld.length<2)
     return false;
  return true;
}


