function showasmoney(x) {
    x -= 0;
    x = (Math.round(x*100))/100;
    return (x == Math.floor(x)) ? x + '.00' : (  (x*10 == Math.floor(x*10)) ? x + '0' : x);
}

function formatnum_d(str) {
   str = str.replace(/[^0-9.-]/g,'');
   if (str == '') {str = '0'}

   if (!isNaN(str)) {
      var fx = parseFloat(str);
      } else {
      var fx = 0;
      }
   return fx;     
}

function formatnum_c(str) {
   str = str.replace(/[^0-9,-]/g,'');
   if (str == '') {str = '0'}

   if (!isNaN(str)) {
      var fx = parseFloat(str);
      } else {
      var fx = 0;
      }
   return fx;     
}

function formatnumeric_eu(str) {
   str = str.replace(/\./g,'');
   str = str.replace(/\ /g,'');
   if (str == '') {str = '0'}

   if (!isNaN(str)) {
      var fx = parseFloat(str);
      } else {
      var fx = 0;
      }
   return fx;
}

function gety(){
    if ((event.screenY+170) < screen.height)
       {return (event.screenY - 85);}
       else
       {return event.screenY-200;}
}

function getx(){
    if ((event.screenX+170) < screen.width)
       {return event.screenX-85;}
       else
       {return event.screenX-200;};
}

function setsave(){
   top.frames['tdata'].document.forms[0].elements['sv'].value='yes';
   document.forms[0].elements['save_button'].src='images/save_a.gif';
}

function dosave(){
   top.frames['tdata'].document.forms[0].elements['sv'].value='no';
   document.forms[0].submit();
}


function savemsg(){
   if (top.frames['tdata'].document.forms[0].elements['sv'].value=='yes') {
      if (confirm('Click OK to discard your changes. To save changes, click Cancel and then the Save button.')) {       
         top.frames['tdata'].document.forms[0].elements['sv'].value='no';
         return true;
         }
         else {
         return false;
         }
      }
      else {
      return true;
      }
} 

function isLeapYear(wdt) {
	return (wdt.getFullYear() % 4 == 0);
}

function getDaysInMonth(wdt) {
	switch(wdt.getMonth()) {
		//november
		case 10 : return 30;
		//april
		case 3  : return 30;
		//june
		case 5  : return 30;
		//september
		case 8  : return 30;
		//february
		case 1  :
			if (isLeapYear(wdt)) {return 29;}
			else {return 28;}
		//others
		default :
			return 31;
	}
}

function getDaysInPrevMonth(wdt) {
	switch(wdt.getMonth()) {
		//november
		case 11 : return -30;
		//april
		case 4  : return -30;
		//june
		case 6  : return -30;
		//september
		case 9  : return -30;
		//february
		case 2  :
			if (isLeapYear(wdt)) {return -29;}
			else {return -28;}
		//others
		default :
			return -31;
	}
}

function newItem(dropdown) {
   ws=prompt("Field Value:", "");
   if (ws != null) {
      if (ws.length > 0) {
         var wOption = new Option(ws, ws);
         x = dropdown.length;
         dropdown.options[x] = wOption;
         dropdown.options[x].selected = 1;
      }
   }
}

function newItemSave(dropdown) {
   ws=prompt("Field Value:", "");
   if (ws != null) {
      if (ws.length > 0) {
         var wOption = new Option(ws, ws);
         x = dropdown.length;
         dropdown.options[x] = wOption;
         dropdown.options[x].selected = 1;
         setsave();
      }
   }
}

function isZero(wVal) {
   if (wVal == 0) {
      return true; } else {   
      return false;} 
   }

function CalcTotal() {
   x=document.forms[0].elements['units'].value;
   y=document.forms[0].elements['unitprice'].value;
   z=document.forms[0].elements['amount'].value;

   if (isZero(z)) {
      t=x*y;
      if (!isNaN(t)) {
         document.forms[0].elements['amount'].value=showasmoney(x*y);
         }
      } 

   if (isZero(y)) {
      t=z/x;
      if (!isNaN(t)) {
         document.forms[0].elements['unitprice'].value=showasmoney(z/x);
         }
      } 

   if (isZero(x))  {
      t=z/y;
      if (!isNaN(t)) {
         document.forms[0].elements['units'].value=z/y;
         }
      }
   }


function DateRecalc(wedit,wx) {
   ws = wedit.value;
   wdt = new Date(ws);
   if (wx > 0) {
      if (wx == 30) {wx = getDaysInMonth(wdt);}
      } 
      else {
      if (wx == -30)  {wx = getDaysInPrevMonth(wdt);}
      }
   wdt.setTime(wdt.getTime()+(wx*1000*60*60*24));
   wedit.value = wdt.getMonth()+1+"/"+wdt.getDate()+"/"+wdt.getYear();
   }

