/**********************************************************************
File Name:		common.js
Author:				Dr John Xu
Date:					18 September 2004
Company:			Altlinks Personalized Software
**********************************************************************/

function showRow(row, status){
	row.style.display = status;
}

function showError(labelCell, errorCell, oElement){
	alert(errorCell.innerText);
	changeColor(labelCell, "red");
	showElement(errorCell, true);
	oElement.focus();
}

function showError1(labelCell, errorCell, oElement){
	changeColor(labelCell, "red");
	showElement(errorCell, true);
	oElement.focus();
}

function hideError(labelCell, errorCell){
	changeColor(labelCell, "black");
	showElement(errorCell, false);
}

function changeColor(oElement, sColor){
	oElement.style.color = sColor;
}

function showElement(oElement, bVisible){
	if(bVisible == true){
		oElement.style.visibility = "visible";
	}else{
		oElement.style.visibility = "hidden";
	}
}

function displayElement(oElement, bDisplay){
	if(bDisplay == true){
		oElement.style.display = "";
	}else{
		oElement.style.display = "none";
	}
}

function showInstruction(oRow, oCell, sMsg){
  oRow.style.display = "";
  oCell.innerText = sMsg;
}

function hideInstruction(oRow){
  oRow.style.display = "none";
}

function returnAlphaApostropheHyphen(){
	var iNum = event.keyCode;
	if((iNum < 65 || (iNum > 90 && iNum < 97) || iNum > 122) && 
			iNum != 8 && iNum != 9 && iNum != 39 && iNum != 45){
		return false;
	}
}

function isEmailAddress(s){
	if(s.indexOf("@") == -1 || s.indexOf(".") == -1 
		|| s.indexOf("@") == 0 || s.indexOf(".") == 0 
		|| s.charAt(s.length - 1) == "@" || s.charAt(s.length - 1) == "."
		|| s.indexOf("@.") > -1 || s.indexOf(".@") > -1
		|| s.indexOf("@") != s.lastIndexOf("@")){
		return false;
	}
	return true;
}

function hasUserInput(sInput, sMsg){
	if(isEmpty(sInput)){alert(sMsg);return false;}
	return true;
}

function isEmpty(s){
	return (s == null || trim(s) == "");
}

function isZero(i){
	return (i == 0);
}

function trim(s){
	return rtrim(ltrim(s));
}

function ltrim(s){
	return s.replace(/^\s*/, "" );
}

function rtrim(s){
	return s.replace(/\s*$/, "" );
}

function getRowCount(sField){
	return parseInt(eval("document.forms[0].hidNew" + sField + "Count.value"));
}

function setRowCount(sField, iCount){
	eval("document.forms[0].hidNew" + sField + "Count.value=" + iCount);
}

function showConfirmation(oDisplayCell, oInputCell, oSource, oLabelCell){
  var sPrefix = oSource.name.substring(0,3);
	oDisplayCell.style.display = "";
	if(sPrefix == "txt"){
		oDisplayCell.innerText = oSource.value;
	}else if(sPrefix == "sel"){
		oDisplayCell.innerText = getSelectedItemText(oSource);
	}
	oInputCell.style.display = "none";
	oLabelCell.style.fontWeight = "bold";
}

function hideConfirmation(oDisplayCell, oInputCell, oLabelCell){
	oDisplayCell.style.display = "none";
	oInputCell.style.display = "";
	oLabelCell.style.fontWeight = "normal";
}

function isRadioSelected(oName){
	var i, bSelected = false;
	for(i=0; i<oName.length; i++){
		if(oName[i].checked == true){
			bSelected = true;
			break;
		}
	}
	return bSelected;
}

function getSelectedItemText(oSelect){
	var id = oSelect.value;
	var options = oSelect.innerHTML;
	var iPos1 = options.indexOf("value="+id+" ");
	iPos1 = options.indexOf(">", iPos1);
	var iPos2 = options.indexOf("<", iPos1);
	return options.substring(iPos1+1, iPos2);
}

function addSelectOption(oSelect, iValue, sText){
  var oOption = document.createElement("OPTION");
  oSelect.options.add(oOption);
  oOption.value = iValue;
  oOption.innerText = sText;
}
    
function removeSelectOptions(oSelect){
	for(var i = oSelect.options.length - 1; i > -1; i--){
		oSelect.options.remove(i);
	}
}

function removeUsedOptions(oSelect, oUsedSelect){
	var usedValue;
	for(var i=0; i < oUsedSelect.options.length; i++){
		usedValue = oUsedSelect.options[i].value;
		for(var j=0; j < oSelect.options.length; j++){
			if(oSelect.options[j].value == usedValue){
				oSelect.options.remove(j);
			}
		}
	}
}

function upcaseFirstLetter(s){
  var u = s.substring(0,1);
  return u.toUpperCase() + s.substring(1,s.length);
}

function enforceLeadRecordSelection(sFrame, metaLeadEntityName, leadRecordId, leadRecordName){
  var sSelect = "sel" + upcaseFirstLetter(metaLeadEntityName) + "Id";
  var oSelect, oOption;
  try{
    oSelect = eval(sFrame + ".oForm.elements('" + sSelect + "')");
    removeSelectOptions(oSelect);
    oOption = document.createElement("OPTION");
    oSelect.options.add(oOption);
    oOption.value = leadRecordId;
    oOption.innerText = leadRecordName;
    eval(sFrame + ".oForm.hidOld" + upcaseFirstLetter(metaLeadEntityName) + "Id.value =" + leadRecordId + ";");
  }catch(e){
    //alert(e.message);
    sSelect = "";
  }
}

function limitInputLength(oTextArea,iLength){
  if(oTextArea.innerText.length > iLength){
    oTextArea.innerText = oTextArea.innerText.substring(0,iLength);
  }
}
