function checkEmail(emailCntrl)
{
	ok=true
	if(emailCntrl.value=="")
	{
		alert("Please enter e-mail address.")
		emailCntrl.focus()
		ok=false
	}
	else
	{
		mail=emailCntrl.value
		at_pos=mail.indexOf("@")
		dot_pos=mail.indexOf(".")
		if(at_pos<1 || dot_pos<1)
		{
			alert("Please enter valid email address.")
			emailCntrl.focus()
			ok=false
		}
		else
		{
			mail=emailCntrl.value
			condition="yes"
			var at_count=0
			var dot_count=0
			var temp=0
			for(var i=0;i<mail.length;i++)
			{
				if((mail.charCodeAt(i)>0 && mail.charCodeAt(i)<48 && mail.charCodeAt(i)!=45)||(mail.charCodeAt(i)>57 && mail.charCodeAt(i)<65)||(mail.charCodeAt(i)>91 && mail.charCodeAt(i)<95)||(mail.charCodeAt(i)>96 && mail.charCodeAt(i)<97) || mail.charCodeAt(i)>122)
				{
					if(mail.charAt(i)=="@"||mail.charAt(i)==".")
					{
							if(mail.charAt(i)=="@"){at_count++}else{dot_count++} // counts the no. of times @ and . appears in email
							if(dot_count>=1)
							{
								dot_pos=i
								if((dot_pos>at_pos) && temp==0)
								{
									pos=dot_pos-at_pos
									temp++
								}								
							}
					}
					else
					{
						condition="no"
						i=mail.length
					}
				}
			}
			if(condition=="no")
			{
				alert("Your email contains a blank space or special character.")
				emailCntrl.focus()
				ok=false
			}
			else
			{
				if(at_count>1)
				{
					alert("E-mail contains extra @ ")
					emailCntrl.focus()
					ok=false
				}
				else
				{
					if(pos<2)
					{
						alert("Missing domain name between '@' and '.'")
						emailCntrl.focus()
						ok=false
						i=mail.length
					}
					else
					{	
						count=dot_pos+1
						domain=""
						for(count;count<mail.length;count++)
						{
							domain=domain+mail.charAt(count)		
						}
						dom=new Array("au","com","net","org","edu","in","mil","gov","arpa","biz","aero","name","coop","info","pro","museum","de")
						error="yes"
						for(var k=0;k<dom.length;k++)
						{
							if(domain==dom[k])
							{
								k=dom.length
								error="no"
							}
						}
						if((error=="yes" && (domain.length>2)) || (domain.length<2))
						{
							alert("Domain name must end with well known domains \n or 2-lettered country name. eg com,edu,in etc.")
							emailCntrl.focus()
							ok=false
						}								
					}
				}
			}
		}
	}
	return ok
}
function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
		result = true;
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789.");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	return result;
}
function validEmail(formField,fieldLabel,required)
{
	var result = true;
	if (required && !validRequired(formField,fieldLabel))
		result = false;
	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
  return result;
}

function validPassword(formField,fieldLabel,required)
{
	var result = true;
	if (required && !validRequired(formField,fieldLabel))
		result = false;
	if (result && ((formField.value.length < 5)) )
	{
		alert("Your \'" + fieldLabel + "\' must be equal or greater than 5 digits");
		formField.focus();
		result = false;
	}
  return result;
}

function chkPassword(formField1,formField2,required)
{
	var result = true;
	if (result && ((formField1.value!=formField2.value)))
	{
		alert("Your both passwords do not match");
		formField1.focus();
		result = false;
	}
  return result;
}

function validNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}

function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var num = parseInt(formField.value,10);
 		if (isNaN(num))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}
function LTrim(String)
{
	var i = 0;
	var j = String.length - 1;

	if (String == null)
		return (false);

	for (i = 0; i < String.length; i++)
	{
		if (String.substr(i, 1) != ' ' &&
		    String.substr(i, 1) != '\t')
			break;
	}

	if (i <= j)
		return (String.substr(i, (j+1)-i));
	else
		return ('');
}

function RTrim(String)
{
	var i = 0;
	var j = String.length - 1;

	if (String == null)
		return (false);

	for(j = String.length - 1; j >= 0; j--)
	{
		if (String.substr(j, 1) != ' ' &&
			String.substr(j, 1) != '\t')
		break;
	}

	if (i <= j)
		return (String.substr(i, (j+1)-i));
	else
		return ('');
}

function Trim(String)
{
	if (String == null)
		return (false);

	return RTrim(LTrim(String));
}

function validateControl(cntrl, cntrlName, cntrlType)
{
	if(cntrl)
	{
		cntrlValue = Trim(cntrl.value);
		switch(cntrlType)
		{
			case "text":
				if (cntrlValue.length < 1)
				{
					alert(cntrlName + " is required.");
					cntrl.focus();
					return false;
				}
				break;
			case "dd":
				if (cntrlValue <= 0)
				{
					alert(cntrlName + " is required.");
					cntrl.focus();
					return false;
				}
				break;
			case 'list':
				if(cntrl.selectedIndex == -1)
					var listId = -1;
				else
					var listId = parseInt(cntrl.options[cntrl.selectedIndex].value);
				if (listId == -1)
				{
					alert("Please Select "+cntrlName+".");
					return false;
				}
				break;
		}
	}
	return true;
}