function validateusername(str,name) 
{
	var txt=str.value;
	var retval=true;
	var len=0;

	if (/^[a-z][a-z0-9._-]{0,13}[a-z0-9]$/i .test(txt))
	{	
		if (txt.length <6)
		{	
			len=txt.length;
			retval= false;
			alert ("Username should be minimum 6 characters");
			str.focus();
			str.select();
		}
	}
	else
		retval=false;		
	
	if(!retval && len==0)
	{
		alert (name);
		str.focus();
		str.select();
	}
	return retval;	
}

function validateusername1(str,name) 
{
	var txt=str.value;
	var retval=true;

	if (/^[a-z][a-z0-9._-]{0,13}[a-z0-9]$/i .test(txt))	
	 retval=true;		
	else
	 retval=false;		
	 
	if(!retval)
	{
		alert (name);
		str.focus();
		str.select();
	}
	return retval;
}

function validatepassword(str,name) 
{
	var txt=str.value;
	var retval=true;
	
	if(txt.length<6)
	{
		alert (name);
		retval=false;		
		str.focus();
		str.select();
	}	

	return retval;	
}
function validateName(str,name) 
{
	var validchar="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
	var txt=str.value;
	var retval=true;
	for (var i=0;i<txt.length;i++)
	{
		var ex=validchar.indexOf(txt.substring(i,i+1));
		if(ex==-1)
		{
		alert (name);
		retval=false;		
		str.focus();
		str.select();
		break;

		}	
	}
	return retval;	
}

function validempty(txt,name)
{
	val=txt.value;
	var retval=true;
	if (trim(val)=="")
	{
		alert (name);
		txt.focus();
		retval=false;
	}
	return retval; 
}

function validcompare(txt1,txt2,name)
{
	var val1=txt1.value;
	var val2=txt2.value;
	
	var retval=true;
	if (val1!=val2)
	{
	alert (name);
	txt2.focus();
	txt2.select();
	retval=false;
	}
	return retval; 
}

function trim(inputString) 
{
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") 
   {
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ")
    {
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1)
    {
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue;
}

function validatemail(str,name) 
{
	var txt=str.value;
	var retval=true;

	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt))	
		retval=true;		
	else
		retval=false;		
	 
	if(!retval)
	{
		alert (name);
		str.focus();
		str.select();
	}
	return retval;
}

function validatefax(str,name) 
{
	var validchar="1234567890-";
	var txt=str.value;
	var retval=true;
	if (txt.length>0)
		{
		for (var i=0;i<txt.length;i++)
		{
			var ex=validchar.indexOf(txt.substring(i,i+1));
			if(ex==-1)
			{
				retval=false;		
				break;
			}	
		}
		
		if (!(txt.charAt(3)=="-" && txt.charAt(7)=="-"))
			retval= false;
		
		if (txt=="000-000-000" || txt.indexOf("000")==0 || txt.indexOf("0")==0)
			retval= false;
		
		if(!retval)
		{
			alert (name);
			str.focus();
			str.select();
		}
	}
	
	return retval;	
}



function validateintegers(str,name) 
{
	var validchar="1234567890";
	var txt=str.value;
	var retval=true;
	for (var i=0;i<txt.length;i++)
	{
		var ex=validchar.indexOf(txt.substring(i,i+1));
		if(ex==-1)
		{
		alert (name);
		retval=false;		
		str.focus();
		str.select();
		break;

		}	
	}
	return retval;
}

function checkAmount(str,name){
		if(str.value > 0 ){
	return true;	
	}else{
	alert (name);
	str.focus();
	str.select();
	return false;
	}

}

function validatephone(str,name) 
{	
	var retval=true;
	var txt=str.value;
	if(trim(txt)=="")retval=true;
	else if ((/^\d{3}-\d{3}-\d{4}$/.test(txt)) && str.value !="000-000-0000")
		retval=true;		
	else if ((/^\d{3}\.\d{3}\.\d{4}$/.test(txt)) && str.value !="000.000.0000")
		retval=true;
	else
	{
		if(!validateintegers(str,name))
			retval=false;
		else if (!checkAmount(str,name))
			retval=false;
		else if(!(txt.length ==10))
		{
			retval=false;
			alert (name);
		}
		else
			retval=true;
	}
	 
	if(!retval)
	{
		str.focus();
		str.select();
	}
	return retval;
}

function validemptyCombo(cbo,name)
{
	val = -1;
	val=cbo.value;
	
	var retval=true;
	if (cbo.options.selectedIndex == 0)
	{
		alert (name);
		cbo.focus();
		retval=false;
	}
	return retval; 
}

function validatezipcode(str,name)		
{
	var retval=true;				
	var zipCode	=	str.value;
	
	if(zipCode.length<4)
	{
		alert(name);	
		str.focus();	
		str.select();
		retval=false;
	}
	return retval;
}

function getSelectedIndex(rdbUserType)
{
	var j = -1;	
	for (var i=1; i<document.forms(0).elements.length; i++)   
				{ if(document.forms(0).elements[i].id )
				{ if(document.forms(0).elements[i].id.indexOf("rdbUserType")!=-1) 
				{if (document.forms(0).elements[i].checked)
					{// Set the flag if any radio button is checked
					found_it = true;
					j=i;
					break;
					}}}} 
					alert(j);
			return j;
}


function fnCheckVal(rdbUserType)
{
				var found_it=false ;
				for (var i=1; i<document.forms[0].elements.length; i++)   
				{				
				 if(document.forms(0).elements[i].id ){
					 if(document.forms(0).elements[i].id.indexOf("rdbUserType")!=-1){
						if (document.forms(0).elements[i].checked==true)
						{	
							found_it = true;
							break;
						}
					}
					}
				} 					
				if(!found_it)
				{
				return false;
				}
				else 
				{return true;
				} 
		}
function validateMaxInvAmount(control1, control2, message) 
{
	var str1= parseFloat(control1.value);
	var str2= parseFloat(control2.value);
	var retval=true;
	
	if (str1> str2)
	{
		alert (message);
		retval=false;		
		control2.focus();
	}	
	else if (str1== str2)
	{
		alert (message);
		retval=false;		
		control2.focus();
	}	
	else
		retval= true;
	return retval;
}

function validatephonetext(str,name) 
{
	var validchar="1234567890-";
	var txt=str.value;
	var retval=true;
	if (txt.length>0)
		{
		for (var i=0;i<txt.length;i++)
		{
			var ex=validchar.indexOf(txt.substring(i,i+1));
			if(ex==-1)
			{
				retval=false;		
				break;
			}	
		}
			
		if(!retval)
		{
			alert (name);
			str.focus();
			str.select();
		}
	}
	
	return retval;	
}



function Validate_Phone(control1, control2, control3)		
{			
	var tval1	=	control1.value;				
	var tval2	=	control2.value;				
	var tval3	=	control3.value;	
						
	if(tval1=="" && tval2=="" && tval3 == "")
	{
		alert("Please enter atleast one phone number.");						
		control1.focus();
		return false;
	}
	else if(tval1=="000-000-0000" && tval2=="000-000-0000" && tval3=="000-000-0000")
	{
		alert("Please enter any of the valid Phone numbers");
		control1.focus();
		control1.select();
		return false;
	}	
	else if((tval1=="000-000-0000" && tval2=="000-000-0000") || (tval1=="000-000-0000" && tval3=="000-000-0000") || (tval2=="000-000-0000" && tval3=="000-000-0000"))
	{
		alert("Please enter any of the valid Phone numbers");
		control1.focus();
		control1.select();
		return false;
	}
	
	else if (tval1.length>0 && tval2=="" & tval3=="")			//Validate Work Phone
	{
		if(tval1=="000-000-0000" || tval1.indexOf("000")==0 || tval1.indexOf("0")==0)
		{
			alert("Please enter a valid phone number.");
			control1.focus();
			control1.select();
			return false;
		}
		else if (tval1.charAt(3)!="-" || tval1.charAt(7)!="-")
		{
			alert("Please enter a valid phone number.");
			control1.focus();
			control1.select();
			return false;
		}
		else 
			return true;	
	}
	else if (tval1=="" && tval2.length>0 & tval3=="")			//Validate Home Phone
	{
		if(tval2=="000-000-0000" || tval2.indexOf("000")==0 || tval2.indexOf("0")==0)
		{
			alert("Please enter a valid phone number.");
			control2.focus();
			control2.select();
			return false;
		}
		else if (tval2.charAt(3)!="-" || tval2.charAt(7)!="-")
		{
			alert("Please enter a valid phone number.");
			control2.focus();
			control2.select();
			return false;
		}
		else 
			return true;	
	}
	else if (tval1=="" && tval2=="" && tval3.length>0)			//Validate Mobile
	{
		if(tval3=="000-000-0000" || tval3.indexOf("000")==0 || tval3.indexOf("0")==0)
		{
			alert("Please enter a valid phone number.");
			control3.focus();
			control3.select();
			return false;
		}
		else if (tval3.charAt(3)!="-" || tval3.charAt(7)!="-")
		{
			alert("Please enter a valid phone number.");
			control3.focus();
			control3.select();
			return false;
		}
		else 
			return true;	
	}
	else
		return true;
}



function validatealphanumeric(str,name) 
{
	var validchar="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var txt=str.value;
	var retval=true;
	for (var i=0;i<txt.length;i++)
	{
		var ex=validchar.indexOf(txt.substring(i,i+1));
		if(ex==-1)
		{
		alert (name);
		retval=false;		
		str.focus();
		str.select();
		break;

		}	
	}
	return retval;
}
    function allow_alpha(obj)
{
 if (/[^a-z]/i.test(obj.value))
  obj.value=obj.value.replace(/[^a-z]/gi,'')
  obj.value+=''
  obj.focus()
}
