// JavaScript Document


function createRequestObject() {
	var ro;
    var browser = navigator.appName;

	if( navigator.userAgent.indexOf('Opera') != -1 ) {
        ro = new XMLHttpRequest();
	} else if(browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        ro = new XMLHttpRequest();
    }
    return ro;
}

var ajaxConnect = createRequestObject();
var inputMask = '';
var SortOrder = 'asc';
var SortColumn = 'date';
var OldSortColumn = '';
var headerImages = Array( );
var Page = 1;
var SearchField = '';
var PerPage = 20;
var MaxRecords = 0;
var isBusy = false;
var oldShows = false;

headerImages['artist'] = 'artist';
headerImages['date'] = 'date';
headerImages['city'] = 'city';
headerImages['venue'] = 'venue';



function sendLimitSearch( e,input ) {
	var KeyID = (window.event) ? event.keyCode : e.keyCode;

	SearchField = document.getElementById( 'searchBy' );

	if( input != '' && KeyID > 20 ) {
		if( !isBusy ) {
			ajaxConnect.open('get', '../../includes/ajax/getCatalouge.php?field=' + SearchField.value + '&input=' + input );
			ajaxConnect.onreadystatechange = handleLimitSearchRequest;
			
			try {
				ajaxConnect.send(null);
			} catch(e) {
				1;	
			}

		} else {
			ajaxConnect.abort();
			
			ajaxConnect.open('get', '../../includes/ajax/getCatalouge.php?field=' + SearchField.value + '&input=' + input );
			ajaxConnect.onreadystatechange = handleLimitSearchRequest;
			
			try {
				ajaxConnect.send(null);
			} catch(e) {
				1;	
			}
			
		}
	} else {
		var inputMaskPopup = document.getElementById( 'inputMaskPopup' );
		var inputMaskContent = document.getElementById( 'inputMaskContent' );		

		inputMaskPopup.style.visibility = 'hidden';
		inputMaskContent.innerHTML = '';
		
		inputMask = '';
	}


}

function selectName( name, spot ) {
	var inputMaskPopup = document.getElementById( 'inputMaskPopup' );
	var inputMaskContent = document.getElementById( 'inputMaskContent' );		
	var inputMaskVal = document.getElementById( 'inputMask' );
	var searchIndex = document.getElementById( 'searchIndex' );
	var spotHolder = document.getElementById( spot );

	inputMaskVal.value = deEntity(name.replace("\.\.\.", ""));

	searchIndex.value = spotHolder.value;

	inputMaskPopup.style.visibility = 'hidden';
	inputMaskContent.innerHTML = '';
}


function closePopUp() {
	var inputMaskPopup = document.getElementById( 'inputMaskPopup' );
	var inputMaskContent = document.getElementById( 'inputMaskContent' );		
	
	inputMaskPopup.style.visibility = 'hidden';
	inputMaskContent.innerHTML = '';
	
	inputMask = '';	
	
	return;
}

function handleLimitSearchRequest() {
	
	isBusy = true;
	
    if(ajaxConnect.readyState == 4){
        var response = ajaxConnect.responseText;

		isBusy = false;

		if( response != 'no results' && response != '' ) {
		
			var spots = '';
			var newHTML = '';
			var names = response.split("|");
			var inputMaskPopup = document.getElementById( 'inputMaskPopup' );
			var inputMaskContent = document.getElementById( 'inputMaskContent' );		
			var inputMaskVal = document.getElementById( 'inputMask' );

				newHTML = newHTML + "<div style=\"cursor: pointer; width: 183px; height: 19px; background-color: #251600;border-bottom:solid 1px #fff;color: #fff;\"  onclick=\"closePopUp();\"><img src='/template/TrueNorthRecords/images/mainSite/catalouge/closetip.gif'></div>";


			for( i = 0; i < names.length; i++ ) {
				var bgcolor = "#cfcab7";
				var art_id = names[i].split( "%" );

				if( i % 2 == 0 ) {
					bgcolor = "#b9b3a3";
				} 

				newHTML = newHTML + "<div style=\"cursor: pointer; width: 183px; height: 19px; background-color: " + bgcolor + ";border-bottom:solid 1px #fff;\"><div style=\"padding: 3px;width:177px;\" onclick=\"selectName( this.innerHTML, 'spotAl" + i + "' );\" onmouseover=\"this.style.backgroundColor = '#66cc00';this.style.color = '#fff';\" onmouseout=\"this.style.backgroundColor = '" + bgcolor + "';this.style.color = '#000'\" >" + art_id[0] + "</div></div>";
				newHTML = newHTML + "<input type='hidden' id='spotAl" + i + "' value='" + art_id[1] + "'>\n";

			}

			in_posx = findPosX( inputMaskVal );
			in_posy = findPosY( inputMaskVal );

			inputMaskPopup.style.top = ( in_posy + 25 ) + "px";
			inputMaskPopup.style.left = ( in_posx ) + "px" ;

			inputMaskContent.innerHTML = newHTML;
			inputMaskPopup.style.visibility = 'visible';

			
		} else {
			var inputMaskPopup = document.getElementById( 'inputMaskPopup' );
			var inputMaskContent = document.getElementById( 'inputMaskContent' );		

			inputMaskPopup.style.visibility = 'hidden';
			inputMaskContent.innerHTML = '';
			
			inputMask = '';
		}
		


    }
}


function DoSearchSubmit() {
	theform = document.getElementById( 'store_search' );
	SearchField = document.getElementById( 'searchBy' );
	SearchIndex = document.getElementById( 'searchIndex' );
	
	/*if( SearchField.value == 'artist' && SearchIndex.value == '' ) {
		alert( 'Please select an artist name from the pop-up menu and hit view again.' );
		return false;
	}*/
	
	theform.submit();
	
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}