/**
 * 
 * _required0 = Should not be Empty
 * _required1 = Required and Should be Numeric
 * _required2 = Not Required and Should be Numeric Only
 * _required3 = Required and Should be Email
 * _required4 = Not Required And Should be Email Only
 * _required5 = html area field Should not be Empty 
 * _required6 = Required and Should be Alphanumeric
 **/
function isNumericValue (strString)
{   
	if((strString == null) || (strString.length == 0)) return true;	
	return !isNaN(strString);
}

function isAlphaNumericValue (strString)
{
	if((strString == null) || (strString.length == 0)) return false;	
	var regex = /^[A-Za-z0-9]+$/; 
	return regex.test(strString);
}

function hasNumericValue (strString)
{
	if((strString == null) || (strString.length == 0)) return false;
	var regex = /^[A-Za-z]+$/; 
	return regex.test(strString);
}

// Check whether string s is empty.
// whitespace characters
var whitespace = " \t\n\r";
function isEmpty(strString)
{
	if((strString == null) || (strString.length == 0)) return true;	
	// Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.
	for (iLoop = 0; iLoop < strString.length; iLoop++)
    {
                // Check that current character isn't whitespace.
                var chr = strString.charAt(iLoop);
                if (whitespace.indexOf(chr) == -1) return false;
	}
	// All characters are whitespace.
    return true;
}

function isEmptyHTML(strString)
{
	var strcmp;
	strcmp = "<p>&nbsp;</p>";
	if((strString == null) || (strString.length == 0) || (strString == strcmp)) return true;	
	// Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.
	for (iLoop = 0; iLoop < strString.length; iLoop++)
    {
                // Check that current character isn't whitespace.
                var chr = strString.charAt(iLoop);
				//alert(chr);
                if (whitespace.indexOf(chr) == -1) return false;
	}
	// All characters are whitespace.
    return true;
}

/***Email Function *****/
function isEmail(strString)
{
	if((strString == null) || (strString.length == 0)) return true;	
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	return regex.test(strString);
}

function showErrMsg(frmName,hiddenFieldPos,fieldName) {
	alert(frmName.elements[hiddenFieldPos].value);
	//frmName.elements[fieldName].focus();
	return false;
}	

function showErrMsgHtml(frmName,hiddenFieldPos,fieldName) {
	alert(frmName.elements[hiddenFieldPos].value);
	return false;
}	

function validate(thisform)
{	
		
        for (var iLoop=0; iLoop<thisform.elements.length; iLoop++)
        {
			valu = thisform.elements[iLoop].name;
			indx = valu.indexOf('_required');		
			if (indx != -1)
			{						
				fieldname=thisform.elements[iLoop].name.substring(0,indx);						                        
				switch (parseInt(valu.charAt(valu.length-1)))
				{
					case 0: //Required
					{
						if (isEmpty(thisform.elements[fieldname].value))
						{				
							return showErrMsg(thisform,iLoop,fieldname);
						}
						break;
					}
					case 1: //Required And Should Be Integer
					{
						if (isEmpty(thisform.elements[fieldname].value))
						{				
							return showErrMsg(thisform,iLoop,fieldname);
						}

						if(!(isNumericValue(thisform.elements[fieldname].value)))
						{
							return showErrMsg(thisform,iLoop,fieldname);
						}
						break;
					}
					case 2: //Not Required but should be Integer
					{
						if(!(isNumericValue(thisform.elements[fieldname].value)))
						{
							return showErrMsg(thisform,iLoop,fieldname);
						}
						break;
					}
					case 3: // Required and Should be Email
					{
						if (isEmpty(thisform.elements[fieldname].value))
						{				
							return showErrMsg(thisform,iLoop,fieldname);
						}

						if(!(isEmail(thisform.elements[fieldname].value)))
						{
							return showErrMsg(thisform,iLoop,fieldname);	
						}
						break;
					}
					case 4: // Not Required and Should be Email
					{							
						if(!(isEmail(thisform.elements[fieldname].value)))
						{
							return showErrMsg(thisform,iLoop,fieldname);	
						}
						break;
					}		
					case 5: //Required html area
					{
						if (isEmptyHTML(thisform.elements[fieldname].value))
						{		
							return showErrMsgHtml(thisform,iLoop,fieldname);
						}
						break;
					}	
					case 6: //Required html area
					{
						if(!(isAlphaNumericValue(thisform.elements[fieldname].value)))
						{
							return showErrMsg(thisform,iLoop,fieldname);
						}
						break;
					}	
					case 7: //Required And Should Be Integer
					{
						if (isEmpty(thisform.elements[fieldname].value))
						{				
							return showErrMsg(thisform,iLoop,fieldname);
						}

						if(!(isNumericValue(thisform.elements[fieldname].value)))
						{
							return showErrMsg(thisform,iLoop,fieldname);
						}

						if(thisform.elements[fieldname].value <= 0)
						{
							return showErrMsg(thisform,iLoop,fieldname);
						}
						break;
					}
					
				} //End Of SWITCH
			} // End Of IF
		} // End OF FOR	
		
		Comments=document.frmRequestInfo.cComments.value;
		
		var Commentshttpmatch=Comments.search(/http/i);
		if(Commentshttpmatch!=-1)
		{
			alert("You can not enter any web address.");
			document.frmRequestInfo.cComments.value="";
			document.frmRequestInfo.cComments.focus();
			return false;
		}

		var Commentswwwmatch=Comments.search(/www/i);
		if(Commentswwwmatch!=-1)
		{
			alert("You can not enter any web address.");
			document.frmRequestInfo.cComments.value="";
			document.frmRequestInfo.cComments.focus();
			return false;
		}	
	return true;
} // End Of Function