<!--
// The variables for form input validatation.
// Add "err" as prefix is to prevent conflict with other Javascript variables.
var errLoop;          // Loop variable for error checker
var errCnt;           // Counter variable for error checker
var errChar;          // The character at the value.
var errEmpty;         // Flag to indicate whether is empty
var errMidSpace;      // Flag to indicate whether there is a space in the middle.
var errHasDigit;      // Flag to indicate whether at least one digit.
var errHasDot;        // Flag to indicate whether has a dot.
var errHasMoreDigit;  // Flag to indicate whether has digit after a dot.

// The variables for popup window location.
var lastMouseX;       // Last X coordinate of the mouse click.
var lastMouseY;       // Last Y coordinate of the mouse click.
var popupWin = null;  // The reference to the popup window.


////////////////////////////////////////////////////////////////////////////////
// PURPOSE : Check for whether a textfield or textarea is empty.
// USAGE   : boolean isEmpty(form_element)
// INPUT   : A textfield contains a string
// OUTPUT  : A boolean (true or false) to indicate whether the form element 
//           is empty or not.
////////////////////////////////////////////////////////////////////////////////
function isEmpty(elm) {   
    if (elm.value.length == 0)
        return(true);
        
    errCnt = 0;       
    for (errLoop = 0; errLoop < elm.value.length; errLoop++) {
        errChar = elm.value.charAt(errLoop);
        
        if (errChar == ' ' || errChar == '\t')
            errCnt++;
    }
    if (errCnt == elm.value.length)
        return(true);
        
    return(false);   
}

////////////////////////////////////////////////////////////////////////////////
// PURPOSE : Check for whether a textfield containing an integral value.
// USAGE   : boolean isInteger(form_element)
// INPUT   : A textfield contains a string
// OUTPUT  : A boolean (true or false) to indicate whether the form element 
//           is an integral value or not.
////////////////////////////////////////////////////////////////////////////////
function isInteger(elm)
{       
    errEmpty = true;
    errMidSpace = false;
    errHasDigit = false;
    for (errLoop = 0; errLoop < elm.value.length; errLoop++) {
        errChar = elm.value.charAt(errLoop);
        
        if ((errChar < '0' || errChar > '9')
          && errChar != ' ' && errChar != '-') {
            return(false);
        }
                        
        if (errChar == '-' && !errEmpty)
            return(false);
                    
        if (errChar != ' ')
            errEmpty = false;

        if (errMidSpace && errChar != ' ')
            return(false);
        
        if (errChar == ' ' && !errEmpty)
            errMidSpace = true;
        
        if (errChar >= '0' && errChar <= '9')
            errHasDigit = true;
    }        
    
    if (errHasDigit)
        return(true);
    else
        return(false);
}

////////////////////////////////////////////////////////////////////////////////
// PURPOSE : Check for whether a textfield containing a floating value.
// USAGE   : boolean isFloat(form_element)
// INPUT   : A textfield contains a string
// OUTPUT  : A boolean (true or false) to indicate whether the form element 
//           is a floating point value or not.
////////////////////////////////////////////////////////////////////////////////
function isFloat(elm) {          
    errHasMoreDigit = false;    
    errEmpty = true;
    errMidSpace = false;
    errHasDigit = false;
    errHasDot = false;
    for (errLoop = 0; errLoop < elm.value.length; errLoop++) {
        errChar = elm.value.charAt(errLoop);
        
        if ((errChar < '0' || errChar > '9')
          && errChar != ' ' && errChar != '-' && errChar != '.') {
            return(false);
        }
                        
        if (errChar == '-' && !errEmpty)
            return(false);
                    
        if (errChar != ' ')
            errEmpty = false;

        if (errMidSpace && errChar != ' ')
            return(false);
        
        if (errChar == ' ' && !errEmpty)
            errMidSpace = true;
        
        if (errChar >= '0' && errChar <= '9')
            errHasDigit = true;
            
        if (!errHasDigit && errChar == '.')
            return(false);
            
        if (errChar == '.' && errHasDot)
            return(false);
            
        if (errChar == '.')
            errHasDot = true;
        
        if (errHasDot && (errChar >= '0' && errChar <= '9'))
            errHasMoreDigit = true;
    }        
        
    if ((errHasDigit && errHasMoreDigit && errHasDot) || (errHasDigit && !errHasDot))
        return(true);
    else
        return(false);
}

////////////////////////////////////////////////////////////////////////////////
// PURPOSE : Popup a window showing a calendar picker.
// USAGE   : void popupCalendar(form_element_name)
// INPUT   : A string
//////////////////////////////////////////////////////////////////////////////// 
function popupCalendar(obj) {
    var popupUrl = DEF_URL_PREFIX + "/lib/" + obj;
    var winName = "Calendar";
    var pWidth = 210; //300
    var pHeight = 285; //360
    var features = "width=" + pWidth + ",height=" + pHeight + ",dependent=yes,resizable=no,toolbar=no,status=no,directories=no,menubar=no";

	closePopup();
    if (lastMouseX - pWidth < 0) {
		lastMouseX = pWidth;
	}
	if (lastMouseY + pHeight > screen.height) {
		lastMouseY -= (lastMouseY + pHeight + 50) - screen.height;
	}
    lastMouseX -= pWidth;
    lastMouseY += 10;
	lastMouseX = 300;
	lastMouseY = 120;
	features +=	"screenX=" + lastMouseX + ",left=" + lastMouseX + "screenY=" + lastMouseY + ",top=" + lastMouseY;

	popupWin = window.open(popupUrl, winName, features, false);
}
////////////////////////////////////////////////////////////////////////////////
// PURPOSE : Popup a window showing a search picker.
// USAGE   : void popupSearch(form_element_name)
// INPUT   : A string
//////////////////////////////////////////////////////////////////////////////// 
function popupSearch(obj) {
    var popupUrl = obj;
    var winName = "Search";
    var pWidth = 600; //320
    var pHeight = 450; //340
    var features = "width=" + pWidth + ",height=" + pHeight + ",scrollbars=yes,dependent=yes,resizable=no,toolbar=no,status=no,directories=no,menubar=no";

	closePopup();
    if (lastMouseX - pWidth < 0) {
		lastMouseX = pWidth;
	}
	if (lastMouseY + pHeight > screen.height) {
		lastMouseY -= (lastMouseY + pHeight + 50) - screen.height;
	}
    lastMouseX -= pWidth;
    lastMouseY += 10;
	features +=	"screenX=" + lastMouseX + ",left=" + lastMouseX + "screenY=" + lastMouseY + ",top=" + lastMouseY;

	popupWin = window.open(popupUrl, winName, features, false);
}

////////////////////////////////////////////////////////////////////////////////
// PURPOSE : Get the last click mouse position.
// USAGE   : void setLastMousePosition(event_handler)
//////////////////////////////////////////////////////////////////////////////// 
function setLastMousePosition(e) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
	    e = window.event;
	}
	lastMouseX = e.screenX;
	lastMouseY = e.screenY;
}

////////////////////////////////////////////////////////////////////////////////
// PURPOSE : Close the current popup window.
// USAGE   : void closePopup()
//////////////////////////////////////////////////////////////////////////////// 
function closePopup() {
	if (popupWin != null) {
		if (!popupWin.closed) {
			popupWin.close();
		}
		popupWin = null;
	}
}

////////////////////////////////////////////////////////////////////////////////
// PURPOSE : New a popup window.
// USAGE   : void newPopup()
//////////////////////////////////////////////////////////////////////////////// 

function newPopup(mypage, myname) {
	
	winprops = 'height=400,width=360,scrollbars=yes,dependent=yes,resizable=no,toolbar=no,status=no,directories=no,menubar=no'
	win = window.open(mypage, myname)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}


////////////////////////////////////////////////////////////////////////////////
// PURPOSE : New another popup window.
// USAGE   : void newPopup()
//////////////////////////////////////////////////////////////////////////////// 

function newPopup2(mypage, myname) {
	
	winprops = 'height=400,width=700,scrollbars=yes,dependent=yes,resizable=no,toolbar=no,status=no,directories=no,menubar=yes'
	win = window.open(mypage, myname, winprops, false)
	
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
////////////////////////////////////////////////////////////////////////////////
// PURPOSE : Transfer the elements in select list between two select lists.
// USAGE   : void transferSelect(form_select_element, form_select_element)
// INPUT   : A form element of original select list.
//           A form element of destinated select list.
//////////////////////////////////////////////////////////////////////////////// 
function transferSelect(originalSelect, originalHidden, destinatedSelect, destinatedHidden) { 
	tranSelect(originalSelect, originalHidden, destinatedSelect, destinatedHidden, true); 

	return(false);
}

// willDecrease is boolean type. 'true' means after tansfered the selected items, the selected items
// will remove form the select list. While 'false' means no items will remove from the source select list
function tranSelect(originalSelect, originalHidden, destinatedSelect, destinatedHidden, willDecrease) { 
    var whenEmptyValue = "###EMPTY###";
    var whenLockValue = "%$#@LOCK%$#@";
    var whenEmptyText = "-- EMPTY LIST --";   
    var emptyOption = new Option(whenEmptyText, whenEmptyValue);

    if (originalSelect.selectedIndex > -1) {
        for (i = 0; i < originalSelect.length; ++i) {
            var selectedOption = originalSelect.options[i];         
            if (selectedOption.selected && selectedOption.value != whenLockValue) {                                  
                if (selectedOption.value != whenEmptyValue) {
                    var newOption = new Option(selectedOption.text, selectedOption.value);
					//alert(selectedOption.value == null);
					//alert(selectedOption.value.length);
                    if (destinatedSelect.options.length > 0 && destinatedSelect.options[0].value == whenEmptyValue) {					   
                        destinatedSelect.options[0] = newOption;                        
                    } else {
                        destinatedSelect.options[destinatedSelect.options.length] = newOption;
                    }
                } else {
                    originalSelect.selectedIndex = -1;
                }
            }
        }
		if (willDecrease) {
			for (i = originalSelect.length - 1; i > -1; i--) {
				if (originalSelect.options[i].selected && originalSelect.options[i].value != whenLockValue) 
					originalSelect.options[i] = null;				
			}
		}
        
        if (originalSelect.options.length == 0) {
            originalSelect.options[0] = emptyOption;
        } 
		
    }    
    
    storeValue(originalSelect, originalHidden);
    storeValue(destinatedSelect, destinatedHidden);
    return(false);    
}

////////////////////////////////////////////////////////////////////////////////
// PURPOSE : Transfer all elements in select list between two select lists.
// USAGE   : void transferAllSelect(form_select_element, form_select_element)
// INPUT   : A form element of original select list.
//           A form element of destinated select list.
//////////////////////////////////////////////////////////////////////////////// 
function transferAllSelect(originalSelect, originalHidden, destinatedSelect, destinatedHidden) { 
    for (i = 0; i < originalSelect.length; ++i) {
        originalSelect.options[i].selected = true;
    }      
   
    transferSelect(originalSelect, originalHidden, destinatedSelect, destinatedHidden);
    
    return(false);    
}

////////////////////////////////////////////////////////////////////////////////
// PURPOSE : Store the all the values of a select list into a hidden field with
//           the delimitator.
// USAGE   : void storeValue(form_select_element, form_hidden_element)
// INPUT   : A form element of select list.
//           A form element of hidden field.
//////////////////////////////////////////////////////////////////////////////// 
function storeValue(selectList, hiddenValue) {
    var whenEmptyValue = "###EMPTY###";
    var whenLockValue = "%$#@LOCK%$#@";
   // var delimitator = "\t";
    
    hiddenValue.value = "";
	for (i = 0; i < selectList.options.length; ++i) {
		if (selectList.options[i].value != whenEmptyValue && selectList.options[i].value != whenLockValue) {
			hiddenValue.value += "'" + selectList.options[i].value + "'," ;
			//alert(hiddenValue.value+"<-hidden value");
		}
	}
	hiddenValue.value = hiddenValue.value.substring(0, hiddenValue.value.length - 1);
	//alert(hiddenValue.value+"<-hidden value");
}


////////////////////////////////////////////////////////////////////////////////
// PURPOSE : Push the selected item in a selection list up a level
// USAGE   : void up(form_select_element)
// INPUT   : A form element of select list
//////////////////////////////////////////////////////////////////////////////// 
function up(selectList, hiddenValue) {
    var idx = selectList.selectedIndex;
    var tmpVal = "";
    var tmpText = "";
    
    if (idx > 0) {
        tmpVal = selectList.options[idx].value;
        tmpText = selectList.options[idx].text;
        selectList.options[idx].value = selectList.options[idx - 1].value;
        selectList.options[idx].text = selectList.options[idx - 1].text;
        selectList.options[idx - 1].value = tmpVal;
        selectList.options[idx - 1].text = tmpText;
        selectList.selectedIndex = idx - 1;
    }

    storeValue(selectList, hiddenValue);
    return(false);
}


////////////////////////////////////////////////////////////////////////////////
// PURPOSE : Press the selected item in a selection list down a level
// USAGE   : void down(form_select_element)
// INPUT   : A form element of select list
//////////////////////////////////////////////////////////////////////////////// 
function down(selectList, hiddenValue) {
    var idx = selectList.selectedIndex;
    var tmpVal = "";
    var tmpText = "";
    
	if (idx >= 0) {
		if (idx < selectList.options.length - 1) {
			tmpVal = selectList.options[idx].value;
			tmpText = selectList.options[idx].text;
			selectList.options[idx].value = selectList.options[idx + 1].value;
			selectList.options[idx].text = selectList.options[idx + 1].text;
			selectList.options[idx + 1].value = tmpVal;
			selectList.options[idx + 1].text = tmpText;
			selectList.selectedIndex = idx + 1;
		}
	}

    storeValue(selectList, hiddenValue);  
    return(false);
}


////////////////////////////////////////////////////////////////////////////////
// PURPOSE : check email address is valid
// USAGE   : boolean isValidEmail(form_element)
// INPUT   : form_element (name of email address field)
//////////////////////////////////////////////////////////////////////////////// 
function isValidEmail(inEmail) {
    if (inEmail.value.length == 0)
		return(false);

	reg = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/g; 
	if (reg.test(inEmail.value)) // input email is valid    
		return(true);
	else      
		return(false);
}



////////////////////////////////////////////////////////////////////////////////
// PURPOSE : check date is valid
// USAGE   : boolean isValidDate(form element) 
// INPUT   : form element -> a string in the format of MM/DD/YY
//////////////////////////////////////////////////////////////////////////////// 
function isValidDate(elm) {
  // valid format: MM/DD/YY
  var mon_v, day_v, year_v;

  if (elm.value.length < 8 || elm.value.length > 8)
      return(false);

  mon_v = elm.value.substring(0, 2);
  day_v = elm.value.substring(3, 5);
  year_v = elm.value.substring(6, 8);

  tmpS1 = elm.value.substring(2, 3);
  tmpS2 = elm.value.substring(5, 6);

  if (isNaN(mon_v) || isNaN(day_v) || isNaN("20" + year_v) )
    return(false);

  if (tmpS1 != "/" || tmpS2 != "/")
    return(false);

  if (ChkMonthDays(0 + mon_v, 0 + day_v, 0 + year_v))
    return(true);
  else
    return(false);
}

////////////////////////////////////////////////////////////////////////////////
// PURPOSE : check date is valid
// USAGE   : boolean isValidDate4Year(form element) 
// INPUT   : form element -> a string in the format of MM/DD/YYYY
//////////////////////////////////////////////////////////////////////////////// 
function isValidDate4Year(elm) {
  // valid format: MM/DD/YYYY
  var mon_v, day_v, year_v;

  if (elm.value.length < 10 || elm.value.length > 10)
      return(false);

  mon_v = elm.value.substring(0, 2);
  day_v = elm.value.substring(3, 5);
  year_v = elm.value.substring(6, 10);

  tmpS1 = elm.value.substring(2, 3);
  tmpS2 = elm.value.substring(5, 6);

  if (isNaN(mon_v) || isNaN(day_v) || isNaN(year_v) )
    return(false);

  if (tmpS1 != "/" || tmpS2 != "/")
    return(false);

  if (ChkMonthDays(0 + mon_v, 0 + day_v, 0 + year_v))
    return(true);
  else
    return(false);
}


////////////////////////////////////////////////////////////////////////////////
// PURPOSE : check date is valid
// USAGE   : boolean ChkMonthDays(month, day, year) 
// INPUT   : month / day / year: 2 digits number
////////////////////////////////////////////////////////////////////////////////     
	function ChkMonthDays(MonIndex,SelDay,SelYear)
	{
    if(MonIndex>12 || MonIndex<1 || SelDay<1 || SelDay>31 ||SelYear<0)
      return false;

		if(MonIndex==4 || MonIndex==6 || MonIndex==9 || MonIndex==11)
		{
			if(parseInt(SelDay)==31)
        return false;
		} 

    if(MonIndex==2)	//For Feb.
		{
			if((parseInt(SelYear) % 4)!=0)
				if(parseInt(SelDay)>28)
					return false;
			else if((parseInt(SelYear) % 400)==0)
				if(parseInt(SelDay)>29)
					return false;
			else if((parseInt(SelYear) % 100)==0)
				if(parseInt(SelDay)>28)
					return false;
			else
				if(parseInt(SelDay)>29)
					return false;
    }
    return true; 
	}
    
//-->
