// Based on user ** selection ** request, change url address of current window
function goToLink(theLink) {
	window.top.location.href=theLink.options[theLink.selectedIndex].value;
}

function openWin (theUrl, theName, theWidth, theHeight, theTop, theLeft, theMenuBar, theToolbar, theLocation) {

  //if (document.location.search == "") {
    window.open(theUrl, name, "fullscreen=no,menubar="+theMenuBar+",toolbar="+theToolbar+",location="+theLocation+",status=yes,scrollbars=yes,resizable=yes,directories=no,width="+theWidth+",height="+theHeight+",left="+theLeft+",top="+theTop);
    self.opener="";
    // Amardeep Hoonjan reported a popup window closing error.
    // temporally disabled.
    //self.close();
  //} 
  //else {
    //window.opener.close();
  //}
} 

// Maximize window
function maxWin () {
	window.moveTo(0,0); 
	if (document.all) {
		window.resizeTo(window.screen.availWidth,window.screen.availHeight);
	}
	else if (document.layers || document.getElementById) {
		if (top.window.outerHeight<window.screen.availHeight || top.window.outerWidth<window.screen.availWidth) {
			top.window.outerHeight = window.screen.availHeight;
			top.window.outerWidth = window.screen.availWidth;
		}
	}
}

// Bookmark current page, only for IE 4.0 up
function bookMark(myUrl, myTitle) {
	if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
		var url=myUrl;
		var title=myTitle;
		window.external.AddFavorite(url,title);
	}
	else {
		var msg = "Bookmark this page for your future visit!";
		if(navigator.appName == "Netscape") msg += " (Click CTRL-D)";
		alert(msg);
	}
}

// Browser detection simple version: only browser name and version detection
function detectBrowser() {
	// substituted by object detection
	var bType;
	
	// Opera
	if (navigator.userAgent.indexOf("Opera")!=-1 && document.getElementById) bType="OP";
	// MS IE 4.0 ups
	else if (document.all) bType="IE";
	// Mozilla and NS
	else if (document.getElementById) bType="MO";
	// NS 4.0
	else if (document.layers) bType="NN";
	// Other
	else  bType="Other";
	
	//alert (bType);
	return bType;
}

// This component prevents users from inputing special "prohibited" characters.
function checkSpecialChar(val) { 
	var specialChar = /[$\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;
	var strText = val.value; 
	var strLength = strText.length; 
	var blChar = val.value.charAt((strLength) - 1); 
	if(blChar.search(specialChar) != -1) { 
		var theNew = val.value.substring(0, (strLength) - 1); 
		val.value = theNew; 
		alert("You cannot input the following characters: \n\r\n\r@ $ % ^ & * # ( ) [ ] \\ { + } ` ~ =  | \n\r\n\r"); 
   } 
} 

function sortTable(colNum, targetTab) {
	// since first row is a header, skips to the second row.
    var currentCell = colNum + targetTab.cols;
    var totalRows = targetTab.rows.length - 1;
    var theCheck = 0;
    var targetArray = new Array();
    var cloneArray = new Array();
    var indexArray = new Array();
    var i, j, c;

	//alert (currentCell);
	//alert (totalRows);
    // Converts the whole target column data to proper data format and stores into targetArray.
    for (i=0; i < totalRows; i++) {
        targetArray[i] = setDataType(targetTab.cells(currentCell).innerText);
        currentCell = currentCell + targetTab.cols;
    }
	
    // Makes a duplicated array for comparison after sorting
    for (i=0; i < targetArray.length; i++) {
        cloneArray[i] = targetArray[i];
    }
	
    // Sorts the target column in ascending order
    //alert (targetArray);
    targetArray.sort();
    //alert (targetArray);
    
	// Sets up an index array to track the relationship between original column items and sorted
	// column items. It contains the original column's row number with indexes based on newly 
	// sorted array order like below. 
	// indexArray("targetArray's index number") = "cloneArray's row number"
    for (i=0; i < targetArray.length; i++) {
        for(j=0; j < cloneArray.length; j++) {
            if (targetArray[i] == cloneArray[j]) {
                // Check out the redundancy
                for (c=0; c<i; c++) {
                    if ( indexArray[c] == (j+1) ) {
                      theCheck = 1;
                    }
                }
                if (theCheck == 0) {
                	indexArray[i] = (j+1);
                }
                theCheck = 0;
        	}
    	}
	}
	// Calls updateTable function
	//                                          Component Running Time Estimation: AVERAGE 930mS "SLOW"
												//var time1 = new Date().getTime();
	updateTable(targetTab, indexArray);
												//var time2 = new Date().getTime();
												//var theTime = time2-time1;
												//alert (theTime);
}
  
function setDataType(innerValue) {
	// Check out the data type and convert to the appropriate data type for proper sorting
    var isDate = new Date(innerValue);
    if (isDate == "NaN") {
		// If the value is a string, convert to upper case.
        if (isNaN(innerValue)) {
            innerValue = innerValue.toUpperCase();
            return innerValue;
        }
		// If the value is a number, convert to string number type for sorting purpose
		// to prevent malfunctional sorting like 1-11-15-2-5-51-6..... 
        else {
            var myNum;
			myNum = String.fromCharCode(48 + innerValue.length) + innerValue;
			return myNum;
        }
	}
	// If the value is a date, convert to string value
    else {
        var myDate = new String();
        myDate = isDate.getFullYear() + " " ;
        myDate = myDate + isDate.getMonth() + " ";
        myDate = myDate + isDate.getDate(); + " ";
        myDate = myDate + isDate.getHours(); + " ";
        myDate = myDate + isDate.getMinutes(); + " ";
        myDate = myDate + isDate.getSeconds();
        return myDate ;
	}
}

function updateTable(targetTab, indexArray) {
    var newRow, newCell;
	var i, j;
	var totalRows = targetTab.rows.length - 1;	
	
	//                                          Component Running Time Estimation: AVERAGE 840mS "Bottle Neck"
												//var time3 = new Date().getTime();
	// Adds new rows in ascending order at the bottom of the target Table.
    for (i=0; i<indexArray.length; i++) {
    	newRow = targetTab.insertRow();
    											//var time5 = new Date().getTime();
      	for (j=0; j<targetTab.cols; j++) {
          	newCell = newRow.insertCell();
			newCell.innerHTML = targetTab.rows(indexArray[i]).cells(j).innerHTML;
        }
												//var time6 = new Date().getTime();
												//var spotTime = time6-time5;
												//alert (spotTime);
	}
												//var time4 = new Date().getTime();
												//var theTime = time4-time3;
												//alert (theTime);
	
    // Moves the newly added row to the second place (first is a header)
  	for (i=0; i<totalRows; i++) {
      	targetTab.moveRow((targetTab.rows.length -1),1);
    }

  	// Deletes the old row
  	for (i=0; i<totalRows; i++) {
      	targetTab.deleteRow();
    }
	
}

function imgChange(imgNum) {
	// A collection of images, which will be randomly selected.
	// DCU AboutUs image change function 
	var arrayImg = new Array (imgNum);

	arrayImg[0] = "/images/a2on_0.jpg";
 	arrayImg[1] = "/images/a2on_1.jpg";
	arrayImg[2] = "/images/a2on_2.jpg";
	arrayImg[3] = "/images/a2on_3.jpg";
	arrayImg[4] = "/images/a2on_4.jpg";

	var randomNum = randomGenerator(imgNum);
	return arrayImg[randomNum];
}

function randomGenerator(maxNum) {
	if (Math.random)
		return Math.round(Math.random() * (maxNum-1));
	else {
		var now = new Date();
		return (now.getTime() / 1000) % maxNum;
	}
}