//Common js function
//Created by : tuyenvv
//Created date : 8-6-2006
//Modified :
function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}	
	return sString;
}

function emailValidated(txtInput){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(txtInput))
	{
		//alert("email dung");
		return true;
	}
	else {
		alert("Ban phai nhap Dia chi Email chinh xac");
		return false;
	}
}

function isNumeric(sText) {
	strValue = trimAll(sText);	
	//var re = new RegExp("[0-9]*");
	var re = /^[0-9]+$/;
	//return strValue.match(re);
	return re.test(strValue);
}

function isDate(sText) {
	//dd-mm-yyyy
	var objRegExp = /^\d{1,2}(\-)\d{1,2}\1\d{4}$/

	//strValue=trimAll(sText);
	strValue = trimAll(sText);

	//check to see if in correct format
	if (!objRegExp.test(strValue)) {
    	return false; //doesn't match pattern, bad date
	}
  	else {
    	var strSeparator = '-'; //find date separator
    	var arrayDate = strValue.split(strSeparator); //split date into month, day, year
    	//create a lookup for months not equal to Feb.
    	var arrayLookup = { '01' : 31, '1' : 31, '03' : 31, '3' : 31, '04' : 30, '4' : 30, '05' : 31, '5' : 31, '06' : 30, '6' : 30, '07' : 31, '7' : 31,
        	                '08' : 31, '8' : 31, '09' : 30, '9' : 30, '10' : 31, '11' : 30, '12' : 31}
    	var intDay = 0;
		if (arrayDate[0].substr(0,1)=='0') {
			intDay = parseInt(arrayDate[0].substr(1,1));
		}
		else {
			intDay = parseInt(arrayDate[0]);
		}

		//check if month value and day value agree
    	if (arrayLookup[arrayDate[1]] != null) {
			if ((intDay <= arrayLookup[arrayDate[1]]) && (intDay != 0))
        		return true; //found in lookup table, good date
    	}
		else {
			//check for February
			var intYear = parseInt(arrayDate[2]);

			var intMonth = 0;
			if (arrayDate[1].substr(0,1)=='0') {
				intMonth = parseInt(arrayDate[1].substr(1,1));
			}
			else {
				intMonth = parseInt(arrayDate[1]);
			}
			if (intMonth>12) {
				return false;
			}
			
			if (((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0) {
				return true; //Feb. had valid number of days
			}
			else {
				return false; //Feb. had valid number of days
			}
		}
	}
}

function isDateInput(sInput) {
	var re = new RegExp("([1-9]|[12][0-9]|3[01])[- /.]([1-9]|1[012])[- /.](19|20)\d\d");
	return sInput.value.match(re);
}



function doValidate(pInput,pAlert,pMessage,type) {
	var ok = true;
	switch (type) {
		// not empty text
		case 1 : {
			if (pInput.value == "") {
				ok = false;
				if (pMessage.style.visibility = "hidden") {
					pMessage.style.visibility = "visible";
					pAlert.style.visibility = "visible";
				}
			}
			else {
				ok = true;
				if (pMessage.style.visibility = "visible") {
					pMessage.style.visibility = "hidden";
					pAlert.style.visibility = "hidden";
				}
			}
			break;
		}

		// non zero numeric
		case 2 : {
			if ((!isNumeric(pInput.value)) || (pInput.value == 0)) {
				ok = false;
				if (pMessage.style.visibility = "hidden") {
					pMessage.style.visibility = "visible";
					pAlert.style.visibility = "visible";
				}
			}
			else {
				ok = true;
				if (pMessage.style.visibility = "visible") {
					pMessage.style.visibility = "hidden";
					pAlert.style.visibility = "hidden";
				}
			}
			break;
		}

		// valid numeric
		case 3 : {
			if (!isNumeric(pInput.value)) {
				ok = false;
				if (pMessage.style.visibility = "hidden") {
					pMessage.style.visibility = "visible";
					pAlert.style.visibility = "visible";
				}
			}
			else {
				ok = true;
				if (pMessage.style.visibility = "visible") {
					pMessage.style.visibility = "hidden";
					pAlert.style.visibility = "hidden";
				}
			}
			break;
		}
		
		// valid date
		case 4 : {
			if (!isDate(pInput.value)) {
				ok = false;
				if (pMessage.style.visibility = "hidden") {
					pMessage.style.visibility = "visible";
					pAlert.style.visibility = "visible";
				}
			}
			else {
				ok = true;
				if (pMessage.style.visibility = "visible") {
					pMessage.style.visibility = "hidden";
					pAlert.style.visibility = "hidden";
				}
			}
			break;
			/*if (pInput.value == "") {
				ok = false;
				if (pMessage.style.visibility = "hidden") {
					pMessage.style.visibility = "visible";
					pAlert.style.visibility = "visible";
				}
			}
			else {
				ok = true;
				if (pMessage.style.visibility = "visible") {
					pMessage.style.visibility = "hidden";
					pAlert.style.visibility = "hidden";
				}
			}
			break;*/
		}
	}
	return ok;
}

function showPopup(url) {
	 var NhapMoi = window.open(url,'console','toolbar=0,status=0,menubar=0,fullscreen=no,width=400,height=220,scrollbars=no,resizable=0');
}

function showPopupWH(url,w,h) {
	 var NhapMoi = window.open(url,'console','toolbar=0,status=0,menubar=0,fullscreen=no,width='+w+',height='+h+',scrollbars=no,resizable=0');
}


function showPopupHeight(url) {
	 var NhapMoi = window.open(url,'console','toolbar=0,status=0,menubar=0,fullscreen=no,width=350,height=270,scrollbars=no,resizable=0');
}

function showPopupLarge(url) {
	 var NhapMoi = window.open(url,'console','toolbar=0,status=0,menubar=0,fullscreen=no,width=720,height=500,scrollbars=yes,resizable=0');
}
function ShowPopupScroll(url)
 {
		 var NhapMoi = window.open(url,'console','toolbar=0,status=0,menubar=0,fullscreen=no,width=350,height=350,resizable=0,scrollbars=1,left=200,top=100');
 }
 function ClosePopup()
 {
 	 window.opener.location = window.opener.location;self.close()
 }
