
function preload_images() {
}

function preload_images1() {
var v = new Array();
	v[0] = new Image();
	v[0].src = 'images/software_f2.jpg';
	v[1] = new Image();
	v[1].src = 'images/medbilling_f2.jpg';
	v[2] = new Image();
	v[2].src = 'images/consulting_f2.jpg';
}

function pop_win(page, id) {
var wTop = window.screenTop + 50;
var wLeft = window.screenLeft + 150;
var v;

	v = window.open(page,"popup","resizable=yes,top=" + wTop + ",left=" + wLeft + ",height=400,width=500,status=no,toolbar=no,menubar=no,location=no,scrollbars=no");
	v.focus();
}

function pracChanged() {
	document.pracform.submit();
}

function formatNumber( obj ) {
   var val = obj.value;
   val += "";
   
   if( val == "" ) {
      return;
   }
   
   val = val.replace(/,/g, "");
   if( isNumeric(val) ) {
      val = Math.floor( val );
      if( val >= 2147483647 ) {
         val = 2147483647;
      }
      if( val <= -2147483647 ) {
         val = -2147483647;
      }
      
      obj.value = val;
   }
   else {
      obj.value = NaN;  
   }
   
}

function formatMoney( obj ) {
   var val = obj.value;
   
   val += "";
   val = val.replace(/\$/g, "");
   val = val.replace(/,/g, "");
   
   if( parseFloat(val,10)==(val*1) ) {
      obj.value = num2money(val);    
   }
}

function num2money(num) {
   num = num.toString().replace(/\$|\,/g,'');
   if(isNaN(num))
      num = "0";
   if( num >= 2147483647 ) {
      num = 2147483647;
   }
   if( num <= -2147483647 ) {
      num = -2147483647;
   }
   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 updateTotals() {
   var a = document.getElementsByName("charges")[0];
   var b = document.getElementsByName("hosp_surg")[0];
   var c = document.getElementsByName("charges_other")[0];
   var totals = document.getElementsByName("charges_total")[0];

   if( a.value != "" || b.value != "" || c.value != "" ) {
      totals.value = money2num(a.value) + money2num(b.value) + money2num(c.value);
      formatMoney( totals );
   }
   else {
      totals.value = ""
   }
   
   a = document.getElementsByName("cash")[0];
   b = document.getElementsByName("checks")[0];
   c = document.getElementsByName("credit_cards")[0];
   totals = document.getElementsByName("total_deposit")[0];
   
   if( a.value != "" || b.value != "" || c.value != "" ) {
      totals.value = money2num(a.value) + money2num(b.value) + money2num(c.value);
      formatMoney( totals );
   }
   else {
      totals.value = ""
   }
}

function money2num( val ) {
   
   if( !val ) return 0;
   
   if( val == "") {
      return 0;
   }

   val = val.replace( /\$/g, "" );
   val = val.replace( /,/g, "" );
   val = parseFloat( val, 10 );
   return val;
}

function isNumeric(val){
   val = val + "";
   val = val.replace(/\$/g,"");
   val = val.replace(/,/g,"");
   return(parseFloat(val,10)==(val*1));
}

function isDigit(num) {
	if (num.length>1){
	   return false;
	}
	var string="1234567890";
	if (string.indexOf(num)!=-1){
	   return true;
	}
	return false;
}
	
function isInteger(val){
	for(var i=0;i<val.length;i++){
		if(!isDigit(val.charAt(i))){
		   return false;
		}
   }
	return true;
}

var validFocus;
var validMsg;

function validateBatchForm() {
   var msg = "";
   var obj;
   var batchForm = document.getElementById("batchform");
   
   validFocus = null;
   validMsg = "";
   
   obj = document.getElementsByName("service_date")[0];
   if( !isDate(obj.value, "M/d/y") ) {
      validMsg += "* Date of Service is missing or invalid.\n";
      
      if( validFocus == null )
         validFocus = obj;
   }
   else {
      var a = new Date(obj.value);
      var b = new Date();
      if( a > b ) {
         validMsg += "* Date of Service cannot be in the future.\n";
      
         if( validFocus == null )
            validFocus = obj;           
      }
   }
   
   validateNumber("sheets", "* Sheet count is invalid.\n");
   validateAmt("cash", "* Cash amount is invalid.\n");
   validateAmt("checks", "* Check amount is invalid.\n");
   validateAmt("credit_cards", "* Credit card amount is invalid.\n");
   validateNumber("encounters", "* Encounter count is invalid.\n");
   validateAmt("charges", "* Charge amount is invalid.\n");
   validateAmt("hosp_surg", "* Hospital/surgery amount is invalid.\n");
   validateAmt("charges_other", "* Other charge amount is invalid.\n");
   
   if( validMsg ) {
      alert( "Please correct the following errors:\n" + validMsg );
      if( validFocus != null ) {
         validFocus.focus();
         validFocus.select();
      }
      return false;
   }
   
   batchForm.submit();
   return true;
}

function validateAmt( objName, msg ) {
   var obj = document.getElementsByName(objName)[0];

   if( obj.value == "" ) {
      return;
   }
   if( !isNumeric(obj.value) ) {
      if( validFocus == null ) {
         validFocus = obj;
      }
      validMsg += msg;
   }
}

function validateNumber( objName, msg ) {
   var obj = document.getElementsByName(objName)[0];

   if( obj.value == "" ) {
      return;
   }
   if( !isInteger(obj.value) ) {
      if( validFocus == null ) {
         validFocus = obj;
      }
      validMsg += msg;
   }
}


function afterPageLoad(focusobj) {
   if( document.getElementsByName("cash")[0] != null ) {
      formatMoney( document.getElementsByName("cash")[0] );
      formatMoney( document.getElementsByName("checks")[0] );
      formatMoney( document.getElementsByName("credit_cards")[0] );
   
      formatMoney( document.getElementsByName("charges")[0] );
      formatMoney( document.getElementsByName("hosp_surg")[0] );
      formatMoney( document.getElementsByName("charges_other")[0] );
   
      updateTotals();
   }   
   focusobj.focus();
  
}

function validateCode() {
   var len;
   
   len = document.getElementsByName("b")[0].value + "";
   if( len.length == 10 ) {
      checkin.submit();
   }
}