// -----------------------
// FUNCTION: fValidateData
// DESCRIPTION: A function that validates the text field and assigns the action urls as per the the selected value 
// ARGUMENTS: None
// RETURN: true or false
// Added for artf624540
// -----------------------
function fValidateData() {
	

	var aSelectedOption=aActionUrlsTopSearch[document.getElementById("searchOption").value];
	if( (aSelectedOption.action == "search.php" || aSelectedOption.action == "search.php") && fTrim(document.getElementById("top-search-query").value).length<=0) {
		alert("Please enter any keyword to search ");
		return false;
	}
	else {
		document.getElementById("top-search-query").value = fTrim(document.getElementById("top-search-query").value); //Removing leading & trailing spaces
		if(aSelectedOption.action == "search.php") {
			if(!fAlphaNumericValidate(document.getElementById("top-search-query").value)) {
				alert("Please enter a valid serach term");
				return false;
			}
			document.getElementById("top-search-query").value=document.getElementById("top-search-query").value.replace(/\"/g,'');
		}
		
		document.forms["searchHeaderId"].action=aSelectedOption.action;	
		document.getElementById("top-search-query").name=aSelectedOption.searchTextName;
		fDynamicAssignInputTags(aSelectedOption,"searchHeaderId");
		return true;
	}
}


// -----------------------
// FUNCTION: fValidateDataFooter
// DESCRIPTION: A function that validates the text field and assigns the action urls as per the the selected value 
// ARGUMENTS: None
// RETURN: true or false
// Added for artf624540
// -----------------------
function fValidateDataFooter(){
	var aSelectedOption=aActionUrlsBottomSearch[document.getElementById("searchOptionFooter").value];
	if( (aSelectedOption.action == "search.php" || aSelectedOption.action == "search.php") && document.getElementById("bottom-search-query").value == ""){
		alert("Please enter any keyword to search ");
		return false;
	}
	else {
		document.getElementById("bottom-search-query").value = fTrim(document.getElementById("bottom-search-query").value); //Removing leading & trailing spaces
		if(aSelectedOption.action == "search.php") {
			if(!fAlphaNumericValidate(document.getElementById("bottom-search-query").value)) {
               	alert("Please enter a valid search term");
				return false;
			}
			document.getElementById("bottom-search-query").value=document.getElementById("bottom-search-query").value.replace(/\"/g,'');
		}
		/*if(fTrim(document.getElementById("top-search-query").value).length<=0){  ///replacing multiple spaces into 1
			document.getElementById("top-search-query").value = '';
		}
		document.getElementById("top-search-query").value = fTrim(document.getElementById("top-search-query").value);*/
		
		
		document.forms["searchFooterId"].action=aSelectedOption.action;	
		document.getElementById("bottom-search-query").name=aSelectedOption.searchTextName;
		fDynamicAssignInputTags(aSelectedOption,"searchFooterId");
		return true;
	}
}


// -----------------------
// FUNCTION: fValidateDataSearchResults
// DESCRIPTION: A function that validates the text field and assigns the action urls as per the the selected value for search results page and changes the name of serach boxes dynamically.
// ARGUMENTS: None
// RETURN: true or false
// -----------------------
function fValidateDataSearchResults() {
	var aSelectedOption=aActionUrlsSearchResult[document.getElementById("search-results-search-options").value];
	if( (aSelectedOption.action == "search.php" || aSelectedOption.action == "search.php") && document.getElementById("search-results-search").value == "") {
		alert("Please enter any keyword to search");
		return false;
	}
	else {
		document.getElementById("search-results-search").value = fTrim(document.getElementById("search-results-search").value); //Removing leading & trailing spaces
		if(aSelectedOption.action == "search.php") {
			if(!fAlphaNumericValidate(document.getElementById("search-results-search").value)) {
                alert("Please enter a valid search term");
				return false;
			}
			document.getElementById("search-results-search").value=document.getElementById("search-results-search").value.replace(/\"/g,'');
		}
		
		document.forms["searchResultId"].action=aSelectedOption.action;	
		document.getElementById("search-results-search").name=aSelectedOption.searchTextName;
		fDynamicAssignInputTags(aSelectedOption,"searchResultId");
		return true;
	}	
}

// -----------------------
// FUNCTION: fDynamicAssignInputTags
// DESCRIPTION: A function that assigns dynamic hiddens tags toa  form elements
// ARGUMENTS: Array, eForm
// RETURN: true or false
// -----------------------
function fDynamicAssignInputTags(aArray, eForm) {
	document.getElementById(eForm+"-additional-parameters").innerHTML="";
	for(i in aArray.parameters) {
		var tempInput=document.createElement('input');
		tempInput.type="hidden";
		tempInput.name=i;
		tempInput.id=i;
		if(aArray.parameters[i] != ""){
			tempInput.value=aArray.parameters[i];
		}
		document.getElementById(eForm+"-additional-parameters").appendChild(tempInput);
	}			
}

// -----------------------
// FUNCTION: fTrim
// DESCRIPTION: A function that trim the leading and trailing spaces
// ARGUMENTS: sString 
// RETURN: Trimmed String(Removing the leading & trailing spaces)
// -----------------------
function fTrim(sString){
 var sRegEx=/\s*((\S+\s*)*)/;
 sString=sString.replace(sRegEx, "$1");
 sRegEx=/((\s*\S+)*)\s*/;
 sString=sString.replace(sRegEx, "$1");
 return sString;
}

// -----------------------
// FUNCTION: fAlphaNumericValidate
// DESCRIPTION: A function that checks the alpha sring for alphabets & numbers as well as null values
// ARGUMENTS: sString. There should be no leading or trailing spaces in it.
// RETURN: true or false
// -----------------------
function fAlphaNumericValidate(sString){
	return /^([a-z]|[A-Z]|[0-9]|['"]|\s)*$/.test(sString); //"'balancing the quotes.
}
