function isURL(argvalue) {

  if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("http://") == -1)
    return false;
  else if (argvalue == "http://")
    return false;
  else if (argvalue.indexOf("http://") > 0)
    return false;

  argvalue = argvalue.substring(7, argvalue.length);
  if (argvalue.indexOf(".") == -1)
    return false;
  else if (argvalue.indexOf(".") == 0)
    return false;
  else if (argvalue.charAt(argvalue.length - 1) == ".")
    return false;

  if (argvalue.indexOf("/") != -1) {
    argvalue = argvalue.substring(0, argvalue.indexOf("/"));
    if (argvalue.charAt(argvalue.length - 1) == ".")
      return false;
  }

  if (argvalue.indexOf(":") != -1) {
    if (argvalue.indexOf(":") == (argvalue.length - 1))
      return false;
    else if (argvalue.charAt(argvalue.indexOf(":") + 1) == ".")
      return false;
    argvalue = argvalue.substring(0, argvalue.indexOf(":"));
    if (argvalue.charAt(argvalue.length - 1) == ".")
      return false;
  }

  return true;

}
function trim(str)
	{	var i, j
		for (i = 0; i < str.length; i++)
			if (str.charAt(i) != ' ') break;
		for (j = str.length - 1; j >= 0; j--)
			if (str.charAt(j) != ' ') break;
		return str.substr(i, j - i + 1)
	}

function isNumeric(str)
{	var iLen;
	iLen=str.length;
	var c ;
	for(var i=0; i<iLen; i++)
	{	c = str.charAt(i);
		if ((c<'0') || (c>'9'))
			return false;
	}
	return true;
}

function isFloat(str)
{	var f = str.indexOf('.')
	var l = str.lastIndexOf('.')
	if (f != l)
		return false
	var iLen;
	iLen=str.length;
	var c ;
	for(var i = 0; i<iLen; i++)
	{	c = str.charAt(i);
		if (((c < '0') || (c > '9')) && (c != '.'))
			return false;
	}
	return true;
}

function isNull(str)
{	
	if(str==null)
		return true;
	var iLen = str.length;
	for (var i = 0; i < iLen; i++)
		if (str.charAt(i)!= ' ')
			return false;
	return true;
}

function Trim(str)
{
	while((str.length > 0) && (str.charAt(0) == ' '))
			str = str.substring(1,str.length);
	while((str.length > 0) && (str.charAt(str.length-1) == ' '))
			str = str.substring(0,str.length-1);
	return str;
}

function NumberValid(thefield)
{	var NumStr = Trim(thefield.value)
	if (isNull(NumStr))
		return false
	if (isNaN(parseInt(NumStr)))
		return false
	return true
}

function FloatValid(thefield)
{	var NumStr = Trim(thefield.value)
	if (isNull(NumStr))
		return false
	if (isNaN(parseFloat(NumStr)))
		return false
	return true
}

function dateValid(thefield)
{
	var dateStr = Trim(thefield.value);
	if (isNull(dateStr))
		return true;
	var i1 = dateStr.indexOf("-");
	var j1 = dateStr.indexOf("-", i1 + 1);
	if ((i1 == -1) || (j1 == -1))
		return false;
	var day = parseInt(dateStr.substr(0, i1), 10);
	var month = parseInt(dateStr.substr(i1 + 1, j1 - i1 - 1), 10);
	var year = parseInt(dateStr.substr(j1 + 1), 10);
	if ((month < 1) || (month > 12))
		return false;
	if (isNaN(day) || isNaN(month) || isNaN(year) || (year < 0)) 		
		return false;
	if (year < 30)
		year += 2000;
	else if (year < 100)
		year += 1900;
	var DOM = 31;
	switch(month)
	{
		case 2:
			DOM = ((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0)) ? 29 : 28;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			DOM = 30;break;
		default:
			DOM = 31;
	}
	if ((day < 1) || (day > DOM))
		return false;
	thefield.value = ((day<10)?"0"+day:day)+"-"+((month < 10)?"0"+month:month) + "-" + year;
	return true;
}

function timeValid(thefield)
{	var timeStr = Trim(thefield.value)
	
	if (isNull(timeStr)) return true
	
	var i1 = timeStr.indexOf(":")
	if (i1 == -1)
	{	timeStr += ":0"
		i1 = timeStr.indexOf(":")
	}
	var j1 = timeStr.indexOf(":", i1 + 1)
	
	var hour = parseInt(timeStr.substr(0, i1), 10)
	
	if(j1==-1)
		var minute = parseInt(timeStr.substr(i1+1), 10)
	else
		var minute = parseInt(timeStr.substr(i1+1, j1 - i1 - 1), 10)
		
	if (isNaN(hour) || isNaN(minute)) return false;	

	if(hour < 0 || hour > 24) return false;		
	if(minute < 0 || minute >59) return false;
	
	thefield.value = ((hour < 10) ? ("0" + hour) : hour) + ":" + ((minute<10) ? "0" + minute : minute);
		
	return true
}

function EmailValid(str)
{	if(isNull(str))
		return true;
	var chrs = "~`!#$%^&*()+=|\{}[]':;<>\",/?";
	for(var i = 0; i < chrs.length; i++)
		if(str.indexOf(chrs.charAt(i)) < 0)
			continue;
		else
			return false;	
	var i = str.indexOf("@"), j = str.indexOf("."), k = str.indexOf("@", i + 1)
	if ((i == -1) || (j == -1) || (str.charAt(str.length-1) == '.') || (k >= 0))
		return false;	
	return true;
}
function isEmail(fld){ 
    var phony= /@(\w+\.)*example\.(com|net|org|info|vn)$/i;
    if(phony.test(fld))
    {          
      return false; 
    }
	
    var emailfmt= /^\w+([.-]\w+)*@\w+([.-]\w+)*\.\w{2,8}$/;
	
    if(!emailfmt.test(fld))
    { 
      return false; 
    }
	return true;
}
function ImageFileValid(str) {
    validImageFile = /(.jpg|.JPG|.gif|.GIF)$/;
    if (str == "") return true;
    if (validImageFile.test(str)) return true;
    return false;
}

function popUp(URL,w,h) {
	winl = (screen.width - w) / 2;
	wint = (screen.height - h) / 2;
	day = new Date();
	id = day.getTime();
	eval("eventPage" + id + " = window.open('','" + id + "', 'toolbar=0,scrollbars=1,location=0,status=1,menubar=0,resizable=1,width="+w+",height="+h+",top="+wint+",left="+winl+"');");
}
function popUpWindow(URL,w,h) {
    window.open(URL, '', 'width='+w+',height='+h +',scrollbars=yes,resizable,');
    return false;
}
function popup(content){ 
    CONTENT = "<HTML><BODY><TABLE cellSpacing=1 cellPadding=3 width=100% border=0 ><tr><td align=left>"+content+"</td></tr></TABLE> </BODY> </HTML>";
    pop = window.open("","",'width=380,height=350,toolbar=0,scrollbars=1,screenX=200,screenY=200,left=200,top=200,resizable');
    pop.document.open();
    pop.focus();
    pop.document.write(CONTENT);
    pop.document.close();

}
/**
* Pops up a new window in the middle of the screen
*/
function popUpMiddleWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
} 
// JS Calendar
var calendar = null; // remember the calendar object so that we reuse
// it and avoid creating another

// This function gets called when an end-user clicks on some date
function selected(cal, date) {
	cal.sel.value = date; // just update the value of the input field
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks the "Close" (X) button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
	cal.hide();			// hide the calendar

	// don't check mousedown on document anymore (used to be able to hide the
	// calendar when someone clicks outside it, see the showCalendar function).
	Calendar.removeEvent(document, "mousedown", checkCalendar);
}

// This gets called when the user presses a mouse button anywhere in the
// document, if the calendar is shown.  If the click was outside the open
// calendar this function closes it.
function checkCalendar(ev) {
	var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
	for (; el != null; el = el.parentNode)
	// FIXME: allow end-user to click some link without closing the
	// calendar.  Good to see real-time stylesheet change :)
	if (el == calendar.element || el.tagName == "A") break;
	if (el == null) {
		// calls closeHandler which should hide the calendar.
		calendar.callCloseHandler(); Calendar.stopEvent(ev);
	}
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id) {
	var el = document.getElementById(id);
	if (calendar != null) {
		// we already have one created, so just update it.
		calendar.hide();		// hide the existing calendar
		calendar.parseDate(el.value); // set it to a new date
	} else {
		// first-time call, create the calendar
		var cal = new Calendar(true, null, selected, closeHandler);
		calendar = cal;		// remember the calendar in the global
		cal.setRange(1900, 2070);	// min/max year allowed
		calendar.create();		// create a popup calendar
	}
	calendar.sel = el;		// inform it about the input field in use
	calendar.showAtElement(el);	// show the calendar next to the input field

	// catch mousedown on the document
	Calendar.addEvent(document, "mousedown", checkCalendar);
	return false;
}

//nhung ham ben duoi chua test
///isDate
function isDate(DateToCheck,strformatdate){
	if(DateToCheck==""){return true;}
	var m_strDate = FormatDate(DateToCheck,strformatdate);
	if(m_strDate==""){
	return false;
	}
	var m_arrDate = m_strDate.split("/");
	var m_DAY = m_arrDate[0];
	var m_MONTH = m_arrDate[1];
	var m_YEAR = m_arrDate[2];
	if(m_YEAR.length > 4){return false;}
	m_strDate = m_MONTH + "/" + m_DAY + "/" + m_YEAR;
	var testDate=new Date(m_strDate);
	if(testDate.getMonth()+1==m_MONTH){
	return true;
	} 
	else{
		return false;
	}
}//end function




function FormatDate(DateToFormat,FormatAs){
	if(DateToFormat==""){return"";}
	if(!FormatAs){FormatAs="dd/mm/yyyy";}
	
	var strReturnDate;
	FormatAs = FormatAs.toLowerCase();
	DateToFormat = DateToFormat.toLowerCase();
	var arrDate
	var arrMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var strMONTH;
	var Separator;
	
	while(DateToFormat.indexOf("st")>-1){
	DateToFormat = DateToFormat.replace("st","");
	}
	
	while(DateToFormat.indexOf("nd")>-1){
	DateToFormat = DateToFormat.replace("nd","");
	}
	
	while(DateToFormat.indexOf("rd")>-1){
	DateToFormat = DateToFormat.replace("rd","");
	}
	
	while(DateToFormat.indexOf("th")>-1){
	DateToFormat = DateToFormat.replace("th","");
	}
	
	if(DateToFormat.indexOf(".")>-1){
	Separator = ".";
	}
	
	if(DateToFormat.indexOf("-")>-1){
	Separator = "-";
	}
	
	
	if(DateToFormat.indexOf("/")>-1){
	Separator = "/";
	}
	
	if(DateToFormat.indexOf(" ")>-1){
	Separator = " ";
	}
	
	arrDate = DateToFormat.split(Separator);
	DateToFormat = "";
		for(var iSD = 0;iSD < arrDate.length;iSD++){
			if(arrDate[iSD]!=""){
			DateToFormat += arrDate[iSD] + Separator;
			}
		}
	DateToFormat = DateToFormat.substring(0,DateToFormat.length-1);
	arrDate = DateToFormat.split(Separator);
	
	if(arrDate.length < 3){
	return "";
	}
	
	var DAY = arrDate[0];
	var MONTH = arrDate[1];
	var YEAR = arrDate[2];
	
	
	
	
	if(parseFloat(arrDate[1]) > 12){
	DAY = arrDate[1];
	MONTH = arrDate[0];
	}
	
	if(parseFloat(DAY) && DAY.toString().length==4){
	YEAR = arrDate[0];
	DAY = arrDate[2];
	MONTH = arrDate[1];
	}
	
	
	for(var iSD = 0;iSD < arrMonths.length;iSD++){
	var ShortMonth = arrMonths[iSD].substring(0,3).toLowerCase();
	var MonthPosition = DateToFormat.indexOf(ShortMonth);
		if(MonthPosition > -1){
		MONTH = iSD + 1;
			if(MonthPosition == 0){
			DAY = arrDate[1];
			YEAR = arrDate[2];
			}
		break;
		}
	}
	
	var strTemp = YEAR.toString();
	if(strTemp.length==2){
	
		if(parseFloat(YEAR)>40){
		YEAR = "19" + YEAR;
		}
		else{
		YEAR = "20" + YEAR;
		}
	
	}


	if(parseInt(MONTH)< 10 && MONTH.toString().length < 2){
	MONTH = "0" + MONTH;
	}
	if(parseInt(DAY)< 10 && DAY.toString().length < 2){
	DAY = "0" + DAY;
	}
	switch (FormatAs){
	case "dd/mm/yyyy":
	return DAY + "/" + MONTH + "/" + YEAR;
	case "mm/dd/yyyy":
	return MONTH + "/" + DAY + "/" + YEAR;
	case "dd/mmm/yyyy":
	return DAY + " " + arrMonths[MONTH -1].substring(0,3) + " " + YEAR;
	case "mmm/dd/yyyy":
	return arrMonths[MONTH -1].substring(0,3) + " " + DAY + " " + YEAR;
	case "dd/mmmm/yyyy":
	return DAY + " " + arrMonths[MONTH -1] + " " + YEAR;	
	case "mmmm/dd/yyyy":
	return arrMonths[MONTH -1] + " " + DAY + " " + YEAR;
	}

	return DAY + "/" + strMONTH + "/" + YEAR;;

} //End Function
