/*
	js/homepage.js
	
	Javascript for DanceWeb Home Page and Common Functions for Whole Site

	29 Feb 2004
	25 Aug 2004 Added 'addbookmark'
	28 Jan 2007 Ref search box by form name
	06 Mar 2007 Use improved createXMLHTTP	
	30 Mar 2007 Support use by parse.aspx
	26 Apr 2007 Improve Error Msgs
	12 Sep 2007 Detect if page still loading when user hits Search
	13 Feb 2008 Move "var country" to homepage; do not wait for onload
	02 Apr 2008 Now used by Generic Search Box, on most pages
	31 Jan 2009 Add "Load AjaxVerMenu" for Home Page
*/

//var country = 0;	// ISO code

var NS4=false
var IE4=false
var IE5=false
var ver4=false

var coll, styleObj
var intervalID=0;
var glowObj= null

function externalLinks() { 
	//	detect <a nodes with rel='external-<name>' 
	//	insert a target property of <name>

	//alert ("++externalLinks");
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
 	for (var i=0; i<anchors.length; i++) { 
		//	for each anchor
   		var anchor = anchors[i]; 
	   	if (anchor.getAttribute("href")) { 
			var relAll=anchor.getAttribute("rel");
			if (relAll !=""){
				//alert ("i: " + i + ", relAll: " + relAll);
				var relA = relAll.split(' ');
			 	for (var j=0; j<relA.length; j++) { 
					//	for each word in rel attribute
					//alert ("j: " + j + ", relA[j]: " + relA[j]);					
					var extA = relA[j].split('-');
					var ext = extA[0];
					//alert ("part A: " + ext);
					if (ext=="external"){
						anchor.target = extA[1];
						//alert ("target: " + extA[1]);
					}
				}
			}	
		}
	} 
} 

function onloaded(){
	return;
}	

function doSelect(inp,sel,sel2){
	//alert (sel.selectedIndex)
	var ind=sel.selectedIndex;
	inp.value=sel.options[ind].value;
	sel2.value=ind;
}

function doLink(m){
	//alert(m)
	window.location.href=m
}

function doClear(){
	document.forms["search"].q.value='';
}

		
function createXMLHTTP(){
	// 	Create XMLDOM Object 
	//
	//	Return:	XMLHttpRequest Object

    var objType = false;
	try
	{
		objType = new XMLHttpRequest();
	}
	catch (e)
	{	
	    try
		{
    	    objType = new ActiveXObject('Msxml2.XMLHTTP');
	    } 
		catch(e) 
		{
    	    try 
			{
        	    objType = new ActiveXObject('Microsoft.XMLHTTP');
	        } 
			catch(e) 
			{
			}
        }
    }
    return objType;
}

function LoadXML (url){
	//	Load XML File from Server into XML Object
	//	xmlhttp:	XML object
	//	url:		path to load from
	//
	//	Return:		the xmlhttp object
	
	var xmlhttp = createXMLHTTP();
	 
	if (!xmlhttp){
		alert ("**ERROR; unable to create NewHttpObject.\n" +
			"Please upgrade to a more modern Browser");
		return false;
	}	
		 
	xmlhttp.open("GET", url, false);
	
	xmlhttp.send(null);

	return xmlhttp;
}

function changeClass(obj, newClass) {
	//	Change class of Element
	//	obj:		element object
	//	newClass:	new class name

	obj.className=newClass;
}

function addbookmark(){
	//	Add bookmark to Browser Favourites
	var bookmarkurl="http://www.danceweb.co.uk";
	var bookmarktitle="DanceWeb - Find Dance Class";
	if (document.all)
		window.external.AddFavorite(bookmarkurl,bookmarktitle);
}

function ParseInternal(mode, q){
	//	mode:	0:	user hit "Go" on a Home Page
	//			1:	user hit "Advanced Search" on a Home Page
	//			2:	user did direct "q=xxx" search on Site Home Page 
	//	q:		query text
	
	//alert ("ParseInternal mode: " + mode + ", q: " + q);

	if (country==0){
		alert ("Please wait a few seconds for page to finish loading, then retry");
		return false;
	}
	
	var hostname = getHostname();
		
	var urlMap  	="http://" + hostname + "/map/?key=dance";	
	var urlSearch  	="http://" + hostname + "/search/?key=dance";
	var urlSearchA 	="http://" + hostname + "/advanced_search/?key=dance";
		
	if (mode != 1 && q.length < 1){
		showSearchWait(false);
		alert("You must enter some keywords in the Search box,\n" +
			"or select 'Advanced Search'.");
		if (mode==1)
			return;
		else
			return (false);
	}
	
	if (q.length < 1){
		showSearchWait(false);
		window.location = urlSearchA + "&m=1";		
		return false;
	}

	//	Parse User-entered query text using "Web Service"
		
	var url ="http://" + hostname + "/webservice/parse/?xml=1&q=" + q + "&co=" + country;
	//alert ("url: " + url);
	//return false;
	
	var qs=""
	
	var xmlhttp = LoadXML (url);

	//showSearchWait(false);
			
	//alert(xmlhttp.responseText)
	//alert(xmlhttp.responseXML.xml)

	var xmlDoc=xmlhttp.responseXML;
	var status=-1;
	try{	
		status=xmlDoc.getElementsByTagName("parseStatus")[0].childNodes[0].nodeValue;
		//alert ("status: " + status);
	}
	catch(e){
		showSearchWait(false);	
		myWindow=window.open();
        myWindow.document.write(xmlhttp.responseText);
		return false
	}
	//return false
	
	//alert ("status: " + status + ", mode: " + mode);
	
	//	Build query string
		
	var tokens=xmlDoc.getElementsByTagName("tokens")[0].childNodes;
	for (var i=0; i<tokens.length;i++){
		var name = tokens[i].nodeName;
		var child = tokens[i].childNodes[0];
		if (child!=null){
			var valu= child.nodeValue;
			if (valu!="" && valu!="0.000000"){
				if (name=="sx" || name =="sy"){
					if (status != 3) qs+= "&" + name + "=" + valu;
				}
				else{ 
					qs+= "&" + name + "=" + valu;
				}
			}
		}
	}

	if (mode==1){
		
		//	Do Advanced Search

		window.location = urlSearchA + qs;
	}	
	else {
		if (status==0){
			
			//	No errors; Do a Search & Display Results
	
			//document.getElementById("page").innerHtml = "<tr><td>Searching; please wait a moment...</td></tr>";
			window.location = urlSearch + qs; 
		}
		else if (status ==1 || status ==2){
			if (mode!=0){

				//	Error in Advanced or Query Search; Do another Advanced Search

				window.location = urlSearchA + qs;
			}			
			else{
				//	Error in Basic Search; request correction
			
				var msg = xmlDoc.getElementsByTagName("em")[0].childNodes[0].nodeValue;
				//alert ("err: " + msg);
				showSearchWait(false);				
				document.getElementById("errMsg").innerText = msg;
			}
		}
		else if (status==3){

			//	Ambiguous Location; Display Map

			window.location = urlMap + qs + "&gm=1" + 
			"&lngn=" + xmlDoc.getElementsByTagName("MinLongitude")[0].childNodes[0].nodeValue +
			"&latn=" + xmlDoc.getElementsByTagName("MinLatitude")[0].childNodes[0].nodeValue +
			"&lngx=" + xmlDoc.getElementsByTagName("MaxLongitude")[0].childNodes[0].nodeValue +
			"&latx=" + xmlDoc.getElementsByTagName("MaxLatitude")[0].childNodes[0].nodeValue;
		}

		return false;
	}
	
	return;	
}		

function showSearchWait(wait){
	//	show waiting / search image...
	if (!wait){
		document.getElementById("searchBut").style.cursor="default";
		document.getElementById("searchBut").src="/graphics/search_1_24.jpg";
	}
	else {
		document.getElementById("searchBut").style.cursor="wait";	
		document.getElementById("searchBut").src="/graphics/indicator_24.gif";
	}
	
	//setTimeout ("return();",100);
}


function AskDWValidate(advanced){
	//	Called when  user hits:	
	//		"Go" or
	//		"Advanced Search" on Home Pages

	//	show waiting image...
	showSearchWait(true);
	
	var q = document.forms["search"].q.value;

	//alert ("AskDWValidate q: [" + q + "]");
		
	return (ParseInternal(advanced ? 1 : 0, q));
}	

function onloaded (countryISO){
	country = countryISO;
	
	//var calObj = document.getElementById("Calendar1")
	//calObj.style.top=100
	//alert ("country: " + country);
}

function getY( oElement ){
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	//alert ("getY: " + iReturnValue);
	return iReturnValue;
}
function getX( oElement ){
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	//alert ("getX: " + iReturnValue);
	return iReturnValue;
}

function RePosition (object, target){
	//	Set position of "object" to position of "target" 	
	var obj =    document.getElementById(object);
	var target = document.getElementById(target);
	obj.style.top = getY(target);
	obj.style.left= getX(target);
}	

function removeAllChildren(cell){
	var cell = document.getElementById(cell);
	if (cell.hasChildNodes() ){
    	while ( cell.childNodes.length >= 1 ) {
    	    cell.removeChild( cell.firstChild );       
	    } 
	}
}	

function loadTopMenu(mode, el, county, country, div, subsite, pd, app){
	//alert ("loadTopMenu, subsite: " + subsite);
	getAXAH(app + '/ajax/menus/hor/default.aspx?mo=' + mode + '&el=' + el + '&co=' + county + '&ca=' + country + '&ss=' + subsite + '&pd=' + pd, div); 
}

function loadVerMenu(mode, el, county, country, div, subsite, pd){
	//alert ("loadTopMenu, subsite: " + subsite);
	getAXAH('ajax/menus/ver/default.aspx?mo=' + mode + '&el=' + el + '&co=' + county + '&ca=' + country + '&ss=' + subsite + '&pd=' + pd, div); 
}

function DoHPAjax (div, butShow, butHide, url){

	var obj=document.getElementById(butShow);
	var obj2 = obj.style.display;

	if (obj2 == null || obj2 == ''){
		getAXAH(url, div);
		obj.style.display="none";		
	}
	
	flipVisible(div, butShow, butHide);
}

function DoHPVideo (div, div2, butShow, butHide){

	var obj=document.getElementById(butShow);
	var objStatic=document.getElementById(div2);
	var obj2 = obj.style.display;

	if (obj2 == null || obj2 == ''){
		LoadVideoBar();
		obj.style.display="none";		
		objStatic.style.display="none";	
	}
	
	flipVisible(div, butShow, butHide);
}

function getHostname(){
	//	return:	true hostname
	//			works with live, localhost & app based site versions 
		
	var hostname = location.hostname;
	var pathname = location.pathname.substring(0,9);
	//alert ("hostname: " + hostname + ", pathname: " + pathname);
	if ((hostname=="localhost") && (pathname=="/danceweb"))
		hostname += "/danceweb";
	return hostname;
}			