// JavaScript Document
function trim(entry) 
{
        while(entry.charAt(0)==' ')
        entry = entry.substring(1,entry.length);
        while(entry.charAt(entry.length-1)==' ')
        entry = entry.substring(0,entry.length-1);
		return entry;
}
function submitDR(form)
{
	form.compName.value = trim(form.compName.value);
	if(!validate(form.compName,"req"))
	{
		btf(form.compName,"Company Name", "required");
		return;
	}
	form.compAddress.value = trim(form.compAddress.value);
	if(!validate(form.compAddress,"req"))
	{
		btf(form.compAddress,"Company Address", "required");
		return;
	}
	form.city.value = trim(form.city.value);
	if(!validate(form.city,"req"))
	{
		btf(form.city,"City", "required");
		return;
	}
	form.state.value = trim(form.state.value);
	if(!validate(form.state,"req"))
	{
		btf(form.state,"State", "required");
		return;
	}
	if(!validate(form.country,"sel"))
	{
		btf(form.country,"Country", "select any one");
		return;
	}
	form.pin.value = trim(form.pin.value);
	if(!validate(form.pin,"req"))
	{
		btf(form.pin,"PIN", "required");
		return;
	}
	if(!validate(form.pin,"num"))
	{
		btf(form.pin,"PIN", "should be a number");
		return;
	}
	form.email.value = trim(form.email.value);
	if(!validate(form.email,"req"))
	{
		btf(form.email,"Email", "required");
		return;
	}
	if(!validate(form.email,"email"))
	{
		btf(form.email,"Email", "should be in the correct format");
		return;
	}
	form.compUrl.value = trim(form.compUrl.value);
	form.business.value = trim(form.business.value);
	if(!validate(form.business,"req"))
	{
		btf(form.business,"Company Business", "required");
		return;
	}
	form.contPerson.value = trim(form.contPerson.value);
	if(!validate(form.contPerson,"req"))
	{
		btf(form.contPerson,"Contact Person", "required");
		return;
	}
	form.designation.value = trim(form.designation.value);
	if(!validate(form.designation,"req"))
	{
		btf(form.designation,"Contact Person Designation", "required");
		return;
	}
	form.comments.value = trim(form.comments.value);
	if(!validate(form.comments,"req"))
	{
		btf(form.comments,"Comments", "required");
		return;
	}
	form.action = "mailer.php";
	form.submit();
}
function validate(obj, flag)
{
	obj.value = trim(obj.value);
	if(flag == "req")
	{
		return checkEmpty(obj);
	}
	else if(flag == "num")
	{
		return checkNum(obj);
	}
	else if(flag == "sel")
	{
		return checkSel(obj);
	}
	else if(flag == "email")
	{
		return checkEmail(obj.value);
	}
	else
		return true;
}
function checkEmail(obj)
{
    if(obj.length <= 0)
	{
	  return true;
	}
    var splitted = obj.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
	return false;
}
function checkEmpty(obj)
{
	if(obj.value == "" || obj.value.length == 0)
		return false;
	else
		return true;
}
function checkNum(obj)
{
	if(obj.value == "" || obj.value.length == 0)
		return true;
	else if(isNaN(parseInt(obj.value)))
		return false;
	else
		return true;
}
function checkSel(obj)
{
	if(obj.value == -1)
		return false;
	else
		return true;
}
function btf(obj, name, errStr)
{
	alert(name +" : "+ errStr);
	obj.select();
	obj.focus();
}	
function resetForm(form)
{
	form.reset();
}
