function validate_contactName(contactNameLength) {
	if( (contactNameLength<2) || (contactNameLength>50) )
	{
		$('#errorContactName').show();
		return 1;
	} else {
		$('#errorContactName').hide();
		return 0;
	}
}
function validate_companyName(companyNameLength) {
	if( (companyNameLength<2) || (companyNameLength>50) )
	{
		$('#errorCompanyName').show();
		return 1;
	} else {
		$('#errorCompanyName').hide();
		return 0;
	}
}
function validate_emailAddress(emailAddress) {
	var emailAddress=$('#emailAddress').val();
	emailAddressValidate=emailAddress.indexOf('@');
	if ( (emailAddressValidate<1) || (emailAddress.length < 3) )
	{
		$('#errorEmail').show();
		return 1;
	} else {
		$('#errorEmail').hide();
		return 0;
	}
}
function validate_username(usernameLength) {
	if( (usernameLength<2) || (usernameLength>50) )
	{
		$('#errorUsername').show();
		return 1;
	} else {
		$('#errorUsername').hide();
		return 0;
	}
}
function validate_password(passwordLength) {
	if( (passwordLength<2) || (passwordLength>50) )
	{
		$('#errorPassword').show();
		return 1;
	} else {
		$('#errorPassword').hide();
		return 0;
	}
}
function validate_industry(industryLength) {
	if(industryLength<2 )
	{
		$('#errorIndustry').show();
		return 1;
	} else {
		$('#errorIndustry').hide();
		return 0;
	}
}
function validate_terms() {
	if ($('#z__TERMS_ACCEPTED').attr('checked'))
	{
		$('#errorTerms').hide();
		return 0;
	} else {
		$('#errorTerms').show();
		return 1;
	}
}
function validate_feedback(feedback) {
	if( (feedback<2) || (feedback>50) )
	{
		$('#errorFeedback').show();
		return 1;
	} else {
		$('#errorFeedback').hide();
		return 0;
	}
}

// BELOW USED TO VALIDATE SEARCH INPUT
function isEmpty(s) // Returns true if empty or null
{
	return ((s == null) || (s.length == 0))
}

function isLetter (c) // Returns true if character c is an English letter (A .. Z, a..z).
{
	state=(((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
	return ( state )
}

function isLetterOrAsterisk (c) // Returns true if character c is an English letter (A .. Z, a..z, *).
{
	state=(((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) || (c == "*") )
	return ( state )
}

function isDigit (c)// Returns true if character c is a digit (0 .. 9).
{
	state=((c >= "0") && (c <= "9"))
	return (state)
}

//W2K and later, index service operates in dialect mode 2 so if * used as wildcard, we need to switch to dialect mode 1
//dialect mode 1 = free-text mode where * treated literally
//dialect mode 1 = phrase mode where * treated as wildcard
//...but we still can't allow * at start of query!
function isAsterisk (c)// Returns true if character c is a *
{
	if (c == "*"){
		$('#CiDialect').val(1);
	}
	
}

function isAlphanumericFirst(c) {
	stateA=(isLetter(c))
	stateB=(isDigit(c))
	stateC=stateA || stateB
	return(stateC)
}
function isAlphanumericLast(c) {
	stateLastA=(isLetterOrAsterisk(c))
	stateLastB=(isDigit(c))
	isAsterisk(c)
	stateLastC=stateLastA || stateLastB
	return(stateLastC)
}

function removeXSWhitespace(item) {
  var t1=item.value;
  var tmp = "";
  var item_length = item.value.length;
  var item_length_minus_1 = item.value.length - 1;
  for (index=0; index<item_length; index++) {
    if (item.value.charAt(index) != ' ') {
      tmp += item.value.charAt(index);
    } else {
      if (tmp.length > 0) {
        if (item.value.charAt(index+1) != ' ' && index != item_length_minus_1) {
          tmp += item.value.charAt(index);
        }
      }
    }
  }
  item.value = tmp;
  t2=item.value
}

function search_validator(theForm)
{	
	var errString="Your search query could not be processed. \n\n ";
	var baseLength=errString.length;
	var allNum = "";
	var searchString=$('#keywords').val();

	if (searchString == "") {
		errString=errString+"No search term was entered.\n";
	} else {
		// check for space only
		if (searchString == " ") {
			errString=errString+"Search terms need to be more than a space.\n";
		} else {
			if (searchString.length==1) {
				if (!isLetter(searchString)) {
					errString=errString+"Search queries need to be more than non-alphanumeric or single numeric characters.\n";
				}
			} else {
				testChar1=searchString.charAt(0);
				testChar2=searchString.charAt((searchString.length)-1);

				if  (! (testChar1=='"' &&  testChar2=='"')) {
					if ( (!isAlphanumericFirst(testChar1)) || (!isAlphanumericLast(testChar2)) ) {
						errString=errString+"Search terms should not contain non-alphanumeric characters in the first or last position.\n";
					}
				} else {
					if (!(searchString.length>2)) {
						errString=errString+"Search terms should have literal keywords between quotes.\n";
					} else {
						quotecount=0;
						for (i=0; i<searchString.length; i++) {
							if (searchString.charAt(i)=='"' ) {
								quotecount++;
							}
						}
						if (quotecount>2) {
							errString=errString+"Literal keyword searches should not contain quotes.\n";
						}
					}
				}
			}
			if ( (searchString.toUpperCase()=="AND") || (searchString.toUpperCase()=="OR") || (searchString.toUpperCase()=="NOT") || (searchString.toUpperCase()=="AT") ) {
				errString=errString+"This word is not a valid query on its own.\n";
			}
		}
	}
	if (errString.length>baseLength) {
		$('.error').html(errString);
		$('.error').show();
		return (false);
	} else {
		$('.error').hide();
		return (true);
	}
}
