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 wr = $("form[@name=" + formName + "]");
	var outF = wr.find("input[@name=" + destName + "]");
	var ckBoxs = wr.find("input[@name=" + fieldName + "]");
	var tmp = '';
	
	outF.val('');
	
	if(ckBoxs.length > 0){
		ckBoxs.each(function(i){
			if($(this)[0].checked){
				tmp += $(this).val() + '#';
				outF.val(tmp);
			}
		});
	}
	
	if(outF.val() == ''){
		alert(notCheckedMsg);
		return false;
	}
	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="abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	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 zoomPicture(pic, picW, picH){
	sW = $(document).width();
	sH = $(document).height();
	if(picH > sH){
		picH = sH;
	}
	if(picW > sW){
		picW = sW;
	}	
	$("#" + pic).attr("style", "display: block;");
	$("#" + pic).dialog({
		width: picW,
		height: picH
	});
}

function bindTextCounter(fldType, fldName, ctrId, maxLimit){
	fldObj = $(fldType + "[@name=" + fldName + "]");
	fldObj.bind("keydown", function(){
		textCounter(fldType, fldName, ctrId, maxLimit);
	});
	fldObj.bind("keyup", function(){
		textCounter(fldType, fldName, ctrId, maxLimit);
	});
	fldObj.bind("mousedown", function(){
		textCounter(fldType, fldName, ctrId, maxLimit);
	});
	$(ctrId).html( maxLimit - (fldObj.val()).length );
}

function textCounter(fldType, fldName, ctrId, maxLimit){	
	fldObj = $(fldType + "[@name=" + fldName + "]");
	total = (fldObj.val()).length;
	if( total > maxLimit){
		fldObj.val( (fldObj.val()).substring(0, maxLimit) );
	}else{
	
	now = maxLimit - total;
	if(now > 0){		
		count = now;
	}else{		
		count = "0";
	}
	$(ctrId).html(count);
	}
	   
	return false;	
}

function initToggleShow(trigId, tId){
	trig = $(trigId);
	if(trig.attr("tshow") == 1){
		trig.html(trig.attr("showtext"));
		$(tId).attr("style", "display:none;");
	}else{
		trig.html(trig.attr("hidetext"));
	}
}

function myToggleShow(trigId, tId){
	trig = $(trigId);
	if(trig.attr("tshow") == 1){
		trig.attr("tshow", "0");
		trig.html(trig.attr("hidetext"));
		$(tId).show("fast");
	}else{
		trig.attr("tshow", "1");
		trig.html(trig.attr("showtext"));
		$(tId).hide("fast");
	}								
}

function withSelectedDo(frmObj, act, vl){
	hf = frmObj.action;
	vf = frmObj.v;

	if(populateCheckbox(frmObj.name, 'checkAction', 'checkboxSelected', 'Please put a check on the item that you want to perform action')){
		if(act == 'delete'){
			if(confirm('Are you sure you want to delete the item(s)?')){
				hf.value = act;
			}
		}else{
			alert($(document).find(vl).val());
			hf.value = act;
			vf.value = $(document).find(vl).val();
		}
		if(hf.value != ''){
			frmObj.submit();
		}
	}
}
