// If the function input str1 is a string, then the function returns true, otherwise false.

function isNumeric(str1)
{
	var strNumeric=new String("1234567890.");
	var bFlag,iLength;
	var str=new String(str1);

	iLength=str.length;

	bFlag=true;
	for(j=0;j<iLength;j++)
	{
		if(strNumeric.indexOf(str.charAt(j))==-1)
		{
			bFlag=false;
			break;
		}
	}
	return(bFlag);
}

// If the function input str1 is an integer, then the function returns true, otherwise false.

function isInteger(str1)
{
	var strInteger=new String("1234567890");
	var iLength;
	var str=new String(str1);

	iLength=str.length;

	for(j=0;j<iLength;j++)
	{
		if(strInteger.indexOf(str.charAt(j))==-1)
		{
			return false;
		}
	}
	
	return true;	
}

// If the function input str1 is an alphabetic char string, then the function returns true, otherwise false.

function isAlphabeticChar(str1)
{
	var strAlphaChar=new String("aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ");
	var bFlag,iLength;
	var str=new String(str1);

	iLength=str.length;

	bFlag=true;
	for(j=0;j<iLength;j++)
	{
		if(strAlphaChar.indexOf(str.charAt(j))==-1)
		{
			bFlag=false;
			break;
		}
	}
	return(bFlag);
}

// If the function input str1 is an alphaNumeric char string, then the function returns true, otherwise false.

function isAlphaNumericChar(str1)
{
	var strAlphaChar=new String("aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ1234567890");
	var bFlag,iLength;
	var str=new String(str1);

	iLength=str.length;

	bFlag=true;
	for(j=0;j<iLength;j++)
	{
		if(strAlphaChar.indexOf(str.charAt(j))==-1)
		{
			bFlag=false;
			break;
		}
	}
	return(bFlag);
}

// If the function input str1 is empty string, then the function returns true, otherwise false.

function isEmpty(str1)
{
	var str=new String(str1);

	if(str.length==0)
	{
		return true;
	}
	return false;
}

// If the function input str1 conforms to email format, then the function returns true, otherwise false.

function isEmail(str1)
{
	var str=new String(str1);
	var iAt,iDot;
	

	if((iAt=str.indexOf("@"))<2||(iDot=str.indexOf("."))<4)
	{
		return false;
	}
	return true;
}

// If the function input str1 conforms to telephone number format, then the function returns true, otherwise false.

function isTel(str1)
{
	var strNumeric="1234567890-.() ";
	var bFlag,iLength;
	var str=new String(str1);

	iLength=str.length;

	bFlag=true;
	for(j=0;j<iLength;j++)
	{
		if(strNumeric.indexOf(str.charAt(j))==-1)
		{
			bFlag=false;
			break;
		}
	}
	
	return(bFlag);
}

// If the function input str1 conforms to date format, then the function returns true, otherwise false.

function isDate(date) 
{
    var err=0
    if (date.length == 0)
        return true;
      
	if ((date.length < 8)||(date.length >10)) err = 1;
	
    if ((date.length > 7)||(date.length < 11)) {
		var firstSlash = date.indexOf('/');
		if(firstSlash == -1) err = 1;
		var firstPart  = date.substring(0,firstSlash);
		if(firstPart.length < 2) 
			firstPart = "0"+firstPart;
		var secondPart  = date.substring(firstSlash+1);			
		var secondSlash = secondPart.indexOf('/');
		if(secondSlash == -1) err = 1;
		secondPart  = secondPart.substring(0,secondSlash);
		if(secondPart.length < 2) 
			secondPart = "0"+secondPart;
		var thirdPart  = date.substring(firstSlash+secondSlash+2);		
		if(thirdPart.indexOf('/')!= -1) err=1;
		date = firstPart+'/'+secondPart+'/'+thirdPart;	
    }

    month = parseInt(date.substring(0, 2),10);
    slash1 = date.substring(2, 3)
    day = parseInt(date.substring(3, 5),10);
    slash2 = date.substring(5, 6)
    year = parseInt(date.substring(6, 10),10);
    

    if (isNaN(month)) err=1;
    if (isNaN(day)) err=1;
    if (isNaN(year)) err=1;

    if (month < 1 || month > 12) err = 1
    if (slash1 != '/') err = 1
    if (day<1 || day>31) err = 1
    if (slash2 != '/') err = 1
    if (year<1900 || year>2099) err = 1
    if (month==4 || month==6 || month==9 || month==11){
    if (day==31) err=1
    }
    if (month==2){
        var validYear=parseInt(year/4)
        if (isNaN(validYear)) {
            err=1
        }
        if (day>29) err=1
        if (day==29 && ((year/4)!=parseInt(year/4))) err=1
    }
    if (err==1) {
         return false;
    }
    return true;
}

// lTrim trims all blanks from the beginning of str, then returns the trimming result. If str is empty, then returns "".

function lTrim(str) 
{
  var string=new String(str);
  for (i=0; i<string.length; i++) {
    if (string.charAt(i) != " ") {
       string = string.substring(i,string.length)
       return(string);
    }
  }
  return("");
}

// rTrim trims all blanks from the end of str, then returns the trimming result. If str is empty, then returns "".

function rTrim(str) 
{
  var string=new String(str)
  for (i=string.length; i>0; i--) {
    if (string.charAt(i-1) != " ") {
       string = string.substring(0,i)
       return(string);
    }
  }
  return("");
}

// Trim trims all blanks from the beginning of str and all blanks from the end of str, then returns the trimming result. If str is empty, then returns "".

function trim(string) 
{
	S1 = lTrim(string);
	S2 = rTrim(S1);
    return(S2);
}

// If the sLength is equal to the length of string, then the function returns true, otherwise false.

function lengthEqual(string, sLength)
{
	var S1, thisLength;
	var Str1=new String(string);
	
	S1=trim(Str1);
	thisLength=S1.length;
	
	if (thisLength==sLength){
		return true;
	}
    	
    	return false;
}

// If the length of string is less than sLength, then the function returns true, otherwise false.

function lengthLess(string, sLength)
{
	var S1, thisLength;
	var Str1=new String(string);
	
	S1=trim(Str1);
	thisLength=S1.length;
	
	if (thisLength<sLength){
		return true;
	}
    	
    	return false;
}

// If the length of string is greater than sLength, then the function returns true, otherwise false.

function lengthOver(string, sLength)
{
	var S1, thisLength;
	var Str1=new String(string);
	
	S1=trim(Str1);
	thisLength=S1.length;
	
	if (thisLength>sLength){
		return true;
	}
    	
    	return false;
}

// If currentPageNo is a valid integer and is a number which is not greater than pageMax and not less than pageMin. If currentPageNo is not a valid integer, or is a number which is greater than pageMax, or less than pageMin.

function isInPageRange(pageMin, pageMax, currentPageNo)
{
	
	if (isInteger(currentPageNo)==false){
		return false;	
	}
	
	if(currentPageNo>pageMax){
		return false;	
	}

	if(currentPageNo<pageMin){
		return false;	
	}
	
	return true;
}

function move(sBox, dBox) {
	var arrSBox = new Array();
	var arrDBox = new Array();
	var arrLookup = new Array();
	var i;
	
	for (i = 0; i < dBox.options.length; i++) {
		arrLookup[dBox.options[i].value] = dBox.options[i].text;
		arrDBox[i] = dBox.options[i].value;
	}
	var fLength = 0;
	var tLength = arrDBox.length;
	
	for(i = 0; i < sBox.options.length; i++) {
		arrLookup[sBox.options[i].value] = sBox.options[i].text;
		
		if (sBox.options[i].selected && sBox.options[i].value != "") {
			arrDBox[tLength] = sBox.options[i].value;
			tLength++;
		} else {
			arrSBox[fLength] = sBox.options[i].value;
			fLength++;
	   }
	}
	
	sBox.length = 0;
	dBox.length = 0;
	
	var c;
	
	for(c = 0; c < arrSBox.length; c++) {
		var no = new Option();
		no.text = arrLookup[arrSBox[c]];
		no.value = arrSBox[c];
		sBox[c] = no;
	}
	
	for(c = 0; c < arrDBox.length; c++) {
		var no = new Option();
		no.text = arrLookup[arrDBox[c]];
		no.value = arrDBox[c];
		dBox[c] = no;
   }
   
   sortBox(sBox);
   sortBox(dBox);
}

function sortBox(box)  {
	var temp_opts = new Array();
	var temp = new Object();

	for(var i=0; i<box.options.length; i++)  {
		temp_opts[i] = box.options[i];
	}
	for(var x=0; x<temp_opts.length-1; x++)  {
		for(var y=(x+1); y<temp_opts.length; y++)  {
			if(temp_opts[x].text > temp_opts[y].text)  {
				temp = temp_opts[x].text;
				temp_opts[x].text = temp_opts[y].text;
				temp_opts[y].text = temp;
				temp = temp_opts[x].value;
				temp_opts[x].value = temp_opts[y].value;
				temp_opts[y].value = temp;
			 }
   		}
	}

	for(var i=0; i<box.options.length; i++)  {
		box.options[i].value = temp_opts[i].value;
		box.options[i].text = temp_opts[i].text;
	}
}

function addAll(srcObj){
	for(i=0; i<srcObj.options.length; i++){
			srcObj.options[i].selected = true;			
	}
}
function addTableRow(formName, tableID, cellStr) {	
	var Tr = tableID.insertRow();
	//Add one row, using cellStr
	for (var ii=0; ii<cellStr.length; ii++) {
		var Td = Tr.insertCell();
		Td.innerHTML = cellStr[ii];
	}
}

function delTableRow(formName, tableID, delFlagName, headerRows) {
	//total rows before delete excluding header rows -1
	var oldRows = tableID.rows.length - 1 - headerRows;
	//alert(oldRows);
  //current total rows excluding header rows -1
	var curRows = oldRows
	// Delete row
    for (var i=oldRows; i>=0; i--) {
		var delF = false;
		if (curRows > 0) {
			var fd=eval("document." + formName + "." + delFlagName + "["+i+"]");
			if (fd.checked == true) delF = true;
		} else {
			//form index=0
			var fd=eval("document." + formName + "." + delFlagName + "[0]");
			if (fd == null) {
				fd=eval("document." + formName + "." + delFlagName);
			}		
			if (fd.checked == true) delF = true;
		}
		if (delF == true) {
			tableID.deleteRow(i+headerRows);
			curRows --;
		}
	}
	
	tableID.scrollIntoView(true);
}

//added by jyu
function attachDistingishedId(url)
{
    if(url.indexOf("?") != -1)
    {
        result = url + "&distinguishedId=" + (new Date()).getTime();
    }
    else
    {
        result = url + "?distinguishedId=" + (new Date()).getTime();
    }
    
    return result;
}


function OptionData(value, text) {
	this.value = value;
	this.text = text;
}

function CategoryData(length) {
	this.length = length;
	this.data = new Array(length);
	this.addOptionData = addOptionData;
	this.getOptionData = getOptionData;
}
function addOptionData(i, optionData) {
	this.data[i] = optionData;	
}
function getOptionData(i) {
	return this.data[i];
}

function ListData(length) {
	this.length = length;
	this.data = new Array(length);
	this.addCategoryData = addCategoryData;
	this.getCategoryData = getCategoryData;
}
function addCategoryData(i, categoryData) {
	this.data[i] = categoryData;
}
function getCategoryData(i) {
	return this.data[i];
}
function change(sBox, dBox, listData, blank) {
	
	 for (loop = dBox.options.length-1; loop >= 0; loop--) {
	   dBox.options[loop] = null;
	 }
	  
	var selectedIndex = sBox.selectedIndex;
	var categoryData = listData.getCategoryData(selectedIndex);
	
	var deviation = 0;
	
	if(blank) {
		var op = new Option();
		
		op.text = "";
		op.value = "";
		
		dBox[0] = op;
		deviation = 1;
	}
	
	for(var i = 0; i < categoryData.length; i++) {
		var op = new Option();
		var optionData = categoryData.getOptionData(i);
		
		op.text = optionData.text;
		op.value = optionData.value;
		dBox[i + deviation] = op;
	  }

	dBox.selectedIndex = 0;
}

// check if file name has spaces
function hasSpace(name) {
	var space = " ";
	
	var fileName = getFileName(name);
	
	if(trim(fileName).indexOf(space) == -1) {
		return false;
	} else {
		return true;
	}
}

function getFileName(str) {
	var temp = new String(str);
	var fileName = str;
	
	var location = temp.lastIndexOf("\\");
	
	if (location != -1) {
		fileName = temp.substr(location + 1);
	}
	
	return fileName;
}

function disable(sSB, dSB, opt) {
	var hasOpt = hasOption(sSB, opt);
	if(hasOpt) {
		unselect(sSB, opt);
	}

	for(var j = 0; j < dSB.options.length; j++) {
		if(dSB.options[j].value == opt) {
			unselect(sSB, null);
		}
	}
}

function unselect(sb, opt) {
	for(var i = 0; i < sb.options.length; i++) {
		if(sb.options[i].selected && (sb.options[i].value != opt)) {
			sb.options[i].selected = false;
		}
	}
}

function hasOption(sb, opt) {
	var hasOpt = false;

	for(var j = 0; j < sb.options.length; j++) {
		if(sb.options[j].selected && (sb.options[j].value == opt)) {
			hasOpt = true;
		}
	}

	return hasOpt;
}

function selectAll(sb) {
	for(var i = 0; i < sb.options.length; i++) {
		sb.options[i].selected = true;
	}
}

function selectA(sb, opt) {
	for(var i = 0; i < sb.options.length; i++) {
		if(sb.options[i].value == opt) {
			sb.options[i].selected = true;
		}
		
	}
}

function moveA(sSB, dSB, opt) {
	var hasOpt = hasOption(sSB, opt);
	if(hasOpt) {
		selectAll(dSB);
		move(dSB, sSB);
		selectA(sSB, opt);
		move(sSB, dSB);
	} else {
		move(sSB, dSB);
	}
}