/** Inject JavaScript function when window is loading */
//window.onload = setupFunc;

/** 
 * Setup init function(s) here.
 * 
 * @return
 */
function setupFunc() {
   document.getElementsByTagName('body')[0].onclick = clickFunc;
   document.getElementsByTagName('body')[0].onchange = changeFunc;
   hideBusysign();
   //Wicket.Ajax.registerPreCallHandler(showBusysign);
   //Wicket.Ajax.registerPostCallHandler(hideBusysign);
   //Wicket.Ajax.registerFailureHandler(hideBusysign);
}

/**
 * Hide busy sign.
 * 
 * @return
 */
function hideBusysign() {
   document.getElementById('bysy_indicator').style.display ='none';
}

/**
 * Show busy sign.
 * 
 * @return
 */
function showBusysign() {
   var busyIndicatorEL = document.getElementById('bysy_indicator');
   busyIndicatorEL.style.visibility ='visible';
   busyIndicatorEL.style.display ='block';
   busyIndicatorEL.style.position ='absolute';
   busyIndicatorEL.style.width = document.body.clientWidth;
   busyIndicatorEL.style.height = document.body.clientHeight;
   busyIndicatorEL.style.top = "0px";
}

/**
 * Overwrite click event function.
 * 
 * @param eventData Click event data.
 * @return
 */
function clickFunc(eventData) {
   var clickedElement = (window.event) ? event.srcElement : eventData.target;
   if (clickedElement.tagName.toUpperCase() == 'BUTTON' || clickedElement.tagName.toUpperCase() == 'A' || clickedElement.parentNode.tagName.toUpperCase() == 'A'
     || (clickedElement.tagName.toUpperCase() == 'INPUT' && (clickedElement.type.toUpperCase() == 'BUTTON' || clickedElement.type.toUpperCase() == 'SUBMIT'))) {
    showBusysign();
   }
}

/**
 * Overwrite change event function.
 * 
 * @param eventData Change event function.
 * @return
 */
function changeFunc(eventData) {
   var changedElement = (window.event) ? event.srcElement : eventData.target;
   if (changedElement.tagName.toUpperCase() == 'SELECT') {
    showBusysign();
   }
}

/** Page variable to maintain state of page */
//var initialState = true;

/**
 * Function to set style of URL element.
 * 
 * @param urlEL URL element.
 * @return
 */
function enterURLNow(urlEL){

	boxstate = document.getElementById('boxstate');
	
	if(boxstate.value=="0"){
		urlEL.value="";
		boxstate.value = "1";
	}
	urlEL.style.color="#000000";
}

/**
 * Used to debug JavaScript object. The function will popup a window and wrote down the list of
 * object attributes.
 *
 * @param debugObj Object to be debuged. 
 */
function debug(debugObj){
	var win = window.open(parent.rootContext+"/common/debug.html");
	win.document.write("<table border=1>\n");
	for(i in debugObj){
		win.document.write("<tr>\n");
		win.document.write("<td>");
		win.document.write(i);
		win.document.write("</td>");
		win.document.write("<td>");
		win.document.write(debugObj[i]);
		win.document.write("</td>");
		win.document.write("</tr>\n");
	}
	win.document.write("</table>\n");
}

/**
 * Swith locale and submit the chosen locale if the locale value is not empty. 
 * If the checkedUrl value is not empty, the checked url value will be submitted as well. 
 * 
 * @param localeSelectionForm Locale selection form.
 * @return
 */
function switchLocale(localeSelectionForm){
	var chosenLocale = localeSelectionForm.localeChoice[localeSelectionForm.localeChoice.selectedIndex].value;
	if(chosenLocale!=""){
		var checkedURL = '';
		var checkedURLEL = eval("document.getElementById('url')");
		if(checkedURLEL!=null){
			checkedURL = checkedURLEL.value;
		}
		localeSelectionForm.submittedUrl.value = checkedURL;
		localeSelectionForm.submit();
	}
}
