function DropDown(iID, iClassName){
	this.Node = document.getElementById(iID);
	this.ClassName = iClassName;
	this.layer = null;
	this.lx=0; this.ly=0;
	this.lw=0; this.lh=0;
	this.init();
}

DropDown.prototype.GetLeft = function(){
	var oNode = this.Node;
	var iLeft = 0;
	while(oNode.tagName!="BODY"){
		iLeft+=oNode.offsetLeft;
		oNode = oNode.offsetParent;
	}
	return iLeft;
};

DropDown.prototype.GetTop = function(){
	var oNode = this.Node;
	var iTop = 0;
	while(oNode.tagName!="BODY"){
		iTop+=oNode.offsetTop;
		oNode = oNode.offsetParent;
	}
	return iTop;
};

DropDown.prototype.init = function(){
	var mThis = this;
	this.CreateDropDown();
	this.Node.onmouseover = function(){
		mThis.ShowDropDown();
	};
	this.layer.onmouseout = function(iEvent){
		iEvent = iEvent||window.event;
		var x = iEvent.pageX || iEvent.clientX+document.body.scrollLeft; var y = iEvent.pageY||iEvent.clientY+document.body.scrollTop;
		if(!(x>mThis.lx && x<mThis.lx+mThis.lw && y>(mThis.ly+mThis.Node.offsetHeight) && y<mThis.ly+mThis.lh)){
			mThis.Win=0;
			mThis.HideDropDown();
		}
	};
	this.Node.onmouseout = function(iEvent){
		iEvent = iEvent||window.event;
		var x = iEvent.pageX || iEvent.clientX+document.body.scrollLeft; var y = iEvent.pageY || iEvent.clientY+document.body.scrollTop;
		if(!(x>mThis.lx && x<mThis.lx+mThis.Node.offsetWidth &&  y>mThis.ly && y<mThis.ly+mThis.lh)){
			mThis.HideDropDown();
		}
	};
};

DropDown.prototype.CreateDropDown = function(){
    this.layer = document.createElement("div");
    this.layer.className = this.ClassName;
    this.layer.style.visibility = "hidden";
    document.body.appendChild(this.layer);
};


DropDown.prototype.Register = function(iText, iAddr){
	var oDiv = document.createElement("div");
	oDiv.innerHTML="<a href="+iAddr+">"+iText+"</a>";
	this.layer.appendChild(oDiv);
};

DropDown.prototype.ShowDropDown = function(){
	this.layer.style.left = this.GetLeft()+"px";
	this.layer.style.top = (this.GetTop()+this.Node.offsetHeight)+"px";
	this.lx = this.GetLeft(); this.ly = this.GetTop();
	this.lw = this.layer.offsetWidth; this.lh = (this.layer.offsetHeight+5);
	this.layer.style.visibility="visible";
};

DropDown.prototype.HideDropDown = function(){
	this.layer.style.visibility = "hidden";
};

function ShowDiv(obj, Show, Hide){
	var inside = obj.parentNode.getElementsByTagName("Div")[0];
	if(inside.style.display=="none"){
		inside.style.display="";
		if(Hide){
			 obj.value=Hide;
		}
	}else{
		inside.style.display = "none";
		if(Show){
			obj.value=Show;
		}
	}
}

function SetupXMLHttp(){
    if(typeof XMLHttpRequest != "undefined"){
        return new XMLHttpRequest();
	}else if(typeof ActiveXObject != "undefined"){
        return  new ActiveXObject("MSXML2.XmlHttp");
    }else{
        alert("No XMLHttpRequest object available.");
    }
}

function MesgController(){
	this.http = SetupXMLHttp();
}

MesgController.prototype.DispMsg = function(obj, mID){
	var inside = obj.parentNode.getElementsByTagName("Div")[0];
	if(inside.style.display=="none"){
		inside.style.display="";
		if(obj.style.fontWeight=="bold"){
			obj.style.fontWeight="";
			var oHttp = this.http;
			if(oHttp.readyState!==0){
				oHttp.abort();
			}
			var sURL = "Remote Func.php?Type=2&Input="+mID;
			oHttp.open("get", sURL, true);
			oHttp.send(null);
		}
	}else{
		inside.style.display="none";
	}
};

function SuggestionProvider(){
    this.http = SetupXMLHttp();
}

SuggestionProvider.prototype.LookUp = function(oSuggestionHandler, bTypeAhead){
	var oHttp = this.http;
	if(oHttp.readyState !== 0){
		oHttp.abort();
	}
	var sURL = "Remote Func.php?Type=1&Input=" + encodeURIComponent(oSuggestionHandler.TextBox.value);
	oHttp.open("get", sURL, true);
	oHttp.onreadystatechange = function(){
		if(oHttp.readyState==4){
			var Suggestions = eval(oHttp.responseText);
			oSuggestionHandler.AutoSuggest(Suggestions, bTypeAhead);
		}
	};
	oHttp.send(null);
};

function SuggestionHandler(iTextBox, iProvider){
	this.cur = 0;
	this.layer = null;
	this.TextBox = iTextBox;
	this.Provider = iProvider;
	this.init();
}

SuggestionHandler.prototype.init = function(){
	var mThis = this;
	this.TextBox.onkeyup = function(iEvent){
		if(!iEvent){
			iEvent = window.event;
		}
		mThis.HandleKeyUp(iEvent);
	};
	this.TextBox.onkeydown = function(iEvent){
		if(!iEvent){
			iEvent = window.event;
		}
		mThis.HandleKeyDown(iEvent);
	};
	this.TextBox.onblur = function(){
		mThis.hideSuggestions();
	};
	this.createDropDown();
};

SuggestionHandler.prototype.hideSuggestions = function(){
	this.layer.style.visibility = "hidden";
};

SuggestionHandler.prototype.highlightSuggestion = function(oSuggestionNode){
	for(var i=0;i<this.layer.childNodes.length;i++){
		var oNode = this.layer.childNodes[i];
		if(oNode == oSuggestionNode){
			oNode.className = "current";
		}else if(oNode.className=="current"){
			oNode.className = "";
		}
	}
};

SuggestionHandler.prototype.createDropDown = function(){
	this.layer = document.createElement("div");
	this.layer.className = "SuggestList";
	this.layer.style.visibility = "hidden";
	this.layer.style.width = this.TextBox.offsetWidth;
	var oThis = this;
	this.layer.onmousedown = this.layer.onmouseup = this.layer.onmouseover = function(oEvent){	
		oEvent = oEvent || window.event;
		oTarget = oEvent.target || oEvent.srcElement;
		if(oEvent.type == "mousedown"){
			oThis.textbox.value = oTarget.firstChild.nodeValue;
			oThis.hideSuggestions();
		}else if(oEvent.type == "mouseover"){
			oThis.highlightSuggestion(oTarget);
		}else{
			oThis.TextBox.focus();
		}
	};
	document.body.appendChild(this.layer);
};

SuggestionHandler.prototype.getLeft = function(){
	var oNode = this.TextBox;
	var iLeft = 0;
	while(oNode.tagName!= "BODY"){
		iLeft+=oNode.offsetLeft;
		oNode = oNode.offsetParent;
	}
	return iLeft;
};

SuggestionHandler.prototype.getTop = function(){
	var oNode = this.TextBox;
	var iTop = 0;
	while(oNode.tagName!="BODY"){
		iTop+=oNode.offsetTop;
		oNode = oNode.offsetParent;
	}
	return iTop;
};

SuggestionHandler.prototype.showSuggestions = function(Suggestions){
	if(this.TextBox.value.length>0){
		var oDiv = null;
		this.layer.innerHTML="";
		for(var i=0;i<Suggestions.length;i++){
			oDiv = document.createElement("div");
			oDiv.appendChild(document.createTextNode(Suggestions[i]));
			this.layer.appendChild(oDiv);
		}
		this.layer.style.left = this.getLeft()+"px";
		this.layer.style.top = (this.getTop()+this.TextBox.offsetHeight)+"px";
		this.layer.style.visibility="visible";
		this.cur=0;
		this.highlightSuggestion(this.layer.childNodes[this.cur]);
	}
};

SuggestionHandler.prototype.PrevSuggestion = function(){
	var cSuggestionNodes = this.layer.childNodes;
	if(cSuggestionNodes.length>0 && this.cur>0){
		var oNode = cSuggestionNodes[--this.cur];
		this.highlightSuggestion(oNode);
		this.TextBox.value = oNode.firstChild.nodeValue;
	}
};

SuggestionHandler.prototype.NextSuggestion = function(){
	var cSuggestionNodes = this.layer.childNodes;
	if(cSuggestionNodes.length>0 && this.cur < cSuggestionNodes.length-1){
		var oNode = cSuggestionNodes[++this.cur];
		this.highlightSuggestion(oNode);
		this.TextBox.value = oNode.firstChild.nodeValue;
	}
};

SuggestionHandler.prototype.AutoSuggest = function(aSuggestions, bTypeAhead){
	if(aSuggestions.length>0 && this.TextBox.value.length>0){
		if(bTypeAhead){
			this.TypeAhead(aSuggestions[0]);
		}
		this.showSuggestions(aSuggestions);
	}else{
		this.hideSuggestions();
	}
};

SuggestionHandler.prototype.TypeAhead = function(iSuggestion){
	if(this.TextBox.createTextRange || this.TextBox.setSelectionRange){
		var iLen = this.TextBox.value.length;
		this.TextBox.value = iSuggestion;
		this.SelectAhead(iLen, iSuggestion.length);
	}
};


SuggestionHandler.prototype.SelectAhead = function(iStart, iLen){
	if(this.TextBox.createTextRange){
		var oRange = this.TextBox.createTextRange();
		oRange.moveStart("character", iStart);
		oRange.moveEnd("character", iLen-this.TextBox.value.length);
		oRange.select();
	}else if(this.TextBox.setSelectionRange){
		this.TextBox.setSelectionRange(iStart, iLen);
	}
	this.TextBox.focus();
};

SuggestionHandler.prototype.HandleKeyUp = function(iEvent){
	var Key = iEvent.keyCode;
	if(Key==8 || Key==46){
		if(this.TextBox.value.length===0){
			this.hideSuggestions();
		}else{
			this.Provider.LookUp(this, false);
		}
	}else if(!(Key<32||(Key>=33 && Key<=46) || (Key>=112 && Key<=123))){
		this.Provider.LookUp(this, true);
	}
};

SuggestionHandler.prototype.HandleKeyDown = function(iEvent){
	if(iEvent.keyCode==38){
		this.PrevSuggestion();
	}else if(iEvent.keyCode==40){
		this.NextSuggestion();
	}else if(iEvent.keyCode==13){
		this.hideSuggestions();
	}
};

function LimitCheckBoxes(CheckBoxes, Limit){
	for(var i=0;i<CheckBoxes.length;i++){
		CheckBoxes[i].onclick = function(){
			var c=0;
			for(var d=0;d<CheckBoxes.length;d++) c+=(CheckBoxes[d].checked)? 1:0;
			if(c>Limit) this.checked=false;
		};
	}
};

function EditComment(id){
	var iNode = document.getElementById("CEdit"+id);
	var OldStr = iNode.innerHTML.replace(/<.*?>/g, '');
	var pNode = document.getElementById("EButton"+id);
	var aNode = document.getElementById("CEditB"+id);
	var f = "<form name=\"CForm"+id+"\" action=\"\" method=\"POST\">";
	f+= "<textarea name=\"NComment\">"+OldStr+"</textarea>";
	f+= "<input type=\"hidden\" name=\"Type\" value=\"2\">";
	f+= "<input type=\"hidden\" name=\"cID\" value=\""+id+"\">";
	f+= "</form>";
	iNode.innerHTML=f;
	var OldLnk = pNode.innerHTML;
	var f="<a href=\"#\" class=\"CButton\" id=\"CCancelB"+id+"\">Cancel</a>";
	pNode.innerHTML+=f;
	aNode = document.getElementById("CEditB"+id);
	var dNode = document.getElementById("CCancelB"+id);
	aNode.innerHTML="submit";
	aNode.onclick = function(){
		document.forms["CForm"+id].submit();
	}
	dNode.onclick = function(){
		iNode.innerHTML=OldStr;
		pNode.innerHTML=OldLnk;
	}
};

function UnDarkenPage(){
	document.body.style.background="#FFFFFF";
	document.getElementById('Page').style.opacity="1.0";
}

function DarkenPage(){
	document.body.style.background="#444444";
	document.getElementById('Page').style.opacity="0.2";
}

function ViewImage(Url){
	DarkenPage();
	var iLayer = document.createElement("div");
	iLayer.innerHTML = "<img src="+Url+">";
	iLayer.className="Overlay";
	document.body.appendChild(iLayer);
	iLayer.style.left=((document.body.clientWidth/2)-(iLayer.offsetWidth/2))+"px";
	iLayer.style.top=((document.body.clientHeight/2)-(iLayer.offsetHeight/2))+"px";
	iLayer.onclick = function(){
		UnDarkenPage();
		document.body.removeChild(iLayer);
	}
};

function ReadReview(Txt){
	DarkenPage();
	var iLayer = document.createElement("div");
	iLayer.innerHTML =Txt;
	iLayer.className="Overlay";
	document.body.appendChild(iLayer);
	iLayer.style.width="60%";
	iLayer.style.maxHeight="60%";
	iLayer.style.overflow="scroll";
	//iLayer.style.backgound="#FFFFFF";
    iLayer.style.left=((document.body.clientWidth/2)-(iLayer.offsetWidth/2))+"px";
    iLayer.style.top=((document.body.clientHeight/2)-(iLayer.offsetHeight/2))+"px";
	iLayer.onclick = function(){
		UnDarkenPage();
		document.body.removeChild(iLayer);
	}
};


