function toggleLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

function popUp(page)
{
	mapWindow=window.open(page, 'Contact','width=700,height=500,scrollbars=yes,resizable=1')
	mapWindow.focus()
}

function showProcess()
{
	toggleLayer('submitButton');
	toggleLayer('processWhirl');
}

function loadXMLDoc(url)
{
// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest()
  xmlhttp.open("GET",url,true)
  xmlhttp.send(null)
  xmlhttp.onreadystatechange=state_Change
  }
// code for IE
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    if (xmlhttp)
    {
    xmlhttp.open("GET",url,true)
    xmlhttp.send()
    xmlhttp.onreadystatechange=state_Change
    }
  }
}
function PrintComments()
{
	var ver = navigator.appVersion;
	var dom = document.getElementById ? 1: 0;
	var nIE6 = ( ver.indexOf('MSIE 6') > -1 && dom ) ? 1 : 0;
	var nIE5 = ( ver.indexOf('MSIE 5') > -1 && dom ) ? 1 : 0;
	var nIE4 = ( document.all && !dom ) ? 1 : 0;
	var nNS5 = ( dom && parseInt(ver) >= 5 ) ? 1 : 0;
	var nNS4 = ( document.layers && !dom ) ? 1 : 0;	
	if ( nIE6 || nIE5 || nNS5 || nNS4 )
		window.print();
	else if ( nIE4 )
		IEPrint();
      	return;
}

function comments(sid) {
	window.open('', 'thecomments', 'toolbar=no, directories=no,'+'status=no, resizable=yes, scrollbars=yes, location=no'+', dependent, width=650, height=450');
}
var mapWindow
function newWindow(s)
{
	mapWindow=window.open(s, 'mapWin','width=550,height=350,scrollbars=yes,resizable=1')
	mapWindow.focus()
}

function submitonce (theform) {
	if (document.all||document.getElementById)
	{
		for (i=0;i<theform.length;i++)
		{
			var tempobj=theform.elements[i]
			if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset")
				tempobj.disabled=true
		}
	}
}

function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
cntfield.value = maxlimit - field.value.length;
}

//<![CDATA[
function emoticon(code) {
	x = document.postform.text;
	x.value = x.value + ' [' + code + ']';
	document.postform.text.focus();
}
function emoticonp(code) {
	if (window.event && window.event.keyCode == 13) {
		return emoticon(code);
         	}
}
//]]>

//<![CDATA[
function qpost(code) {
	x = document.postform.text;
	x.value = x.value + '[' + code + ']';
	document.postform.text.focus();
}
function qpostp(code) {
	if (window.event && window.event.keyCode == 13) {
		return emoticon(code);
	}
}
//]]>

function checkUncheckAll(theElement) {
	var theForm = theElement.form, z = 0;
	while (theForm[z].type == 'checkbox' && theForm[z].name != 'checkall') {
		theForm[z].checked = theElement.checked;
		z++;
	}
}

function avail_Check()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
  {
  // if "OK"
      if (xmlhttp.status==200)
      {
          var avail = ''
          if (xmlhttp.responseText == '0')
          {
            avail = '<font color="green">Available!&nbsp;&nbsp;<img src="http://cdn.petition.fm/images/available.gif" alt="Available"/></font>';
          }
          else
          {
            avail = '<font color="red">Taken.&nbsp;&nbsp;<img src="http://cdn.petition.fm/images/taken.gif" alt="Taken"/></font>';
          }
          document.getElementById("url_chosen_avail").innerHTML = '<b>Availability:</b>&nbsp;&nbsp;&nbsp;' + avail;
      }
  }
}

function updateSub() {
  theurl = document.getElementById("url").value;
  if (theurl.length >= 3)
  {
    var url = 'http://www.petition.fm/url-check.php?name=' + theurl;
    // code for Mozilla, etc.
    if (window.XMLHttpRequest)
      {
      xmlhttp=new XMLHttpRequest()
      xmlhttp.open("GET",url,true)
      xmlhttp.send(null)
      xmlhttp.onreadystatechange=avail_Check
      }
    // code for IE
    else if (window.ActiveXObject)
      {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
        if (xmlhttp)
        {
        xmlhttp.open("GET",url,true)
        xmlhttp.send()
        xmlhttp.onreadystatechange=avail_Check
        }
      }
    document.getElementById("url_chosen").innerHTML = '<b>Selected address:</b>&nbsp;&nbsp;&nbsp;<span dir="ltr">http://www.petition.fm/petitions/' + theurl + '/</span>';
  }
  else
  {
    document.getElementById("url_chosen").innerHTML = '';
    document.getElementById("url_chosen_avail").innerHTML = '';
  }
}

function searchboxClicked() {
	if (document.getElementById("petitionSearchBox").value == 'Search Petition...')
	{
		document.getElementById("petitionSearchBox").value = '';
	}
}

function searchboxBlurred() {
	if (document.getElementById("petitionSearchBox").value == '')
	{
		document.getElementById("petitionSearchBox").value = 'Search Petition...';
	}
}

function Petition_notes_selected()
{
if (document.par.m_notes.checked == true) { 
$.jGrowl('Suggestion: Think twice before marking the Notes field as mandatory. You could lose signatures due to this demand.',{life:6000,closer:'false'});
}
}