// JavaScript Document

	$(document).ready(function(){
		
		var timeout    = 500;
		var closetimer = 0;
		var ddmenuitem = 0;
		
		function jsddm_open()
		{  jsddm_canceltimer();
		   jsddm_close();
		   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}
		
		function jsddm_close()
		{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}
		
		function jsddm_timer()
		{  closetimer = window.setTimeout(jsddm_close, timeout);}
		
		function jsddm_canceltimer()
		{  if(closetimer)
		   {  window.clearTimeout(closetimer);
			  closetimer = null;}}
		
		$(document).ready(function()
		{  $('#jsddm > li').bind('mouseover', jsddm_open)
		   $('#jsddm > li').bind('mouseout',  jsddm_timer)});
		
		document.onclick = jsddm_close;

		//TOP PANEL SLIDER
		$(".btn-slide").click(function(){
			$("#panel").slideToggle("slow");
			$(this).toggleClass("active"); 
			return false;
		});
		
		$("#headerlinks img.arrow").click(function(){ 
								
			$("span.head_menu").removeClass('active');
			
			submenu = $(this).parent().parent().find("div.sub_menu");
			
			if(submenu.css('display')=="block"){
				$(this).parent().removeClass("active"); 	
				submenu.hide(); 		
				$(this).attr('src','../../../../lib/img/arrow_hover2.png');									
			}else{
				$(this).parent().addClass("active"); 	
				submenu.fadeIn(); 		
				$(this).attr('src','../../../../lib/img/arrow_select2.png');	
			}
			
			$("div.sub_menu:visible").not(submenu).hide();
			$("#headerlinks img.arrow").not(this).attr('src','../../../../lib/img/arrow2.png');
							
		})
		.mouseover(function(){ $(this).attr('src','../../../../lib/img/arrow_hover2.png'); })
		.mouseout(function(){ 
			if($(this).parent().parent().find("div.sub_menu").css('display')!="block"){
				$(this).attr('src','../../../../lib/img/arrow2.png');
			}else{
				$(this).attr('src','../../../../lib/img/arrow_select2.png');
			}
		});
	 
		$("#headerlinks span.head_menu").mouseover(function(){ $(this).addClass('over')})
									 .mouseout(function(){ $(this).removeClass('over') });
		
		$("#headerlinks div.sub_menu").mouseover(function(){ $(this).fadeIn(); })
								   .blur(function(){ 
										$(this).hide();
										$("span.head_menu").removeClass('active');
									});		
									
		$(document).click(function(event){ 		
				var target = $(event.target);
				if (target.parents("#headerlinks").length == 0) {				
					$("#headerlinks span.head_menu").removeClass('active');
					$("#headerlinks div.sub_menu").hide();
					$("#headerlinks img.arrow").attr('src','../../../../lib/img/arrow2.png');
				}
		});
		
	});
	
	
	//Display Loading Image
	function ShowLoading(){
		$("#dataLoading").fadeIn(900,0);
		$("#dataLoading").html("Loading... <img src='lib/img/indicator.gif' />");
	}
	
	//Hide Loading Image
	function HideLoading(){
		$("#dataLoading").fadeOut('slow');
	}
	
	function ShowSaving(){
		$("#formLoading").html("Saving... <img src='lib/img/indicator.gif' />");
		$("button").attr("disabled","disabled");
		$("#formLoading").fadeIn(900,0);
	}
	
	function HideSaving(){
		$("#formLoading").fadeOut('slow');
		$("button").attr("disabled","");
	}
	
	function ShowForm(){
		$("#confirmDelete").hide();
		$("#delete").hide();
		$('#form').resetForm();
		$("#datapage").hide();
		$("#formpage").show();
		return false;
	}
	
	function ShowData(){
		$("#formpage").hide();
		$("#confirmDelete").hide();
		$("#delete").hide();
		$('#form').resetForm();
		$("#datapage").show();
		return false;
	}
	
	function SysOk(){
		$("#sysok").slideDown("fast");
		//var t = setTimeout("sysokTmr()", 10000);
	}
	
	function SysAlert(){
		$("#sysalert").slideDown("fast");
		//var t = setTimeout("sysalertTmr()", 10000);
	}
	
	function SysError(){
		$("#syserror").slideDown("fast");
		//var t = setTimeout("syserrorTmr()", 10000);
	}

	function syserrorTmr(){
		$("#syserror").fadeOut("slow");
	}
	
	function sysalertTmr(){
		$("#sysalert").fadeOut("slow");
	}
	
	function sysokTmr(){
		$("#sysok").fadeOut("slow");
	}
	
	//REGULAR JAVASCRIPT STUFF
		
	//NAV FOR TABLE ROWS - MAKES THEM LINKS
	function DoNav(theUrl){
	  document.location.href = theUrl;
	}
	
	
	/* SET POSTACTION ON FORM SUBMIT */
	function submitForm(POSTACTION){
		if(POSTACTION=="ASK_DELETE_REC"){
			var answer = confirm("Are you sure you want to delete this record?");
			if(answer==true){
				document.form.POST_ACTION.value = "DELETE_REC";
				document.form.submit();
			}
		}else if(POSTACTION=="NEXT"){
			var answer = confirm("Are you sure you want to continue?  You will not be able to change anything on your application if you continue.");
			if(answer==true){
				document.form.POST_ACTION.value = "DOWNLOAD";
				document.form.submit();
			}
		}else{
			document.form.POST_ACTION.value = POSTACTION;
			document.form.submit();
		}
	}
	
	/* AUTO TAB */
	var isNN = (navigator.appName.indexOf("Netscape")!=-1);
	function autoTab(input,len, e) {
		var keyCode = (isNN) ? e.which : e.keyCode; 
		var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
		if(input.value.length >= len && !containsElement(filter,keyCode)) {
			input.value = input.value.slice(0, len);
			input.form[(getIndex(input)+1) % input.form.length].focus();
	}
	
	function containsElement(arr, ele) {
		var found = false, index = 0;
		while(!found && index < arr.length)
		if(arr[index] == ele)
			found = true;
		else
			index++;
			return found;
	}
	
	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
		if (input.form[i] == input)index = i;
			else i++;
			return index;
		}
		
		return true;
	}
	//END AUTO TAB

	//ONLY ENTER NUMBERS - onkeypress="return numbersonly(this, event)"
	function numbersonly(myfield, e, dec) {
	  var key;
	  var keychar;
	
	  if (window.event)
		key = window.event.keyCode;
	  else if (e)
		key = e.which;
	  else
		return true;
	  keychar = String.fromCharCode(key);
	
	  // control keys
	  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
		return true;
	
	  // numbers
	  else if ((("0123456789").indexOf(keychar) > -1))
		return true;
	
	  // decimal point jump
	  else if (dec && (keychar == ".")) {
		myfield.form.elements[dec].focus();
		return false;
	  } else
		return false;
	}
	
	//ENTER NUMBERS AND DECIMAL POINT - onkeypress="return numbersdecimal(this, event)"
	function numbersdecimal(myfield, e, dec) {
	  var key;
	  var keychar;
	
	  if (window.event)
		key = window.event.keyCode;
	  else if (e)
		key = e.which;
	  else
		return true;
	  keychar = String.fromCharCode(key);
	
	  // control keys
	  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
		return true;
	
	  // numbers
	  else if ((("0123456789.").indexOf(keychar) > -1))
		return true;
	
	  // decimal point jump
	  else if (dec && (keychar == ".")) {
		myfield.form.elements[dec].focus();
		return false;
	  } else
		return false;
	}
	
	//CURRENCY - onchange="currency(this);"
	function currency(which){
		currencyValue = which.value;
		currencyValue = currencyValue.replace(",", "");
		decimalPos = currencyValue.lastIndexOf(".");
		if (decimalPos != -1){
				decimalPos = decimalPos + 1;
		}
		if (decimalPos != -1){
				decimal = currencyValue.substring(decimalPos, currencyValue.length);
				if (decimal.length > 2){
						decimal = decimal.substring(0, 2);
				}
				if (decimal.length < 2){
						while(decimal.length < 2){
							 decimal += "0";
						}
				}
		}
		if (decimalPos != -1){
				fullPart = currencyValue.substring(0, decimalPos - 1);
		} else {
				fullPart = currencyValue;
				decimal = "00";
		}
		newStr = "";
		for(i=0; i < fullPart.length; i++){
				newStr = fullPart.substring(fullPart.length-i-1, fullPart.length - i) + newStr;
				if (((i+1) % 3 == 0) & ((i+1) > 0)){
						if ((i+1) < fullPart.length){
							 newStr = "," + newStr;
						}
				}
		}
		which.value = newStr + "." + decimal;
	}
	
	function normalize(which){
		alert("Normal");
		val = which.value;
		val = val.replace(",", "");
		which.value = val;
	}
	//END CURRENCY
	
	function addCommas(nStr){
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}
