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)?'inline':'none';
  vis.display = (vis.display==''||vis.display=='inline')?'none':'inline';
}

function toggleBlock( whichLayer )
{
  $("#"+whichLayer).remove();
}

function FP_swapImg() {//v1.0
 var doc=document,args=arguments,elm,n; doc.$imgSwaps=new Array(); for(n=2; n<args.length;
 n+=2) { elm=args[n]; if(elm) { doc.$imgSwaps[doc.$imgSwaps.length]=elm;
 elm.$src=elm.src; elm.src=args[n+1]; } }
}

function FP_preloadImgs() {//v1.0
 var d=document,a=arguments; if(!d.FP_imgs) d.FP_imgs=new Array();
 for(var i=0; i<a.length; i++) { d.FP_imgs[i]=new Image; d.FP_imgs[i].src=a[i]; }
}

/*
function FP_getObjectByID(id,o) {//v1.0
 var c,el,els,f,m,n; if(!o)o=document; if(o.getElementById) el=o.getElementById(id);
 else if(o.layers) c=o.layers; else if(o.all) el=o.all[id]; if(el) return el;
 if(o.id==id || o.name==id) return o; if(o.childNodes) c=o.childNodes; if(c)
 for(n=0; n<c.length; n++) { el=FP_getObjectByID(id,c[n]); if(el) return el; }
 f=o.forms; if(f) for(n=0; n<f.length; n++) { els=f[n].elements;
 for(m=0; m<els.length; m++){ el=FP_getObjectByID(id,els[n]); if(el) return el; } }
 return null;
}*/

function showEvents(el) {
//--
// retrieves the events related to a particular connote (ticket)
// el - a DOM element that must contain ONLY the ticket number as it's innerHTML
// makes extensive use of the DIV "conEvts" - do not reuse
// result will be positioned to the top-right corner of input el
//--
thisspan = $(el);
if ($("#conEvts").length == 0 || $("#conEvts").css("display")!="block"
 || $("#conEvts b:first")[0].innerHTML != el.innerHTML) {
 if ($("#conEvts").length != 0) {
  $("#conEvts").remove();
 }
 $.get("ajax/webcnenquiry.asp?cn=" + el.innerHTML,
  function(data) {
   $(document.body).append(data);
   el2 = $("#conEvts");
   el2.css("display", "block");
   var aleft = thisspan.offset().left + el.offsetWidth;
   var docwidth = $(document).width();
   if (aleft + el2[0].offsetWidth > docwidth - 10) { aleft = docwidth - 10 - el2[0].offsetWidth; }
   el2.css("left", aleft + "px");
   el2.css("top", (thisspan.offset().top - el2[0].clientHeight) + "px");
  });
 }
else {
 $("#conEvts").remove();
 }
}

function popupFileDate(filename) {
 $.get("ajax/getexportfiledate.asp?fn=" + filename,
 function(data) {
  createCustomAlert("Export File", data);
 });
}

function popupSig(sig_id) {
 createCustomAlert2("Signature", "<img alt='signature image' src='sig.dll?s=" + sig_id + "'></img>");
}

function isDate(adate_elm) {
  var outdate = parsedate(adate_elm.value, '.');
  if (outdate.length > 10) { // error message
    adate_elm.value = "";
    alert(outdate);
    return "";
  } else {
    //alert(outdate);
    return outdate;
  }
}

function parsedate(adate, asep) {
  var outdate = '';
	if (adate.value=='now' || adate.value=='NOW' || adate.value=='Now') {
	  var d=new Date();
	  var mm=d.getMonth()+1;
	  adate.value =  "" + ((d.getDate() < 10) ? "0" : "") + d.getDate();
	  adate.value += ((mm < 10) ? "/0" : "/") + mm;
	  adate.value += "/" + d.getFullYear();
	}
  var msg;
  msg='';
	if (adate!='') {
	var vDay = parseInt(adate.substring(0,adate.indexOf('/')),10);
	var vMonth = parseInt(adate.substring(adate.indexOf('/')+1,
		adate.indexOf('/',3)),10);
	var vYear = parseInt(adate.substring(adate.indexOf('/',3)+1,
		adate.length),10);
	if (vYear <10) { vYear = '200' + vYear; }
	else if (vYear <70) { vYear = '20' + vYear; }
	else if (vYear >= 70 && vYear <= 99) { vYear = '19' + vYear; }
	var vLeapY = vYear % 4;
	var isValid = 1;
	if (isNaN(vDay) || isNaN(vMonth) || isNaN(vYear)) {
		isValid = 0;
	}
	if (!((vYear >= 0 && vYear <= 99) ||
		(vYear >= 1970 && vYear <2070))) {
		isValid = 0;
		msg = msg + ' (Year out of range)';
	}
	else if (vMonth <1 || vMonth > 12) {
		isValid = 0;
		msg = msg + ' (Invalid month)';
	}
	else if (vDay <1 || vDay > 31) {
		isValid = 0;
		msg = msg + ' (Invalid day)';
	}
	else if (vDay>30 && (vMonth==2 || vMonth==4 || vMonth==6 ||
		vMonth==9 || vMonth==11)) {
		isValid = 0;
		msg = msg + ' (Invalid Num days in month)';
	}
	else if (vDay>29 && vMonth==2) {
		isValid = 0;
		msg = msg + ' (Invalid Num days in month)';
	}
	if (isValid == 0) {
    return "Invalid Date entered." + msg;
//    return "Dates must be entered in dd/mm/yyyy format.";
	}
	else if (vDay==29 && vMonth==2 && vLeapY>0) {
    return "February 29 does not exist in year " + vYear + "!!";
	}
	else {
		if (vMonth <10) { vMonth = '0' + vMonth; }
		if (vDay <10) { vDay = '0' + vDay; }
		// hack to enable strings to concatenate rather than evaluate expression.
		outdate = "" + vYear + asep + vMonth + asep + vDay;
		//adate.value=vDay + '/' + vMonth + '/' + vYear;
		}
	}
  return outdate;
}

var modalPopupKeyListener = null;
function createCustomAlert(title, txt) {
  if (!(document.getElementById)) {
    // Older browser will see standard alerts
    window.alert("txt");
    return;
  }
  ALERT_BUTTON_TEXT = "OK";
	// shortcut reference to the document object
	d = document;

	// if the modalContainer object already exists in the DOM, bail out.
	if($("body#modalContainer").length > 0) return;

	// create the modalContainer div as a child of the BODY element
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer";
	 // make sure its as tall as it needs to be to overlay all the content on the page
  $(mObj).css("height", document.documentElement.scrollHeight + "px");

	// create the DIV that will be the alert 
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "alertBox";

	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
  alertObjTop = 0;
	if(!window.opera) {
    alertObjTop = d.documentElement.scrollTop;
    $(alertObj).css("top", alertObjTop + "px");
  }
	// center the alert box
  $(alertObj).css("left", (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px");

	// create an H1 element as the title bar
	h1 = alertObj.appendChild(d.createElement("h1"));
	h1.appendChild(d.createTextNode(title));

	// create a paragraph element to contain the txt argument
	msg = alertObj.appendChild(d.createElement("p"));
	msg.innerHTML = txt;

	// create an anchor element to use as the confirmation button.
	btn = alertObj.appendChild(d.createElement("a"));
	btn.id = "closeBtn";
	btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
	btn.href = "#";
	// set up the onclick event to remove the alert when the anchor is clicked
	btn.onclick = function() { removeCustomAlert();return false; }

	alertObj.style.visibility = "visible";

  modalPopupKeyListener = $.event.add(document, "keypress", function (e) {
    if (e.which == 32 || e.which == 27) {
      removeCustomAlert(); return false;
    }
  });
}

function createCustomAlert2(title, txt) {
  if (!(document.getElementById)) {
    // can't do anything
    return;
  }
  ALERT_BUTTON_TEXT = "OK";
	// shortcut reference to the document object
	d = document;

	// if the modalContainer object already exists in the DOM, bail out.
	if($("body#modalContainer").length > 0) return;

	// create the modalContainer div as a child of the BODY element
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer";
	 // make sure its as tall as it needs to be to overlay all the content on the page
  $(mObj).css("height", document.documentElement.scrollHeight + "px");

	// create the DIV that will be the alert 
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "sigBox";

	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
  alertObjTop = 0;
	if(!window.opera) {
    alertObjTop = d.documentElement.scrollTop;
    $(alertObj).css("top", alertObjTop + "px");
  }
	// center the alert box
  $(alertObj).css("left", (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px");
  $(alertObj).css("width", "285px");
  $(alertObj).css("height", "230px");

	// create an H1 element as the title bar
	h1 = alertObj.appendChild(d.createElement("h1"));
	h1.appendChild(d.createTextNode(title));

	// create a paragraph element to contain the txt argument
	msg = alertObj.appendChild(d.createElement("p"));
	msg.innerHTML = txt;
  msg.id = 'sigP';

	// create an anchor element to use as the confirmation button.
	btn = alertObj.appendChild(d.createElement("a"));
	btn.id = "closeBtn2";
	btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
	btn.href = "#";
	// set up the onclick event to remove the alert when the anchor is clicked
	btn.onclick = function() { removeCustomAlert();return false; }

	alertObj.style.visibility = "visible";

  modalPopupKeyListener = $.event.add(document, "keypress", function (e) {
    if (e.which == 32 || e.which == 27) {
      removeCustomAlert(); return false;
    }
  });
}

// removes the custom alert from the DOM
function removeCustomAlert() {
  $(document).unbind(modalPopupKeyListener);
  $("#modalContainer").remove();
}

//Validate the time entries
function isTime(atime) {
	var isvalid;
	if (atime.value=='now' || atime.value=='NOW' || atime.value=='Now') {
		var now = new Date();
		var hours = now.getHours();
		var minutes = now.getMinutes();
		var seconds = now.getSeconds();
		var timeValue = "" + ((hours < 10) ? "0" : "") + hours; //((hours >12) ? hours -12 :hours);
		timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
		//if (timeValue == "0") timeValue = 12;
		//timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
		//timeValue += ((seconds < 10) ? ":0" : ":") + seconds
		//timeValue += (hours >= 12) ? " P.M." : " A.M."
	    atime.value = timeValue;
	}
	if (atime.value=='') {
		isvalid=2; //no action to perform
	} else if (atime.value.indexOf(':')>=0 && atime.value.length >= atime.value.indexOf(':')+3) {
		var vhour = atime.value.substring(0,atime.value.indexOf(':'));
		var vmin = atime.value.substring(atime.value.indexOf(':')+1,atime.value.indexOf(':')+3);
		var vrest = '';
		if (vhour < 0 || vhour > 23 || vmin < 0 || vmin > 59) {
			isvalid = 0;
		} else if (atime.value.length > atime.value.indexOf(':')+3) {
			vrest = atime.value.substring(atime.value.indexOf(':')+3,atime.value.length);
			while (vrest.substring(0,1) == ' ') {
				vrest = vrest.substring(1,vrest.length);
			}
			if (vhour > 12 || !(vrest.toUpperCase()=='AM' ||
				vrest.toUpperCase()=='PM')) {
				isvalid = 0;
			} else {
				isvalid = 1;
			}
		} else {
			isvalid = 1;
		}
	} else {
		isvalid = 0;
	}
	if (isvalid == 0) {
		atime.value = '';
		alert ("Please enter the time using format hh:mm (24hr format)");
		atime.focus();
		return false;
	} else if (isvalid == 1) {
		if (vhour.length==1) vhour = '0' + vhour;
		if (vrest.length>0) vrest = ' ' + vrest;
		atime.value = vhour + ':' + vmin + vrest.toUpperCase();
		return true;
	} else {return false;}
}

// link redirection via js
function link(aloc) {
  location.href=aloc;
}

function menu_over(el) {
  $(el).addClass("menu-over");
}

function menu_out(el) {
  $(el).removeClass("menu-over");
}

function activateMenu(activeMenu) {
  if (activeMenu.length > 0) { activeMenu.addClass("menu2-active"); }
}

function isNumeric( x ) {
  var RegExp = /^(-)?(\d*)(\.?)(\d*)$/; 
  var result = x.match(RegExp);
  return result;
}
function isInteger(a) {
	if (a.value != '') {
		if (a.value.substring(0,1) < '0' || a.value.substring(0,1) > '9') {
			//a.value = '';
      a.focus();
			alert("A number must be entered.");
      return false;
		} else {
			a.value = parseInt(a.value);
      return true;
		}
	}
}
function validate_qnty(a) {
	if (parseInt(a.value) <= 0 ||
		a.value.substring(0,1) > '9' ||
		a.value.substring(0,1) < '0' ) {
    a.focus();
		//a.value='1';
		alert("A valid quantity must be entered.");
	} else {
		a.value = parseInt(a.value);
	} 
}

function validate_float(a) {
	if (parseFloat(a.value) <= 0 ||
		a.value.substring(0,1) > '9' ||
		a.value.substring(0,1) < '0' ) {
    a.focus();
//		a.value='1';
		alert("A valid quantity must be entered.");
	} else {
		a.value = parseFloat(a.value);
	} 
}

function clearfieldsJOB() {
  document.JOB_QUERY.jqd1dsp.value = "";
  document.JOB_QUERY.jqd2dsp.value = "";
  document.JOB_QUERY.qcc.value = "";
  document.JOB_QUERY.qref2.value = "";
  document.JOB_QUERY.jn.value = "";
  document.JOB_QUERY.itn.value = "";
  document.JOB_QUERY.qps.value = "";
  document.JOB_QUERY.qds.value = "";
  document.JOB_QUERY.jbb.value = "";
}

function clearfieldsFA() {
  document.form1.fa_key.value = "";
  document.form1.fa_cp.value = "";
  document.form1.fa_ct.value = "";
  document.form1.fa_ad.value = "";
  document.form1.fa_sb.value = "";
}

function clearfieldsREF() {
  document.maintref1.r1_find.value = "";
}

function openPopup(url, width, height) {
    window.open(url, "popup_id", "scrollbars,resizable,width="+width+",height="+height);
    return false;
}

