/* Global Variables */
var quickPromptCompany = "company name, acn, abn, arbn or duns...";
var quickPromptDocument = "document image number, acn...";
var quickPromptDirector = "lastname, firstname or firstname lastname...";

var quickTypeCompany = "Company"
var quickTypeDocument = "Document"
var quickTypeDirector = "Director"

/* Functions */
function ValidateForm(form)
{
   if (IsEmpty(form.txtQuickCriteria)) 
   { 
      alert('You have not entered anything');
      form.txtQuickCriteria.focus(); 
      return false;
   } 
 
    return true;     
} 

function ChangeSearchType(selectedSearchType)
{
    var searchType = selectedSearchType.options[selectedSearchType.selectedIndex].value;
    document.getElementById('txtQuickCriteria').maxLength = 200;

    switch (searchType)
    {  
        case quickTypeCompany:
            document.getElementById('txtQuickCriteria').value = quickPromptCompany;
            break;
        case quickTypeDirector:
            document.getElementById('txtQuickCriteria').value = quickPromptDirector;
            break;
        case quickTypeDocument:            
            document.getElementById('txtQuickCriteria').value = quickPromptDocument;
            document.getElementById('txtQuickCriteria').maxLength = 10;
            break;
    }                  
}

function SearchClick(path)
{
    var searchType = document.getElementById('selectType').value;
    var searchCriteria = document.getElementById('txtQuickCriteria').value;
    
	if((isEmpty(searchCriteria)) || ((searchCriteria == quickPromptCompany) || (searchCriteria == quickPromptDirector) || (searchCriteria == quickPromptDocument) ))
	{
		alert("Please enter a search");
		return false;
	}
    
    switch (searchType)
    {
        case quickTypeCompany:
            document.qForm.action = path + "redirects/search_redirect.asp";
            break;
        case quickTypeDirector:
            if (!isValidDirectorSearch(searchCriteria))
            {
                alert("Please enter a valid director search");
                return false;
            }
            else
            {
                document.qForm.action = path + "results/director_list.asp";
            }
            break;
        case quickTypeDocument:
            document.qForm.action = path + "redirects/search_redirect.asp";
            break;
    }   
    document.qForm.submit();
    return true;
}

function SearchAdvancedClick(path)
{
    window.location = path + "advanced_search.asp"
}

function isEmpty(s)
{
	var r, re;
	re = /^\s*$/ 	// regular expression: 0 or more whitespace chars
	if (s.match(re))
		return true;
	else
		return false;
}

function isWholeNumber2(s, bAllowSpace, bAllowEmpty)
{
	var r, re;

	if (bAllowSpace && bAllowEmpty)	
		re = /^[\d\s]*$/ ;			// reg exp: 0 or more numbers and spaces
	else if (bAllowSpace && !bAllowEmpty)
		re = /^\s*\d+[\d\s]*$/ ;	// reg exp: 0 or more spaces followed by one or more numbers followed by 0 or more numbers and spaces
	else if (!bAllowSpace && bAllowEmpty)
		re = /^\d*$/ ;				// reg exp: 0 or more numbers
	else
		re = /^\d+$/ ;				// reg exp: 1 or more numbers
			
	if (s.match(re))
		return true;
	else
		return false;
}

function isValidDirectorSearch(s)
{
    var r, re, re2, numberOfSpaces;    
    
    //lastname, firstname E.g. Smith, John
    re = /[A-Za-z\w], [A-Za-z\w]/i ;
    
    //firstname lastname E.g. John Smith
    re2 = /[A-Za-z\w] [A-Za-z\w]/i ;    
    
    numberOfSpaces = s.count(' ');
         
	if ((s.match(re) || s.match(re2)) && numberOfSpaces <= 2)
		return true;
	else
		return false;
}

String.prototype.count=function(s1) { 
	return (this.length - this.replace(new RegExp(s1,"g"), '').length) / s1.length;
}
