function chkForm ( docForm )
{
   trimAllFields(docForm);

   if ( docForm.cboCat.type == "select-one" )
      { if ( docForm.cboCat.selectedIndex==0 ) { alert("Please select a Service Category"); docForm.cboCat.focus(); return false; } }
   if ( !isOKCombo(docForm.cboEnqType) ) { alert("Please select Type of Enquiry"); docForm.cboEnqType.focus(); return false; }

   if ( docForm.txtCustName.value == "" ) { alert("Please enter your name"); docForm.txtCustName.focus(); return false; }
   if ( docForm.txaCustAddr != null ) { if ( docForm.txaCustAddr.value.length > 100 ) { alert("Address exceeds 100 characters !"); docForm.txaCustAddr.focus(); return false; } }

   // home no.
   if ( !isInteger(docForm.txtContactHomeArea.value,true) ) { alert("Please enter valid area code for house number"); docForm.txtContactHomeArea.focus(); return false; }
   if ( !isInteger(docForm.txtContactHomeNo.value,true) ) { alert("Please enter valid house number"); docForm.txtContactHomeNo.focus(); return false; }
   if ( docForm.txtContactHomeArea.value == "" && docForm.txtContactHomeNo.value != "" ) { alert("Please enter area code for house number"); docForm.txtContactHomeArea.focus(); return false; }
   if ( docForm.txtContactHomeArea.value != "" && docForm.txtContactHomeNo.value == "" ) { alert("Please enter house number"); docForm.txtContactHomeNo.focus(); return false; }

   // office no.
   if ( !isInteger(docForm.txtContactOffArea.value,true) ) { alert("Please enter valid area code for office number"); docForm.txtContactOffArea.focus(); return false; }
   if ( !isInteger(docForm.txtContactOffNo.value,true) ) { alert("Please enter valid office number"); docForm.txtContactOffNo.focus(); return false; }
   if ( docForm.txtContactOffArea.value == "" && docForm.txtContactOffNo.value != "" ) { alert("Please enter area code for office number"); docForm.txtContactOffArea.focus(); return false; }
   if ( docForm.txtContactOffArea.value != "" && docForm.txtContactOffNo.value == "" ) { alert("Please enter office number"); docForm.txtContactOffNo.focus(); return false; }

   // mobile no.
   if ( !isInteger(docForm.txtContactMobileArea.value,true) ) { alert("Please enter valid network code for mobile number"); docForm.txtContactMobileArea.focus(); return false; }
   if ( !isInteger(docForm.txtContactMobileNo.value,true) ) { alert("Please enter valid mobile number"); docForm.txtContactMobileNo.focus(); return false; }
   if ( docForm.txtContactMobileArea.value == "" && docForm.txtContactMobileNo.value != "" ) { alert("Please enter network code for mobile number"); docForm.txtContactMobileArea.focus(); return false; }
   if ( docForm.txtContactMobileArea.value != "" && docForm.txtContactMobileNo.value == "" ) { alert("Please enter mobile number"); docForm.txtContactMobileNo.focus(); return false; }

   if ( docForm.txtContactHomeArea.value   == "" && docForm.txtContactHomeNo.value   == "" &&
        docForm.txtContactOffArea.value    == "" && docForm.txtContactOffNo.value    == "" &&
        docForm.txtContactMobileArea.value == "" && docForm.txtContactMobileNo.value == "" )
      { alert("Please enter at least one contact number"); docForm.txtContactMobileArea.focus(); return false; }

   if ( !isEmail(docForm.txtEmail.value) ) { alert("Please enter valid Email address"); docForm.txtEmail.focus(); return false; }
   if ( docForm.rdoContactPref[0].checked ) // by email
      { docForm.rdoContactTime[0].checked = false; docForm.rdoContactTime[1].checked = false; }
   else
      {
       docForm.rdoContactPref[1].checked = true;
       if ( !docForm.rdoContactTime[0].checked ) { docForm.rdoContactTime[1].checked = true; }
	  }

   if ( docForm.txaFeedBack.value == "" ) { alert("Please enter your feedback"); docForm.txaFeedBack.focus(); return false; }
   if ( docForm.txaFeedBack.value.length > 250 ) { alert("Only 250 characters allowed for feedback !"); docForm.txaFeedBack.focus(); return false; }

   return true;
  }


var REG_WHTSPC_START = /^\s+/ig;  //global pattern matching (g), case insensitive (i), matches @ beginning of string (^)
var REG_WHTSPC_END   = /\s+$/ig;  //global pattern matching (g), case insensitive (i), matches @ end of string ($)
var whitespace       = "\t\n\r "; // horizontal tab,newline,carriage return,space etc
var isDefEmptyOK     = false;


function allTrim ( strIn )
  {
   //strIn.value = strIn.replace(REG_WHTSPC_START,"").replace(REG_WHTSPC_END,"");

   var i = 0;                  // trim leading spaces
   while ( i <= strIn.length-1 && whitespace.indexOf(strIn.charAt(i)) != -1 )
         { i++; }
   if ( i == strIn.length )
      { strIn = ""; }
   else
      { strIn = strIn.substring ( i, strIn.length); }

   i = strIn.length-1;         // trim trailing spaces
   while ( i >= 0 && whitespace.indexOf(strIn.charAt(i)) != -1 ) { i--; }
   strIn = strIn.substring ( 0, i+1 );

   return strIn;
  }


function trimAllFields ( docForm )
{
	// http://www.regular-expressions.info/javascriptexample.html  </?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)/?>
	var textReg = "<(.|\n)*?>"; // "<(.|\n)+?>";
	var regex = new RegExp(textReg);

	for ( var i=0; i<docForm.elements.length; i++ )
	{
		if ( docForm.elements[i].type == "text"     || docForm.elements[i].type == "textarea" ||
			docForm.elements[i].type == "password" || docForm.elements[i].type == "hidden" )
		{
			docForm.elements[i].value = RemoveBad(allTrim(docForm.elements[i].value));

			/*
			if (docForm.elements[i].value.match(regex))
			{
				// Found HTML tag
				var locMatch = regex.exec(docForm.elements[i].value);
				if (locMatch == null)
				{
					// No HTML tag
				}
				else
				{
					var s = "HTML tag found at position " + locMatch.index + ":\n";
					for (iLoc = 0; iLoc < locMatch.length; iLoc++)
					{
						s = s + locMatch[iLoc] + "\n";
					}
					//alert(s);

					// Replace. global pattern matching (g), case insensitive (i)
					//var re = new RegExp(textReg, "g");
					//docForm.elements[i].value = docForm.elements[i].value.replace (re,"");

					alert("Invalid character ( < or >) is not allowed !");
					docForm.elements[i].focus();
					return false;
					break;
				}
			}

			docForm.elements[i].value = chkValidHTMLCodes(docForm.elements[i].value,true);
			//docForm.elements[i].value = docForm.elements[i].value.replace(REG_APOSTROPHE,"'");
			*/
		}
	}

	return true;
}


// Remove special characters    < > " ' % ; ) ( & + -   strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-/g,"");
// Remove special characters    < > " ' % ; ) ( / \ & + -   strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-/g,"");
function RemoveBad(strTemp)
{
	strTemp = strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\/|\\|\&|\+|\-/g,"");
	return strTemp;
}


// chkValidHTMLCodes ( STRING s [, BOOLEAN isReplace] )
// chkValidHTMLCodes (elemValue, true)    returns newStr
// chkValidHTMLCodes (elemValue, false)   returns true/false
// chkValidHTMLCodes (elemValue)          returns true/false
function chkValidHTMLCodes ( s )
  {
   // includes New Line "String.fromCharCode(10)" "\n", Carriage Return "String.fromCharCode(13)" "\r", Tab "String.fromCharCode(9)" "\t"
   // © 169   ® 174, disallow '";<>
   var strValidCodes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 " +
                       "~!@#$%^&*()_-+={}[]:,.?/|" + "\\" + "©®" + "\n\r\t" +
                       String.fromCharCode(160); //+ "\""

   var i         = 0;
   var returnStr = "";
   var isReplace = chkValidHTMLCodes.arguments.length == 1 ? false : (chkValidHTMLCodes.arguments[1]==true);

   if ( isReplace )
      {
       for ( i=0; i<s.length; i++ )
         {
          var c = s.charAt(i);
          if ( s.charCodeAt(i) > 255 || strValidCodes.indexOf(c) == -1 )
             { returnStr = returnStr + "&#" + s.charCodeAt(i) + ";"; }
          else
             { returnStr += c; }
         }
       return returnStr;
      }
   else
      { return true; }
  }

function isOKFormText ( formElem, strErrMsg )
  {
   if ( formElem.value == "" )
      { alert(strErrMsg); formElem.focus(); return false; }
   else if ( isOKFormText.arguments.length == 3 )
      {
       if ( formElem.value.length > parseInt(isOKFormText.arguments[2]) )
          {
           alert ( "Text too long ! Maximum " + isOKFormText.arguments[2] + " characters" );
           formElem.focus(); return false;
          }
      }
   return true;
  }


function isOKCombo ( formElem )
  {
   if ( formElem.length == 0 ) { return ( isOKCombo.arguments.length == 1 ? isDefEmptyOK : (isOKCombo.arguments[1]==true) ); }

   var strIdx = formElem.selectedIndex;
   var strVal = formElem.options[strIdx].value;
   if ( strVal == "" )   //if ( strVal == "" || strVal == 0 )
      { return ( isOKCombo.arguments.length == 1 ? isDefEmptyOK : (isOKCombo.arguments[1]==true) ); }
   return true;
  }


// !isOKFormCombo(document.forms[0].cboCtry,"Please select country",false)     mandatory
// !isOKFormCombo(document.forms[0].cboState,"Please select state",true)       optional
function isOKFormCombo ( formElem, strErrMsg )
  {
   var isEmptyOK = isOKFormCombo.arguments.length == 2 ? isDefEmptyOK : (isOKFormCombo.arguments[2]==true);
   if ( !isOKCombo(formElem,isEmptyOK) ) { alert(strErrMsg); formElem.focus(); return false; }
   return true;
  }


function cboSingleValue ( objCombo )
  {
   //return objCombo[objCombo.selectedIndex].value;
   return objCombo.options[objCombo.selectedIndex].value;
  }


function isDigit (c)
  {
   //return ( !( s.charCodeAt(i) >= 48 && s.charCodeAt(i) <= 57 ) );
   //return ( c < "0" || c > "9" ? false : true );
   return ( c >= "0" && c <= "9" );
  }


// isInteger (STRING s [, BOOLEAN emptyOK])
// Alternatively, use isNaN(s) --> returns true if Not-A-Number
function isInteger (s)
  {
   if ( s == "" )
      { return ( isInteger.arguments.length == 1 ? isDefEmptyOK : (isInteger.arguments[1] == true) ); }

   for ( var i = 0; i < s.length; i++ )
       {   
        var c = s.charAt(i);
        if ( !isDigit(c) ) { return false; }
       }
   return true;
  }


function isEmail (str)
  {
   if ( str == "" )
      { return ( isEmail.arguments.length == 1 ? isDefEmptyOK : (isEmail.arguments[1]==true) ); }

   // are regular expressions supported?
   var supported = false;
   if ( window.RegExp )
      {
       var tempStr = "a";
       var tempReg = new RegExp(tempStr);
       if ( tempReg.test(tempStr) ) { supported = true; }
      }

   if ( !supported )
      { return ( str.indexOf(".")>2 ) && ( str.indexOf("@")>0 ) };

   var r1 = new RegExp ( "(@.*@)|(\\.\\.)|(@\\.)|(^\\.)" );
   var r2 = new RegExp ( "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$" );
   return ( !r1.test(str) && r2.test(str) );
  }