/****************************************************************
 * common.js
 * Created: Apr 20 - 8:35:58 PM
 * author: Ian Leonardo
 * version: $Revision: $ $Date: $
 ****************************************************************/

function putFocus(formInst, elementInst) {
  if (document.forms.length > 0) {
     document.forms[formInst].elements[elementInst].focus();
  }
}

function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function isEmpty(ctl, ctlName) {
	if(ctl.value == "")	{
		alert(ctlName + " should not be empty.");
		try {
			ctl.focus();
		} catch(e) {}
		return true;
	}
	return false;
}

var dtCh= "/";

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function datestrtointeger(dtStr) {
    var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)

    var d = day + (month*31) + (year*31*12);

    return d;
}

function compareDate(dtStr1, dtStr2) {
   var dt1 = datestrtointeger(dtStr1);
   var dt2 = datestrtointeger(dtStr2);

    if (dt1 > dt2) return 1; else
    if (dt1 < dt2) return -1; else
        return 0;
}

//=============================================================
//    Function to get radio button
//=============================================================
function getRadio(ctl,defaultval) {
   var i;
   if(ctl != null) {
       if(ctl.length) {
           for (i=0;i<ctl.length;i++) {
             if (ctl[i].checked) {
                 return ctl[i].value;
             }
           }
       } else {
            if(ctl.checked)
            return ctl.value;
       }
   }

   return defaultval;
}

function isTime(x) {
   var d = x.split(':');
   if (d.length != 2) {
      alert('Enter a valid time (hh:mm)');
      return false;
   }

   d[0]=parseInt(d[0]);
   d[1]=parseInt(d[1]);

   if (isNaN(d[0]) ||
       (d[0]<0) ||
       (d[0]>23)
       ) {
       alert('Enter a valid hour (0..23)');
       return false;
    }

   if (isNaN(d[1]) ||
       (d[1]<0) ||
       (d[1]>59)
       ) {
       alert('Enter a valid minute (0..59)');
       return false;
    }

    return true;
}

function popupPage(url, width, height, par) {
   var returnValue = window.showModalDialog(url, par,
       "dialogWidth:" + width + "px;"+
       "dialogHeight:" + height + "px;"+
       "resizeable:no;status:no; help:no;");

   return returnValue;
}

function getValueFromCode(lu,code) {
   for (var i=0;i<lu.length/2;i++) {
      if (lu[i*2] == code) {
         var o = lu[i*2+1];

         if (o instanceof Array)
            return o[0];

         return o;
      }
   }

   return null;
}

function clearCombo(ctl) {
   while(ctl.options.length > 0)
      ctl.options[0] = null;
}

function validEmail(email) {
	if (email.length > 0) {
		i = email.indexOf("@");
		j = email.indexOf(".",i);
		k = email.indexOf(",");
		kk= email.indexOf(" ");
		jj= email.lastIndexOf(".")+1;
		len=email.length;

		if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) &&
			(len-jj >=2) && (len-jj<=3)) {
         return true;
		} else {
    	    return false;
		}
	}
}

function newWindow(mypage,myname,w,h,features) {
	if(screen.width) {
		var winl = (screen.width-w)/2;
		var wint = (screen.height-h)/2;
	} else { winl = 0;wint =0; }
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;
	var settings = 'height=' + h + ',';
	settings += 'width=' + w + ',';
	settings += 'top=' + wint + ',';
	settings += 'left=' + winl; // + ',';
	settings += features;
	win = window.open(mypage,myname,settings);
	win.window.focus();
}

// conversion from string to float, and remove formatting
function toFloat(value) {
    if(value == null || value == '') return 0;
	else return parseFloat(justNumberNF(value));
}

// convert float number to money format
function toMoney(value, digit) {
   var number = new NumberFormat();
	if (digit == null) digit = 0;
	number.setCommas(true);
	number.setCurrency(false);
	number.setPlaces(digit);
    number.setNumber(value);

    return number.toFormatted();
}
