var CONVERTION_ERROR = -9999;

function trim(strToTrim) {
	return strToTrim.replace(/^\s+|\s+$/g,"");
}


function ltrim(strToTrim) {
	return strToTrim.replace(/^\s+/,"");
}


function rtrim(strToTrim) {
	return strToTrim.replace(/\s+$/,"");
}


function isValidEmailAddress(emailAddress) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailAddress)){
		return (true)
	}
	return (false)
}

function isParameter(aText){
	return (/^\[@[\w]+\]$/).test(aText);
}


function isEmptyTextField(aTextField){
	var strValue = trim(aTextField.value);
	return strValue == "";
}


function checkRequiredTextField(aTextField, aTextFieldShowName){
	if (!isEmptyTextField(aTextField)){
		return (true);
	}	
	alert(aTextFieldShowName + ' is Required.');
	aTextField.focus();
	return (false);
}


function checkEmailAddress(aTextField, aTextFieldShowName, required, allowParameters){
	var goAhead = true;
	if (required) {
		goAhead = checkRequiredTextField(aTextField, aTextFieldShowName);
		if (!goAhead){
			return (false);
		}
	}
	else{
		goAhead = trim(aTextField.value) != "";	
		if (!goAhead){
			return (true);
		}
	}
	
	
	//if (goAhead){
		//by default this will check CS email addresses2209
		
		var emailArr = trim(aTextField.value).split(",");
		var notWellFormedEmailAddress = "";
		var wellFormedEmailAddress = "";		
		
		for (i=0; i<emailArr.length; i++){
			emailArr[i] = trim(emailArr[i]);
			if (emailArr[i].length > 0){
				if (isValidEmailAddress(emailArr[i])){				
					wellFormedEmailAddress += emailArr[i] + ",";					
				}	
				else{
					if (allowParameters && isParameter(emailArr[i])){
						wellFormedEmailAddress += emailArr[i] + ",";			
					}
					else{
						notWellFormedEmailAddress += emailArr[i] + ",";		
					}
				}
			}
		}		
		
		if (notWellFormedEmailAddress.length > 0){
			notWellFormedEmailAddress = notWellFormedEmailAddress.substr(0, notWellFormedEmailAddress.length-1);
			alert('Incorrect email address: ' + notWellFormedEmailAddress);
			aTextField.focus();
			return (false);
		}
		
		wellFormedEmailAddress = wellFormedEmailAddress.substr(0, wellFormedEmailAddress.length-1);
		aTextField.value = wellFormedEmailAddress;		
		return (true);
	//}
}


function validateNewTriggerForm(triggerFormName, action){
	triggerForm = eval("document." + triggerFormName);
	if (checkRequiredTextField(triggerForm.Name, 'Trigger Name')){				
		SetVal(triggerFormName,'Action', action);
		return (true);
	}	
	return (false);
}



function validateNotifyForm(NotifyFormName, action){
	notifyForm = eval("document." + NotifyFormName);
	if (checkRequiredTextField(notifyForm.Label, 'Notification Label')){
		if (checkRequiredTextField(notifyForm.Subject, 'Notification Subject')){
			if (checkEmailAddress(notifyForm.FromAddr, 'Notification From', true, true)){
				if (checkEmailAddress(notifyForm.ReplyTo, 'Notification Reply-To', true, true)){
					if (checkEmailAddress(notifyForm.ToAddr, 'Notification To', true, true)){
						if (checkEmailAddress(notifyForm.CC, 'Notification CC', false, true)){
							SetVal(NotifyFormName,'Action', action);
							return (true);						
						}
					}
				}
			}
		}
	}
	return (false);
}


function positiveInteger(aStr){
	var intValue;
	if (/^(?:\+)?\d+$/.test(aStr)){
		intValue = intValue = parseInt(aStr);
		return intValue;
	}	
	return CONVERTION_ERROR;		
}


function generalInteger(aStr){
	var intValue;
	if (/^(?:\+|-)?\d+$/.test(aStr)){
		intValue = parseInt(aStr);
		return intValue;
	}	
	return CONVERTION_ERROR;		
}

function getInt(txtField){
	var result; 
	if (!txtField.disabled){
		if (trim(txtField.value)==""){txtField.value="0"}
		result = positiveInteger(txtField.value);
		if ((result == CONVERTION_ERROR)){
			alert(txtField.value + " is not a positive integer");				
			return CONVERTION_ERROR;
		}
		else{
			return result;
		}
	}
	return 0;
}
