//Interstitial Content Box v1.1- http://www.dynamicdrive.com/dynamicindex17/interstitial.htm
//Last modified: Nov 26th, 06' (New: disable webpage scrollbar, auto hide after x seconds options, 

var count = 0;
var total = 0;
var bGotCC = false;
var CCInnerDiv;
var gnDatum;
var gnVote;
var gaCCChkBoxes;

function img_load() {
	count++; 
	//alert(count+'/'+total); 
	if (count==total) {
		//alert('Showing Container');
		interstitialBox.showcontainer();
		content = document.getElementById("interContent");
		CCInnerDiv = content.firstChild;
		bGotCC = true;
		count=0;
		//makeCanForm();
	}
}

var interstitialBox={
//1) list of files on server to randomly pick from and display
displayfiles: ['chooseCandidates.php'],

//2) display freqency: ["frequency_type", "frequency_value"]
displayfrequency: ["chance", "1"],

//3) HTML for the header bar portion of the interstitial box
defineheader: '<div class="headerbar"><a href="#" onClick="javascript:interstitialBox.closeit(); return false"><img src="javascripts/closeit.gif" style="border: 0" title="Close Box"/></a></div>',

//4) cookie setting: ["cookie_name", "cookie_path"]
cookiesetting: ["stitialcookie", "path=/"],

//5) bust caching of pages fetched via Ajax?
ajaxbustcache: false,

//6) Disable browser scrollbars while interstitial is shown (Only applicable in IE7/Firefox/Opera8+. IE6 will just auto scroll page to top)?
disablescrollbars: false,

//7) Auto hide Interstitial Box after x seconds (0 for no)?
autohidetimer: 0,

////No need to edit beyond here//////////////////////////////////

ie7: window.XMLHttpRequest && document.all && !window.opera,
ie7offline: this.ie7 && window.location.href.indexOf("http")==-1, //check for IE7 and offline
launch:false,
scrollbarwidth: 16,

ajaxconnect:function(url, thediv){
var page_request = false
var bustcacheparameter=""
if (window.XMLHttpRequest && !this.ie7offline) // if Mozilla, IE7 online, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE6 or below, or IE7 offline (for testing purposes)
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
interstitialBox.loadpage(page_request, thediv)
}
if (this.ajaxbustcache) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
},

loadpage:function(page_request, thediv){
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
		content = document.getElementById("interContent");
		content.innerHTML=page_request.responseText;
		imgs = content.getElementsByTagName('img');
		total = imgs.length;
		//alert (imgs.length);
		for (var i=0; i<imgs.length; i++) {
			if (imgs[i].addEventListener) {
				imgs[i].addEventListener('load', img_load, false);
		 	} else if(imgs[i].attachEvent) {
				imgs[i].attachEvent('onload', img_load);
		  	} else {
			  imgs[i].onload=img_load;
		 	}
		}
	}
},

createcontainer:function(){
/*//write out entire HTML for Interstitial Box:
document.write('<div id="interContainer">'+this.defineheader+'<div id="interContent"></div></div>')
this.interContainer=document.getElementById("interContainer") //reference interstitial container
this.interVeil=document.getElementById("interVeil") //reference veil*/
this.standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
/*this.interContainer = document.createElement('DIV');
this.interContainer.setAttribute('id', 'interContainer');
this.interContainer.innerHTML = this.defineheader+'<div id="interContent"></div>';
this.standardbody.appendChild(this.interContainer);*/
//alert(this.standardbody.lastChild.innerHTML);
this.interContainer = document.getElementById('interContainer');
},


showcontainer:function(){
//if (this.interContainer.style.display=="none") return //if interstitial box has already closed, just exit (window.onresize event triggers function)
var ie=document.all && !window.opera
//var dom=document.getElementById
var scroll_top=(ie)? this.standardbody.scrollTop : window.pageYOffset
var scroll_left=(ie)? this.standardbody.scrollLeft : window.pageXOffset
var docwidth=(ie)? this.standardbody.clientWidth : window.innerWidth-this.scrollbarwidth
var docheight=(ie)? this.standardbody.clientHeight: window.innerHeight
var docheightcomplete=(this.standardbody.offsetHeight>this.standardbody.scrollHeight)? this.standardbody.offsetHeight : this.standardbody.scrollHeight
var objwidth=this.interContainer.offsetWidth
var objheight=this.interContainer.offsetHeight
/*this.interVeil.style.width=docwidth+"px" //set up veil over page
this.interVeil.style.height=docheightcomplete+"px" //set up veil over page
this.interVeil.style.left=0 //Position veil over page
this.interVeil.style.top=0 //Position veil over page
this.interVeil.style.visibility="visible" //Show veil over page*/
this.interContainer.style.left=docwidth/2-objwidth/2+"px" //Position interstitial box
var topposition=(docheight>objheight)? scroll_top+docheight/2-objheight/2+"px" : scroll_top+5+"px" //Position interstitial box
this.interContainer.style.top=Math.floor(parseInt(topposition))+"px"
this.interContainer.style.display='block';
this.interContainer.style.visibility="visible"; //Show interstitial box
},

drop_task:function(target, functionref, tasktype){ 
	var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
	if (target.removeEventListener)
		target.removeEventListener(tasktype, functionref, false)
	else if (target.detachEvent)
		target.detachEvent(tasktype, functionref)
},

closeit:function(){
//this.interVeil.style.display="none"
this.interContainer.style.width='600px';
//this.interContainer.style.display="none";
this.interContainer.style.visibility="hidden";
content = document.getElementById("interContent");
if (window.frameLogin || window.frameSubmit) {
	//Fixes werid firefox bug
} else {
	while (content.hasChildNodes()) {
		content.removeChild(content.firstChild);
	}
}
/*h1 = document.createElement('h1');
h1.appendChild(document.createTextNode('Loading...'));
content.appendChild(h1);*/
/*if (this.disablescrollbars && window.XMLHttpRequest) //if disablescrollbars enabled and modern browsers- IE7, Firefox, Safari, Opera 8+ etc
	this.standardbody.style.overflow="auto"
if (typeof this.timervar!="undefined") clearTimeout(this.timervar)*/
	var tasktype=(window.addEventListener)? 'resize' : "onresize"
if (window.removeEventListener)
	window.removeEventListener(tasktype, function(){interstitialBox.showcontainer()}, false)
else if (window.detachEvent)
	window.detachEvent(tasktype, function(){interstitialBox.showcontainer()})
},

getscrollbarwidth:function(){
var scrollbarwidth=window.innerWidth-(this.interVeil.offsetLeft+this.interVeil.offsetWidth) //http://www.howtocreate.co.uk/emails/BrynDyment.html
this.scrollbarwidth=(typeof scrollbarwidth=="number")? scrollbarwidth : this.scrollbarwidth
},

hidescrollbar:function(){
if (this.disablescrollbars){ //if disablescrollbars enabled
if (window.XMLHttpRequest) //if modern browsers- IE7, Firefox, Safari, Opera 8+ etc
this.standardbody.style.overflow="hidden"
else //if IE6 and below, just scroll to top of page to ensure interstitial is in focus
window.scrollTo(0,0)
}
},

dotask:function(target, functionref, tasktype){ //assign a function to execute to an event handler (ie: onunload)
	var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
	if (target.addEventListener)
		target.addEventListener(tasktype, functionref, false)
	else if (target.attachEvent)
		target.attachEvent(tasktype, functionref)
},

initialize:function(sPage){
this.createcontainer() //write out interstitial container
this.ajaxconnect(sPage, this.interContainer) //load page into content via ajax
//this.dotask(window, function(){interstitialBox.hidescrollbar(); interstitialBox.getscrollbarwidth(); setTimeout("interstitialBox.showcontainer()", 100)}, "load")
//this.dotask(window, function(){interstitialBox.showcontainer()}, "resize")
//bSeen = true
//this.showcontainer()
}
}

/////////////End of interstitialBox object declaration here ////////////////////////////////

//var http_request;
//makeCanBox();

function makeCanBox(sPage) {
	sCCGoto=sPage;
	container = document.getElementById('interContainer');
	container.style.width='526px';
//	if(!bGotCC) {
		interstitialBox.createcontainer();
		interstitialBox.ajaxconnect('chooseCandidates.php?goto=' + sPage, container)
		//nterstitialBox.initialize('chooseCandidates.php?goto=' + sPage);
		//interstitialBox.dotask(window, function(){interstitialBox.showcontainer()}, "resize")
		setTimeout("interstitialBox.showcontainer()", 500);
		setTimeout("interstitialBox.showcontainer()", 1550);
		aInptGt = document.getElementsByName('CCgoto');
		for(i=0; i<aInptGt; i++) {
			aInptGt[i].value=sCCgoto;
		}
/*	} else {
		content = document.getElementById("interContent");
		content.innerHTML="";
		content.appendChild(CCInnerDiv);
		interstitialBox.interContainer.style.display="block"
		interstitialBox.showcontainer();
		interstitialBox.dotask(window, function(){interstitialBox.showcontainer()}, "resize");
	}*/
}

function setCans() {
	chkBoxes = document.getElementsByName('cans[]');
	sCans = '';
	i = 0;
	for(; i<chkBoxes.length; i++) {
		if (chkBoxes[i].checked) {
			sCans += chkBoxes[i].getAttribute('value') + ', ';
		}
	}
	if (sCans.length>0) {
		sCans = sCans.substring(0,sCans.length-2);
	}
	//alert('cans:' + sCans);
	Set_Cookie('sCans', sCans, 365, '/', '', '');
	try {
		interstitialBox.closeit();
	} catch (e) {}
	//window.location='http://www.votegopher.com/issue.php';
	window.location=sCCGoto;
}

function setCansProblem(sGoto) {
	chkBoxes = document.getElementsByName('cans[]');
	sCans = '';
	i = 0;
	for(; i<chkBoxes.length; i++) {
		if (chkBoxes[i].checked) {
			sCans += chkBoxes[i].getAttribute('value') + ', ';
		}
	}
	if (sCans.length>0) {
		sCans = sCans.substring(0,sCans.length-2);
	}
	Set_Cookie('sCans', sCans, 365, '/', '', '');
	try {
		interstitialBox.closeit();
	} catch (e) {}
	window.location=sGoto;
}


function ccLimit5(cbox) {
//	alert('Testing');
	cbox = document.getElementById(cbox);
	if(cbox.checked) {
		cbox.checked=false;
	} else if(!cbox.disabled) {
		cbox.checked=true;
	}
	if (!gaCCChkBoxes) {
		gaCCChkBoxes = document.getElementsByName('cans[]');
	}
	nChecked = 0;
	for (i=0; i<gaCCChkBoxes.length; i++) {
		if (gaCCChkBoxes[i].checked) {
			nChecked++;
		}
	}
	if (nChecked>3) {
		for (i=0; i<gaCCChkBoxes.length; i++) {
			if (!gaCCChkBoxes[i].checked) {
				gaCCChkBoxes[i].disabled=true;
			}
		}
	} else {
		for (i=0; i<gaCCChkBoxes.length; i++) {
			gaCCChkBoxes[i].disabled=false;
			//alert(gaCCChkBoxes[i].disabled);
		}
	}
}

/*function ccLimit5(cbox) {
	setTimeout("ccLimit5Helper('" + cbox + "')", 100);
}*/

function watchVideo(sKey) {
	//alert('watchVideo');
	html = '<center><div style="margin-left:auto;margin-right:auto"><object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/' + sKey + '"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/' + sKey + '" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object></div></center>';
	interstitialBox.createcontainer();
	content = document.getElementById("interContent");
	content.innerHTML=html;
	interstitialBox.interContainer.style.display="block"
	interstitialBox.showcontainer();
	//interstitialBox.dotask(window, function(){interstitialBox.showcontainer()}, "resize");
	//bSeen = false;//Make the change candidates gran its stuff again
	return false;
}

function resizeframe(nSize) {
	content = document.getElementById("interContent");
	frame = content.firstChild;
	frame.style.height = nSize + 'px';
	interstitialBox.showcontainer();
}

function login() {
	//alert("arun");
	interstitialBox.createcontainer();
	interstitialBox.interContainer.style.width='590px';
	content = document.getElementById("interContent");
	content.innerHTML='<iframe src="http://www.newsgopher.com/login.php?goto=closeBox.htm" style="overflow:hidden; width:100%; height:620px; border:none; background-color:#002255; B" frameborder="0" name="frameLogin"></iframe>';
	interstitialBox.interContainer.style.display="block"
	interstitialBox.showcontainer();
	//interstitialBox.dotask(window, function(){interstitialBox.showcontainer()}, "resize");
	//bSeen = false;//Make the change candidates grab its stuff again
	return false;
}
function more12(id) {
	//alert(id);
	interstitialBox.createcontainer();
	interstitialBox.interContainer.style.width='550px';
	content = document.getElementById("interContent");
	content.innerHTML='<iframe src="blogPopUP.php?id='+id+'&goto=closeBox.htm" style="overflow:hidden; width:100%; height:330px; border:none; background-color:#002255;" frameborder="0" name="frameLogin"></iframe>';
	interstitialBox.interContainer.style.display="block"
	interstitialBox.showcontainer();
	//interstitialBox.dotask(window, function(){interstitialBox.showcontainer()}, "resize");
	//bSeen = false;//Make the change candidates grab its stuff again
	return true;
}

function issueBack(id) {
	//alert(id);
	interstitialBox.createcontainer();
	interstitialBox.interContainer.style.width='550px';
	content = document.getElementById("interContent");
	content.innerHTML='<iframe src="issueBack.php?id='+id+'&goto=closeBox.htm" style="overflow:auto; width:100%; height:330px; border:none; background-color:#002255;" frameborder="0" name="frameLogin" ></iframe>';
	interstitialBox.interContainer.style.display="block"
	interstitialBox.showcontainer();
	//interstitialBox.dotask(window, function(){interstitialBox.showcontainer()}, "resize");
	//bSeen = false;//Make the change candidates grab its stuff again
	return true;
}
function addTopic() {
	//alert(id);
	interstitialBox.createcontainer();
	interstitialBox.interContainer.style.width='350px';
	content = document.getElementById("interContent");
	content.innerHTML='<iframe src="addTopic.php?&goto=closeBox.htm" style="overflow:hidden; width:100%; height:200px; border:none; background-color:#002255;" frameborder="0" name="frameLogin"></iframe>';
	interstitialBox.interContainer.style.display="block"
	interstitialBox.showcontainer();
	//interstitialBox.dotask(window, function(){interstitialBox.showcontainer()}, "resize");
	//bSeen = false;//Make the change candidates grab its stuff again
	return true;
}
function recentSubmit() {
	//alert("arun");
	interstitialBox.createcontainer();
	interstitialBox.interContainer.style.width='638px';
	content = document.getElementById("interContent");
	content.innerHTML='<iframe src="submitElectiion.php?goto=closeBox.htm" style="overflow:hidden; width:100%; height:490px; border:none; background-color:#002255;" frameborder="0" name="frameLogin"></iframe>';
	interstitialBox.interContainer.style.display="block"
	interstitialBox.showcontainer();
	//interstitialBox.dotask(window, function(){interstitialBox.showcontainer()}, "resize");
	//bSeen = false;//Make the change candidates grab its stuff again
	return true;
}

function register() {

	interstitialBox.createcontainer();

	interstitialBox.interContainer.style.width='550px';

	content = document.getElementById("interContent");

	content.innerHTML='<iframe src="newuser.php?goto=closeBox.htm" style="overflow:hidden; width:100%; height:330px; border:none; background-color:#002255;" frameborder="0" name="frameLogin"></iframe>';

	interstitialBox.interContainer.style.display="block"

	interstitialBox.showcontainer();

	//interstitialBox.dotask(window, function(){interstitialBox.showcontainer()}, "resize");

	//bSeen = false;//Make the change candidates grab its stuff again

	return false;

}

function login_vote() {
	interstitialBox.createcontainer();
	content = document.getElementById("interContent");
	content.innerHTML='<iframe src="login.php?goto=closeBoxVote.php%3Fid=' + gnDatum + '%26vote=' + gnVote + '" style="overflow:hidden; width:100%; height:160px; border:none; background-color:#999999;" name="frameLogin" frameborder="0"></iframe>';
	interstitialBox.interContainer.style.display="block"
	interstitialBox.showcontainer();
	//interstitialBox.dotask(window, function(){interstitialBox.showcontainer()}, "resize");
	//bSeen = false;//Make the change candidates grab its stuff again
	return false;
}

function setLogOut() {
	aLogIn = document.getElementById('LogIn');
	aLogIn.style.display='none';
	aLogIn = document.getElementById('LogOut');
	aLogIn.style.display='inline';
	aAll = document.getElementById('aAll');
	aAll.style.display='none';
	aChP = document.getElementById('aChP');
	aChP.style.display='inline';
}

function submitDatum(nCan, nIssue, nSect) {


interstitialBox.createcontainer();
	//interstitialBox.interContainer.style.width='638px';
	content = document.getElementById("interContent");
	content.innerHTML='<iframe src="newdata_frame.php?sect=' + nSect + '&amp;can=' + nCan + '&amp;issue=' + nIssue + '" style="overflow:hidden; width:100%; height:300px; border:none; background-color:#ffffff;" name="frameLogin" frameborder="0"></iframe>';
	interstitialBox.interContainer.style.display="block"
	interstitialBox.showcontainer();
	//interstitialBox.dotask(window, function(){interstitialBox.showcontainer()}, "resize");
	//bSeen = false;//Make the change candidates grab its stuff again
	return true;

	
}

function addVote(nDatum, nVote) {
	//alert('check');
	//sJSAfterLogIn = addVote(nDatum, nVote);
	if (window.XMLHttpRequest) {// if Mozilla, IE7 online, Safari etc
		httpRequest = new XMLHttpRequest();
	} else if (window.ActiveXObject){ // if IE6 or below
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try{
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	} else {
		return false;
	}
	
	gnDatum = nDatum;
	gnVote = nVote;
	httpRequest.open('GET', 'addvote_js.php?id=' + nDatum + '&vote=' + nVote);
	httpRequest.onreadystatechange=voteAdded;
	httpRequest.send(null);	
}

function voteAdded() {
	//alert('chek12');
	if (httpRequest.readyState == 4 && httpRequest.status==200) {
		//alert(httpRequest.responseText);
		xml = httpRequest.responseXML;
		if (!xml) {
			return;
		}
		responses = xml.getElementsByTagName('voteresponse');
		response = responses[0];
		if (response.getAttribute('nouser')) { //User is not logged in
			login_vote();
			return;
		}
		if(responses) {
			response = responses[0];
			text = response.getAttribute('response');
			nDatum = response.getAttribute('datum');
			nVote = response.getAttribute('vote');
			interstitialBox.createcontainer();
			content = document.getElementById("interContent");
			content.innerHTML='<h2 style="margin:2px; text-align:center">' + text + '</h2>';
			interstitialBox.interContainer.style.width="300px";
			interstitialBox.interContainer.style.display="block";
			interstitialBox.showcontainer();
			//interstitialBox.dotask(window, function(){interstitialBox.showcontainer()}, "resize");
			if (nVote!=0) {
				var spnTotal;
				var spnDrop;
				if (nVote==1) {
					spnTotal = document.getElementById('spnVote' + nDatum + '1');
					spnDrop = document.getElementById('spnVote' + nDatum + '_1');
				} else {
					spnTotal = document.getElementById('spnVote' + nDatum + '_1');
					spnDrop = document.getElementById('spnVote' + nDatum + '1');
				}
				sTotal = spnTotal.firstChild;
				nTotal = 1 + parseInt(sTotal.nodeValue);
				sTotal.nodeValue = ''.concat(nTotal);
				if(response.getAttribute('change')==1) {
					sDrop = spnDrop.firstChild;
					nTotal = parseInt(sTotal.nodeValue)-1;
					sDrop.nodeValue = ''.concat(nTotal);
				}
				//FIX ME: vote changed. test?
			}
		}
	}	
}


function newComment(nCan, nIssue, nParent) {
	interstitialBox.createcontainer();
	content = document.getElementById("interContent");
	gets = "?can=" + nCan + "&issue=" + nIssue + "&parent=" + nParent;
	content.innerHTML='<iframe src="addComment_frame.php' + gets + '" style="overflow:hidden; width:100%; height:350px; border:none; background-color:#999999;" name="frameLogin" frameborder="0"></iframe>';
	interstitialBox.interContainer.style.display="block"
	interstitialBox.showcontainer();
	//interstitialBox.dotask(window, function(){interstitialBox.showcontainer()}, "resize");
	//bSeen = false;//Make the change candidates grab its stuff again
	return false;
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function Get_Cookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];
 
  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   //dumped_text += dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
} 
