function isNum(fld)
{
	return checkfld(fld,"0123456789- ");
}
function checkfld(fld,str1)
{
	str=new String(str1);
	if (fld.value.length==0) return true;
	for (var i=0;i<fld.value.length;i++)
	{
		found=false;
		for (var j=0;j<str.length;j++)
			if((fld.value.substring(i,i+1))==(str.substring(j,j+1)))
			{
				found=true;
				break;
			}
		if (!found)
		{
			return false;
		}
	}
	return true;
}
function isEmail(obj)
{	
	str = obj.value;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (!filter.test(str))
	{
		return false;
	}
	return true;
}

function ltrim(s)
{
	return s.replace(/^\s*/, "")
}

function rtrim(s)
{
	return s.replace(/\s*$/, "");
}
function trim(s)
{		
	return rtrim(ltrim(s));
}

function isEmpty(eltObj)
{
    strValue    =    trimAll(eltObj.value);
    if(strValue.length > 0)
    {
        return false;
   }
    return true;
} 

function trimAll(strValue)
{
  var objRegExp = /^(\s*)$/;

  //check for all spaces
  if(objRegExp.test(strValue))   {
      strValue = strValue.replace(objRegExp, '');
      if( strValue.length == 0)
         return strValue;
  }
     //check for leading & trailing spaces
  objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
  if(objRegExp.test(strValue))   {
      //remove leading and trailing whitespace characters
      strValue = strValue.replace(objRegExp, '$2');
  }
  return strValue;
} 

function validate_friend(form)
{
    var firstelem = null;
    var num=0;
    var len = form.elements.length;


	if(trim(form.fromname.value) == "")
	{
		alert("Please enter your name");
		form.fromname.focus();
		return false;
	}


	if(trim(form.fromemail.value) == "")
	{
		alert("Please enter your email");
		form.fromemail.focus();
		return false;
	}

	if(!isEmail(form.fromemail))
	{
		alert("Please enter a valid E-mail ID");
		form.fromemail.focus();
		return false;
	}


    for(i=0;i<len;i++)
    {
        var e = form.elements[i];
        if (e.name == 'friendname[]')
        {
            if (firstelem == null)
            {
                firstelem = e;
            }
            var e2 = form.elements[i+1];

            if(isEmpty(e) && !isEmpty(e2))
            {
                alert("Please enter friend name");e.focus();
                return false;
            }
           
            if(!isEmpty(e) && isEmpty(e2))
            {
                alert("Please enter E-mail");e2.focus();
                return false;
            }
            if(!isEmpty(e) && !isEmpty(e2))
            {
                if (!isEmail(e2))
                {
                    alert("Please enter valid email");
                    e2.focus();e2.select();
                    return false;
                }
                else
                {
                    num++;
                }
            }
        }
    }

    if (num == 0)
    {
        alert("Please enter atleast one recipient name and email");
        firstelem.focus();
        return false;
    }
    
    return true;
} 


