function getcookie(cookiename)
{
	var cookiestring = document.cookie;
	var keys = cookiestring.split(';');
	retval = "";
	for (i=0; i < keys.length; i++)
	{
		var cookieKey=keys[i];
		if (cookieKey.indexOf(cookiename) >= 0)
		{
			retval = keys[i];
		}
	}
	return retval;
}

function refreshCommit()
{
  window.onfocus = ''; 
  location.reload();
}

function refresh()
{
  window.onfocus = null;
}

/*
function surfto(loc, w, h, callingWin)
{
  var top = ""+(screen.height/2)-(h/2);
  var left = ""+(screen.width/2)-(w/2);
  winStr = "'resizable=yes,status=yes,scrollbars=yes,toolbar=no,location=no,menu=no,top=" + top + ",left=" + left + ",width=" + w + ",height=" + h;
  window.onfocus = refresh;
  win = window.open(loc, "_blank", winStr);
}
*/

function surfto(loc, w, h, callingWin, bRefresh, bReturnWin)
{
  if (typeof(bRefresh)=="undefined")
  	 bRefresh = true;
  if (typeof(bReturnWin)=="undefined")
  	 bReturnWin = false;

  var top = new String( (screen.height/2)-(h/2) );
  var left = new String( (screen.width/2)-(w/2) );

  // remove any decimals if there are any
  if (top.indexOf(".") > -1)
  	top = top.substring(0,top.indexOf("."));
	
  if (left.indexOf(".") > -1)
  	left = left.substring(0,left.indexOf("."));
  
  winStr = "resizable=yes,status=yes,scrollbars=yes,toolbar=no,location=no,menu=no,top=" + top +",screenY=" + top + ",left=" + left + ",width=" + w + ",height=" + h ;

  if (bRefresh)
  {
  	window.onfocus = refreshCommit;
  }

  win = window.open(loc, "_blank", winStr);
  if (!win)
  {
	alert("SpinSite has detected a popup blocker on your system.  In order work with this site, you must allow administration windows to open.  \n\nPlease disable popup blocking for this URL.");
  }

  if (typeof(callingWin) != "undefined")
  {
  	win.callWin = callingWin;
  }
  
  if (bReturnWin){
	return win;
  }
}

function surfDialog(loc, w, h, callingWin, bRefresh, bReturnWin)
{
  if (typeof(bRefresh)=="undefined")
  	 bRefresh = true;
  if (typeof(bReturnWin)=="undefined")
  	 bReturnWin = false;

  var top = new String( (screen.height/2)-(h/2) );
  var left = new String( (screen.width/2)-(w/2) );

  // remove any decimals if there are any
  if (top.indexOf(".") > -1)
  	top = top.substring(0,top.indexOf("."));
	
  if (left.indexOf(".") > -1)
  	left = left.substring(0,left.indexOf("."));
  
  winStr = "resizable=yes,status=yes,scrollbars=no,toolbar=no,location=no,menu=no,top=" + top +",screenY=" + top + ",left=" + left + ",width=" + w + ",height=" + h ;

  if (bRefresh)
  {
  	window.onfocus = refreshCommit;
  }

  win = window.open(loc, "_blank", winStr);
  if (!win)
  {
	alert("SpinSite has detected a popup blocker on your system.  In order work with this site, you must allow administration windows to open.  \n\nPlease disable popup blocking for this URL.");
  }

  if (typeof(callingWin) != "undefined")
  {
  	win.callWin = callingWin;
  }
  
  if (bReturnWin){
	return win;
  }
}

function loginDialog(loc, w, h, action)
{
	if (typeof(action) == 'undefined')
	{
//		alert('logging out');
		obj = window.parent.document.getElementById("ActionFrame");	
		obj.src = "/main/admin/logout.php?action=noclose";
	}
	
	// hide congrats dialog if it's open
	sDiv = document.getElementById('staticDiv');
	if (typeof(sDiv) != 'undefined')
	{
		sDiv.style.visibility = 'hidden';
	}
	
	frameName = 'loginFrame';
	obj = document.getElementById(frameName);	
	obj.src = loc;
	//alert(obj.src);
	setDivHeight(w, h, 'center', 'login');
	oMenu = document.getElementById('loginDiv');
	oMenu.style.visibility = 'visible';
}

function commitText(loc)
{
  var top = -100;//(screen.height/2) - 50;
  var left = 2200;
  winStr = "resizable=yes,status=no,scrollbars=yes,toolbar=no,location=no,menu=no,width=100,height=100,top=" + top + ",left=" + left;
  window.onfocus = refreshCommit; //refresh;
  win = window.open(loc, "_blank", winStr);
  if (!win)
  {
	alert("SpinSite has detected a popup blocker on your system.  In order work with this site, you must allow administration windows to open.  \n\nPlease disable popup blocking for this URL.");
  }
}

function surftoNoRefresh(loc, w, h)
{
  if (typeof w == 'undefined')
  {
    w=800;
    h=600;
  }

  var top = new String( (screen.height/2)-(h/2) );
  var left = new String( (screen.width/2)-(w/2) );

  // remove any decimals if there are any
  if (top.indexOf(".") > -1)
    top = top.substring(0,top.indexOf("."));
    
  if (left.indexOf(".") > -1)
    left = left.substring(0,left.indexOf("."));
  
  winStr = "resizable=1,status=yes,scrollbars=yes,toolbar=no,location=no,menu=no,top=" + top + ",screenY=" + top + ",left=" + left + ",width=" + w + ",height=" + h;
  win = window.open(loc, "_blank", winStr);
  if (!win)
  {
	alert("SpinSite has detected a popup blocker on your system.  In order work with this site, you must allow administration windows to open.  \n\nPlease disable popup blocking for this URL.");
  }
  return;
}

function commitNoRefresh(loc, URLRoot)
{
	var w = 335;
	var h = 88;
  
    var top = new String( (screen.height/2)-(h/2) );
    var left = new String( (screen.width/2)-(w/2) );

    // remove any decimals if there are any
    if (top.indexOf(".") > -1)
      top = top.substring(0,top.indexOf("."));
	
    if (left.indexOf(".") > -1)
	  left = left.substring(0,left.indexOf("."));
	  
	winStr = "resizable=yes,status=no,scrollbars=yes,toolbar=no,location=no,menu=no,width=" + w + ",height=" + h + ",top=" + top + ",screenY=" + top + ",left=" + left;
	newURL = loc.replace(/[&]/g, '~');
	var commitWin = URLRoot + "/main/commit.php?url=" + newURL;
	win = window.open(commitWin, "_blank", winStr);
	  if (!win)
	  {
		alert("SpinSite has detected a popup blocker on your system.  In order work with this site, you must allow administration windows to open.  \n\nPlease disable popup blocking for this URL.");
	  }
	return;
}

function commit(loc, hideWait, callingWin)
{

  var w = 335;
  var h = 88;

  var top = new String( (screen.height/2)-(h/2) );
  var left = new String( (screen.width/2)-(w/2) );
	
  // remove any decimals if there are any
  if (top.indexOf(".") > -1)
    top = top.substring(0,top.indexOf("."));
	
  if (left.indexOf(".") > -1)
    left = left.substring(0,left.indexOf("."));

  winStr = "resizable=yes,status=no,scrollbars=yes,toolbar=no,location=no,menu=no,width=" + w + ",height=" + h + ",top=" + top + ",screenY=" + top + ",left=" + left;
  if (arguments.length > 2)
  {
    var commitWin = loc;
	if (arguments[1] == 1) // hide wait window
	{
	  var commitWin = loc;
	}
	else
	{
	  newURL = loc.replace(/[&]/g, '~');
	  var commitWin = "/main/commit.php?url=" + newURL;
	}
	win = window.open(commitWin, "_blank", winStr);
	if (!win)
	{
	alert("SpinSite has detected a popup blocker on your system.  In order work with this site, you must allow administration windows to open.  \n\nPlease disable popup blocking for this URL.");
	}
  	win.callWin = callingWin;
	return;
  }
  
  if (arguments.length == 2)
  {
  	  var commitWin = loc;
  }

  if (arguments.length == 1)
  {
	  newURL = loc.replace(/[&]/g, '~');
	  var commitWin = "/main/commit.php?url=" + newURL;
  }
  win = window.open(commitWin, "_blank", winStr);
  if (!win)
  {
	alert("SpinSite has detected a popup blocker on your system.  In order work with this site, you must allow administration windows to open.  \n\nPlease disable popup blocking for this URL.");
  }
}

function closeCallinWin()
{
	window.opener.location.reload();
}


function windowOpen(loc, w, h, t, l)
{
   window.open(loc, "_blank", "scrollbars=yes,toolbar=no,menubar=no,location=no,resizable=yes,top=" + t + ",screenY=" + t + ",left=" + l + ",width=" + w + ",height=" + h);
}

function assignPageToMenu(pageID, linkText)
{
	/*
     This function assigns the pageId and MenuId to hidden form elements, and then calls
     a php page for processing.
     
     IMPORTANT: Be sure that your page has the following form definition:
     
		<form name="assignPageForm" method="post" action="addPageToMenu.php" enctype="multipart/form-data">
		<input type="hidden" name="pageID" value="<?=empty($itemID)?'Add':'Edit';?>">
		<input type="hidden" name="linkText" value="<?=empty($itemID)?'Add':'Edit';?>">
		</form>  
	*/ 
	
	document.getElementById("pageID").value = pageidID;
	document.getElementById("linkText").value = linkText;
 	document.getElementById("assignPageForm").submit();
}

function featureAction(feature)
{
  selection = document.getElementById(feature).value;
  eval(selection);
}

function refresh()
{
  window.onfocus = ''; 
//  location.reload();
}

function isEmail(str) 
{
	// are regular expressions supported?
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported)
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}

function reposition(w, h)
{
  // Centers the current window and resizes to the w, h passed
  var top = ""+(screen.height/2)-(h/2);
  var left = ""+(screen.width/2)-(w/2);
  window.resizeTo(w, h);
  window.moveTo(left, top);
}

function validChars(txt, fld) 
{
	var illegalChars = /\W/; // allow letters, numbers, and underscores
	if (txt == "")
	{
		alert(fld + " cannot be blank");
		return false;
	}
	if (illegalChars.test(txt)) 
	{
		alert( "The " + fld + " contains illegal characters.\n" );
		return false;
	} 
	return true;
}

function numbersOnly(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	return true;
}

function featureAlign(obj, id, state, feature)
{
	commit('/main/features/' + feature + '/featureAlign.php?id=' + id + '&align=' + obj.value + state);
}

function isZipcode(strZip)
{
	return true;
	
	var s = new String(strZip);

	if (s.length != 5 && s.length != 10)
		// inappropriate length
		return false;


	for (var i=0; i < s.length; i++)
		if ((s.charAt(i) < '0' || s.charAt(s) > '9') && s.charAt(i) != '-')
			return false;

	return true;
}

