ajaxTimeout = 50000;
url = null;
urlstring = null;
var historyInitialized  = false;
var newwindow;

/** RSH must be initialized after the
    page is finished loading. */

function initialize() {
  // initialize RSH
  dhtmlHistory.initialize();
  
  // add ourselves as a listener for history
  // change events
  dhtmlHistory.addListener(handleHistoryChange);
  
  // determine our current location so we can
  // initialize ourselves at startup
  var initialLocation = dhtmlHistory.getCurrentLocation();
  
  // if no location specified, use the default
historyInitialized   = true;
  if (initialLocation == null)
  initialLocation = location.href;

  
  // now initialize our starting UI
  updateUI(initialLocation, null);
}

/** A function that is called whenever the user
    presses the back or forward buttons. This
    function will be passed the newLocation,
    as well as any history data we associated
    with the location. */
function handleHistoryChange(newLocation,
                             historyData) {
  // use the history data to update our UI
  if(location.hash !=''){
	  updateUI(newLocation, historyData);                           
  }
	if(location.hash ==''){
		//location.reload();	
	}
}

/** A simple method that updates our user
    interface using the new location. */
function updateUI(newLocation,historyData) {
  var output = document.getElementById("debugarea");
	
/*
  // simply display the location and the
  // data
  var historyMessage;
  if (historyData != null)
    historyMessage = historyData;
    
  var message = "New location: "
                + newLocation 
                + ", history data="
                + historyMessage;
                
  output.innerHTML = message;
*/
infoArray = new Array();
//infoArray = historyMessage.split(",");
infoArray = newLocation.split(".");
page = infoArray[0];
action = infoArray[1];
id = infoArray[2];

if ((page!=null)&&(action!=null)&&(id!=null)){
	
	showInfo(page,action,id);
} 
	if (page==null){
			window.location.reload;
	}




}



//var test = document.getElementById('test').innerHTML;
//	var remoteExample = new comment(callback); 

function submitValues(){
	var callback = {
		submitrating: function(result) {
			document.getElementById('siteratings').innerHTML = result;
		}	
	}

	var remoteExample = new comment(callback); 
	ratings = document.comments_form.rating.value;
	if (ratings==0){
		alert('Please Select a rating for this page');
		var brating = document.getElementById('brating');
		brating.style.borderStyle = 'solid';
		brating.style.borderWidths = 1;
		brating.style.borderColor = '#8888FF';
		
		
		//alert (brating.style.backgroundColor);
//		brating.borderstyle = 'solid';
	//	brating.borderWidths = 1;
	} else {
		comments = document.comments_form.comments.value;
		values = new Array();
		values[0] = ratings;
		values[1] = comments;
		values[2] = test;
		values[3] = window.location.href;
		remoteExample.submitrating(values);
	}
return false;
}




function doUpload(filename){
	var callback = {
		doPortalService: function(result) {
			HTML_AJAX_Util.setInnerHTML('contentarea',result);
		}	
	}
	var getData = new portal(callback); 
	getData.doPortalService('personalities','upload',filename);
}



function chkState(state){
/*	
	if (document.getElementById("HTML_AJAX_LOADING")){
	
	 	var loadingmsg = document.getElementById("HTML_AJAX_LOADING").style;

		if (loadingmsg.display!='none'){
			if (state=='start'){
				//alert('Server is taking too long to respond to the request.\n Internet Connection may timing out.\n Request is still running in the background.');
				//window.setTimeout("toggleErrormsg();",100);
				window.setTimeout("chkState('stop');",120000);
				
			} else	if (state=='stop'){
			//	toggleErrormsg();
	//alert('Sorry, The server has taken too long to respond to the request.\n Internet Connection may have completely timed out.\n Request may have been aborted.\n Automatically re-sending request');

				loadingmsg.display='none';
				infoArray = new Array();
				//infoArray = historyMessage.split(",");
				infoArray = urlstring.split(",");
				page = infoArray[0];
				action = infoArray[1];
				id = infoArray[2];
				if ((page!=null)&&(action!=null)&&(id!=null)){

					showInfo(page,action,id);
				} else {
					location.href = url;	
				}
		
			} 
	
		}
	}

*/
}

function toggleErrormsg(){
	var errormsg = document.getElementById("TIMEOUTERROR").style;	
	errormsg.display = (errormsg.display=='block')? 'none':'block';
}
	function compareUrlPage(page,action,id,extra){
		strHref  = location.href;
		strSearch = location.search;
		strPathname = location.pathname;
		
		//alert(page+ " " + strSearch + " " + strHref +"\n"+strPathname);	
		if (strSearch.search(page) != -1){
      		//do nothing, everything's fine
		} else {
    
			//alert ("Automatically Redirecting you to the correct page");	
			if (extra!=''){
				extra = "."+extra;
			}
			
			newUrl = "?p="+page+"#"+page+"."+action+"."+id+extra;
			if (page!='invite'){
				location.href = newUrl;			
			}
			//end request 
		}		
		
	}
	
function showInfo(page,action,id,extra){
	
    window.frames['loadingiframe'].location = "loading.php";; 
	if (document.getElementById('HTML_AJAX_LOADING')){
		var loadinganim = document.getElementById('HTML_AJAX_LOADING').style;
	//	loadinganim.display="none";
	}
	request_completed = false;
	if(extra==null){
			extra ='';
	}

	
	url = "?p="+page+"&a="+action+"&id="+id+extra;
	urlstring = page+","+action+","+id+extra;

	if (!useAjax){
		location.href = url;
	} else {
		compareUrlPage(page,action,id,extra); 		
		
//historyDt = new Object;
		//if ((page!=null)&&(action!=null)){
			
		//		window.setTimeout("chkState('start');",1);
			window.setTimeout("chkState('start');",100);
			if (page!='invite'){
				dhtmlHistory.add(page+"."+action+"."+id,page+","+action+","+id);
			}
		//}
		
		
		
			var contentarea = document.getElementById('contentarea');
		
			switch(page){
				case "infoservice":
				
					var callback = {
					doInfoservice: function(result) {
							appendReplaceContent('contentarea',result);
							doScroll();
						
						}	
					
					}
					var getData = new infs(callback); 
					getData.dispatcher.timeout = ajaxTimeout;
					getData.doInfoservice(page,action,id);
					break;
					
			case "about":
				
					var callback = {
					doInfoservice: function(result) {
							appendReplaceContent('contentarea',result);
							doScroll();
						}	
					}
					var getData = new infs(callback); 
					getData.dispatcher.timeout = ajaxTimeout;
					getData.doInfoservice(page,action,id);
					
					break;		
			case "schools":
				var callback = {
					doPortalService: function(result) {
							appendReplaceContent('contentarea',result);
							doScroll();
					}
				}
				var getData = new portal(callback); 
				getData.dispatcher.timeout = ajaxTimeout;
				getData.doPortalService(page,action,id);
				break;
			
			case "gsearch":
				var callback = {
					doPortalService: function(result) {
							appendReplaceContent('contentarea',result);
							doScroll();
					}
				}
				var getData = new portal(callback); 

				getData.dispatcher.timeout = ajaxTimeout;
				getData.doPortalService(page,action,id);
				break;
				
			case "news":	
				var callback = {
					doNewsService: function(result) {
							appendReplaceContent('contentarea',result);
							doScroll();
					}
				}
				var getData = new news(callback); 
				getData.dispatcher.timeout = ajaxTimeout;
				getData.doNewsService(page,action,id);
				
				break;
			
			case "request":
				my_window= window.open ("","mywindow1","status=1,width=400,height=220");
				my_window.document.write('<div class="spagetextgrey" id="form"></div>' );;
					var callback = {
					submitRequest: function(result) {
						
						window.my_window.document.getElementById('form').innerHTML = result;
						
					}	
				}
						
				var remoteExample = new comment(callback); 
				
				break;

			case "personalities":
				
				var callback = {
					doPortalService: function(result) {
							appendReplaceContent('contentarea',result);
							doScroll();
					}
				}
				var getData = new portal(callback); 
				getData.dispatcher.timeout = ajaxTimeout;
				getData.doPortalService(page,action,id);
				break;
				
				case "invite":
					var callback = {
								doGenr: function(result) {
									appendReplaceContent('inviteform',result+id);
									alert('Thanks! Invitation sent.');
									}	
								}
								var getData = new genr(callback); 

								getData.dispatcher.timeout = ajaxTimeout;
								getData.doGenr('invite',action);
								
					break;
				case "getads":
					var callback = {
								doGenr: function(result) {
									appendReplaceContent('inviteform',result);
									}	
								}
								var getData = new genr(callback); 
								getData.dispatcher.timeout = ajaxTimeout;
								getData.doGenr('getads',i);
					break;
					
				case "flex":
					var callback = {
								doFlex: function(result) {
									appendReplaceContent('contentarea',result);
									}	
								}
								var getData = new flex(callback); 
								getData.dispatcher.timeout = ajaxTimeout;
								getData.doFlex(page,action,id);
					break;
					
				default:
				alert(page + "? Request is not Registered!");
				
		}
		

}

}

function doScroll(){
	
	window.setTimeout("window.scrollTo(0,0)",10);
	
	}
function doEvalCreate(fullstring){
	startpos = fullstring.search('<script type="text/javascript"');
	if (startpos != -1){
		endpos = fullstring.search('-END-')	
		jspart = fullstring.substr(startpos+31,endpos-startpos+31);
		htmlpart1 = fullstring.substr(0,startpos-1);
		htmlpart2 = fullstring.substr(endpos+1,fullstring.length);
		textarray = new Array();
		textarray[0] = jspart;
		textarray[1] = htmlpart1 + htmlpart2;
		return textarray;
	} else {
		nfullstring = new Array();
				nfullstring[0] = '';
		nfullstring[1] = fullstring;
		return nfullstring;	
		}
		
}

function showForm(formtype){
/*		my_window= window.open (null,"mywindow1","toolbar=1");
		my_window.document.write('<head><title>Make Request</title><link rel="stylesheet" type="text/css" href="common/common.css" /></head><body class="pagetext">');

	*/	
		var contentarea = document.getElementById('contentarea');
			var callback = {
			submitrequest: function(result) {
					result  = doEvalCreate(result);
					jspart = result[0];
					htmlpart = result[1];
					//eval(jspart);
					contentarea.innerHTML = "<pre>"+jspart+"</pre>";
			}	
		}
				
		var getData = new comment(callback); 
		getData.dispatcher.timeout = ajaxTimeout;
		getData.submitrequest();
		



}

function appendReplaceContent(targetLayer,newdata,action){
	try{
		if(document.getElementById(targetLayer)){

			if((action==null)||(action=='undefined')){


/*
				fader[0] = new fadeObj(0, 'contentarea', 'ffffff', '000000', 70, 70, true);
				fader[0].message[1] = document.getElementById('contentarea').innerHTML;
				fader[0].message[2] = newdata;
				fade(0, 2, true);

*/

				HTML_AJAX_Util.setInnerHTML(targetLayer,newdata);


			} else {
				HTML_AJAX_Util.setInnerHTML	(targetLayer,newdata,'append');				
			}
		}
	} catch(err) {
		_errHandler('appendReplaceContent',err)
	}
}

function _errHandler(fnct,err){
	alert('Err in ' + fnct + '()\n' +err.message);
}


adsDelay = 15000;

function block1(){
	getAds(1,'advertblock');
	window.setTimeout('block2()',adsDelay);		
	
}

function block2(){
	getAds(2,'advertblock');
	window.setTimeout('block3()',adsDelay);		
	
}

function block3(){
	getAds(3,'advertblock');
	window.setTimeout('block4()',adsDelay);		
	
}

function block4(){
	getAds(4,'advertblock');
	window.setTimeout('block1()',adsDelay);		
}





function getAds(i,block){
	
		var callback = {
					doGenr: function(result) {

	//						var target = document.getElementById(block);
//							alert(i + ' | ' + block);
//							target.innerHTML = result;
							HTML_AJAX_Util.setInnerHTML(block,result);
						}	
					}
					var getData = new genr(callback); 
					getData.dispatcher.timeout = ajaxTimeout;
					getData.doGenr('getads',i);
}









function submitRequest(){
	alert(document.requestform.name.value);	
}


function hideJsTags(){
	total = document.links.length;
	for (i=0;i<=total;i++){
		if((document.links[i].href.search("javascript")!=-1) && (document.links[i].title='')){	
			document.links[i].title="HELLO!";	
		}
	}
}
//window.onload = initialize;		//initialize dhtmlhistory object




function doSearch(mode){
	searchwords = trimspaces(document.searchform.searchword.value);
		if (document.searchform.where){
			searchwhere = trimspaces(document.searchform.where.value);
		}
	
	document.searchform.searchword.value = searchwords;
	if (searchwords ==''){
		alert('Please enter what to search for');
		document.searchform.searchword.value = searchwords;
	} else {
		document.searchform.submitbtn.disabled = true;
		switch (mode){
			case 0:
				if (!useAjax){
					showInfo('infoservice','search'+searchwhere,0,'&searchfor='+ searchwords);	
	
				} else {
					showInfo('infoservice','search'+searchwhere,searchwords);	
				}
				
				break;
				
			case 1:
			if (!useAjax){
					showInfo('schools','search'+searchwhere,0,'&searchfor='+ searchwords);	
	
				} else {
					showInfo('schools','search'+searchwhere,searchwords);	
				}
				break;
			case 2:
				showInfo('personalities','search',searchwords);	
				break;	
			case 3:
				showInfo('flex','search',searchwords);	
				break;
			case 4:
				s_w = document.searchform.w.selectedIndex;
				t = document.searchform.t.selectedIndex;
				w = document.searchform.w.options[s_w].value;
				if (!useAjax){
					showInfo('news','search',0,'&searchfor='+ searchwords+'&w='+w+'&t='+t);	
	
				} else {
				showInfo('news','search',searchwords+ "," + w + "," + t);	
				}
				break;
			default:
				alert('Currently Unavailable');
		}
		
		
	}
	window.setTimeout('document.searchform.submitbtn.disabled = false',2500);
	return false;
}

function tellSomeone(){
	emailadds = trimspaces(document.tellafriend.email.value);
	emailerrors ='';
	document.tellafriend.email.value = emailadds;
	if ((emailadds =='')||(emailadds == 'Tell a friend about this site')){
		alert('Please enter your friend(s) email address.\n You can enter multiple addresses by separating each one with a comma');
	} else {
		
		splitaddresses = emailadds.split(",");
		for(i=0;i<splitaddresses.length;i++){
			curEmail = splitaddresses[i];
			
			if(!validEmail(curEmail)){
				emailerrors = emailerrors + "\n" + curEmail;
			}
			
		} 
		if (emailerrors !=''){
			
			alert("Please correct the following email addresses " + emailerrors);	
		} else {
			var content = document.getElementById("inviteform").innerHTML;
			showInfo('invite',emailadds,content);
			location.replace = '#top';
			
		}
		
		
	}
	
	return false;
}

function validEmail(email) {
  var emailReg = "^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$";
  var regex = new RegExp(emailReg);
  return regex.test(email);
}

	
function printableVersion(){
var printcontent = document.getElementById("contentarea");
var copyright = document.getElementById("footerarea");
	
	newwindow = window.open ("/content/index.htm","newwindow","menubar,toolbar,resizable,scrollbars");
	
	with(newwindow.document){
		open();
		write('<html><head><link href="/common/printpage.css" rel="stylesheet" type="text/css" media="screen" /><link href="/common/printpage.css" rel="stylesheet" type="text/css" media="print" /></head><body class="content">');
		write(printcontent.innerHTML);
		write(copyright.innerHTML);
		write('<br><a class="bottomlinks" href="javascript: self.close()">Close</a></body></html>');
		
		close();
	}
//	if (window.focus) {newwindow.focus()}
	
}

function doGoogleSearch(){

	searchwords = trimspaces(document.googlesearch.q.value);
	document.googlesearch.q.value = searchwords;
	if (searchwords ==''){
		alert('Please enter what to search for');
		document.googlesearch.q.value = searchwords;
	} else {
		//url = "?p="+page+"&a="+action+"&id="+id+extra;
		//urlstring = page+","+action+","+id+extra;

		if (!useAjax){
			location.href = url;
		} else {
		
		}
		var contentarea = document.getElementById('contentarea');
		
		var callback = {
			doPortalService: function(result) {
					HTML_AJAX_Util.setInnerHTML('contentarea',result);
					doScroll();
			}
		}
		var getData = new portal(callback); 
		getData.dispatcher.timeout = 50000;
		//HTML_AJAX_Util.setInnerHTML('contentarea','Running Search, Please Wait');
		doScroll();
		getData.doPortalService('gsearch',searchwords,0);
	}
	return false;
}


//BEHAVIOURS 
Behavior.register(
        "._advanced",
        function(element) {
            element.onclick = function(){
				showThis("_advanced_tab");
				//hideThis("_simple_tab");
				simple = document.getElementById("_simple");
				simple.className='tabreset';
				element.className= 'activetab';
				
			}
        }
    );
Behavior.register(
        "._simple",
        function(element) {
            element.onclick = function(){
				hideThis("_advanced_tab");
			//	showThis("_simple_tab");
				advanced = document.getElementById("_advanced");
				advanced.className='tabreset';
				element.className= 'activetab';
				
				document.searchform.w.selectedIndex=0;
				document.searchform.t.selectedIndex =0;	
			}
        }
    );
Behavior.register(
        "._allNews",
        function(element) {
            element.onclick = function(){
				if(element.checked){
					document.searchform.searchword.value = 'all';
				};
				
			}
        }
    );

Behavior.register(
        "#_searchword",
        function(element) {
            element.onblur = function(){
				if(trimspaces(element.value)!='all'){
					document.searchform.chkall.checked = false;
				};
			}
			
        }
    );

Behavior.register(
        "._invitelink",
        function(element) {
            element.onclick= function(){
				strSearch  = location.search;
				strHash = location.hash;
				element.href = '?p=invite&m='+ strSearch;
			}
			
        }
    );


