function trim(str) {
  str = str.replace( /^\s+/g, "" );// strip leading
  return str.replace( /\s+$/g, "" );// strip trailing
}
var arrcount=0;
function checkFields(){
        updateTextArea('comment');
		if(document.getElementById("fname").value == ""){
			arrcount=0;
 		    getFeedbackErrorString("FIRST_NAME_REQUIRED");
			document.getElementById("fname").focus();	
			return false;
		}else if(document.getElementById("lname").value == ""){
			arrcount=1;
			getFeedbackErrorString("LAST_NAME_REQUIRED");
			document.getElementById("lname").focus();	
			return false;
		}else
                        var o = document.getElementById('sex');
                        var t = document.getElementById('sex1');

                 if ( (o.checked == false ) && (t.checked == false ) )
                    {    arrcount=2;
                          getFeedbackErrorString("GENDER_SELECT");
                          document.getElementById("sex").focus();
                          return false;
                    }
                /*   if(!(document.getElementById("sex").checked)){
                          alert(document.getElementById("sex").checked);
                          getFeedbackErrorString("GENDER_SELECT");
                          document.getElementById("sex").focus()	
			return false;
                }*/
                else if(document.getElementById("email").value == ""){
			arrcount=3;
			getFeedbackErrorString("EMAIL_ADDRESS_REQUIRED");
			document.getElementById("email").focus();
			return false;
		}else if(document.getElementById("phone").value == ""){
			arrcount=4;
			getFeedbackErrorString("PHONE_NO_REQUIRED");
			document.getElementById("phone").focus();
			return false;
		}else if(document.getElementById("userType").value == ""){
			arrcount=5;
			getFeedbackErrorString("USER_TYPE_REQUIRED");
			document.getElementById("userType").focus();
			return false;
		}else if(document.getElementById("subject").value == ""){
			arrcount=6;
			 getFeedbackErrorString("FEEDBACK_SUB_REQUIRED"); 
                         //alert(document.getElementById("comment").value);
			 document.getElementById("subject").focus();
                         return false; 
		}else if(document.getElementById("comment").value == "" || document.getElementById("comment").value =="<br>" ){                
			arrcount=7;
			 getFeedbackErrorString("FEEDBACK_REQUIRED");
                        // document.getElementById("comment").focus();
			 return false;
		}else if(document.getElementById("cword").value == ""){
			arrcount=8;
			getFeedbackErrorString("SECURITY_CODE_REQUIRED");
			document.getElementById("cword").focus();
			return false;
		}else if (eMailAddressCheck(document.getElementById("email").value)==false){
			arrcount=3;
                        getFeedbackErrorString("INVALID_EMAIL");
			document.getElementById("email").value="";
			document.getElementById("email").focus();
			return false		
		}	
		else if(document.getElementById("xcode").value != '' && 
				document.getElementById("cword").value != '' &&
					(document.getElementById("xcode").value.toLowerCase() == document.getElementById("cword").value.toLowerCase() ))
			{                        
				return true;
		}else{
            arrcount=8;                        
			getFeedbackErrorString("INVALID_SECURITY_CODE");
			document.getElementById("cword").value="";
			document.getElementById("cword").focus();	
			return false;	
		}
                
}

function getFeedbackErrorString(key){
	
	var fdBkXmlHttp = GetXmlHttpObject();
	if (fdBkXmlHttp==null){
	  alert ("Your browser does not support AJAX!");
	  return false;
	}	

	var url=ctxtPath+"/jsp/getErrorString.jsp?KEY="+key+"&lang="+document.getElementById("lang").value;
	fdBkXmlHttp.onreadystatechange= function(){getFeedbackErrStr(fdBkXmlHttp);};		
	fdBkXmlHttp.open("POST",url,true);
	fdBkXmlHttp.send(null); 
}

function getFeedbackErrStr(fdBkXmlHttp){
	if (fdBkXmlHttp.readyState==4){
		if (fdBkXmlHttp.status == 200) {		
			var errorString = trim(fdBkXmlHttp.responseText);				
			if (errorString != ""){	                    
	                     fillerror(errorString);	                    
			}else{
				displayErrorPopupDialog('Missing/Invalid Field Value,Please Check!');
			}	
		}
	} // end of main if
}
function fillerror(errorString){
    var fillarray = new Array("namedec","snamedec","gen","emaildec","phonedec","utypedec","subdec","comdec","secdec");
    if(errorString!=""){
    document.getElementsByName(fillarray[arrcount])[0].innerHTML=errorString;
       }else{
   document.getElementsByName(fillarray[arrcount])[0].innerHTML="";
     }
}
function getSecurityCode(){
	var secXmlHttp=GetXmlHttpObject();
	if (secXmlHttp==null){
	  alert ("Your browser does not support AJAX!");
	  return false;
	}	
	//Get Security Code from Server Verify it with the value if ok then submit
	var url=ctxtPath+"/jsp/getSecurityCode.jsp";
	secXmlHttp.onreadystatechange=function(){processSecurityCode(secXmlHttp);};		
	secXmlHttp.open("POST",url,true);
	secXmlHttp.send(null); 
}

function processSecurityCode(secXmlHttp){
	if (secXmlHttp.readyState==4){ 
		var securityCodeServer = trim(secXmlHttp.responseText);		
		if (securityCodeServer != ""){
			
			if(document.getElementById('xcode').value=="yes")
			{
				document.getElementById("cword").value=securityCodeServer;
			}
			
			document.getElementById('xcode').value = securityCodeServer;
			//alert(securityCodeServer); - here we should make the values...
		}	
	} // end of main if
}

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
function eMailAddressCheck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }
 		 return true					
}


/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
	s = stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidateForm(){
	var Phone=document.frmSample.txtPhone
	
	if ((Phone.value==null)||(Phone.value=="")){
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }	
	
function imposeMaxLength(Object, MaxLen){
  return (Object.value.length <= MaxLen);
}
