/*
Dynamically changing class name in javascript

document.getElementById("element").class = "new Style";
or
document.getElementById('changeme').className = "new Style";
or
this.style.className = 'someclassname';
Can't forget the style object.
And the 'N' in className must be capitalized.
*/

var t;
var h_count = 0;
var max_count = 0;
function rotateHeaders()
{
	rotateYN = document.getElementById('headerYN').value;
	if (rotateYN == 'Y')
	{	
		
		headers_data = document.getElementById('headers').value;
		var headers = headers_data.split('-');
		max_count = headers.length;
		dis_header = headers[h_count].split(',');

		h_count++;
		if (h_count == max_count)
			h_count = 0;
			
		//ID('header_id').style.display = "none";
		//$("#header_id").fadeOut("slow");
		
		$('#header_id').fadeOut('slow', function() {
			document.getElementById('header_id').style.backgroundImage = 'url("'+dis_header[1]+'")';
			$("#header_id").fadeIn('slow');



			if (dis_header[2])
			{
				eval("document.getElementById('header_id').onclick = function(){goToURL('"+dis_header[0]+"','"+dis_header[2]+"');};");
			}

			document.getElementById('h_title').value = dis_header[3];
			setTimeout("rotateHeaders()", 8000);
		  });

		/*
		document.getElementById('header_id').style.backgroundImage = 'url("'+dis_header[1]+'")';
		$("#header_id").fadeIn("slow");

		

		if (dis_header[2])
		{
			eval("document.getElementById('header_id').onclick = function(){goToURL('"+dis_header[0]+"','"+dis_header[2]+"');};");
		}

		document.getElementById('h_title').value = dis_header[3];
		setTimeout("rotateHeaders()", 8000);
		*/
	}
}

function ID(id)
{
	return document.getElementById(id);
}

function goToURL(url,task)
{
	if (task == 'home')
		window.location='index.php';
	else
		window.location='index.php?'+task+'+'+url;
}

function hover()
{
	var o2 = document.getElementById('title_id');
	var x = 175;
	var y = 0;

	o2.style.left = x+'px';
	o2.style.top = y+'px';

	var h_title = document.getElementById('h_title').value;
	if (h_title != '')
	{
		document.getElementById('title_id').innerHTML = h_title;
		document.getElementById('title_id').style.visibility = 'visible';
	}

}

function hoverOut()
{
	document.getElementById('title_id').style.visibility = 'hidden';
}

function goToURLNewPage(url)
{
	window.open(url);
}

function loginSubmit()
{
	var name = document.getElementById('user_name').value;
	var pass = document.getElementById('user_pass').value;
	var error = '';

	if (name == '')
		error += 'Please enter username.\r\n';
	if (pass == '')
		error += 'Please enter password.\r\n';

	if (error == '')
		document.getElementById('loginForm').submit();
	else
		alert(error);
}

function makeSelected(task)
{

	var a = 0;
	var pos = -1;
	while(lvl1_arr[a])
	{
		var attchild = '';
		if(lvl1_arr[a].text == task)
		{
			pos = a;
			break;
		}
		a++;
	}
	if(pos != -1)
	{
		pos = pos+1;
		var left = "bt_bot_left_"+pos;
		var middle = "bt_bot_loop_"+pos;
		var right = "bt_bot_right_"+pos;
		document.getElementById(left).style.background="url('img/template/top_menu_left_sl.gif')";
		document.getElementById(middle).style.background="url('img/template/top_menu_loop_sl.gif')";
		document.getElementById(right).style.background="url('img/template/top_menu_right_sl.gif')";
	}

}


function loadLevel2(link)
{
	var link_data = 'bottom_menu';

	var outputhtml = '<table cellpadding="0" cellspacing="0" style="height:15px"><tr><td width="18px">&nbsp;</td>';

	var b = 0;
	var childcounter = 0;

	while(lvl2_arr[b])
	{
		var attchild = '';
		if(lvl2_arr[b].parent_text == link)
		{
			if(childcounter > 0)
				outputhtml += '<td>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;</td>';

			if(lvl2_arr[b].url != '')
				attchild += ' onclick="window.location=\''+lvl2_arr[b].url+'\'" ';

			if(lvl2_arr[b].code != undefined)
			{
				//alert(lvl2_arr[b].code );
				attchild += ' onmouseover="loadLevel3(this,\''+lvl2_arr[b].code+'\');"';
			}

			outputhtml += '<td '+attchild+' style="cursor:pointer"  class="menu_lvl2">'+lvl2_arr[b].text.toLowerCase()+'</td>';
			childcounter++;
		}
		b++;
	}
	outputhtml += '</tr></table>';
	document.getElementById('bottom_menu').innerHTML = outputhtml;
}

function loadLevel3(o,code)
{
	var popup = 'ajax_mini_pop_div';
	var popup_data = 'ajax_mini_pop_div_content';
	var id  = o.id;
	var data = o.value;

	var o2 = document.getElementById(popup);
	var x = findPosX(o);
	var y = findPosY(o)+20;

	o2.style.left = x+'px';
	o2.style.top = y+'px';

	var outputhtml = '<table cellpadding="2" cellpadding="2" border="0" style=";border:0px solid #000000" >';
	var style = 'style="background:#000000"';
	var b = 0;
	var childcounter = 0;
	while(lvl3_arr[b])
	{
		var attchild = '';
		if(lvl3_arr[b].parent_text == code)
		{
			if(lvl3_arr[b].url != '')
				attchild += ' onclick="window.location=\''+lvl3_arr[b].url+'\'" ';
			outputhtml += '<tr><td '+attchild+' style="cursor:pointer;color:#FFFFFF;background:#858280;padding-left:2px" onmouseover="style.backgroundColor=\'#9e2421\';" onmouseout="style.backgroundColor=\'#858280\';"  class="menu_lvl2">'+lvl3_arr[b].text.toLowerCase()+'</td></tr>';
			childcounter++;
		}
		b++;
	}
	outputhtml += '</table>';
	if(childcounter == 0)
		style = '';
	var html = '<table cellpadding="3" cellpadding="0" border="0" '+style+'>';
	html += '<tr><td ><div id="menu_level_three" >';
	html += outputhtml;
	html += '</div></td></tr>';
	html += '</table>';
	document.getElementById(popup_data).innerHTML = html;
	document.getElementById(popup).style.visibility = 'visible';
	clearTimeout(t);
	t=setTimeout("closeAndClearDiv(\'ajax_mini_pop_div\',\'ajax_mini_pop_div_content\');",6000);
}

function loadSecondLevelLinks(link)
{

	var link_data = 'bottom_menu';
	ajaxGetFuncHTML('index.php?ajax+run_task+loadSecondLevelLinks+module/modules/ajax/class.func.php+'+link,link_data);
}

function loadSecondLevelLinks(o,link)
{

	var popup = 'ajax_mini_pop_div';
	var popup_data = 'ajax_mini_pop_div_content';
	var id  = o.id;
	var data = o.value;

	var o2 = document.getElementById(popup);
	var x = findPosX(o);
	var y = findPosY(o)+20;

	o2.style.left = x+'px';
	o2.style.top = y+'px';

	document.getElementById(popup).style.visibility = 'visible';
	ajaxGetFuncHTML('index.php?ajax+run_task+loadSecondLevelLinks+module/modules/ajax/class.func.php+'+link,popup_data);

}

function loadTopLinks(district)
{

	if (district == 'H')
	{
		window.location='index.php?home';
	}
	else
	{
		window.location='index.php?districthome+'+district;
	}
}


function loadTopLinks2(district)
{
	var link_data2 = 'topLinks';


	ajaxGetFuncHTML('index.php?ajax+run_task+reloadTopLinks+module/modules/ajax/class.func.php+'+district,link_data2);
}



function getMyXY(o)
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) //Non-IE
	{
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) //IE 6+ in 'standards compliant mode'
	{
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) //IE 4 compatible
	{
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	else
	{
		myHeight = document.getElementById('mesure').offsetHeight;
		myWidth = document.getElementById('mesure').offsetWidth;
	}

	var w = o.offsetWidth;
	var h = o.offsetHeight;

	var x = Math.round((myWidth/2)-(w/2));
	var y = Math.round((myHeight/2)-(h/2));
	y += document.body.scrollTop;

	o.style.left = x+'px';
	o.style.top = y+'px';
}


function findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
	while(1)
	{
		curleft += obj.offsetLeft;
		if(!obj.offsetParent)
		break;
		obj = obj.offsetParent;
	}
	else if(obj.x)
	curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
	while(1)
	{
		curtop += obj.offsetTop;
		if(!obj.offsetParent)
		break;
		obj = obj.offsetParent;
	}
	else if(obj.y)
	curtop += obj.y;
	return curtop;
}

function closeAndClearDiv(id,id2)
{
	var o = document.getElementById(id);
	var odiv = document.getElementById(id2);
	o.style.left = '-1000px';
	o.style.top = '-1000px';
	odiv.innerHTML = '';
	window.removeDivHandle = null;
}

function loadMainAttSearch()
{
	var link_data = 'mainsearch_area';
	document.getElementById(link_data).innerHTML = "<table cellpadding=0 cellspacing=0 width=100%><tr><td align=center><img src=img/content/ajax-loader.gif></td></tr></table>";
	ajaxGetFuncHTML('index.php?ajax+run_task+loadMainAttSearch+module/modules/ajax/public/class.public.php',link_data);
}

function loadMainAccomSearch()
{
	var link_data = 'mainsearch_area';
	document.getElementById(link_data).innerHTML = "<table cellpadding=0 cellspacing=0 width=100%><tr><td align=center><img src=img/content/ajax-loader.gif></td></tr></table>";
	ajaxGetFuncHTML('index.php?ajax+run_task+loadMainAccomSearch+module/modules/ajax/public/class.public.php',link_data);
}

function closeMainSearch(id,html)
{
	document.getElementById(id).innerHTML = html;
}

function sendSearch()
{
	var search_active = document.getElementById("mainsearch_active").value;
	if(search_active == 1)
	{
		var dest_name = document.getElementById('dest_name').value;
		if(document.getElementById('district_id'))
			var region = document.getElementById('district_id').value;
		else
			var region = '';

		if(dest_name == "" &&  region == "0")
		{
			if(confirm("Are you sure you want to search without any name or district search parameters?"))

				document.getElementById('main_search_form').submit();
		}
		else
		{
			document.getElementById('main_search_form').submit();
		}
	}
}

function checkKey(e)
{
	if(e.keyCode == '13')
	{
		loginSubmit();
	}
}

function getSubRegions(ret_task)
{
	var district_id = document.getElementById("district_id").value;
	ajaxGetFuncHTML('index.php?ajax+run_task+getSubregions+module/modules/ajax/class.func.php+'+district_id+'+task_accomodation_php+subdistrict+160px+getdivsubcities','subregion');

}


function getdivsubcities()
{
	district_id = document.getElementById('district_id').value;
	subdistrict_id = document.getElementById('subdistrict_id').value;
	if (subdistrict_id == -2)
	{
		ajaxGetFuncHTML('index.php?ajax+run_task+getStructureCities+module/modules/ajax/class.func.php+'+district_id+'+'+subdistrict_id+'+task_accomodation_php+city_id+160px','town');
	}
	else
	{
		ajaxGetFuncHTML('index.php?ajax+run_task+getAccomCities+module/modules/ajax/class.func.php+'+subdistrict_id+'+task_accomodation_php+city_id+160px','town');
	}
	//ajaxGetFuncHTML('index.php?ajax+run_task+getAccomCities+module/modules/ajax/class.func.php+'+subdistrict_id+'+task_accomodation_php+city_id+160px','town');
}

function getdivsubcities2()
{
	var district_id = document.getElementById("district_id").value;
	ajaxGetFuncHTML('index.php?ajax+run_task+getAccomCities+module/modules/ajax/class.func.php+'+district_id+'+task_accomodation_php+city_id+160px','town');
}


function getdivsubcities3()
{
	district_id = document.getElementById('district_id').value;
	var subdistrict_id = '-2';
	ajaxGetFuncHTML('index.php?ajax+run_task+getStructureCities+module/modules/ajax/class.func.php+'+district_id+'+'+subdistrict_id+'+task_accomodation_php+city_id+160px','town');
}


function getTowns(ret_task)
{
	var subregion_id = document.getElementById("subregionDD").value;
	ajaxGetFuncHTML('index.php?ajax+run_task+getTowns+module/modules/ajax/public/class.public.php+'+subregion_id+"+"+ret_task,'town');
}

function notRegistered()
{
	window.location = "index.php?reg";
}

function lostPassword()
{
	window.location = "index.php?getpass";
}

function autoCompleteTown(o,outid)
{

	var popup = 'ajax_mini_pop_div';
	var popup_data = 'ajax_mini_pop_div_content';
	var id  = o.id;
	var data = o.value;

	var o2 = document.getElementById(popup);
	var x = findPosX(o);
	var y = findPosY(o)+20;

	o2.style.left = x+'px';
	o2.style.top = y+'px';

	document.getElementById(popup).style.visibility = 'visible';
	ajaxGetFuncHTML('index.php?ajax+run_task+autoCompleteTown+module/modules/ajax/public/class.public.php+'+id+"+"+data+"+"+outid+"+"+popup+"+"+popup_data,popup_data);
}

function setFavorites()
{
	if(confirm('Are you sure you want to add this website to your favorites?'))
	{
		var  title = "KwaZulu-Natal Tourism";
		var  url = "http://www.kzn.org.za";
		makeBMark(title,url);
	}
}

function makeBMark(title,url)
{
	 if (window.sidebar)
	 {
		  window.sidebar.addPanel(title, url,"");
	 }
	 else if( window.opera && window.print )
	 {
		var mbm = document.createElement('a');
		mbm.setAttribute('rel','sidebar');
		mbm.setAttribute('href',url);
		mbm.setAttribute('title',title);
		mbm.click();
	 }
	 else if( document.all )
	 {
		window.external.AddFavorite( url, title);
	 }
}

function mailPage()
{
	var sbject = escape("Here's something I think you'll like");
	var message = escape("Click on this page link I found for you on the Kwa-Zulu Natal Tourism website - the official website for tourism in Kwa-Zulu Natal:" + "\n\n" + window.location + "\n\n" + "Kwa-Zulu Natal Tourism provides official destination guides, travel and accommodation booking including quality-assured hotels, holiday planning tools, transport information including flights to Kwa-Zulu Natal and travelling around Kwa-Zulu Natal, and lots of practical travel advice.");
	window.location = "mailto:?subject=" + sbject + "&body=" + message;
}

function clearText()
{
	var search = document.getElementById('q').value;
	if (search == 'Search')
	{
		document.getElementById('q').value = '';
	}
}

function siteSearch()
{
	var search = document.getElementById('search_query').value;
	if (search == 'Search the site' || search == '')
	{
		alert('Please enter search query!');
	}
	else
		window.location='index.php?sitesearch+'+search;
}


function serial_getobject(s)
{
	var re=new Object();
	var l,n,t,p=0,r=1,sp,k,v,m=0,op=0;
	while(r)
	{
		n=s.charAt(p);
		if(n=="a"){t="a";p++;n=s.charAt(p)}
		else t="s";

		if(n==":"){l=1;p++}
		else{sp=s.indexOf(":",p);if(sp>-1){l=parseInt(s.substring(p,sp));p=sp+1}else r=0;}

		if(r==1)
		{
			v=s.substr(p,l);p+=l;
			if(t=='a')v=serial_getobject(v);
			if(m==0){k=v;m=1;}
			else{re[k]=v;m=0;}
			if(op==p)r=0;op=p;
		}
	}
	return re;
}

function getSubCats(cat,ret_task,id,width,outputid)
{
	if(document.getElementById("region_id"))
		var region_id = document.getElementById("region_id").value;
	ajaxGetFuncHTML('index.php?ajax+run_task+getSubCats+module/modules/ajax/class.func.php+'+cat+'+'+ret_task+'+'+id+'+'+width,outputid);
}
