/*************
formvalidation.js
This script is used for validating the form fields
*************/

function TextValidate(txtCtrl,val)
{
	var ctrName = val;
	Remove_Spaces(txtCtrl);
	if (txtCtrl.value == "")
	{
		alert("Please enter " + ctrName + ".");
		txtCtrl.focus();	
		return false;		
	}
	if(txtCtrl.value.indexOf("'") > -1)
	{
		alert("Please Don't enter ' in " + ctrName + ".");		
		txtCtrl.focus();   	
		return false;		
	}			
}

function TextAreaValidate(txtCtrl, val) 
{
	var ctrName	= val;
	Remove_Spaces(txtCtrl);	
	var str = txtCtrl.value;
	if(str.length == 0)
	{
		alert("Please enter " + ctrName + ".");
		txtCtrl.focus();	   	
		return false;
	}		
	if(txtCtrl.value.indexOf("'") > -1)
	{
		alert("Please Don't enter ' in " + ctrName + ".");		
		txtCtrl.focus();   	
		return false;		
	}
				
}

function EmailValidate(txtCtrl, val) 
{
	var ctrName = val;
	if (txtCtrl.value.indexOf("@") < 1 ||txtCtrl.value.indexOf(".") < 0) 
	{
		alert("Please enter valid " + ctrName + ".\t\n eg: username@domainname.com");
		txtCtrl.focus();	   	
		txtCtrl.select();
		return false;
	}		
	if(txtCtrl.value.indexOf("'") > -1)
	{
		alert("Please Don't enter ' in " + ctrName + ".");		
		txtCtrl.focus();   	
		return false;		
	}		
	
	if ((txtCtrl.value.indexOf("..") > -1) || txtCtrl.value.indexOf("@.") > -1 || (txtCtrl.value.length - txtCtrl.value.indexOf(".") <= 2))
	{
		alert("Please enter a valid email.");
		txtCtrl.focus();
		return false;
	}
	if ((txtCtrl.value.indexOf(">") > -1) || (txtCtrl.value.indexOf("<") > -1))
	{
		alert("Please Don't enter > or < in " + ctrName + ".");		
		txtCtrl.focus();
		return false;
	}
}

function SelectValidate(selCtrl,msg) 
{
	if (selCtrl.value == "" || selCtrl.value == "0" || selCtrl.value == "-1" || selCtrl.value.indexOf("Please") != -1) 
	{
		alert("Please select " + msg +".");
		selCtrl.focus();	   	
		return false;
	}		
}

function OptValidate(optCtrl,msg) 
{
    var el = document.forms[0].elements;
	for(var i = 0 ; i < el.length ; ++i)
	{
		if(el[i].type == "radio")
		{
			var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
			var itemchecked = false;
			for(var j = 0 ; j < radiogroup.length ; ++j) 
			{
				if(radiogroup[j].checked)
				{
				  itemchecked = true;
				  break;
				}
    		}
			if(!itemchecked) 
			{
				if (msg=="")
					alert("Please choose an option for "+el[i].name+".");
				else
					alert(msg);
				if(el[i].focus)
					el[i].focus();
				return false;
			}
	   	}
  	}
}
function Remove_Spaces(txtCtrl)
{
  txtCtrl.value = txtCtrl.value.replace(/\r/g, " ");

  txtCtrl.value = txtCtrl.value.replace(/[^ A-Za-z0-9`~!@#\$%\^&\*\(\)-_=\+\\\|\]\[\}\{'";:\?\/\.>,<]/g, "");

  txtCtrl.value = txtCtrl.value.replace(/'/g, "");

  txtCtrl.value = txtCtrl.value.replace(/ +/g, " ");

  txtCtrl.value = txtCtrl.value.replace(/^\s/g, "");

  txtCtrl.value = txtCtrl.value.replace(/\s$/g, "");
  
  if (txtCtrl.value == ' ')
  {
	 txtCtrl.value = '';
   }
 
 }
 
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
   		  IsNumber = false;
      }
   }
   return IsNumber;
}
/**
function isInt(theNum) 
{
	return ((theNum+'').match(/^(+|-)?\d+$/) != null);
}
function isDecimal(theNum) 
{
    return ((theNum+'').match(/^(((+|-)?\d+(\.\d*)?)|((+|-)?(\d*\.)?\d+))$/) != null);
}


function IsDecimal(str)
{
	mystring = str;
	if (mystring.match(/^\d+\.\d{2}$/ ) ) {
		return true;
	}
	else
	{
		return false;
	}
}**/
