// =========================== 
// AJAX Calls
// ===========================

function getXMLHTTP()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

// =========================== 
//
// ===========================

function replaceDiv(strUrl,target) {		

   var req = getXMLHTTP();		
   if (req) {			
      req.onreadystatechange = function() {
         if (req.readyState == 4) {
            // only if "OK"
            if (req.status == 200) {						
               document.getElementById(target).innerHTML=req.responseText;						
            } else {
               alert("There was a problem while using XMLHTTP:\n" + req.statusText);
            }
         }				
	  }	
      req.open("GET",strUrl, true);
      req.send(null);
   }		
}

// =========================== 
//
// ===========================

function confirmLink(txt) {

	if (confirm(txt)) {
	   return true;
	}
	else {
	  return false;
	}
}

// =========================== 
//
// ===========================

function validate(form) {
 var valid = true;

 for (i=0;i<form.length;i++) {
 		var tempobj=form.elements[i];
		if (tempobj.name.substring(0,9)== "required_") {
			shortFieldName=tempobj.name.substring(9,30).toUpperCase();
			// THIS IS TO CHECK TEXT BOXES AND TEXTAREA
			if (tempobj.type == 'text' || tempobj.type == 'textarea' || tempobj.type == 'hidden' || tempobj.type == 'password') {
			    if (isWhitespace(tempobj.value)) { 
					if (tempobj.type == 'hidden') { alert(mPrefix + 'Hidden' + mSuffix);valid = false; break; }
					warnEmpty(tempobj,shortFieldName); valid = false; break; }
			}
			//  THIS IS TO CHECK SELECT BOXES THAT ARE NOT SET TO THE FIRST OPTION
			if (tempobj.type.toString().charAt(0)=="s"&& tempobj.selectedIndex==0) { 
		     FieldName = tempobj.name.substring(0,30).toUpperCase();
			 cName = convert_name(FieldName);
		     alert(mPrefix + cName + mSuffix);
		     valid = false;
		     break;
            }
		}
  }

   if (!valid) { form.returnValue = false; } else { form.submit(); }

} // CLOSING THE VALIDATE FUNCTION

function isEmpty(s)       { return ((s == null) || (s.length == 0)) }

function isWhitespace (s) { return (isEmpty(s)  || regWhitespace.test(s)); }

function wholeNumber(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;
}

function flip(name,src) {
    if (document.images)
    document.images[name].src = src;
}

function warnEmpty (theField, s) { 
	theField.focus();
	cName = convert_name(s);
	alert('EMPTY:\n' + mPrefix + cName + mSuffix);
}

function cent(amount) {
   amount -= 0;
   amount = (Math.round(amount*100))/100;
 return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}


// =========================== 
//
// ===========================
function comboItemSelected(oList1,oList2,cType) {
	if (oList2!=null){
 	    alert('oList2 Not Null and need to clear it out');
		clearComboOrList(oList2);
       if (oList1.selectedIndex==-1){
         oList2.options[oList2.options.length] = new Option('SELECT','');
       } else {
	  // fillCombobox(oList2,cType + '=' + oList1.options[oList1.selectedIndex].value);
	   fillCombobox(oList2,cType + oList1.options[oList1.selectedIndex].value);
	   }
	}
}

function clearComboOrList(oList) {
   for (var i = oList.options.length-1; i>=0 ; i--) {
	   oList.options[i]=null;
   }
   oList.selectedIndex = -1;
   if (oList.onchange) oList.onchange();
}

// =========================== 
//
// ===========================
function intgerOnly(i) {
	if(i.value.length>0) {
       i.value = i.value.replace(/[^\d|\.|\,]+/g, ''); 
	}
}

function fillCombobox(oList,vValue){
   if (vValue!=''){
	   if (assocArray[vValue]){
		   oList.options[0] = new Option('SELECT','');
		   var arrX = assocArray[vValue];
           for ( var i = 0; i<arrX.length ; i = i+2 ){
			   if (arrX[i]!='EOF') oList.options[oList.options.length] = new Option(arrX[i+1],arrX[i]);
           }
           if (oList.options.length == 1){
			   oList.selectedIndex=0;
			   if (oList.onchange) oList.onchange();
           }
       } else {
		 oList.options[0] = new Option('None Selected','');  
	   }
   }
}

function listboxItemSelected(oList1,oList2,cType) {
   if (oList2!=null) {
	   clearComboOrList(oList2);
      if (oList1.selectedIndex==-1) {
         oList2.options[oList2.options.length] = new Option('SELECT','');
      } else {
		 fillListbox(oList2,cType + oList1.options[oList1.selectedIndex].value)
		 // fillListbox(oList2,oList1.name + '=' + oList1.options[oList1.selectedIndex].value)
	  }
   }
   else {

   }
}


function fillListbox(oList,vValue){
   if (vValue!=''){
	   if (assocArray[vValue]){
		   var arrX = assocArray[vValue];
		   for ( var i = 0; i<arrX.length ; i = i+2 ){
			   
			   if (arrX[i]!='EOF') oList.options[oList.options.length] = new Option(arrX[i+1],arrX[i]);

           }
           if (oList.options.length == 1){
			   oList.selectedIndex=0;
			   if (oList.onchange) oList.onchange();
 
           }
       } else {
		 oList.options[0] = new Option('None Selected','');  
	   }

   }
   else {
     	
   }
 
}

function showCustomPopUp(thisUrl,thisName,theseParams)
{
	remote = open(thisUrl, thisName, theseParams);
}

function toggle( node , which ) {

    var target = document.getElementById(node);

    if ( which == "block") {
    
       target.style.display = "block";
       
    } else {

       target.style.display = "none"; 
    }
}

