// JavaScript Document

// getObj :: get the HTML object 
// strName string
// return object 
function getObj(strName) 
{
	return document.getElementById(strName);
}

// mergeSpace :: mergeSpace object's space values in to one space
// obj object(TextBox, TextArea);
// return String replaced
function mergeSpace(obj)  // True or False
{
	var str = obj.value; 
	str = str.replace(/(^\s*)|(\s*$)/g, ""); // Trim 
	var regExp = /\s+/g;
	str = str.replace(regExp," ");
	obj.value = str;
}

// maxLenCheck :: Check obj's maxlen 
// obj object(TextBox, TextArea); maxlen numeric; strObjName string
// return Boolean
function maxLenCheck(obj,maxlen,strObjName) // True or False
{
	var len = obj.value.length;
	if(len>maxlen)
	{
		alert(strObjName+" is too long! The max length is "+maxlen+".");
		obj.focus();
		return false;
	}
	return true;
}

// isEmpty :: Check obj is empty or not
// obj object(TextBox, TextArea, Selection); strObjName string
// return Boolean
function isEmpty(obj,strObjName)  // True or False
{
	var str = obj.value;
	var regExp = /\s+/g;
	str = str.replace(regExp,"");
	var len = str.length;
	if(len==0) {
		alert("Empty "+strObjName+" is not allowed!");
		obj.focus();
		return true;
	}
	return false;
}

// isAlphaNumeric :: Check obj is alphauumeric formated or not
// obj object(TextBox); strObjName string
// return Boolean
function isAlphaNumeric(obj,strObjName)  // True or False
{
	var str = obj.value;
	var regExp = /^[a-zA-Z0-9]+$/g;
	if(obj.value.match(regExp)) {
		return true;
	}else{
		alert(strObjName+" is not alphanumeric value! Only letters and numbers allowed.");
		obj.value = '';
		obj.focus();
		return false;
	}
}

// checkPassword :: Check obj is checkPassword formated
// obj object(Password); objCheck ; minLen=6, maxLen=10
// return Boolean
function checkPassword(obj,objCheck,minLen,maxLen) 
{
	if(!isEmpty(obj,'Password'))
	{
		if( obj.value.length<minLen || obj.value.length>maxLen )
		{
			alert('Password only allow (' +minLen+ ' to ' +maxLen+ ') alphanumeric values!');
			obj.value = '';
			objCheck.value = '';
			obj.focus();
			return false;
		} else if( !isAlphaNumeric(obj,'Password') )  
			return false;
		else if( obj.value != objCheck.value ) 
		{ 
			alert('Two passwords does not match!'); 
			objCheck.value = '';
			objCheck.focus();
			return false;
		} else 
			return true;
		// here we success 
	} else {
		return false;
	}
}

/*
** @ Name: checkEmail()
** @ Parameter: obj -> an input object; strObjName -> string
** @ Function: validating the input object's value is email or not
*/
function checkEmail(obj,strObjName)  // True or False
{
	//var objEmailFeild = document.getElementById("email");
	//var emailExp = /^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+([a-zA-Z]{2,4}|\d{1,3})+$/;
	var emailExp = /^[a-zA-Z0-9_\-\.]+@([a-zA-Z0-9][a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,4}$/; 
	
	if( obj.value.match(emailExp) || obj.value.Trim()=='' ) {
		return true;
	}else{
		alert(strObjName+" is invalid Email address!");
		obj.focus();
		return false;
	}
}

/*
** @ Name: checkDate()
** @ Parameter: obj -> an input object; strObjName -> string
** @ Function: validating the input object's value is valid Date or not
*/
function checkDate(obj,strObjName)
{
	var dateExp = /^[0-9]{4}\-[0-9]{1,2}\-[0-9]{1,2}$/;   // e.g. 2009-05-14 
	
	if(obj.value.match(dateExp)) {
		return true;
	}else{
		alert(strObjName+" is invalid Date!");
		obj.focus();
		return false;
	}
}

/*
** @ Name: isFloat()
** @ Parameter: obj -> an input object; strObjName -> string
** @ Function: validating the input object's value is float or not
*/
function isFloat(obj,strObjName)
{
	if(obj.value.length==1)
		var floatExp = /^[0-9]$/;
	else
		var floatExp = /^[0-9]\d*\.?\d*$/; // /^([0]$|^[1-9]){1}([0-9]+|(\.[0-9]+)*)?[0-9]{0,}$/;
	
	if(obj.value.match(floatExp)) {
		return true;
	} else {
		alert(strObjName+" is invalid Digit!");
		obj.focus();
		return false;
	}
}

/*
** @ Name: isUnsignedInteger()
** @ Parameter: obj -> an input object; strObjName -> string
** @ Function: validating the input object's value is unsigned Integer or not
*/
function isUnsignedInteger(obj,strObjName)
{
	var regExp = /^[1-9][0-9]*[0-9]{0,}$/;
	
	if(obj.value.match(regExp)) {
		return true;
	}else{
		alert(strObjName+" is invalid! Provide unsigned Integer value.");
		obj.focus();
		return false;
	}
}

/*
** @ Name: isInteger()
** @ Parameter: obj -> an input object; strObjName -> string
** @ Function: validating the input object's value is signed Integer or not
*/
function isInteger(obj,strObjName)
{
	var regExp = /^[+\-]?[1-9][0-9]+$|^[+\-]?[0-9]+$/;
	
	if(obj.value.match(regExp)) {
		return true;
	}else{
		alert(strObjName+" is invalid! Provide Integer value.");
		obj.focus();
		return false;
	}
}

/*
** @ Name: isURL()
** @ Parameter: obj -> an input object; strObjName -> string
** @ Function: validating the input object's value is valid URL address or not
*/
function isURL(obj,strObjName)
{
	// var strRegex = "";
	var strRegex ="^(https|http|ftp){1}://";
	var re = new RegExp(strRegex); 
	// validate null 
	var str = obj.value.replace(re,"");
	str = str.Trim();
	// re.test()
	if ( re.test(obj.value) && str!='' ) 
	{
		return true; 
	} else { 
		alert(strObjName+" is invalid URL address! Please check.");
		obj.focus();
		return false;
	}
}

/*
** @ Name: checkFileType()
** @ Parameter: obj -> an input object; strObjName -> string; types -> Array('jpg','gif','exe',...)
** @ Function: validating the input object's value is the proper file types or not
*/
function checkFileType( obj,strObjName,types)
{
	var arrType = types.split(',');
	// Just as trim Function  
	// We extent it ourselves 
	var str = obj.value.Trim();  // str.replace(/(^\s*)|(\s*$)/g, "");
	var flag = false; // the extension validator flag 
	var errMsg = '';
	
	if( str.indexOf(".")>0 ) 
	{
		var arrURL = str.split('.'); 
		var len = arrURL.length-1; 
		var getExtension = arrURL[len]; // get the last string as extension 
		
		for ( i in arrType ) // i is the key for Array types
		{
			// the extension We Defined to accept
			var extension = arrType[i].toUpperCase(); // convert it first (toUpperCase is also ok)
			
			// not all peopel use lowercase characters
			if( getExtension!='' && getExtension.toUpperCase()==extension )  // (toUpperCase is also ok)
				flag = true;
		}
		errMsg = "File type ."+getExtension+" is forbidden! ";
	}
	
	if(!flag)
	{
		alert(errMsg+"Only "+ types.toUpperCase()+" accepted for "+strObjName+".");
		obj.focus();
		return false;
	}	
	
	return true;
}


