// (c) Lynx Information Systems Ltd 2004
// JavaScript functions for use on HTML pages
// Version History
// V1.00 CM 31/08/04 Origin date
// V1.10 CM 24/11/04 Added getURLParameters function
// V1.20 CM 22/09/09 added toggleLayer, showHide functions

// ---------------------------------------------------------------------
// toggleLayer - show/hide a DIV by clicking on a link
// 2/12/2008
// from http://www.netlobo.con/div_hiding.html
function toggleLayer(whichLayer) {
  //alert("toggleLayer");
  var elem, vis;  
  if (document.getElementById) elem = document.getElementById(whichLayer);
  else if (document.all) elem = document.all[whichLayer];
  else if (document.layers) elem = 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 showHide(link,whichLayer,swapText) {
  toggleLayer(whichLayer);
  
  var text = swapText.split(",");
  var s = link.innerHTML;
  if (s.indexOf(text[0])>=0) 
    link.innerHTML = s.replace(text[0],text[1]);
  else 
    link.innerHTML = s.replace(text[1],text[0]);
  return false;
}

// ---------------------------------------------------------------------
// get parameter name/value pairs from URL
function getURLParameters()
{
	var url = document.location.href;

	var params = new Array();

	if (url.indexOf("?") > 0) {
		var arrParams = url.split("?");
		var arrURLParams = arrParams[1].split("&");

		var i = 0;
		var name = "";
		var value = "";

		for (i=0;i<arrURLParams.length;i++) {

			var sParams =  arrURLParams[i].split("=");
			name = sParams[0];
			if (sParams[1] != "") value = unescape(sParams[1]);
			else value = "";

			params[name] = value;
		}

	}

	return params; // returns associative array of params[name]=value

}
// ---------------------------------------------------------------------
// Init
// get a parameter value from document's URL, and populate an edit box
function Init(paramname, control) {
   var url = document.location.href;
   var i = url.indexOf("?"+paramname);

   if (i>=0) {
      i = i+paramname.length+2;
      var j = url.indexOf("?",i);
      if (j<0) {
         j = url.length;
      }

      var paramvalue = url.substring(i,j);

      control.value = paramvalue;
   }
   return true;
}

// -----------------------------------------------------------------------
// EmailCheck
// check the value in the specified control is a valid email address
function EmailCheck(control) {
  if ((control.value != "") || (control.value != "Your Email Address")) {

    var goodEmail = control.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);

    if (goodEmail) {
      return true;
    }
    else {
      alert('Please enter a valid e-mail address.')
      control.focus();
      control.select();
      return false;
    }
  }
}

// ---------------------------------------------------------------------
// HexCheck
// check the value in the specified control is a valid hex number
function HexCheck(control, errormessage) {
   var s = control.value;
   var i = parseInt(s,16); // convert string to hex integer
   if (isNaN(i) || (i<=0)) {
      alert(errormessage);
      control.focus();
      control.select();
      return false;
   }
   else {
      return true;
   }
}

// -----------------------------------------------------------------------
// NotBlankCheck
// check the value in the specified control is not a blank string,
// and does not contain double-quote characters
function NotBlankCheck(control, errormessage) {
	if (control.value != "") {
		if (control.value.indexOf("\"")<0) {
			return true;
		}
		else {
			alert("Invalid character (double-quote) in text");
			control.focus();
			return false;
		}
	}
	else {
		alert(errormessage);
		control.focus();
		//control.select();
		return false;
	}
}

