var showEffects = true;

function fadePurple(){
	var pagesize = getPageSizeWithScroll();
	var windowsize = getWindowSize();
	var fadepurplebg = document.createElement('div');

	fadepurplebg.className = 'fadepurplebg';
	fadepurplebg.setAttribute('id','fadepurplebg');
	
	var divheight;
	
	if(pagesize[1]>document.body.offsetHeight)
		divheight = pagesize[1];
	else if (windowsize[1] > document.body.offsetHeight)
		divheight = windowsize[1];
	else
		divheight = document.body.offsetHeight
	
	fadepurplebg.style.height = divheight+'px';

	document.body.appendChild(fadepurplebg)
}

function removefadePurple(){
	if (showEffects){
		new Effect.Fade('fadepurplebg',{duration:1})
		setTimeout("document.body.removeChild(document.getElementById('fadepurplebg'))",2001)
	} else
		document.body.removeChild(document.getElementById('fadepurplebg'));
}

function showPopup (popuptext,nobackfade,notinviewport){	
	var divid = 'pop_'+Math.floor(Math.random()*11)
	var handleid = 'hand_'+Math.floor(Math.random()*11)
	
	var containerdiv = document.createElement('div');
	var centrediv = document.createElement('div');
	var registration = document.createElement('div');
	var registrationbox = document.createElement('div');
	var contentcontainer = document.createElement('div');
	var containertext = document.createElement('div');

	var imgheader = document.createElement('img');
	var imgclose = document.createElement('img');
	var imgfooter = document.createElement('img');
	
	var lnkclose = document.createElement('a');
	
	containerdiv.className = 'popupcontainer';
	registrationbox.className = 'registrationbox';
	contentcontainer.className = 'contentcontainer';
	containertext.className = 'containertext';
	
	containerdiv.setAttribute('id',divid);
	registration.setAttribute('id','registration');
	containertext.setAttribute('id','containertext');
	imgheader.setAttribute('id',handleid);
	
	containerdiv.style.top = notinviewport?'0px':f_scrollTop()+'px';
	
	centrediv.setAttribute('align','center');
	registrationbox.style.textAlign='center';
	
	lnkclose.setAttribute('href','#');
	lnkclose.onclick =	function(){
							closePopup(divid)
							removefadePurple();
							return false;
						}

	imgheader.src = '/mmimages/popup-header.png'
	imgheader.style.width = '540px';
	imgheader.style.height = '65px';
	
	imgclose.src = '/mmimages/popup-close.png'
	imgclose.style.width = '90px';
	imgclose.style.height = '65px';

	imgfooter.src = '/mmimages/popup-footer.png'
	imgfooter.style.width = '630px';
	imgfooter.style.height = '25px';

	containertext.innerHTML = popuptext;
	
	lnkclose.appendChild(imgclose);
	
	contentcontainer.appendChild(containertext);
	registrationbox.appendChild(imgheader);
	registrationbox.appendChild(lnkclose);
	registrationbox.appendChild(contentcontainer);
	registrationbox.appendChild(imgfooter);
	registration.appendChild(registrationbox);
	centrediv.appendChild(registration);
	containerdiv.appendChild(centrediv);
	
	//Background should already be here by now, but just in case it's not, put it there.
	if(!document.getElementById('fadepurplebg') && !nobackfade)
		fadePurple();
	
	if (showEffects){
		containerdiv.style.display = 'none';
		
		document.body.appendChild(containerdiv);
		new Effect.SlideDown(divid,{duration:1})
	} else {
		document.body.appendChild(containerdiv);
	}

	var scriptelements = containertext.getElementsByTagName('script');
	for (i =0;i<scriptelements.length;i++)
		eval(scriptelements[i].innerHTML);

	if(notinviewport)
		document.scrollTo(0,0);
		
	fixPNG();
	
	new Draggable(divid,{handle: handleid});

}

function closePopup(divid){
	if (showEffects){
		new Effect.SlideUp(divid,{duration:1})
		setTimeout("document.body.removeChild(document.getElementById('"+divid+"'));",2001)
	} else
		document.body.removeChild(document.getElementById(divid));
}


function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {
		// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		// all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else {
		// works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
	}     arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);

	return arrayPageSizeWithScroll;
} 

function forgotPass(){
//	var query = 'APPNAME=AIM&PRGNAME=Master&ARGUMENTS=-APassRem'
	ajaxPopUp('GET','/passreminder.html','', false, true)
}

function submitPass(frm){
	if (frm.Email.value == '')
		alert ('You must enter an email address!');
	else
		ajaxPost('POST', '', create_request_string(frm))
}

function showQuestions(status){
	var query = 'APPNAME=AIM&PRGNAME=PortalQuestionsAjax&ARGUMENTS=-N'+status
	ajaxPost('POST','', query)
}


function getWindowSize() {
  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;
  }
  return new Array(myWidth,myHeight);

}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

var startedUpload = false;

function checkUpload(qstid) {
	var frmexists = document.getElementById('upfile').contentWindow.document.body.innerHTML.substr(1,5).search(/form/i) > -1;

	if (frmexists){
		var ifm = document.getElementById('upfile').contentWindow.document.form1;
		var stardedupl = ifm.submitted.value == 'yes' || startedUpload;
		var filetoup = ifm.filename.value != '';
	}
	
	if (!filetoup && frmexists) {
		document.form1.question.value = '';
		document.form1.subbut.disabled = false;
		showQuestions(document.getElementById('qstrange').value);
		document.getElementById('ok').style.display='block';
		document.getElementById('up').style.display='none';
		startedUpload = false;
	} else {
		if (!stardedupl && frmexists){
			ifm.filenumber.value = qstid;
			document.getElementById('ok').style.display='none';
			document.getElementById('up').style.display='block';
			upFile();
			startedUpload = true;
		}
	
		setTimeout('checkUpload('+qstid+')',500);
	}
}

function upFile(qstid){
	document.getElementById('upfile').contentWindow.document.form1.submit();
}

function showAnswer(qst){
	var query = 'APPNAME=AIM&PRGNAME=PortalAnswer&ARGUMENTS=-N'+qst;
	ajaxPopUp('POST','',query)
}

function showCodedAnswer(qst){
	var query = 'APPNAME=AIM&PRGNAME=QuestionCodedAnswer&ARGUMENTS=-N'+qst;
	ajaxPopUp('POST','',query);
}

function getQuestions(year, topic, own, page){
	if(typeof(page)=='undefined')
		page = '';
	document.getElementById('kbquestions').innerHTML='<p style="text-align:center"><img src="/mmimages/ajax-loader-2.gif"><br>Loading Questions...</p>';
	var query = 'APPNAME=AIM&PRGNAME=PortalKBQs&ARGUMENTS=-A'+year+',-A'+topic+',-A'+own+',-N'+page;
	ajaxPost('POST','',query)
}

function goPage(p){
	getQuestions(document.form1.year[document.form1.year.selectedIndex].value,document.form1.topic[document.form1.topic.selectedIndex].value, isOwn(), p);
}

function checkFileName(filename){
	var query = 'APPNAME=AIM&PRGNAME=PortalCheckFilename&ARGUMENTS=-A'+filename;
	ajaxPost('POST','',query)
}

function validFileTypes(){
	var query = 'APPNAME=AIM&PRGNAME=PortalValidFiles';
	ajaxPopUp('POST','',query)
}

function explainResult(r){
	var query = 'APPNAME=AIM&PRGNAME=PortalExplainResult&ARGUMENTS=-A'+r;
	ajaxPopUp('POST','',query)
}

function explainCID(){
	ajaxPopUp('GET','/portal-compid.html','')
}

function explainOverdue(){
	ajaxPopUp('GET','/portal-overdue.html','')
}

function assignCIDName(cid){
	var query = 'APPNAME=AIM&PRGNAME=PortalAssignScreen&ARGUMENTS=-A'+cid;
	ajaxPopUp('POST','',query)
}
function viewResDetails(f){
	var query = 'APPNAME=AIM&PRGNAME=PortalResResult&ARGUMENTS=-A'+f;
	ajaxPopUp('POST','',query)
}

function viewMathsDictionaryTopic(id){
	var query = 'APPNAME=AIM&PRGNAME=PortalDictTopic&ARGUMENTS=-N'+id;
	ajaxPopUp('POST','',query)
}

function updateDictionaryViews(){
	if (document.getElementById('dictrecent')){
		var query = 'APPNAME=AIM&PRGNAME=PortalDictionaryViews';
		ajaxPost('POST','',query)
	}
}

function doDictSearch(){
	document.getElementById('dictsearchres').innerHTML='<div style="text-align:center;"><img src="/mmimages/ajax-loader-2.gif" /><br>Searching...</div>';
	var query = 'APPNAME=AIM&PRGNAME=PortalDictionarySearch&ARGUMENTS=-A'+document.getElementById('dictsearchterm').value;
	ajaxPost('POST','',query)
}

function openWindow(url,width,height){
	window.open(url ,"_blank","toolbar=0,scrollbars=0location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height);
}

function startChat(){
	var frm = document.frmnewchat;
		if(frm.fname.value == ''){
		alert ('You must enter your first name!');
		frm.fname.focus();
	} else {
		document.getElementById('startresponse').innerHTML = 'Loading...';
		ajaxPost('POST','',create_request_string(frm));
	}
}

function setupGameAccount(cid,pid){
	var query = 'APPNAME=AIM&PRGNAME=PortalGameAccSetup&ARGUMENTS=-N'+cid+',-N'+pid;
	ajaxPopUp('POST','',query)
}

function checkGamePlayer(frmobj,newacc){
	if(newacc && frmobj.playeralias.value == ""){
		alert ('You must enter a username');
		frmobj.playeralias.focus();
		return false;
	} else if(newacc && frmobj.playeralias.value.length < 3){
		alert ('Username must be at least 3 characters');
		frmobj.playeralias.focus();
		return false;
	} else if ((newacc && frmobj.playerpass1.value.length < 6) || (frmobj.playerpass1.value.length > 0 && frmobj.playerpass1.value.length < 6)){
		alert ('Password should be at least 6 characters');
		frmobj.playerpass1.focus();
		return false;
	} else if (frmobj.playerpass1.value != '' && frmobj.playerpass1.value != frmobj.playerpass2.value){
		alert ('Passwords do not match!');
		frmobj.playerpass2.focus();
		return false;
	} else if (frmobj.playeralias.value != "")
		return confirm('Are you sure you want to set this players username to:\n\n'+frmobj.playeralias.value+'\n\nThis name can NOT be changed after it has been set!');
	  else 
		return true;
}