/*
+----------------------------------------------------------------------------------------------------------------------+
| Copyright (c) 2006-2007 Andrew G. Samoilov, Universal Data Solutions inc.			|
| All rights reserved.                                                  							|
|                                                                       									|
| Redistribution and use in source and binary forms, with or without    				|
| modification, are permitted provided that the following conditions    				|
| are met:                                                              								|
|                                                                       									|
| o Redistributions of source code must retain the above copyright      				|
|   notice, this list of conditions and the following disclaimer.       					|
| o Redistributions in binary form must reproduce the above copyright   				|
|   notice, this list of conditions and the following disclaimer in the 					|
|   documentation and/or other materials provided with the distribution.				|
|                                                                       									|
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   |
| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     	|
| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  	|
| OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 	|
| SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      	|
| LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 	|
| DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   	|
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  	|
+----------------------------------------------------------------------------------------------------------------------+
*/

//var hide  = true;		
var cur = -1;
var selectsToHide = new Array();
var suggestValues = new Array();
var lookupValues = new Array();
var detailsCounter = 0;
var isLookupError = false;
var isSetFocus = false;
var isMouseOnDiv = false;

$(document).ready(function(){ 
	$("#search_suggest").mouseover(function(){ isMouseOnDiv = true; });
	$("#search_suggest").mouseout(function(){ isMouseOnDiv = false; });
	$("#ctlSearchFor").blur(function(){ if (!isMouseOnDiv) { DestroySuggestDiv(); }	});
	$("input[@name*=value_]").blur(function(){ if (!isMouseOnDiv) { DestroySuggestDiv(); } });
});

$(document).click(function(){ 
	DestroySuggestDiv();
});
		
function myEncode(value)
{
	if ( value ) {
		value = value.replace(/:/g,"%3A");
		value = value.replace(/=/g,"%3D");
		value = value.replace(/&/g,"%26");
		value = value.replace(/\//g,"%2F");
		value = value.replace(/\?/g,"%3F");
		value = value.replace(/\s/g,"%20");
	}
	return value;
}

Array.prototype.inArray = function (value,caseSensitive)
{
	var i;
	for (i=0; i < this.length; i++) {
		if (caseSensitive) {
			if (this[i] == value) { return true; }
		} else {
			if (this[i].toLowerCase() == value.toLowerCase()) { return true; }
		}
	}
	return false;
};

function DestroySuggestDiv() 
{
	cur = -1;
	isMouseOnDiv = false;
	$("#search_suggest").html("");
	$("#search_suggest").css({ visibility: "hidden"});
	$.each( selectsToHide, function(i, n){
		n.style.visibility = 'visible';
	});
	selectsToHide.splice(0,selectsToHide.length);
}

function PtInBox(oElement) 
{
	var bFlag = false;
	var el = $("#search_suggest")[0];
	var left = findPos(oElement)[0];
	var top = findPos(oElement)[1];
	var width = findPos(oElement)[2];
	var height = findPos(oElement)[3];
	
	if ( left >= el.offsetLeft && left <= (el.offsetLeft+el.offsetWidth) && top >= el.offsetTop && top <= (el.offsetTop+el.offsetHeight) ) { bFlag = true; }
	if ( (left+width) >= el.offsetLeft && (left+width) <= (el.offsetLeft+el.offsetWidth) && top >= el.offsetTop && top <= (el.offsetTop+el.offsetHeight) ) { bFlag = true; }
	if ( left >= el.offsetLeft && left <= (el.offsetLeft+el.offsetWidth) && (top+height) >= el.offsetTop && (top+height) <= (el.offsetTop+el.offsetHeight) ) { bFlag = true; }
	if ( (left+width) >= el.offsetLeft && (left+width) <= (el.offsetLeft+el.offsetWidth) && (top+height) >= el.offsetTop && (top+height) <= (el.offsetTop+el.offsetHeight) ) { bFlag = true; }
	if ( ( left <= el.offsetLeft && (left+width) >= (el.offsetLeft+el.offsetWidth) ) && ( (el.offsetTop+el.offsetHeight) >= top && el.offsetTop <= (top+height) ) ) { bFlag = true; }

	if ( bFlag ) {
		return true;
	}
	return false;
}

function showhide(obj,hide)
{
//	hide = !hide;
	$("#master_details").css("visibility",(hide) ? "hidden" : "visible");
	setLyr(obj,"master_details");
}

function setLyr(obj,lyr)
{
	var coors = findPos(obj);
	if (lyr == 'master_details') coors[0] += coors[2];
	if (lyr == 'search_suggest') coors[1] += coors[3];
	$("#"+lyr).css("top",coors[1] + "px");
	$("#"+lyr).css("left",coors[0] + "px");
}	

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		curwidth = obj.offsetWidth
		curheight = obj.offsetHeight
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop,curwidth,curheight];
}
	
function moveUp(oElement, searchType) 
{
	if($("#search_suggest").children().length>0 && cur>=-1)
	{
		cur--;
		if (cur==-2) { cur=$("#search_suggest").children().length-1; oElement.focus(); }
		for(var i=0;i<$("#search_suggest").children().length;i++)
		{
			if(i==cur)
			{
				$("#search_suggest").children().get(i).className = "suggest_link_over";
				oElement.value = suggestValues[cur].replace(/\<(\/b|b)\>/gi,"");
				if ( searchType == 'lookup' ) 
				{ 
					isLookupError = false;
					$(oElement).removeClass("highlight");
					var helement=$("#"+oElement.id.substring(8))[0];
					if ( $(helement).val() != lookupValues[cur] ) {
						$(helement).val(lookupValues[cur]);
						$(helement).change();
					}
				}
			}
			else
			{
				$("#search_suggest").children().get(i).className = "suggest_link";
			}
		}
	}
}
		
function moveDown(oElement, searchType) 
{
	if($("#search_suggest").children().length>0 && cur<($("#search_suggest").children().length))
	{
		cur++;
		for(var i=0;i<$("#search_suggest").children().length;i++)
		{
			if(i==cur)
			{
				$("#search_suggest").children().get(i).className = "suggest_link_over";
				oElement.value = suggestValues[cur].replace(/\<(\/b|b)\>/gi,"");
				if ( searchType == 'lookup' ) 
				{ 
					isLookupError = false;
					var helement=$("#"+oElement.id.substring(8))[0];
					$(oElement).removeClass("highlight");
					if ( $(helement).val() != lookupValues[cur] ) 
					{
						$(helement).val(lookupValues[cur]);
						$(helement).change();
					}
				}
			}
			else
			{
				$("#search_suggest").children().get(i).className = "suggest_link";
			}
		}
		if (cur==($("#search_suggest").children().length)) { cur=-1; oElement.focus(); }
	}
}

function suggestOver(div_value) 
{
	$("div.suggest_link_over").each(function(){
		this.className = 'suggest_link';
	}) ;
	div_value.className = 'suggest_link_over';
	cur = div_value.id.substring(10);
}

function suggestOut(div_value) 
{
	div_value.className = 'suggest_link';
}

function setSearch(inputName, value) 
{
	if (setSearch.arguments[2] == 'lookup') {
		isLookupError = false;
		var helement=$("#"+inputName.substring(8)+setSearch.arguments[4])[0];
		$("#"+inputName+setSearch.arguments[4]).removeClass("highlight");
		$("#"+inputName+setSearch.arguments[4]).val(value);
		if ( $(helement).val() != setSearch.arguments[3] )
		{
			$(helement).val(setSearch.arguments[3]);
			$(helement).change();
		}
	}
	else
		$("input[@type=text][@name="+inputName+"]").val(value);
	DestroySuggestDiv();
}

function showDetailsPreview(obj, str) 
{
	detailsCounter++;
	$("#master_details").html("");
	$.get(str, 
	{
		counter: detailsCounter,
		rndVal: (new Date().getTime())
	}, 
	function(txt){ 
		var str = txt.split("counterSeparator");
		if ( detailsCounter == str[1] ) { 
			$("#master_details").html(str[0]); 
			if ( $.browser.mozilla && ( $("#master_details").get(0).offsetTop > window.innerHeight/2 + document.body.scrollTop ) ) {
				$("#master_details").css("top", ($("#master_details").get(0).offsetTop - $("#master_details").get(0).offsetHeight) +"px");
			} else if ( $.browser.msie && ( $("#master_details").get(0).offsetTop > document.body.clientHeight/2 + document.body.scrollTop ) ) {
				$("#master_details").css("top", ($("#master_details").get(0).offsetTop - $("#master_details").get(0).offsetHeight) +"px");
			}
			showhide(obj,false);
		}
	});
};		

function hideDetailsPreview(obj) 
{
	showhide(obj,true);
	$("#master_details").html("");
};			

function listenEvent(oEvent,oElement,searchType) 
{
	oEvent=window.event || oEvent;
	iKeyCode=oEvent.keyCode;

	switch(iKeyCode)
	{	
		case 38: //up arrow
			moveUp(oElement,searchType);
			break;
		case 40: //down arrow
			moveDown(oElement,searchType);
			break;
		case 13: //enter 
			//if (oElement.value != undefined || oElement.value != "") {
			//setSearch(oElement.name, oElement.value, searchType);
			DestroySuggestDiv();
			if ( searchType == 'ordinary' ) { RunSearch(); return false; } 
			if ( searchType == 'advanced' || searchType == 'advanced1' ) { document.forms[0].submit(); return false;}
			if ( searchType == 'lookup' ) { return false; } 
			//}
			break;
	}
	return true;
}

function searchSuggest(oEvent,oElement,searchType) 
{
	oEvent=window.event || oEvent;
	iKeyCode=oEvent.keyCode;
	
	switch(searchType)
	{
		case "ordinary":
			fieldForSearch = $("select#ctlSearchField").val();
			break;
		case "advanced":
			fieldForSearch = oElement.name.substring(6);
			break;
		case "advanced1":
			fieldForSearch = oElement.name.substring(7);
			break;
	}

	if (((iKeyCode >= 65) && (iKeyCode <= 90)) || ((iKeyCode >= 48) && (iKeyCode <= 57))
		|| ((iKeyCode >= 96) && (iKeyCode <= 105)) || (iKeyCode==8) || (iKeyCode==46) || (iKeyCode==32)
		|| (iKeyCode==222)) {
		cur = -1;
		$.get(SUGGEST_TABLE,
		{
			searchFor: myEncode( oElement.value ), 
			searchField: myEncode( fieldForSearch ),
			rndVal: (new Date().getTime())
		},
		function(txt){
			$("#search_suggest").html("");
			var str = txt.split("\n");
			for(i=0,j=0; i < str.length-1; i++,j++) {
				var suggest = '<div id="suggestDiv'+i+'" style="cursor:pointer;" onmouseover="suggestOver(this);" ';
				suggest += 'onmouseout="suggestOut(this);" ';
				suggest += 'onclick="setSearch(\'' + oElement.name + '\',suggestValues[' + j + '].replace(/\\<(\\/b|b)\\>/gi,\'\'));" ';
				suggest += 'class="suggest_link">' + str[i] + '</div>';
				$(suggest).appendTo("#search_suggest");
				suggestValues[j] = str[i];
			}
			if (txt) {
				$("select").each(function(){
					if ( PtInBox(this) ) {
						selectsToHide[selectsToHide.length] = this;
						this.style.visibility = 'hidden';
					}
				});
				$("#search_suggest").css({ visibility: "visible"});
			} else {
				DestroySuggestDiv();
			}
		});
	}
	setLyr(oElement,"search_suggest");
}

function showHideLookupError(oElement)
{
	if ( isLookupError ) {
		$(oElement).addClass("highlight");
	} else {
		$(oElement).removeClass("highlight");
	}
}

function lookupSuggest(oEvent,oElement, lpValue,record_id) 
{
	if(record_id!="")
		record_id="_"+record_id
	oEvent=window.event || oEvent;
	iKeyCode=oEvent.keyCode;
	var helement=$("#"+oElement.name.substring(8)+record_id)[0];
	
	if (((iKeyCode >= 65) && (iKeyCode <= 90)) || ((iKeyCode >= 48) && (iKeyCode <= 57))
		|| ((iKeyCode >= 96) && (iKeyCode <= 105)) || (iKeyCode==8) || (iKeyCode==46) || (iKeyCode==32)
		|| (iKeyCode==222)) {
		cur = -1;
		
		if (oElement.value == "") {
			DestroySuggestDiv();
			isLookupError = false;
//			$("input[@name="+oElement.name+"]").removeClass("highlight");
			$(oElement).removeClass("highlight");
			$(helement).val(""); 
			$(helement).change();
			return;
		}
		
		$.get(SUGGEST_LOOKUP_TABLE,
		{
			searchFor: myEncode( oElement.value ), 
			searchField: myEncode( oElement.name.substring(14) ),
			lookupValue: myEncode( lpValue ),
			rndVal: (new Date().getTime())
		},
		function(txt){
			$("#search_suggest").html("");
			var str = txt.split("\n");
			$.each( str, function(i, n){
				str[i] = unescape(n);
			})
						
			if (str.inArray(oElement.value,false)) {
				isLookupError = false;
//				$("input[@name="+oElement.name+"]").removeClass("highlight");
				$(oElement).removeClass("highlight");
				$.each( str, function(i, n){
					if((n.toLowerCase()==oElement.value.toLowerCase()) && ($(helement).val()!=str[i-1] )) { 
						$(helement).val(str[i-1]); 
						$(helement).change();
					}
				});
			} else {
				isLookupError = true;
				if ( !isSetFocus ) 
//					$("input[@name="+oElement.name+"]").addClass("highlight");
					$(oElement).addClass("highlight");
			}
			
			for(i=0,j=0; i < str.length-1; i=i+2,j++) {
				var suggest = '<div id="suggestDiv'+i+'" style="cursor:pointer;" onmouseover="javascript:suggestOver(this);" ';
				suggest += 'onmouseout="javascript:suggestOut(this);" ';
				suggest += 'onclick="javascript:setSearch(\'' + oElement.name + '\',suggestValues[' + j + '],\'lookup\',\'' + str[i] + '\',\''+record_id+'\');" ';
				suggest += 'class="suggest_link">' + str[i+1] + '</div>';
				$(suggest).appendTo("#search_suggest")
				suggestValues[j] = str[i+1];
				lookupValues[j] = str[i];
			}
			if (txt) {
				$("select").each(function(){
					if ( PtInBox(this) ) {
						selectsToHide[selectsToHide.length] = this;
						this.style.visibility = 'hidden';
					}
				});
				if ( isSetFocus ) { $("#search_suggest").css({ visibility: "visible"}); }
			} else {
				DestroySuggestDiv();
			}
		});
	}
	setLyr(oElement,"search_suggest");
}

function preloadSelectContent(txt, selectControl, selectValue, record_id) 
{
	if(record_id!="")
		record_id="_"+record_id
	$('#'+selectControl+record_id).get(0).options[0]=new Option('Please select','');
	var str = txt.split('\n');
	var index = 0;
	for(i=0,j=0; i < str.length - 1; i=i+2, j++) {
		$('#'+selectControl+record_id).get(0).options[j+1]=new Option(unescape(str[i+1]),unescape(str[i]));
		if ( unescape(str[i]) == selectValue ) {index = j+1;}
	}
	$('#'+selectControl+record_id).get(0).selectedIndex = index;
	if ( j == 1 ) { $('#'+selectControl+record_id).get(0).selectedIndex = 1; }
}	

function loadSelectContent(main_field, dependent_field, record_id) 
{
	if(record_id!="")
		record_id="_"+record_id
	$.get(AUTOCOMPLETE_TABLE, 
	{
		field: myEncode( dependent_field ),
		value: myEncode( $('#value_'+main_field+record_id).val() )
	}, 
	function(txt){
		$('#value_'+dependent_field+record_id).html('<option value="">Please select</option>');
		var str = txt.split('\n');
		var index = 0;
		for(i=0,j=0; i < str.length - 1; i=i+2, j++) {
			$('#value_'+dependent_field+record_id).get(0).options[j+1]=new Option(unescape(str[i+1]),unescape(str[i]));
		}
		$('#value_'+dependent_field+record_id).get(0).selectedIndex = index;
		if ( j == 1 ) { $('#value_'+dependent_field+record_id).get(0).selectedIndex = 1; }
		$('#value_'+dependent_field+record_id)[0].onchange();
	});
}
