function confirmRedirect(confirmText, urlAddress){
	if(confirm(confirmText)){
		window.location.href = urlAddress;
	}
}

function myRedirect(urlAddress){
	window.location.href = urlAddress;
}

function pageNaviProcess(urlAddress, formName, fields, inputField, totalPage, errorMsg){
	inputField = parseInt(inputField.value);
	if( (inputField == NaN) || (inputField < 1 ) ||  (inputField > totalPage) ){
		alert(errorMsg);
		return false;
	}
	var x = 0;
	var fieldList = fields.split(",");
	total = fieldList.length;
	while(x < total){
		fieldList[x] = fieldList[x].replace(/^\s+|\s+$/g, "");
		urlAddress += "&"+fieldList[x]+"="+formName.elements[fieldList[x]].value;
		x++;
	}
	myRedirect(urlAddress);
}


function setAllCheckBoxes(mainSwitch, formName, fieldName){
	var checkValue = false;
	if(document.forms[formName].elements[mainSwitch].checked){
		checkValue = true;
	}

	if(!document.forms[formName]) return;
	var objCheckBoxes = document.forms[formName].elements[fieldName];
	if(!objCheckBoxes){
		return;
	}
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes){
		objCheckBoxes.checked = checkValue;
	}else{
		for(var i = 0; i < countCheckBoxes; i++){
			objCheckBoxes[i].checked = checkValue;
		}
	}
}

function populateCheckbox(formName, fieldName, destName, notCheckedMsg){	
	var outputField = document.forms[formName].elements[destName];
	outputField.value = '';
	if(!document.forms[formName]) return;
	var objCheckBoxes = document.forms[formName].elements[fieldName];
	if(!objCheckBoxes){
		return false;
	}
	var countCheckBoxes = objCheckBoxes.length;
	if(countCheckBoxes){
		for(var i = 0; i < countCheckBoxes; i++){
			if(objCheckBoxes[i].checked){
				outputField.value += objCheckBoxes[i].value + '#';
			}
		}
	}else{
		if(objCheckBoxes.checked){
			outputField.value = objCheckBoxes.value + '#';
		}
	}
	if(outputField.value == ''){
		alert(notCheckedMsg);
		return false;
	}else{
		return true;
	}
}

function populateSelect(selectObj, targetObj){
	var listSelected = '';
	var total = selectObj.options.length
	for(var i = 0; i < total; i++){
		if(selectObj.options[i].selected){
			listSelected += selectObj.options[i].value + '#';
		}
	}
	targetObj.value = listSelected;
}

function revealInputOnSelect(frmName, revName, inputType, defaultValue){
	revObj = document.forms[frmName].elements[revName];
	if(inputType == 'text'){
		revObj.value = defaultValue;
	}else if(inputType == 'select'){			
		revObj.options[getSelectId(revObj, defaultValue)].selected = true;
	}
	return;
}

function getSelectId(selectObj, byValue){
	x = selectObj.length;
	for(var i=0; i<x; i++){
		if(selectObj.options[i].value == byValue){
			return i;
		}
	}
	return;
}


function generatePassword(plength, output1, output2, outputLabel){
	var keylist="abcdefghijklmnopqrstuvwxyz123456789";
	var temp = '';
	for(i=0;i<plength;i++){
		temp += keylist.charAt(Math.floor(Math.random()*keylist.length));
	}
	output1.value = temp;
	if(output2){
		output2.value = temp;
	}
	if(document.getElementById(outputLabel)){
		document.getElementById(outputLabel).innerHTML = temp;
	}
	return;
}

function decM(textData){
	var keyMNumber = 1;
	var result = '';
	var method = 'hexa';
	if(method == 'hexa'){
		textData = textData.replace(/;/g, '');
		textData = textData.replace(/&#/g, '%');
	}
	temp = textData.split('%');
	temp = temp.reverse();
	for(i=0; i<(temp.length - 1); i++){
		result += String.fromCharCode(temp[i] - keyMNumber);
	}
	return result;
}

function clickMAddress(theM){
	return '<a href="mailto:' + theM + '" class="myEmailLink">' + theM + '</a>';
}

function inputOnFocus(obj, defValue, cssClass){
	if(obj.value == defValue){
		obj.value = '';
		obj.className = 'fieldFocus';
	}
}

function inputOnBlur(obj, defValue, cssClass){
	if(obj.value == ''){
		obj.value = defValue;
		obj.className = 'field';
	}
}

function inputOnSubmit(obj, defValue){
	if(obj.value == defValue){
		obj.value = '';
	}
}

function doQSearch(frmObj, catObj, keyObj){
	x = catObj.options[catObj.selectedIndex].value;
	frmObj.action = x + '&keyword=' + keyObj.value;
	myRedirect(frmObj.action);
	return false;
}


function autoLogoutTimer(){
	if(timeSec == 1){
		clearTimeout(t);
		content = 'Logging you out...';
		window.location = '';
		return true;
	}else{ 
		timeSec -= 1;

		d = timeSec / 86400;
		days = Math.floor(d);

		h = (d - days) * 24;
		hours = Math.floor(h);

		m = (h - hours) * 60;
		minutes = Math.floor(m);

		s = (m - minutes) * 60;
		secs = Math.floor(s);

		content = 'Auto Logout in: ';

		if(days != 0){
			content += days + 'd ';
		}
		if(hours != 0){
			content += hours + 'h ';
		}
		if(minutes != 0){
			content += minutes + 'm ';
		}
		if(secs != 0){
			content += secs + 's ';
		}		
	}
	ltc = document.getElementById('logoutTimeCount');
	if(ltc){
		ltc.innerHTML = content;
	}
	//window.status = content;
	t = setTimeout("autoLogoutTimer()", 1000);
}
