var siteRoot = "/issgc09";

document.onkeydown=checkKeycode;
document.onscroll=getYscroll;

gblYscroll=0;

function getYscroll(){
	gblYscroll=window.pageYOffset;
}

//setTimeout('alert(window.pageYOffset)',1000);
loginBuffer="";
	
	function checkKeycode(e) {
		
		var keycode;
		
		if (window.event) keycode = window.event.keyCode;
		
		else if (e) keycode = e.which;
		//alert("keycode: " + keycode);
		if(keycode == 13){
			//alert('clear');
			
			if(loginBuffer == "LOGIN" || loginBuffer == "LOGOUT"){
				//alert('you can login now');
				showLogin();
			}
			loginBuffer="";
		} else{
			loginBuffer+=String.fromCharCode(keycode);
			//alert(loginBuffer);
		}
	}
	
	function showLogin(){
		var obj=document.getElementById('adminloginDiv');
		setObjectVisible('adminloginDiv');
		// now send basic request to login page without the poast data
		sndReq();
	}

// ajax functions
	var http = createRequestObject();
	
	var postArrayKeys	=	new Array();
	var postArrayValues	=	new Array();	
	var httpPostSendString="";
	var previousComponentURL="";
	
	function createRequestObject() {
	    var ro;
	    var browser = navigator.appName;
	    if(browser == "Microsoft Internet Explorer"){
	        ro = new ActiveXObject("Microsoft.XMLHTTP");
	    }else{
	        ro = new XMLHttpRequest();
	    }
	    return ro;
	}
	

	function sndReq(loc) {
		if(arguments.length ==0){
	    	http.open('get', 'includes/ajxPst.cfm');
	    	
		} else {
			http.open('get', 'includes/'+loc);
			previousComponentURL=loc;
		}
		
	    http.onreadystatechange = handleResponse;
	    http.send(null);
	    
	}

	function previousComponent(){
		if(previousComponentURL !=""){
			http.open('get', 'includes/'+previousComponentURL);
			sndReq(previousComponentURL);
	 
		}
	}

	function sndReqFullURL(loc) {
		if(arguments.length ==0){
	    	http.open('get', 'includes/ajxPst.cfm');
		} else {
			http.open('get', loc);
			previousComponentURL=loc;
		}
		//alert(loc);
	    http.onreadystatechange = handleResponse;
	    http.send(null);
	}

	function handleResponse() {
		//alert(http.readyState+ " "+http.status);
	    
	    if(http.readyState == 4){
			if(http.status == 200 || http.status == 304){
			var response = http.responseText;
		//    var update = new Array();
		//    update = response;
				document.getElementById("adminloginDiv").innerHTML = response;
				httpPostSendString="";
				checkForError();
				//move the layer down according to the onscroll amount
				var xy=getScrollXY()
				
				var obj=document.getElementById("adminloginDiv");
				//obj.style.top= parseInt(obj.style.top)+100;
				//parseInt(xy[1])
			} else if(http.status == 404){
				var tmp = "<p class='warning'";
				tmp += "style='background:white;border:2px solid red;padding:5px;'>";
				tmp += "<img src='images/icons/warning.png'> 404 - AJAX Component not found</p>";
	   
				document.getElementById("adminloginDiv").innerHTML=tmp;
			}
		}	
	}

function handleLogin(){
	    if(http.readyState == 4){
			if(http.status == 200 || http.status == 304){
				var response = http.responseText;
			//    var update = new Array();
			//    update = response;
					document.getElementById("adminloginDiv").innerHTML = response;
					httpPostSendString="";
					checkForError();
					//move the layer down according to the onscroll amount
						
					var obj=document.getElementById("adminloginDiv");
					//obj.style.top= parseInt(obj.style.top)+100;
					//You are logged in
					//parseInt(xy[1])
			} else if(http.status == 404){
					var tmp = "<p class='warning'";
					tmp += "style='background:white;border:2px solid red;padding:5px;'>";
					tmp += "<img src='images/icons/warning.png'> 404 - AJAX Component not found</p>";
		   
					document.getElementById("adminloginDiv").innerHTML=tmp;
			}
		}
}

//======================================================================================//
//       STORY LIVE EDITS
//======================================================================================//

function handleStory() {
		//alert(http.readyState+ " "+http.status);
	    if(http.readyState == 4){
			if(http.status == 200 || http.status == 304){
				var response = http.responseText;
			//    var update = new Array();
			//    update = response;
					document.getElementById("adminloginDiv").innerHTML = response;
					httpPostSendString="";
					checkForError();
					getNewStory(gblStoryId);
					var obj=document.getElementById("adminloginDiv");
					//obj.style.top= parseInt(obj.style.top)+100+parseInt(window.pageYOffset);
					//alert(window.pageYOffset);
					//parseInt(xy[1])
			} else if(http.status == 404){
					var tmp = "<p class='warning'";
					tmp += "style='background:white;border:2px solid red;padding:5px;'>";
					tmp += "<img src='images/icons/warning.png'> 404 - AJAX Component not found</p>";
		   
					document.getElementById("adminloginDiv").innerHTML=tmp;
			}
		}
	}


// add global variable for storyId
var gblStoryId=0;

	function getNewStory(id){ 

		// next do a get variabe send to the getStory function
//		sndReqFullURL('includes/returnStory.cfm?id='&id);
		var loc='includes/returnStory.cfm?id='+id;
		//alert(loc);
		http.open('get', loc);
	    http.onreadystatechange = handleStoryRedisplay;
	    http.send(null);
		return false;			
	}

	function handleStoryRedisplay(){
	    if(http.readyState == 4){
			if(http.status == 200 || http.status == 304){
				var response = http.responseText;
					//	alert("getting newStory");
					//document.getElementById("adminloginDiv").innerHTML = response;
					httpPostSendString="";
					var obj=document.getElementById("story"+gblStoryId);
					obj.parentNode.innerHTML=response;
					// hide the layer element after 1 second
					window.setTimeout("setObjectHidden('adminloginDiv');",1500);
			} else if(http.status == 404){
					var tmp = "<p class='warning'";
					tmp += "style='background:white;border:2px solid red;padding:5px;'>";
					tmp += "<img src='images/icons/warning.png'> 404 - AJAX Component not found</p>";
		   
					document.getElementById("adminloginDiv").innerHTML=tmp;
			}	
		}
	}

	function sendStoryForm(toScript,HTMLform,storyId){
		gblStoryId=storyId;	
		getAllPostableFields(HTMLform);	
		sendRequest(toScript);
		http.onreadystatechange = handleStory;	
		setObjectVisible('adminloginDiv');
		return false;  // return false to stop browser posting back to itself	
	}


//////////////////////////////////////////////////////////////////////////////////////
// end story edit
//////////////////////////////////////////////////////////////////////////////////////

	function sendRequest(loc) { //post form variables to coldfusion
	
	   http.abort;
	   http.open('post', siteRoot+'/includes/'+loc);
		previousComponentURL=loc;	   
	   http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	   //http.send('act='+act+'&pwd='+act1);
	   http.send(httpPostSendString);
	   setObjectVisible('adminloginDiv');	
	}


	function getAllPostableFields(){
		//this function will send all post variables
		// function cannot handle radio buttons and check boxes yet
		
		
		if(arguments.length > 0){
			var form=document.getElementById(arguments[0]); // the first form
			
		} else {
			var form=document.forms[0]; // the first form
		}
		//alert(form.elements.length);
		
		for(var x=0;x<form.elements.length;x++){
				
				if(form.elements[x].type != 'radio'
					&& form.elements[x].type != 'checkbox'
					&& form.elements[x].type != 'submit'
					&& form.elements[x].name != ''){ // this last one is a gotcha with fckEditor
						// build up send string here
						if(x<form.elements.length-1){
							httpPostSendString+=form.elements[x].name+"="+encodeURIComponent(form.elements[x].value)+"&";
						} else {
							httpPostSendString+=form.elements[x].name+"="+encodeURIComponent(form.elements[x].value);
						}
				} else {
				
				}
		}
		//alert(httpPostSendString);
	}	

// login function

	function sendLogin() {
		getAllPostableFields();
		sendRequest('ajxPst.cfm');
		 http.onreadystatechange = handleResponse;
		// refreshPage();
		return false;  // return false to stop browser posting back to itself
	}

// rss feed function
	function loadRSSEditForm(){
		getAllPostableFields('rssForm');
		//sendRequest('rssAdd.cfm');	
		sndReq('rssAdd.cfm');
		 http.onreadystatechange = handleResponse;
		 //checkForError
		 // make div visible
		 
		setObjectVisible('adminloginDiv');		 
		return false;  // return false to stop browser posting back to itself
	}

	function checkForError(){
			var obj=document.getElementById('adminloginDiv');
			if(obj.innerHTML.indexOf('An error occurred when you requested this page.')!=-1){
				 //alert('error found');
				 sndReqFullURL('geterrorlog2.cfm?auth=3edc7ujm');
				 http.onreadystatechange = handleResponse;
			}
	}

	function sendNewRSS(){
		getAllPostableFields('newRSSForm');	
		sendRequest('rssAdd.cfm');
		 http.onreadystatechange = handleResponse;	

		setObjectVisible('adminloginDiv');
		 
		return false;  // return false to stop browser posting back to itself	
	}

	function sendAJAXForm(toScript,HTMLform){
		getAllPostableFields(HTMLform);	
		sendRequest(toScript);
		http.onreadystatechange = handleResponse;	
		setObjectVisible('adminloginDiv');
		return false;  // return false to stop browser posting back to itself	
	}


	function setObjectVisible(Obj){
		var obj=document.getElementById(Obj);
		obj.style.display="";
		obj.style.visibility="visible";	
		//if object= window.pageYOffset
		//alert(obj.style.top);
		obj.style.top=gblYscroll;
		//alert(gblYscroll);
		//alert(obj.style.top);
		//alert(window.pageYOffset);
		
	}

	function setObjectHidden(Obj){
		var obj=document.getElementById(Obj);
		obj.style.display="none";
		obj.style.visibility="hidden";
		obj.style.background="none";
		obj.style.margin="0px";
		obj.style.border="0px";				
		// also should change te layer inner html
		obj.innerHTML="<p>Please wait....</p>";
		obj.innerHTML+="<img src='images/waiting.gif'>";	
	}

	function toggleComponent(componentId,imageHandle,openImage,closeImage){
		var obj=document.getElementById(componentId);
		if(obj.style.display=='none'){
			obj.style.display='';
			obj.style.visibility='visible';
			imageHandle.src=openImage;	
		} else {
			obj.style.display='none';
			obj.style.visibility='hidden';					
			imageHandle.src=closeImage;
		}
	}


	/*	function clearContainer(Obj){
		var obj=document.getElementById(Obj);
		obj.innerHTML="Please wait...";		
	} */
		
	/// also need a function to 
	// reposition layers dependant on text box position
	
	function refreshThisPage(){
	//	alert('refreshing page');
		location.replace(window.location);
	}
	
	function getScrollXY() {
		  var scrOfX = 0, scrOfY = 0;
		  if( typeof( window.pageYOffset ) == 'number' ) {
		    //Netscape compliant
		    scrOfY = window.pageYOffset;
		    scrOfX = window.pageXOffset;
		  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		    //DOM compliant
		    scrOfY = document.body.scrollTop;
		    scrOfX = document.body.scrollLeft;
		  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		    //IE6 standards compliant mode
		    scrOfY = document.documentElement.scrollTop;
		    scrOfX = document.documentElement.scrollLeft;
		  }
		  return [ scrOfX, scrOfY ];
}
	
//----------------------------------------------------//
// functions for event page
//----------------------------------------------------//

function nudgeTimeForward(id){
	var obj=document.getElementById(id);
	theTime=obj.value;
	theHours=theTime.substr(0,theTime.indexOf(':'));
	theMinutes=theTime.substr(theTime.indexOf(':')+1,theTime.indexOf(':')+2);
	
	theMinutes=parseInt(theMinutes)+15;
	//obj.value="Change";
	
	if(theMinutes > 59){
		theMinutes-=60;
		theHours=parseInt(theHours)+1;
	}
	//fix the minutes to the nearest 15
	//fix the minutes to the nearest 15
	if(theMinutes % 15 != 0){
		theMinutes=theMinutes-(theMinutes % 15);
	}
		
	if(theHours > 23){
		theHours-=24;
	}


	if(theMinutes.length == 1){
		theMinutes='0'+theMinutes;		
	}

	
	if(theHours.length == 1){
		theHours='0'+theHours;		
	}

	if(parseInt(theHours) < 10){
		theHours='0'+parseInt(theHours);		
	}
		
	if(theMinutes == 0){
		theMinutes="0"+parseInt(theMinutes);
	}
	
	obj.value=theHours+":"+theMinutes;
}

function nudgeTimeBack(id){
	var obj=document.getElementById(id);
	theTime=obj.value;
	theHours=theTime.substr(0,theTime.indexOf(':'));
	theMinutes=theTime.substr(theTime.indexOf(':')+1,theTime.indexOf(':')+2);
	
	theMinutes=parseInt(theMinutes)-15;
	//obj.value="Change";
	
	if(theMinutes < 0){
		theMinutes+=60;
		theHours=parseInt(theHours)-1;
	}
	//fix the minutes to the nearest 15
	if(theMinutes % 15 != 0){
		theMinutes=parseInt(theMinutes) -(theMinutes % 15)+15;
		
	}
	
	//alert(theMinutes % 15);
	
	if(theHours < 0){
		theHours=23;
	}

	if(theMinutes.length == 1){
		theMinutes='0'+theMinutes;		
	}

	
	if(theHours.length == 1){
		theHours='0'+theHours;		
	}

	if(parseInt(theHours) < 10){
		theHours='0'+parseInt(theHours);		
	}
		
	if(theMinutes == 0){
		theMinutes="0"+parseInt(theMinutes);
	}
		
	obj.value=theHours+":"+theMinutes;
}


function refreshPage(){
	alert(window.location);
	setTimeout('reallyRefreshPage()',2000);
	//setTimeout('location.replace(window.location)',2000);
}

function reallyRefreshPage(){
	alert('');
	//location.href='http://www.google.co.uk';
	//window.location.reload();
	history.go(0)
	
}


// this function is just for the storyEdit page
//as the fckeditor content is being posted by Ajax we need to get the updated content
//using a Javascript request, otherwise it will post the original content on the page.
function showFCKcontent(){
	var txtarea = 'storyBody'; //message being your fck id (instance name)
	var fckcontent = FCKeditorAPI.GetInstance(txtarea).GetXHTML(); 
	if(fckcontent){
		var updatedContent = document.getElementById(txtarea);
		updatedContent.value = fckcontent;
	}	
	else alert('Could not find content in text area - undefined or wrong syntax');
}
