/*FUNCTIONS for legends club nov 09
By Totok Maryanto*/

var nReload = 6;
//var URLsite = "http://localhost/legendsclub/";
var URLsite 	= "http://www.legendsclub.com.au/";

//curvy corner var for hidden error alert message
var curvyCornersVerbose = false;

function trim(str) {
  if(str) {
    str = str.replace(/^\s+/, "");
    str = str.replace(/\s+$/, "");
  }
  return str;
}

function checkEvents(showalert,inckeyword,basepage){
	var location = document.getElementById('location').value;
	var categories = document.getElementById('Etype').value;
	var month = document.getElementById('Emonth').value;
	if (inckeyword) var keyword = document.getElementById('keyword').value;
	var year,status;
	
	if (inckeyword){
		if (trim(month)!=0)	status = true;
		else status = false;
	}
	else {
		if (trim(location)!=0 && trim(categories)!=0 && trim(month)!=0) status = true;
		else status = false;
	}

		if (status) {
			spliter = month.split("-");
			month = trim(spliter[0]);
			year = trim(spliter[1]);
	
			if (typeof(keyword)=="undefined") keyword = '';
			window.location = basepage + '?page=eventscalendar&event_cat='+ categories +'&event_loc='+ location +'&keyword='+ keyword +'&month=' + month + '&year=' + year;
		}
		else {
			message = '';
			if (!inckeyword){
				if (location==0 && showalert==true) message += 'Please Choose Location !\n';
				if (categories==0 && showalert==true) message += 'Please Choose Categories !\n';
			}
			if (month==0 && showalert==true) message += 'Please Choose Month !\n';
			if (showalert) alert(message);
			return false;
		}

}

function openProgram(name,title,width,height){
	window.open(name,title,'resizable=no,scrollbars=no,left=' + (screen.width-width)/2 +  ',top=' + (screen.height-height)/2 + ',width=' + width + ',height=' + height + ',toolbar=no,status=no');
}

function closeMe(){
	window.open('','_parent','');
	window.close();
}

function NewVerifyImage()
{
    if (nReload <= 2)
        if (nReload <= 0)
        {
            alert("Sorry, too many reloads.");
            return;
        }
        else
            alert("Only " + nReload + " more reloads are allowed");
    nReload--;
    date = new Date();
    document.vimg.src = URLsite + "lib/jhfcaptcha.php?function=captchaimage&t="+date;

}

function openvideo(video,width,height){ 
	ajaxwin=dhtmlwindow.open("newsbox", "iframe", "eclipse_video.php?video="+video+"&width="+width+"&height="+height, "News Video", "width="+ (width + 20) + "px,height=" + (height+20) + "px,left="+ (screen.width-width)/2 + "px,top=" + (screen.height-height)/2 + "px,resize=1,scrolling=1")
	//ajaxwin=dhtmlwindow.open("newsbox", "ajax", "eclipse_video.php?video="+video+"&width="+width+"&height="+height, "News Video", "width="+ width + "px,height=" + height + "px,left="+ (screen.width-width)/2 + "px,top=" + (screen.height-height)/2 + "px,resize=1,scrolling=1")
	ajaxwin.onclose=function(){return window.confirm("Close Video Window ?")}
}

function createRightBox(){
	var eDiv=document.createElement("div");
	var htmlBox=document.getElementById('RightContents').innerHTML;
	//var bodyit=document.body;
	var bodyit=document.getElementById('PrintContent');
	
	eDiv.innerHTML = htmlBox;
	eDiv.setAttribute('class','RightContents');
	eDiv.setAttribute('id','RightBox');
	//eDiv.style.display="none";
	eDiv.style.position="absolute";
	eDiv.style.top="-1000px";
	eDiv.style.left="-1000px";
	eDiv.style.visibility="hidden";
	bodyit.appendChild(eDiv);
}

function removeRightBox(){
	var obj=document.getElementById('RightBox');
	//var bodyit=document.body;
	var bodyit=document.getElementById('PrintContent');
	try {
		bodyit.removeChild(obj);
	}
	catch(err){
		//alert(err.description);
	}
}

function arrangeLayout(loopVar,ajaxTimer){	
	var browser = navigator.userAgent;
	var theight = document.getElementById('LeftContentsHomePage').offsetHeight;	
	//var oriheight = document.getElementById('RightContents').offsetHeight;
	var oriwidth = document.getElementById('RightContents').offsetWidth;
	var leftheight = document.getElementById('SideBar').offsetHeight;
	var myTimer = ajaxTimer/1000;
	
	//browser is Opera, Firefox, Safari, SeaMonkey, etc (not IE) 
	if(browser.indexOf("MSIE")==-1){
		if(loopVar==0 || loopVar>myTimer){
			removeRightBox();
			createRightBox();
			if(loopVar>myTimer) loopVar=0;
		}
		
		//document.getElementById('RightBox').style.height = oriheight + "px";
		//alert(document.getElementById('RightBox').innerHTML);
		var htmlBox=document.getElementById('RightContents').innerHTML;
		var obj = document.getElementById('RightBox');
		var curheight = obj.offsetHeight;
		obj.style.width = oriwidth + "px";
		obj.innerHTML = htmlBox;		 
		//var curheight = document.getElementById('RightBox').style.height;
		//alert(curheight);alert(theight);alert(leftheight);				
		if(parseInt(theight)>=parseInt(curheight) && parseInt(theight)>=parseInt(leftheight))
			document.getElementById('RightContents').style.height=theight + "px";
		else if(parseInt(leftheight)>=parseInt(curheight) && parseInt(leftheight)>=parseInt(theight))
			document.getElementById('RightContents').style.height=leftheight + "px";
		else 
			document.getElementById('RightContents').style.height=curheight + "px";
			
		var t=setTimeout("arrangeLayout("+(loopVar+1)+","+ajaxTimer+")",1000);
	}	
}

var t;

function doFixing(mytime){
	if(!mytime)
		var mytime = 50; 	
	
	clearTimeout(t);
	t=setTimeout("doFixing()",mytime);
	fixSomeElements();	
}

function fixSomeElements_old(){
	var theight = document.getElementById('LeftContentsHomePage').offsetHeight;	
	var oriheight = document.getElementById('RightContents').offsetHeight;
	var leftheight = document.getElementById('SideBar').offsetHeight;
	var mainheight = document.getElementById('Main').offsetHeight;
	var contentheight = document.getElementById('Content').offsetHeight;
	var setheight=0;
	/*alert('theight:'+theight);
	alert('oriheight:'+oriheight);
	alert('leftheight:'+leftheight);
	alert(setheight);
	alert(document.getElementById('LeftContentsHomePage').style.height);
	alert(document.getElementById('RightContents').style.height);
	alert(document.getElementById('SideBar').style.height);*/
	if(parseInt(oriheight) >= parseInt(theight)){
		setheight = oriheight;
	}		
	else if(parseInt(theight) > parseInt(oriheight)) {
		setheight = theight;
	}
	
	if(parseInt(setheight) < parseInt(leftheight)){
		setheight = leftheight;
	}
	
	//alert(setheight);
	document.getElementById('LeftContentsHomePage').style.height=setheight + "px";
	document.getElementById('RightContents').style.height = setheight + "px";
	document.getElementById('SideBar').style.height = setheight + "px";
	
	//clearTimeout(t);
}

function $_(objName){
	return (document.getElementById(objName))?document.getElementById(objName):false;
}

function fixSomeElements(){	
	//fix height of content
	//left content
	if($_('loginBox')) {
		var loginheight = $_('loginBox').offsetHeight;
		var logintop = $_('loginBox').offsetTop;
	} 
	else {
		var loginheight=0; var logintop=0; 
	} 
	if($_('categoryBox')) {
		var catheight = $_('categoryBox').offsetHeight;
		var cattop = $_('categoryBox').offsetTop;	
	} 
	else {
		var catheight=0; var cattop=0;	
	}
	if($_('partnerBox')) {
		var partnerheight = $_('partnerBox').offsetHeight;
		var partnertop = $_('partnerBox').offsetTop;	
	}
	else {
		var partnerheight=0; var partnertop=0;	
	} 
	
	//main content
	if($_('EventDetail')) {
		var theight = $_('EventDetail').offsetHeight;
		var ttop = $_('EventDetail').offsetTop;	
	}
	else {
		var theight=0; var ttop=0;	
	} 
	if($_('EDtitle')) {
		var oriheight = $_('EDtitle').offsetHeight;
		var oritop = $_('EDtitle').offsetTop;
	} 
	else {
		var oriheight=0; var oritop=0;	
	}
	if($_('StorePath')) {
		var leftheight = $_('StorePath').offsetHeight;
		var lefttop = $_('StorePath').offsetTop;
	}	
	else {
		var leftheight=0; var lefttop=0;	
	} 
	
	//right content
	if($_('EventsDetail'))	{
		var detheight = $_('EventsDetail').offsetHeight;
		var dettop = $_('EventsDetail').offsetTop;
	}
	else {
		var detheight=0; var dettop=0;
	}
	if($_('SearchEvents'))	{
		var searchheight = $_('SearchEvents').offsetHeight;
		var searchtop = $_('SearchEvents').offsetTop;
	}
	else {
		var searchheight=0; var searchtop=0;	
	}
	if($_('RelatedEvents'))	{
		var relheight = $_('RelatedEvents').offsetHeight;
		var reltop = $_('RelatedEvents').offsetTop;	
	}
	else {
		var relheight=0; var reltop=0;	
	}
	if($_('UpcomingEvents')) {
		var upheight = $_('UpcomingEvents').offsetHeight;
		var uptop = $_('UpcomingEvents').offsetTop;	
	}
	else {
		var upheight=0; var uptop=0;	
	}
	
	var mainheight = 0;	var sideheight = 0;	var setheight=0;
	
	sideheight = parseInt(loginheight) + parseInt(catheight) + parseInt(partnerheight) + 20;
	setheight = parseInt(theight) + parseInt(oriheight) + parseInt(leftheight) + 20;
	rightheight = parseInt(detheight) + parseInt(searchheight) + parseInt(relheight) + parseInt(upheight) + 20;	
	
	if(rightheight >= setheight)
		mainheight = rightheight;
	else
		mainheight = setheight;
		
	if(sideheight > mainheight)
		mainheight = sideheight;
	
	document.getElementById('LeftContentsHomePage_Loader').style.height=mainheight + "px";
	document.getElementById('RightContents_Loader').style.height = mainheight + "px";
	document.getElementById('SideBar_Loader').style.height = mainheight + "px";
	
	//fix right box position
	if($_('EventsDetail'))	{
		var searchcurrtop = detheight+dettop;		
	}
	else var searchcurrtop=0;
		
	if($_('SearchEvents'))	{
		var relcurrtop = parseInt(searchcurrtop) + searchheight;
		document.getElementById('SearchEvents').style.top=searchcurrtop + "px";		
	}
	else var relcurrtop=parseInt(searchcurrtop);
	
	if($_('RelatedEvents')) {
		var upcurrtop = parseInt(relcurrtop) +  relheight;
		document.getElementById('RelatedEvents').style.top = relcurrtop + "px";
	}
	else var upcurrtop=relcurrtop;
	
	if($_('UpcomingEvents')) {
		document.getElementById('UpcomingEvents').style.top = upcurrtop + "px";
	}
	
	//fix left box position
	if($_('loginBox')) {
		var categorycurrtop = loginheight+logintop;
	}
	else var categorycurrtop=0;
	
	if($_('categoryBox')) {
		var partnercurrtop = parseInt(categorycurrtop)+catheight;
		document.getElementById('categoryBox').style.top=categorycurrtop + "px";	
	}
	else var partnercurrtop=parseInt(categorycurrtop);
	
	if($_('partnerBox')) {
		document.getElementById('partnerBox').style.top=partnercurrtop + "px";	
	}
	
	//fix header logo left position
	if($_('Header_Loader')) {
		var loaderleft = $_('Header_Loader').offsetLeft;
		document.getElementById('Header_Logo').style.left=(loaderleft+33) + "px";
		document.getElementById('SearchBox').style.left=(loaderleft+650) + "px";
	} 
	
	//fix main content
	if($_('StorePath')) {
		var storecurrtop = leftheight+lefttop;	
	}
	else var storecurrtop = 160;

	if($_('EDtitle')) {
		var titlecurrtop = parseInt(storecurrtop)+oriheight;
			
		document.getElementById('EDtitle').style.top=storecurrtop + "px";	
	}
	else var titlecurrtop = parseInt(storecurrtop);
	
	if($_('EventDetail')) {
		document.getElementById('EventDetail').style.top=titlecurrtop + "px";
	}
	
	//clearTimeout(t);
}

function checkCharacterCode(e){ //e is event object passed from function invocation
	var characterCode //literal character code will be stored in this variable
	
	if(e && e.which){ //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
	}
	
	return characterCode;
}
