// JavaScript Document
function validate(frm)
{
		
		if(trim(frm.txtEmail.value) == "")
		{
			alert("The email address can not be empty");
			frm.txtEmail.focus();
			frm.txtEmail.select();
			return false;
		}
		s = validate_field(frm.txtEmail,'email',false)
		if(!s)
		{
			frm.txtEmail.focus();
			frm.txtEmail.select();
			return false;
		}
				

		if(trim(frm.txaDescription.value) == "")
		{
			alert("Description of service needs can not be empty");
			frm.txaDescription.focus();
			frm.txaDescription.select();
			return false;
		}
		return true;
		
}


function validate_field(field, type, autoplace)
{
	if (field.value == null || field.value.length == 0)
	{
		window.isvalid = true;
		return true;
	}
	var validflag = true;
	
	if (type == "integer" || type == "posinteger" || type == "float" || type == "posfloat")
	{
		var numval;
		var minclip=-Math.pow(2,32);
		var maxclip=Math.pow(2,64);
		var val = field.value;
		val = val.replace(/,/g,"");

		if (type == "integer")
			numval = parseInt(val,10);
		else if (type == "posinteger")
		{
			numval = parseInt(val,10);
			minclip=0;
		}
		else if (type == "posfloat")
		{
			numval = parseFloat(val);
			minclip=0;
		}
		else
			numval = parseFloat(val);
		if (isNaN(numval) || numval >= maxclip || numval <= minclip)
		{
			if (type=="posinteger" || type=="posfloat")
				alert("Invalid number (must be positive and less than 1.845E19)");
			else if (type=="integer" || type=="float")
				alert("Illegal number: " + val);
			else
				alert("Invalid number (must be greater than -4.29B");
			validflag = false;
		}
		else
		{
			field.value = numval;
			validflag = true;
		}


	}
	
	else if (type == "email")
	{
		if (checkemail(field, true, true))
			validflag = true;
		else
		validflag = false;
	}
	if (!validflag)
	{
		field.focus();
		field.select();
	}
	window.isvalid = validflag;
	return validflag;
}

function checkemail(fld1,emptyok,alrt)
{

	fld1.value = trim(fld1.value);
	return checkemail2(fld1,fld1,emptyok,alrt);
}

function checkemail2(fld1,fld2,emptyok,alrt)
{
		var s_email = fld1.value;

		if (s_email != fld2.value)
		{
			alert('E-mail addresses must match');
			return false;
		}
		if (emptyok && s_email.length==0)
		{
			return true;
		}

	/* 	shortest possible email is a@a.aa ; can't start with @; only one @ allowed ;
	should be min of 4 chars after @, 2 for '.', min of 3 chars before first '.', etc.....
	*/
	if ((s_email.length < 6) ||
		(s_email.indexOf('@',0) < 1) ||
		(s_email.lastIndexOf('@') != s_email.indexOf('@',0)) ||
		(s_email.lastIndexOf('@') > (s_email.length - 5)) ||
		(s_email.lastIndexOf('.') > (s_email.length - 3)) ||
		(s_email.lastIndexOf('.') < (s_email.length - 4)) ||
		(s_email.indexOf('..',0) > -1) ||
		(s_email.indexOf('@.',0) > -1) ||
		(s_email.indexOf('.@',0) > -1) ||
		(s_email.indexOf(',',0) > -1))
	{
		if (alrt)
		{
			alert('Please enter a valid e-mail address.');
			return false;
		}
	}
	return true;
}
function trim(str)
{

	var trimmedString = new String(str);

	if ((0 < str.length) && ((str.charCodeAt(0) <= 32) || (str.charCodeAt(str.length-1) <= 32)))
	{
		
		var startSlice = 0;
		var endSlice   = -1;


		for (var i = 0; i < str.length-1; i++)
		{
			if (str.charCodeAt(i) > 32)
			{
				startSlice = i;
				break;
			}
		}
		
		for (var i = str.length-1; startSlice <= i; i--)
		{
			if (str.charCodeAt(i) > 32)
			{
				endSlice = i;
				break;
			}
		}
		
		trimmedString = str.slice(startSlice, endSlice+1);
	}
	return trimmedString;
}

// field Validation