// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

//081222 dan - changes for the AT search
function quickSearch(query ,type ){
    var query = encodeURIComponent(query);

	var newAddress="";
    if(query != ''){
		if (type == ''){
			newAddress = '/ServiceTools/Search.aspx?q=' + query+ "&sortorder=asc";

		}
		else{
			var loc = location.href;
			var arrayloc = loc.split('/');
			
			/*var url = loc.substring(0,loc.length-5);*/
			var url = '';
			for(var i = 0; i < 4; i++){
				url = url + arrayloc[i] +'/';
			}

			if (arrayloc.length > 5)
				url = url + arrayloc[4];
			else
				url = url + arrayloc[4].substring(0, arrayloc[4].length - 5);

			newAddress = url + '/ServiceTools/Search.aspx?q=' + query+ "&sortorder=asc";
				
		}
		
	/*	if (trim(category!='') && (trim(category!='0')) )
			{
				newAddress +="&mc=scCategory=:"+category;
			}
			if (trim(subCategory!='') && (trim(subCategory!='0')) )
			{
				newAddress +="&mc=scSubCategory=:"+subCategory;
			} */
		location.href  = newAddress;
    }   
}

//081222 dan - added for the BM search
function changeSearchCateg()
{
	var query ='/ServiceTools/Search.aspx';

	var txtSearchValue="";
	var txtSearch = document.getElementById("txtPageSearch");
	if (txtSearch != null )
	{
		txtSearchValue = txtSearch.value;
	}

//090217 mrr - for now we don't have subcategories, onyl categories
    //txtSearchValue="";
	var searchCategoryValue="";
	var searchSubCategoryValue="";
	if (trim(txtSearchValue)!='')
	{
		 query+='?q='+encodeURIComponent(txtSearchValue);
		
		var searchCategory = document.getElementById("searchCategory");
		if (searchCategory != null )
		{
			searchCategoryValue = searchCategory.value;
			query+='&categ='+searchCategoryValue;
		}
		var searchSubCategory = document.getElementById("searchSubCategory");
		if ((searchSubCategory != null)  && (trim(searchCategoryValue) !='0'))
		{
			searchSubCategoryValue = searchSubCategory.value;
			query+='&subCateg='+searchSubCategoryValue;
		}
		
	location.href=query;
	}
}

//081222 dan - changed for the BM search
function doSearch()
{
	var query = location.href;
	var urlarray = query.split('?');
	var urlf = urlarray[0];
	
	var txtSearch = document.getElementById("txtPageSearch");
	if (txtSearch != null )
	{
		txtSearchValue = txtSearch.value;
	}
	

	var searchCategoryValue="";
	var searchSubCategoryValue="";
	if (trim(txtSearchValue)!='')
	{
		query = urlf + "?q=" +txtSearchValue+'&l=1';
		 
		var searchCategory = document.getElementById("searchCategory");
		if (searchCategory != null ) 
		{
			searchCategoryValue = searchCategory.value;
			query+='&categ='+searchCategoryValue;
			
			if (trim(searchCategoryValue) !='0')
			{
				query+='&mc=scCategory=:'+searchCategoryValue;
			}	
		}
		var searchSubCategory = document.getElementById("searchSubCategory");
		if ((searchSubCategory != null)  && (trim(searchSubCategoryValue) !='0'))
		{
			searchSubCategoryValue = searchSubCategory.value;
			query+='&subCateg='+searchSubCategoryValue;
			
			if (trim(searchSubCategoryValue) !='0')
			{
				query+='&mc=scSubCategory=:'+searchSubCategoryValue;
			}
			
		}
	}	
	
	location.href=query;
}

//080105 dan - added some new functions

function checkEnter(e,caller) //e is event object passed from function invocation
{

	var characterCode //literal character code will be stored in this variable

	if(e && e.which ||e.which == 0)
	{ //if which property of event object is supported (NN4)
	
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else
	{
		e = event;
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13)//if generated character code is equal to ascii 13 (if enter key)
	{ 
	    if(document.all){
		    e.returnValue=false;
            e.cancel = true;
        }
        else{
         e.preventDefault();
        }
        var obj = document.getElementById(caller);
        if(obj){        
            
            if(obj.click){
		        obj.click();
		    }
		}
		return false;
	}
	else
	{
		return true;
	}
		
}

function groupSearch(startPage, resultsPerGroup, resultPage){
   
	//searchStr = encodeURI(searchStr);
	var query = location.href;
	var urlarray = query.split('?');
	var urlf = urlarray[0];
	
	var txtSearch = document.getElementById("txtPageSearch");
	if (txtSearch != null )
	{
		txtSearchValue = txtSearch.value;
	}
	
	var searchCategoryValue="";
	var searchSubCategoryValue="";
	if (trim(txtSearchValue)!='')
	{
		query = urlf + "?q=" +encodeURI(txtSearchValue)+ '&l=' + startPage + '&results=' + resultsPerGroup;
		 
		var searchCategory = document.getElementById("searchCategory");
		if (searchCategory != null ) 
		{
			searchCategoryValue = searchCategory.value;
			query+='&categ='+searchCategoryValue;
			
			if (trim(searchCategoryValue) !='0')
			{
				query+='&mc=scCategory=:'+searchCategoryValue;
			}	
		}
		var searchSubCategory = document.getElementById("searchSubCategory");
		if ((searchSubCategory != null)  && (trim(searchSubCategoryValue) !='0'))
		{
			searchSubCategoryValue = searchSubCategory.value;
			query+='&subCateg='+searchSubCategoryValue;
			
			if (trim(searchSubCategoryValue) !='0')
			{
				query+='&mc=scSubCategory=:'+searchSubCategoryValue;
			}
			
		}
	}	
	location.href=query;
	
}

function openPopUp(pUrl, title) {	
	window.open(pUrl,"","width=420,height=450,resizable,scrollbars=no,status=1");
}

function newsCategoryDropDown(){
	
	var newsCatElem = document.getElementById("newsCateg");
	if (newsCatElem.selectedIndex>0){
		location.href='?newsCategoryID='+ newsCatElem.options[newsCatElem.selectedIndex].value;
	}
	else
	{
		location.href = location.href.split('?')[0];
	}
	
	//location.href='?newsCategoryID='+ this.options[this.selectedIndex].value;
}

function catThemeOnClick(newurl){
	if (newurl != '')
		location.href = newurl;
}

function getKeyTF(evt, element) {
    
    
    var typedKey = null;
	if(evt && evt.which)
	{ //if which property of event object is supported (NN4)
		//e = e ?? 
		typedKey = evt.which //character code is contained in NN4's which property
	}
	else
	{		
		typedKey = event.keyCode; //character code is contained in IE's keyCode property
	}

	if (typedKey == 13) {	    
		if(document.all) {
			window.event.cancelBubble='true';
		    window.event.returnValue = false;
		} else {
			evt.stopPropagation();
		    evt.preventDefault();
		}
		
		if (element.id == "tbxFromMail"){
			document.getElementById("tbToMail").focus();
		}
		else{
			if (element.id == "tbToMail"){
				document.getElementById("tbBody").focus();
			}
			else{
				if (element.id == "tbBody"){
					//document.getElementById("btSend").focus();
				}
				else{
					if (element.id == "btSend"){	
						document.getElementById("btSend").click();
					}
				}
			}
		}
		
		
		//document.getElementById("btSend").click();
	 }
}

// 090302 dan- added for the search syle 
function seeWithStyle(newLocation) 
{
	var txtSearchValue="";
	var txtSearch = document.getElementById("txtPageSearch");
	if (txtSearch != null )
	{
		txtSearchValue = txtSearch.value;
	}
    if (txtSearchValue!='')
	{
	  
		//alert(printlink);
		var oldurl = newLocation + "?";
		var stringHref = oldurl.split("?");
		var url;
		if (stringHref[1] != "")
			url = newLocation + "&s="+encodeURIComponent(txtSearchValue);
		else
			url = newLocation+ "?s="+encodeURIComponent(txtSearchValue);
		
		//alert(url);
		location.href = url;
	}
}

function getURL(qs)
{
    var objc;    
    if (location.href.indexOf('?')>0)
      {
        objc=location.href+'&p=1';
      }
      else
        objc=location.href+'?p=1';
    if(qs != null){
        objc += '&' + qs;
    }
    window.open(objc);
}


    
function getPopUp()
{
    var obj = '/ServiceTools/TellAFriend.aspx?url='+location.href;
    openPopUp(obj, 'Send mail to a friend');

}  

function getPopUp(url, qs){
    var obj = url;
    if(qs != null && qs != ''){
        obj += '?url=' + encodeURI(location.href+'?'+qs);   
    } 
    else{
        obj += '?url=' + encodeURI(location.href);
    }
    openPopUp(obj, 'Send mail to a friend');

}
