function validateControlEmpty(control, fieldName){
	if(control==null) return true;
	if(control.value==""){
		alert((getElementById("validate.controlEmpty").value).replace("%1",fieldName));
		return false;
	}
	return true;
}

function validateControlEmptyWithMsg(control, fieldName, msg){
	if(control==null) return true;
	if(control.value==""){
		alert(msg.replace("%1",fieldName));
		return false;
	}
	return true;
}

function validatePeriod(startControl, endControl, startFieldName, endFieldName){
	if(startControl!=null&&endControl!=null){
		if(isControlEmpty(startControl)&&isControlEmpty(endControl)){
			alert(((getElementById("validate.period").value).replace("%1",startFieldName)).replace("%2",endFieldName));
			return false;
		}
	}
	return true;
}

function validateListControlEmpty(control,fieldName){
	if(control==null) return true;
	if(control.options.length<=0){
		alert((getElementById("validate.listControlEmpty").value).replace("%1",fieldName));
		return false;
	}
	return true;
}

function validateDate(control, fieldName){
	if(control==null ||control.value=="") return true;
	if(!isDate(control.value)){
		alert((getElementById("validate.date").value).replace("%1",fieldName));
		return false; 
	}
	return true;
}
function validateRoughDate(control, fieldName){
	if(control==null ||control.value=="") return true;
	if(!isRoughDate(control.value)){
		alert((getElementById("validate.roughDate").value).replace("%1",fieldName));
		return false; 
	}
	return true;
}

function validateRoughYear(control, fieldName){
	if(control==null ||control.value=="") return true;
	if(!isRoughYear(control.value)){
		alert((getElementById("validate.roughYear").value).replace("%1",fieldName));
		return false; 
	}
	return true;
}

function validateInteger(control, fieldName){
	if(control==null||control.value=="") return true;
	if(!isInteger(currencyToNumber(control.value))){
		alert((getElementById("validate.integer").value).replace("%1",fieldName));
		return false; 
	}
	return true;
}

function validateFloat(control, fieldName){
	if(control==null||control.value=="") return true;
	if(!isFloat(currencyToNumber(control.value))){
		alert((getElementById("validate.float").value).replace("%1",fieldName));
		return false; 
	}
	return true;
}

function validateCurrency(control, fieldName){
	if(control==null||control.value=="") return true;
	if(!isCurrency(control.value)){
		alert((getElementById("validate.currency").value).replace("%1",fieldName));
		return false; 
	}
	return true;
}
function validateEmail(control, fieldName){
	if(control==null||control.value=="") return true;
	if(!isEMail(control.value)){
		alert((getElementById("validate.eMail").value).replace("%1", fieldName));
		return false;
	}
	return true;
}

function validateEmailWithMsg(control, fieldName, msg){
	if(control==null||control.value=="") return true;
	if(!isEMail(control.value)){
		alert(msg.replace("%1", fieldName));
		return false;
	}
	return true;
}

function isInteger(tsValue){
	var exp, op;
	op = tsValue;
    exp = /^\s*[-\+]?\d+\s*$/;
    if (op.match(exp) == null){ 
        return false;
    }else{
		return true;
    }
}

function isFloat(tsValue){
	var exp, op;
	op = tsValue;
	exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\" + "." + "(\\d+))?\\s*$");
    if(op.match(exp)==null) return false;
    else return true;
}

function isDate(tsValue){
	var exp, op,m,dateorder;
	op = tsValue;
    var yearFirstExp = new RegExp("^\\s*(\\d{4})([-./])(\\d{1,2})\\2(\\d{1,2})\\s*$");
    m=op.match(yearFirstExp);
    if(m==null){
		return false;
	}else{
		if (m != null && (m[1]>= 1900) && (m[3]<13 && m[3]>0) && (m[4]<32 && m[4] > 0))
			return true;
		else
			return false;
    }
}

function isRoughDate(tsValue){
	var exp, op,m,dateorder; 
	op = tsValue;
    var yearFirstExp = new RegExp("^\\s*(BC|bc)?\\s?(\\d{1,4})(([/])(\\d{1,2}))?(([/])(\\d{1,2}))?\\s*$");
    m=op.match(yearFirstExp);
    if(m==null){
		return false;
	}else{
		if(m[5]!=null && m[5]!="" && !(m[5]<13 && m[5]>0)) return false;
		if(m[8]!=null && m[8]!="" && !(m[8]<32 && m[8]>0)) return false;
		if(m[5]!=null && m[8]!=null && m[5]==""&&m[8]!="") return false;
		return true;
    }
}

function isRoughYear(tsValue){
	var exp, op,m,dateorder; 
	op = tsValue;
    var yearFirstExp = new RegExp("^\\s*(BC|bc)?\\s?(\\d{1,4})\\s*$");
    m=op.match(yearFirstExp);
    if(m==null){
		return false;
	}else{
		return true;
    }
}

function isCurrency(tsValue){
	var exp;
	exp = new RegExp("^\\s*([-\\+])?((((\\d+),)*(\\d+))|(\\d+)?)(\\.(\\d+))?\\s*$");
    if(tsValue.match(exp)==null) return false;
    else return true;
}

function isEMail(tsEMail)
{
	if (tsEMail.length > 150)
		return false;
	if(tsEMail == "") return true;
	var regu = "^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|NET|com|COM|gov|GOV|mil|MIL|org|ORG|edu|EDU|int|INT)$"
	var re = new RegExp(regu);
	if (tsEMail.search(re) != -1)
		return true;
	else
		return false;
}

function currencyToNumber(tsCurrency){
	return tsCurrency.replace(/\,/g ,"");	
}

function numberToCurrency(tnNumber){
	var tsNumber = new String(tnNumber);
	var tsDecimalPart = "";
	var tsIntPart = "";
	var tsSection;
	var lsSign = "";
	var lnPointIndex;
	
	if(tsNumber.length>0&&(tsNumber.substr(0,1)=="-"||tsNumber.substr(0,1)=="+")){
		lsSign = tsNumber.substr(0,1);
		tsNumber = tsNumber.substr(1,tsNumber.length-1);
	}
	
	lnPointIndex = tsNumber.indexOf(".", 0);

	if(lnPointIndex>=0){
		tsDecimalPart = tsNumber.substr(lnPointIndex+1, tsNumber.length-lnPointIndex-1);
	}else{
		lnPointIndex = tsNumber.length;
	}

	for(var lnIndex=lnPointIndex-3; lnIndex>=0; lnIndex=lnIndex-3){
		tsSection = tsNumber.substr(lnIndex, 3);
		if(tsIntPart!="") tsIntPart = tsSection + "," + tsIntPart;
		else tsIntPart = tsSection;
	}
	if(lnIndex!=-3){
		tsSection = tsNumber.substr(0, 3+lnIndex);
		if(tsIntPart!="") tsIntPart = tsSection + "," + tsIntPart;
		else tsIntPart = tsSection;
	}
	if(tsDecimalPart!="") return lsSign + tsIntPart + "." + tsDecimalPart;
	else return lsSign + tsIntPart;
}

function currencyTextBox_onfocusin(toTextBox){
	if(toTextBox.value!=''&&isCurrency(toTextBox.value)){
		toTextBox.value=currencyToNumber(toTextBox.value);
	}
}

function currencyTextBox_onfocusout(toTextBox){
	if(toTextBox.value!=''){
		if(isCurrency(toTextBox.value)){
			toTextBox.value=numberToCurrency(currencyToNumber(toTextBox.value));
		}			
	}
}
function isControlEmpty(control){
	if(control==null) return true;
	if(control.value==""){
		return true;
	}
	return false;
}
