
//=================( SLIDESHOW )=======================
// (\.[$itemNumber].*\.jpg$)|(\.[$itemNumber].*\.png$)|(\.[$itemNumber].*\.jpeg$)|(\.[$itemNumber].*\.gif$)
var galleryarray=new Array();galleryarray[0]="4.gif";galleryarray[1]="4_1.gif";galleryarray[2]="4_2.gif";galleryarray[3]="4_3.gif";galleryarray[4]="4_0.gif"; 
//=================( PAGE LOADS )=====================
//Check if we are using IE.
	try {
		//If the Javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		// alert ("You are using Microsoft Internet Explorer.");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using Internet Explorer.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			// alert ("You are using Microsoft Internet Explorder");
		} catch (E) {
			//Else we must be using a non-IE browser.
			xmlhttp = false;
		}
	}
	
	//If we are using a non-IE browser, create a javascript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
		// alert ("You are not using Microsoft Internet Explorer");
	}
	
	function makerequest(serverPage, objID) {
		
		var obj = document.getElementById(objID);
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
	}
//================( POST REQUEST )====================
var http_request = false;
   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('body').innerHTML = result;
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function get(obj) {
      var poststr = "username=" + encodeURI( document.getElementById("username").value ) +
                    "&email=" + encodeURI( document.getElementById("email").value ) +
                    "&timetaken=" + encodeURI( document.getElementById("timetaken").value ) +
                    "&score=" + encodeURI( document.getElementById("score").value ) +
                    "&ipaddress=" + encodeURI( document.getElementById("ipaddress").value ) +
                    "&contestnum=" + encodeURI( document.getElementById("contestnum").value ) +
                    "&eligible=" + "1" +
                    "&anon=" + "1" +
                    "&id=" + encodeURI( document.getElementById("id").value ) +
                    "&MM_update=" + "form1";
      makePOSTRequest('editcontestant.php', poststr);
   }
	
//=================( VALIDATION )=====================
function validate() {	
	var x = document.form1.email.value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (document.form1.username.value!="" && document.form1.email.value!="" && filter.test(x)) {
		startQuiz();
	} else {
		alert("Missing Something?\r\nPlease check the username and email.");
		if (document.form1.username.value==null||document.form1.username.value=="") {
	   		document.form1.username.style.backgroundColor='#FFFF66';
		} else {
	   		document.form1.username.style.backgroundColor='#FFFFFF';
		}
		if (document.form1.email.value==null||document.form1.email.value=="") {
	   		document.form1.email.style.backgroundColor='#FFFF66';
		} else {
	   		document.form1.email.style.backgroundColor='#FFFFFF';
		}
	}	
}

//=================( SLIDESHOW )======================
var curimg=0
function rotateimages() {
document.getElementById("slideshow").setAttribute("src", galleryarray[curimg]);
curimg = (curimg < galleryarray.length-1) ? curimg+1 : 0;
}

window.onload=function(){
setInterval("rotateimages()", 3000)
}

//=================( START QUIZ )======================

function startQuiz() {
	document.getElementById('usernameDiv').style.display = "none";
	document.getElementById('subquestions').style.display = "block";
	var currentTime = new Date();
	var month = (currentTime.getMonth() +1);
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	var hours = currentTime.getHours();
	var minutes = currentTime.getMinutes();
	var seconds = currentTime.getSeconds();
		if (hours < 10){
			hours = "0" + hours;
		}
		if (minutes < 10){
			minutes = "0" + minutes;
		}
		if (seconds < 10){
			seconds = "0" + seconds;
		}
		if (day < 10){
			day = "0" + day;
		}
		if (month < 10){
			month = "0" + month;
		}
	//document.write(month + "/" + day + "/" + year);
	//alert(year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds);
	document.getElementById('timeStarted').value = String(year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds);
	countdown();
}

//====================( TIMER )========================

// how many minutes
var mins = 1;

// how many seconds (don't change this)
var secs = mins * 60;

function countdown() {
	setTimeout('Decrement()',1000);
}
function Decrement() {
	if (document.getElementById) {
		minutes = document.getElementById("minutes");
		seconds = document.getElementById("seconds");
		// if less than a minute remaining
		if (seconds < 59) {
			seconds.value = secs;
		} else {
			minutes.value = getminutes();
			seconds.value = getseconds();
		}
		secs--;
		if (secs > -1) {
			setTimeout('Decrement()',1000);	
		} else {
			document.getElementById('subquestions').style.display = "none";
			document.form1.Submit.disabled = false;
			seconds.value = 0;
				        setTimeout('SendForm()',2000);
						//alert('Thanks for playing!\r\n\r\nYour scores have been submitted.\r\n\r\nYou may now close this window.');
					}
	}
}
function SendForm() {
	document.form1.submit();
}
function getminutes() {
	// minutes is seconds divided by 60, rounded down
	mins = Math.floor(secs / 60);
	return mins; 
}
function getseconds() {
	// take mins remaining (as seconds) away from total seconds remaining
	return secs-Math.round(mins *60);
}


//====================( SLIDER )==========================

var sliderIntervalId = 0;
var sliderHeight = 18;
var sliding = false;
var slideSpeed = 18;
var tempDiv = "";
var tempHeight = "";

function Slide(thisElement, height) {
	
   tempDiv = thisElement;
   tempHeight = height;
   
   if(sliding)
      return;
   sliding = true;
   if(sliderHeight == tempHeight)
      sliderIntervalId = setInterval('SlideUpRun()', 30);
   else
      sliderIntervalId = setInterval('SlideDownRun()', 30);
}

function SlideUpRun() {
   slider = document.getElementById(tempDiv);
   if(sliderHeight <= 18) {
      sliding = false;
      sliderHeight = 18;
      slider.style.height = '18px';
      clearInterval(sliderIntervalId);
   } else {
      sliderHeight -= slideSpeed;
      if(sliderHeight < 18)
         sliderHeight = 18
      slider.style.height = sliderHeight + 'px';
   }
}

function SlideDownRun() {
   slider = document.getElementById(tempDiv);
   if(sliderHeight >= tempHeight) {
      sliding = false;
      sliderHeight = tempHeight;
      slider.style.height = tempHeight + 'px';
      clearInterval(sliderIntervalId);
   } else {
      sliderHeight += slideSpeed;
      if(sliderHeight > tempHeight)
         sliderHeight = tempHeight;
      slider.style.height = sliderHeight + 'px';
   }
}