var regex_numeric = /[^0-9-]/g;
var regex_decimal = /[^0-9.-]/g;
var regex_date = /[^0-9-]/g;

function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="random"){
	LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
	TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
}
if(pos=="center"){
	LeftPosition=(screen.width)?(screen.width-w)/2:100;
	TopPosition=(screen.height)?(screen.height-h)/2:100;
}
else if((pos!="center" && pos!="random") || pos==null){
	LeftPosition=0;TopPosition=20
}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
win=window.open(mypage,myname,settings);
}

/* Made by Mathias Bynens <http://mathiasbynens.be/> */
/* http://krijnhoetmer.nl/stuff/javascript/number-format */
function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}


// ---------------------------------------------------------------------
// -- Return a false (or alert) if the date is invalid, used within an event or within onblur(this, true)
function myDateValidate(objThis, IsAlert) {
	
	var varValue = objThis.value;
	if (varValue == "0000-00-00" || varValue.length == 0) {
		return true;
	}
	var intYear = parseInt(varValue.substr(6,4), 10);		// radix = base10, avoid 09 octal problem
	var intMonth = parseInt(varValue.substr(0,2), 10) - 1;
	var intDay = parseInt(varValue.substr(3,2), 10);
	varDate = new Date(intYear, intMonth, intDay);

	if (isNaN(varDate) == true) {
		if (IsAlert == true) alert("The date value or format is invalid!\nPlease use dd-mm-yyyy.");
		return false;
	}
	
	if (intYear != varDate.getFullYear() || intMonth != varDate.getMonth() || intDay != varDate.getDate()) {
		if (IsAlert == true) alert("The date value or format is invalid!\nPlease use dd-mm-yyyy.");
		return false;
	}
	else {
		return true;
	}
}


// ---------------------------------------------------------------------
// -- Encoding mask for date in DD-MM-YYYY format using onkeyup event
function myDateMask(objThis, objEvent) {
	// BROWSER SAFE: perform a key-by-key masking in format YYYY-MM-DD
	// valid numbers & format
	var strCheck = "48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105";
	var strSeparator = "109,189";

	var varKey = "";
	if (objEvent.keyCode) 	varKey = objEvent.keyCode;	// work for IE
	if (objEvent.which) 	varKey = objEvent.which;	// work Moz & NS6/7

	var varValue = objThis.value;
	var varLength = varValue.length;
	
	var intKey = parseInt(varKey);
	
	trgt = objThis.value;
	
	/* if (regex_date.test(trgt)) {
		objThis.value = trgt.replace(regex_date, '');     
		return;		 
	} */
	
	if (intKey == 32) objThis.value = varValue.substr(0, varValue.length - 1);

	if ((intKey < 48 || intKey == 144 || intKey == 145 || intKey == 93) || (intKey >= 112 && intKey <= 123)) 
		return true;

	if ((varLength >= 1 && varLength <= 2) || (varLength >= 4 && varLength <= 5) || (varLength >= 9 && varLength <= 10)) {
		if (strCheck.indexOf(varKey) == -1) {
			objThis.value = varValue.substr(0, varValue.length - 1);
		}
		else {
			if (varLength == 2)	objThis.value = varValue + "-";
			if (varLength == 5)	objThis.value = varValue + "-";
		}
		return true;
	}
	// allow only the valid separator on 5th and 8th
	if (varLength == 3 || varLength == 6) {
		if ((strSeparator.indexOf(varKey) == -1)) {
			objThis.value = varValue.substr(0, varValue.length - 1);
		}
		return true;
	}
	// invalid length greater than 10
	if (varLength > 10) {
		objThis.value = varValue.substr(0, 10);
	}
}
function toggle_display (id) {
	obj = document.getElementById(id);
	if (obj) {
		if (obj.style.display == 'block') obj.style.display = 'none';
		else obj.style.display = 'block';
	}
}


function onblur_isnumeric (obj) {
    var trgt = obj.value;
    if (regex_numeric.test(trgt)) {
        window.alert('Invalid characters. Only numeric numbers are allowed.');
        obj.value = trgt.replace( regex_numeric, '');       
    }

}

function onkeyup_isnumeric (obj, ev) {
    var trgt = obj.value;
    key = ev.keyCode || ev.which;
    if ((key < 48 || key > 57) && (key < 96 || key > 105) && key != 8) {
        obj.value = trgt.replace(regex_numeric, '');
    }
}


function onblur_isdecimal (obj) {
    var trgt = obj.value;
    if (regex_decimal.test(trgt)) {
        window.alert('Invalid characters. Only numeric numbers and dot(.) are allowed.');
        obj.value = trgt.replace( regex_decimal, '');       
    }
	alert();
}

function onkeyup_isdecimal (obj, ev) {
    var trgt = obj.value;
    key = ev.keyCode || ev.which;
    if ((key < 48 || key > 57) && (key < 96 || key > 105) && key != 8) {
        obj.value = trgt.replace(regex_decimal, '');
    }
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
function emailcheck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }
	 if (str.indexOf(" ")!=-1){
		return false
	 }
	 return true					
}


function cbo_onchange(varCBO, varTXT) {
	if (document.getElementById(varCBO).value == "A") {
		document.getElementById(varTXT).value = "";
		document.getElementById(varTXT).style.display= "block";
		document.getElementById(varCBO).style.display= "none";
		document.getElementById(varTXT).focus();
	}
	/* else {
		document.getElementById(varTXT).value = document.getElementById(varCBO)[document.getElementById(varCBO).selectedIndex].text;
	} */
}
function txt_onblur(varCBO, varTXT) {
	varTemp = document.getElementById(varTXT).value;
	if (varTemp.length == 0) {
		document.getElementById(varTXT).style.display= "none";
		document.getElementById(varCBO).style.display= "block";
		document.getElementById(varCBO).value = '';
	}
}