Admin=0
IsIE=1
var Phase
var PageLoad
var AdminPageLoad
var Footer

HRef=location.href.toLowerCase()
onload=function () {
	$("#ContentDiv").html($("#Content").html())
	$("#Content").remove()
	
	Browser=navigator.userAgent
	if (Browser.indexOf("Safari")!=-1 || Browser.indexOf("Chrome")!=-1) {
		$("#HomeMain").parent()
			.css("white-space","pre-wrap")
			.css("overflow","hidden")
		$("#HomeUpperBoxDetails").css("overflow","normal")
	}

	if (AdminPageLoad) {
		AdminPageLoad()
	}

	if (PageLoad) {
		PageLoad()
	}

	if (navigator.appName.indexOf("Internet Explorer")==-1) {
		IsIE=0
		$("*").each(function () {
			Obj=$(this)
			if (Obj.css("text-align")=="center") {
				Obj.css("text-align","-moz-center")
			}
			/*if (this.tagName!="IMG" && Obj.css("float")=="left") {
				Obj.css("float","")
			}*/
		})
		$(".Section.ContactUs").css("width","48.1%")
	}

	$("img").each(function () {
		$(this)
			.attr("hspace","0")
			.attr("vspace","0")
	})

	$(".RemainW").each(function () {
		Obj=$(this)
		Parent=Obj.parent()
		W=Parent.width()
		Parent.children().each(function () {
			OtherObj=$(this)
			if (OtherObj.html()!=Obj.html()) {
				W-=OtherObj.outerWidth(true)
			}
		})
		Margins=0
		MarginLeft=parseInt(Obj.css("margin-left"))
		if (!isNaN(MarginLeft)) {
			Margins+=MarginLeft
		}
		MarginRight=parseInt(Obj.css("margin-right"))
		if (!isNaN(MarginRight)) {
			Margins+=MarginRight
		}
		W=W-Margins-1
		Obj.css("width",W)
	})

	$(".RemainH").each(function () {
		Obj=$(this)
		Parent=Obj.parent()
		H=Parent.height()
		Parent.children().each(function () {
			OtherObj=$(this)
			if (OtherObj.html()!=Obj.html()) {
				H-=OtherObj.outerHeight(true)
			}
		})
		Margins=0
		MarginTop=parseInt(Obj.css("margin-top"))
		if (!isNaN(MarginTop)) {
			Margins+=MarginTop
		}
		MarginBottom=parseInt(Obj.css("margin-bottom"))
		if (!isNaN(MarginBottom)) {
			Margins+=MarginBottom
		}
		H=H-Margins
		if (Obj.attr("id")=="ContentDiv" && H<450) {
			H=450
		}
		Obj.height(H)
	})

	$(".Section .Details").each(function () {
		Obj=$(this)
		Parent=Obj.parent()
		ParentH=Parent.height()
		Obj.height(ParentH-30)
	})

	$(".Brief").each(function () {
		Div=$(this)
		HTML=Div.html()
		Div.html("")
		H=Div.parent().height()-21
		Div.css("height","auto")
		Div.css("overflow","")
		SpcI=0
		while (Div.height()<H) {
			SpcI=HTML.indexOf(" ",SpcI+1)
			if (SpcI==-1) {
				break
			}
			Div.html(HTML.substring(0,SpcI))
		}
		if (SpcI==-1) {
			Div.html(HTML)
			Div.next().hide()
		} else {
			HTML=Div.html()
			SpcI=HTML.lastIndexOf(" ")
			Div.html(HTML.substring(0,SpcI)+"...")
		}
	})

	if (IsIE==0) {
		$("#ContentDiv .Section .Details")
			.css("padding","1px")
			.height("")
	}

	$("#LeftMenu li a").each(function () {
		$(this).html("<div class='InMiddle'>"+$(this).text()+"</div>")
	})

	$(".InCenter").each(function () {
		Obj=$(this)
		W=Obj.width()
		ParentW=Obj.parent().width()
		Obj.css("left",(ParentW-W)/2)
	})

	$(".InMiddle").each(function () {
		Obj=$(this)
		H=Obj.height()
		ParentH=Obj.parent().height()
		Obj.css("top",(ParentH-H)/2)
	})

	$("#ContentDiv .More").each(function () {
		Obj=$(this)
		H=Obj.height()
		Parent=Obj.parent()
		ParentH=Parent.height()
		Offset=Obj.position().top-Parent.position().top
		Obj.css("position","relative")
		Obj.css("top",ParentH-H-Offset-6)
		Obj.css("left",10)
	})
	$(".InBottom").each(function () {
		Obj=$(this)
		H=Obj.height()
		Parent=Obj.parent()
		ParentH=Parent.height()
		Offset=Obj.position().top-Parent.position().top
		ParentPad=parseInt(Parent.css("padding"))
		Obj.css("top",ParentH-H-ParentPad-Offset)
	})

	$("#TopMenu ul").each(function () {
		Parent=$(this).parent()
		ParentParent=Parent.parent()
		L1=Parent.offset().left
		L2=ParentParent.offset().left
		$(this).css("left",L1-L2*1.5)
		H1=Parent.height()
		H2=ParentParent.parent().height()
		$(this).css("top",H2/2-H1/2)
	})
	
	if (location.href.toLowerCase().indexOf("admin")==-1) {
		$("a").not("#BottomMenu a").each(function () {
			HRef=$(this).attr("href")
			if (HRef.toLowerCase().indexOf("javascript:")==-1) {
				if (HRef.indexOf("?")==-1) {
					HRef=HRef+"?EventID="+EventID
				} else {
					HRef=HRef+"&EventID="+EventID
				}
			}
			$(this).attr("href",HRef)
		})
	}

	Forms=document.forms
	for (I=0;I<Forms.length;I++) {
		with (Forms[I]) {
			method="post"
			onkeydown=function (event) {
				if (!event) {
					event=window.event
				}
				SrcEl=event.srcElement? event.srcElement : event.target
				ValKeyDn=SrcEl.value
			}
			onkeyup=function (event) {
				if (!event) {
					event=window.event
				}
				SrcEl=event.srcElement? event.srcElement : event.target
				Tag=SrcEl.tagName
				if (Tag=="TEXTAREA" || Tag=="A") {
					return
				}
				K=event.keyCode
				if (K==13) {
					ValKeyUp=SrcEl.value
					if (ValKeyDn==ValKeyUp) {
						Validate(event)
					}
				}
			}
			Els=elements
			for (J=0;J<Els.length;J++) {
				with (Els[J]) {
				 	if (id!="") {
						name=id
					}
				}
			}
		}
	}
	if (Forms[0] && Forms[0].id!="AdminFormView") {
		I=0
		while (Forms[0].elements[I].type=="hidden") {
			I++
		}
		Forms[0].elements[I].focus()
	}
	/*setTimeout(function () {
		ImgT=$("#HomeMain").position().top
		BoxT=$("#HomeUpperBox").position().top
		if (ImgT!=BoxT) {
			$("#HomeUpperBox").css("width",($("#HomeUpperBox").width-50)+"px")
		} 
	},1000)*/
}

function MenuFill(MenuName,ItemI,DbItem) {
	Cmd="SELECT ID,Name FROM "+DbItem+"s WHERE Visible=Yes ORDER BY TheOrder"
	Data=AJAXGet(Cmd)
	Menu=document.createElement("ul")
	SubMenus.push(Menu)
	Item=GetObj(MenuName).childNodes[ItemI]
	with (Item) {
		onmouseover=ItemMouseOver
		onmouseout=ItemMouseOut
		appendChild(Menu)
		with (childNodes[0]) {
			href="JavaScript:"
			style.cursor="default"
		}
	}
	Item.Alpha=0
	Item.AlphaDir=0

	for (I=0;I<Data.length;I++) {
		Row=Data[I]
		SubItem=document.createElement("li")
		if (I==0) {
			with (SubItem.style) {
				borderTopStyle=Item.style.borderTopStyle
				borderTopColor="transparent"
				
				borderLeftStyle=Item.style.borderLeftStyle
				borderLeftColor="transparent"
			}
		}
		Menu.appendChild(SubItem)

		Link=document.createElement("a")
		SubItem.appendChild(Link)
		with (Link) {
			href=DbItem+"Details.asp?ID="+Row["ID"]
			innerText=Row["Name"]
		}
	}
	return true
}

function SelectFill(SelectName,Cmd,OptText,OptVal,DefVal) {
	Select=GetObj(SelectName)
	if (Cmd) {
		OptsData=AJAXGet(Cmd)
		for (RowI=0;RowI<OptsData.length;RowI++) {
			Row=OptsData[RowI]
			Select.options[Select.options.length]=new Option(Row[OptText],Row[OptVal])
		}
	} else {
		if (SelectName.indexOf("Date")!=-1) {
			Ar=Mons
		} else {
			Ar=eval(SelectName+"s")
		}
		Select.options[Select.options.length]=new Option("--- Please select ---","")
		for (ArI=0;ArI<Ar.length;ArI++) {
			Select.options[Select.options.length]=new Option(Ar[ArI],Ar[ArI])
		}
	}
	if (DefVal) {
		Select.value=DefVal
	}
}

function DataGridFill(DataGridID) {
	DataGrid=GetObj(DataGridID)
	Cmd=DataGrid.Cmd
	Data=AJAXGet(Cmd)
	if (DataGrid.id=="AdminDataGrid") {
		Admin=1
	}
	if (Admin==1) {
		Rows=DataGrid.rows
		RowsN=Rows.length
		if (RowsN>1) {
			for (I=1;I<RowsN;I++) {
				DataGrid.deleteRow(1)
			}
		}
		if (!GetObj("AddBtn")) {
			P=document.createElement("p")
			P.style.textAlign="right"
			DataGrid.parentNode.insertBefore(P,DataGrid.nextSibling)
			PutAdminBtn(P,"Add","DataGridAdd()")
		}
		if (!Footer) {
			Footer=document.createElement("p")
			if (DataGrid.childNodes[0].tagName=="CAPTION") {
				DataGrid.childNodes[0].appendChild(Footer)
			} else {
				DataGrid.parentNode.insertBefore(Footer,DataGrid)
			}
		}
		Footer.innerText="Rows count: "+Data.length
	}
	if (Data.length==0) {
		return
	}
	with (DataGrid) {
		Head=rows[0].cells
		ColsN=Head.length
		for (I=0;I<Data.length;I++) {
			TR=insertRow()
			with (TR) {
				Val=Data[I]["ID"]
				id=Val
				if (I%2==1) {
					className="AlterRow"
				}
				for (J=0;J<ColsN;J++) {
					Type=Head[J].Type
					Field=Head[J].DataField
					Val=Data[I][Field]
					switch (Type) {
						case "Text":
							Obj=document.createElement("div")
							Obj.innerHTML=Val
							break
						case "CheckBox":
							Obj=document.createElement("input")
							with (Obj) {
								type="checkbox"
								disabled=1
							}
							break
						case "Options":
							Obj=document.createElement("span")
							PutAdminBtn(Obj,"Edit","DataGridEdit()")
							if (DataGrid.AllowSort=="True") {
								PutAdminBtn(Obj,"Up","ChangeOrder(-1)")
								PutAdminBtn(Obj,"Down","ChangeOrder(1)")
							}
							PutAdminBtn(Obj,"Delete","DataGridDelete()")
							break
					}
					Cell=insertCell()
					if ((Type=="Text" || Type=="TextArea") && DataGridID=="AdminDataGrid") {
						Obj.style.width=(Cell.offsetWidth-4)+"px"
					}
					Cell.appendChild(Obj)
					if (Type=="CheckBox" && Val=="True") {
						Obj.checked=1
					}
				}
			}
		}
	}
}

function GetObj(ObjName) {
	return document.getElementById(ObjName)
}

function FindObj(ObjName,ParentObj) {
	Children=ParentObj.childNodes
	for (I=0;I<Children.length;I++) {
		if (Children[I].id==ObjName) {
			return Children[I]
		} else {
			return FindObj(ObjName,Children[I])
		}
	}
}

function FindSpecParent(Obj,Tag) {
	while (Obj.tagName!=Tag) {
		Obj=Obj.parentNode
	} 
	return Obj
}

function PosX(Obj) {
	X=0
	do {
		X+=Obj.offsetLeft
		Obj=Obj.offsetParent
	} while (Obj!=null)
	return X
}

function PosY(Obj) {
	Y=0
	do {
		Y+=Obj.offsetTop
		Obj=Obj.offsetParent
	} while (Obj!=null)
	return Y
}

function DisableLink(Obj) {
	Code=Obj.outerHTML()
	Code=Code.replace("<a","<label")
	Code=Code.replace("<A","<label")
	Code=Code.replace("</a","</label")
	Code=Code.replace("</A","</label")
	Obj.outerHTML(Code)
}

function EnableLink(Obj) {
	Code=Obj.outerHTML()
	Code=Code.replace("<label","<a")
	Code=Code.replace("<LABEL","<a")
	Code=Code.replace("</label","</a")
	Code=Code.replace("</LABEL","</a")
	Obj.outerHTML(Code)
}

jQuery.fn.outerHTML = function(s) {
return (s)
? this.before(s).remove()
: jQuery("<p>").append(this.eq(0).clone()).html();
}
