/* 
    javascript : global.js
    Created on : 10/03/2011, 12:15:13 AM
    Author     : Simon Assouline
    Copyright  : © 2011 IT BUFF - Newcastle
    Description: Global Scripts
*/

// Text Field validation 
// there is an extra 2 default values that will result in false if the value is true.
// usage: <a onclick="javascript: return isNotNull(document.search_form.q.value,'not equal to','not equal as well')"
var isUserInput = false ;
function isNotNull(value,default1,default2)
{
	  if (value == null || trim(value).length == 0  || value == default1 || value ==default2 || isUserInput == false )
	  {
	    alert('You did not enter a search text.  Please try again.');
	    document.search_form.q.value='';
	    isUserInput = true;
	    document.search_form.q.focus();
	    return false;
	  }
	  else if (isSplCharsExist(value)) {
	  
		   if (trim(splCharsInKeyword).length > 1 ) {
		     splCharsInKeyword = 'Special characters ' + splCharsInKeyword + ' are ';
		   }
		   else {
		     splCharsInKeyword = 'Special character ' + splCharsInKeyword + ' is ';
		   }
		   
	   alert ( splCharsInKeyword +"not allowed.\n");
	   document.search_form.q.focus();
	   return false;
	  }
	 else
	 return true;
}


function checkSearch( value )
{
  if ( document.search_form && document.search_form.datasetId && typeof( langDataSetId ) != 'undefined' && langDataSetId )
  {
    document.search_form.datasetId.value = langDataSetId;
  }

  if ( value == null || trim(value).length == 0 )
  {
    alert('Please enter the keyword(s) to search for');
    return false;
  }
  else 
  {
    if ( document.search_form ) document.search_form.submit();
    return true;
  }
}

function checkGlobalSearch( value )
{
  return checkSearch( value );
}

// trim functions to truncate text with spaces.
function trim(value)
{
  s = new String(value);
  if (value != null) {
    var beginIndex = -1;
    var endIndex   = s.length;

    for (var i = 0; i < s.length; i++)
    {
      if (s.charAt(i) != " ") {
        beginIndex = i;
        break;
      }
    }
    if (beginIndex == -1) return "";

    for (var j = s.length -1; j > beginIndex; j--) {
      if (s.charAt(j) != " ") {
        endIndex = j;
        break;
      }
    }
    if (endIndex != s.length) return s.substring(beginIndex, endIndex);
    else return s.charAt(beginIndex);
  }
  return value;
}


