// JavaScript Document for www.watershed.co.uk
// Copyright Watershed Media Media Centre 2004




// ********************************************************************
// ************************ Keyboard Shortcuts ************************
// ********************************************************************

function keyboardShortcut(anEvent) {
    var theKey;
    if (window.event) {
        theKey = window.event.keyCode;
    } else {
        theKey = anEvent.charCode;
    }
    if (theKey == 161) // 1 = Home Page
    location = '/';
		  
    if (theKey == 8364) //2 = What's On Today
    location = '/today/';
		  
    
}


var altModifierFlag = false;

function checkAltModifier() {
	if (window.event) {
		altModifierFlag = window.event.altKey;
	} else {
	   altModifierFlag = event.altKey;
    }

}

function checkKeyEvent() {
    if (window.event) {
        if (altModifierFlag) {
            if (window.event.keyCode == 49) // 1 = Home Page
			location = '/Apps/WebObjects/Watershed.woa/';
		  
            if (window.event.keyCode == 50) //2 = What's On Today
			location = '/Apps/WebObjects/Watershed.woa/wa/today';		 
	   }
	} else {
        if (altModifierFlag) {

            if (Event.keyCode == 49) // 1 = Home Page
			location.href = '/';
		  
            if (Event.keyCode == 50) //2 = What's On Today
			location = '/today/';
	   }
	}
}




// ********************************************************************
// *********************** Feedback Form Entry ************************
// ********************************************************************

function validateFeedbackForm() {
	userComments = document.getElementById("commentsField").value;
    document.getElementById("submitFeedbackButton").disabled = (userComments.length == 0);
	warningMessage = document.getElementById("HTMLCommentWarningMessage");
	if (warningMessage) {
		if (userComments.indexOf("<a ") != -1 || userComments.indexOf(" href") != -1 || userComments.indexOf("[url") != -1) {
			document.getElementById("submitFeedbackButton").disabled = true;
			warningMessage.style.display = "block";
		} else {
			warningMessage.style.display = "none";
		}
	}
}

function openFeatureSuggestion(url) {
    window.open(url,'featureSuggestion','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=520,height=460');
}

function openBugReport(url) {
    window.open(url,'bugReport','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=520,height=700');
}


// ********************************************************************
// *************************** Form Entry *****************************
// ********************************************************************



function validateUIBen(sender) {
alert (sender);
    registrationForm.submitButton.disabled = (sender.value == "");
}


function validateEmailUIForm(sender) {
	isError = false;
	fields = new Array("email_address","first_name","last_name", "postcode");
	for (i=0; i < fields.length; i++) {
		theValue = document.getElementById(fields[i]).value;
		if (theValue == null || theValue == "") {
			isError = true;
			break;
		}
	}
	document.getElementById("subscribeButton").disabled = isError;
}


// ********************************************************************
// ************************* Navigation Entry *************************
// ********************************************************************

function popupMenuNavigation(sender) {
    if (sender.options[sender.selectedIndex].value == null || sender.options[sender.selectedIndex].value == "" || sender.options[sender.selectedIndex].value == "#") {
        sender.selectedIndex = 0;
    } else {
        window.location = sender.options[sender.selectedIndex].value;
    }
}


function filterCalendar(sender) {
	var cookieName = "Calendar"+sender.name.substring(0, 1).toUpperCase()+sender.name.substring(1);
	setCookie( cookieName, sender.selectedIndex, null, "/", ".watershed.co.uk", false );
	document.location.reload();
}


// ********************************************************************
// ************************** Images & Media **************************
// ********************************************************************

function viewMediaSet(id) {
url = "/Apps/WebObjects/Watershed.woa/wa/MediaSetActions/mediaSetPopup?object=";
url = url + id;
window.open(url,'mediaSetPopup','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=400');
}





// ********************************************************************
// ************************** Search Preview **************************
// ********************************************************************


var req;
var searchDisabled = false;

function disableSearchPreview() {
	searchDisabled = true;
	    clearPreviewResults();
}


function searchKeyUp(sender) {
	if (!sender.value || sender.value.length < 1|| searchDisabled) {
    		clearPreviewResults();
    		return;
	}

	if (req) req.abort();

	searchURL = "/Apps/WebObjects/Watershed.woa/wa/SearchActions/previewQuickSearchQuery";
	//searchURL = searchURL + sender.value;
	postArgs = "searchTerms=" + sender.value;
	loadXMLDoc(searchURL, postArgs);
}


function sessionSearchKeyUp(session, sender) {
	if (!sender.value || sender.value.length < 1|| searchDisabled) {
    		clearPreviewResults();
    		return;
	}

	if (req) req.abort();
	
	searchURL = "/Apps/WebObjects/Watershed.woa/wa/SearchActions/previewQuickSearchQuery";
	//searchURL = searchURL + sender.value;
	//searchURL = searchURL + "&wosid=" + session;

	postArgs = "searchTerms=" + sender.value;
	postArgs = postArgs + "&wosid=" + session;
	loadXMLDoc(searchURL, postArgs);
}



function loadXMLDoc(url, content) {
    // branch for native XMLHttpRequest object

    if (window.XMLHttpRequest) {

        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("POST", url, true);
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length", content.length);
		req.setRequestHeader("Connection", "close");
        req.send(content);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        // bail out if Macintosh IE. I know browser selection is a bad thing
        // but so is IE Mac and the sooner it is gone the better.
        if (navigator.userAgent.indexOf("Mac") != -1) return; 

        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("POST", url, true);
            req.send(content);
        }
    }
}





// handle onreadystatechange event of req object
function processReqChange() {

    // only if req shows "loaded"
    if (req.readyState && req.readyState == 4) {
        // only if "OK"
        //if (req.status != 200) alert("req.status " + req.status + " " + req);
        //if (req.status && req.status == 200) {
            //alert(req.status);

            //clearTopicList();
            //buildTopicList();
            //alert(req.responseText.length);
            if (document.getElementById("quickSearchTermsField").value.length > 0) {
                displayPreviewResults();
            } else {
                clearPreviewResults();
            }
         //} else {
         //clearPreviewResults();
            //alert("There was a problem retrieving the XML data:\n" + req.statusText);
         //}
    }
}


		
		
function clearPreviewResults() {
document.getElementById("previewSearchPanel").style.display="none";

    resultsArea = document.getElementById("previewSearchPanelBody");

    //Remove the old results
    //for (i=0; i < resultsArea.childNodes.length; i++) {
        //resultsArea.removeChild(resultsArea.childNodes[i]);
    //}
    insertHTMLText("");
}



function displayPreviewResults() {
//alert("displayPreviewResults " + req.responseText.length);
    // Check we have results - we get a clearing div back anyway 
    if (req.responseText.length < 74) {
        clearPreviewResults();
        return;
    }
    
    // Add the new data
    resultsArea = document.getElementById("previewSearchPanelBody");
    document.getElementById("previewSearchPanel").style.display="block";
    insertHTMLText(req.responseText);


    //alert (req.responseText);
    
        //alert(req.responseXML.childNodes[0].childNodes.length);
//resultsArea.appendChild( req.responseXML.childNodes[0]);
    //for (i=0; i < req.responseXML.childNodes.length; i++) {
        //alert(req.responseXML.childNodes[i].nodeValue);
        //alert(req.responseXML.childNodes.length);
        //resultsArea.appendChild(req.responseXML.childNodes[i]);
        //if (req.responseXML.childNodes[0].childNodes[i].data != 
        //resultsArea.appendChild(req.responseXML.childNodes[0].childNodes[i]);

    //}
    //req.responseXML.getElementsByTagName("multiRef");
    
    
    //document.getElementById("startingTimes").appendChild(document.createTextNode(exhibitStartTimes()));

}


function insertHTMLText(text) {
var id = 'previewSearchPanelBody';
var theText = "";
if (document.getElementById) {
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
} else if (document.all) {
		x = document.all[id];
		x.innerHTML = text;
} else if (document.layers) {
		x = document.layers[id];
		text2 = '<P CLASS="testclass">' + text + '</P>;';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}





// ********************************************************************
// ************************* Advanced Search **************************
// ********************************************************************


function clearSearchTerms(sender) {
	if ((sender.value == "Enter search terms here") ||  (sender.value == "Enter se")) {
		sender.value = "";
	}
}




// ********************************************************************
// **************************** User Help *****************************
// ********************************************************************

function openHelp(folderName) {
    if (folderName == null || folderName == "") return;
    url = "http://new.watershed.co.uk/site/help/" + folderName;
    window.open(url,'helpPopup','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=680,height=500');
}




// ********************************************************************
// ************************ Conference Contact ************************
// ********************************************************************


function validateConferenceContactForm() {
    document.getElementById("submitContactButton").disabled = (document.forms["contactForm"].senderEmail.value.length == 0);
}







// ********************************************************************
// ************************ Utility Functions *************************
// ********************************************************************



function setRolloverContent(text, id) {
var theText = "";
if (document.getElementById) {
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
} else if (document.all) {
		x = document.all[id];
		x.innerHTML = text;
} else if (document.layers) {
		x = document.layers[id];
		text2 = '<P CLASS="testclass">' + text + '</P>;';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}
