// function: rotate()
// Used to give one element a class and give all others within the same container a different class.  Commonly used on rotator spots on frontpages.
// @thisId : string : id of element to be displayed
// @containerId : string : id of the container of elements to be hidden
// @thisClass(optional) : string : class to be given to the element. If not set, 'visible' will be used
// @containerId(optional) : string : class to be given to all other elements within container. If not set, 'hidden' will be used
function rotate( thisId, containerId, thisClass, otherClass ){
	//this is inneficient, should adapt for Prototype -- CS
	// set default values for thisClass and otherClass
	var thisClass = (thisClass == null) ? "visible" : thisClass;
	var otherClass = (otherClass == null) ? "hidden" : otherClass;
	
	// grab all elements within container
	var elements = document.getElementById(containerId).childNodes;
	
	// loop through elements
	for(var i = 0; i < elements.length; i++){
	
		// if this is the element, give it thisClass
		if( elements[i].id == thisId ){
			elements[i].className = elements[i].className.replace(thisClass, '');
			elements[i].className = elements[i].className.replace(otherClass, '');
			elements[i].className = elements[i].className + ' ' + thisClass;
		
		// if this isn't the element and the element can have a class, give it otherClass
		}else if(elements[i].className != undefined){
			elements[i].className = elements[i].className.replace(thisClass, '');
			elements[i].className = elements[i].className.replace(otherClass, '');
			elements[i].className = elements[i].className + ' ' + otherClass;
		}
	}
	
	// return id of current element
	return thisId;
}

// function: loadURL()
// Loads the URL passed to it.  This gets used for loading a page from a select box
// @url : string : the URL to be opened
function loadURL( url ){
	if( url != undefined && url != '' ){
		window.location = url;
	}
}

// function: CheckInputData()
// description stub
// @information : stub
function CheckInputData(information){
	var sendform = true;
	var regproblem = '';
	var problems = 'Your registration cannot be submitted until the\nfollowing is corrected:\n\n';

	var Form = information;	
	checkEmail = Form.email.value

		if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.'))){ 
		problems += '-You have entered an invalid email address. Please try again.\n'
			sendform = false;
			regproblem = true;
		}
	 
		if (information.pwd.value =="") {
		problems += '-Password is required.\n'
			sendform = false;
			regproblem = true;
		}

		 if (information.pwdr.value =="") {
		problems += '-Please confirm password.\n'
			sendform = false;
			regproblem = true;
		}

		if (information.pwd.value != information.pwdr.value) {
		problems += '-The passwords supplied do not match.\n'
			sendform = false;
			regproblem = true;
		}

		if (information.gender.value =="") {
		problems += '-Please select gender.\n'
			sendform = false;
			regproblem = true;
		}

		if (information.extra_birthyear.value =="") {
		problems += '-Year of birth is required.\n'
			sendform = false;
			regproblem = true;
		}

		if (information.extra_zipcode.value =="00000" || information.extra_zipcode.value == '') {
		problems += '-Zip code is required.\n'
			sendform = false;
			regproblem = true;
		}
		
		if (information.extra_countryCode.value =="") {
		problems += '-Country is required.\n'
			sendform = false;
			regproblem = true;
		}
		
		if (information.extra_incomeID.value =="" || information.extra_incomeID.value == "0") {
		problems += '-Income is required.\n'
			sendform = false;
			regproblem = true;
		}
		
		if (information.extra_industryId.value =="") {
		problems += '-Industry is required.\n'
			sendform = false;
			regproblem = true;
		}
	 
		if (information.extra_jobTitleId.value =="0") {
		problems += '-Job Title is required.\n'
			sendform = false;
			regproblem = true;
		 }

		 if (information.extra_newspaper.selectedIndex < 1) {
		problems += '-Please select usage.\n'
			sendform = false;
			regproblem = true;
		 }
	
		if (information.agreement.checked == false ) {
		problems += '-Please agree to the subscriber agreement.\n'
			sendform = false;
			regproblem = true;
		 }

		if (regproblem != true)	 {
			sendform = true;
		}
					
		// send form
		if (sendform == true) {
		//return false // change to true

		return true;
		pbsSetCookie(information);
		} else {
		alert(problems);
		return false;
		}
}

// function: NewWindow()
// Opens a popup window
// @width : int : width of new window
// @height : int : height of new window in pixels
// @url : string : url of new window
function NewWindow(width,height,url) {
	window.open(url,"PopUp","menubars=0,scrollbars=1,resizable=1,height="+height+",width="+width);
}

// function: openWin()
// description stub
// @windowURL : stub
// @windowName : stub
// @windowFeatures : stub
function openWin( windowURL, windowName, windowFeatures ){
	return window.open( windowURL, windowName, windowFeatures );
}

// function: popup()
// description stub
// @url : stub
// @name : stub
// @height : stub
// @width : stub
// @scrollbars : stub
function popup(url, name, height, width, scrollbars)
{
	var popwin;
	var opts = "toolbar=no,status=no,location=no,menubar=no,resizable=no";
	opts += ",height=" + height + ",width=" + width + ",scrollbars=" + scrollbars;

	popwin = window.open("", name, opts);

	popwin.focus();

	popwin.location = url;

}

// function: EatCookie()
// description stub
function EatCookie() {
	document.cookie="usernamepassword=;Path=/"
	document.cookie="UserRegID=;Path=/"
	document.location="/apps/pbcs.dll/frontpage?RegLogout=1"
}

// function: ChangeClassName()
// description stub
// @id : stub
// @newClassName : stub
function ChangeClassName(id, newClassName) {
		$(id).className = newClassName;
}

// function: ajaxRequest()
// description stub
// @url : stub
// @pars : string : vars to be passed to the given url in the format of 'var1=value1&var2=value2'
// @viewFunction : string : name of function to parse the returned output
//Should use Prototype's built in AJAX instead -- CS
function ajaxRequest( url, pars, viewFunction ){
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post',
			parameters: pars,
			onComplete: eval(viewFunction)
		});
}


function ajaxRequest2( url, pars, viewFunction ){
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post',
			parameters: pars,
			onComplete: eval(viewFunction)
		});
}



// function: displayPoll()
// this function should not be called directly.  It parses an ajax request for submitting the poll
// @originalRequest : object : the response received from the ajax request
function displayPoll(originalRequest){
	$('poll').style.display = 'none';
	var regex = /[\s\S]*<BODY BGCOLOR="#FFFFFF">([\s\S]*)<\/BODY>[\s\S]*/gm;
	$('poll').innerHTML = originalRequest.responseText.replace(regex,"$1");
	try{
		$('poll_submit').onclick = function(e){
			ajaxRequest( $('poll_submit').parentNode.action, Form.serialize($('poll_submit').parentNode), 'displayPoll');
			$('poll').innerHTML = '<img src="/graphics/loading.gif" alt="" /> Submitting vote...';
			return false;
		}
	}catch(e){}
	new Effect.BlindDown('poll', {duration: .5});
}


function displayPoll2(originalRequest){
	$('poll').style.display = 'none';
	var regex = /[\s\S]*<BODY BGCOLOR="#FFFFFF">([\s\S]*)<\/BODY>[\s\S]*/gm;
	$('poll').innerHTML = originalRequest.responseText.replace(regex,"$1");
	try{
		$('poll_submit').onclick = function(e){
			ajaxRequest( $('poll_submit').parentNode.action, $('poll_submit').parentNode.serialize(), 'displayPoll');
			//$('poll').innerHTML = '<img src="/graphics/loading.gif" alt="" /> Submitting vote...';
			return false;
		}
	}catch(e){}
	new Effect.BlindDown('poll', {duration: .5});
}


// function: makeVisible(), hideme()
// description: newslist and agentlist object file - show and hide thumbnails and lede
// agent102 newslist16

//should use Prototype's elem.show() or elem.hide() methods -- CS
function makeVisible(id){
	
	element = "details"+id;
	teaser = "tease" + id;

	theader = document.getElementById(teaser);
	target = document.getElementById(element);
	
	theader.style.display = 'none';
	target.style.display = 'block';
	

}

function hideMe(id){

	element = "details"+id;
	teaser = "tease" + id;

	theader = document.getElementById(teaser);
	target = document.getElementById(element);
	
	theader.style.display = 'block';
	target.style.display = 'none';
	

}



// functions: choosedate(), ResetDate(), DoPrint()
// description: for events search
// 

function choosedate () {
	window.open('/g/kalender_eng.html?searchform.dateselected','','menubar=0,titlebar=0,width=268,height=236');
	document.searchform.Interval.options[document.searchform.Interval.options.length-1].selected = true;
}

function ResetDate () {
	if (document.searchform.Interval.selectedIndex != (document.searchform.Interval.options.length-1))
	{
		document.searchform.dateselected.value = "";
	}
}

function DoPrint () {
	var f = document.searchform;
	f.action = "<pbs:prog>/events?category=print";
	f.submit();
}




// functions: setSocCat(bucketsearch)
// description: for SEARCH BOX CATEGORY
// 



// functions: 
// description: for MULTIMEDIA VIDEO PAGE
// 

		function goto(ID)
		{
		document.location.href = "/apps/pbcs.dll/section?Category=multimedia&ID=" + ID;
		}
		function getQueryVariable(variable)
		{
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		  for (var i=0;i<vars.length;i++) 
		  {
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
			  return pair[1];
			}
		  } 
		}
		function getmovieid()
		{
			document.getElementById("test").value=getQueryVariable("ID");
		}
		function mout(ID,imagename)
		{
			  document.getElementById(ID).src = "/images/" + imagename;
		}
		function mover(ID,imagename)
		{
			  document.getElementById(ID).src = "/images/" + imagename;
		}
		
		function MIn(objId,opacity,msg) 
		{
		  if (document.getElementById)
		  {
			obj = document.getElementById(objId);
			setOpacity(obj, 100);
			ddrivetip(msg, 300);
		  }
		}
		function Mout(objId,opacity) 
		{
		  if (document.getElementById)
		  {
			obj = document.getElementById(objId);
			setOpacity(obj, 40);
			hideddrivetip();
		  }
		}
		function setOpacity(obj, opacity) 
		{
			opacity = (opacity == 100)?99.999:opacity;
		    obj.style.filter = "alpha(opacity=" + opacity + ")";		  
		    obj.style.KHTMLOpacity = opacity/100;		  
		    obj.style.MozOpacity = opacity/100;		  
		    obj.style.opacity = opacity/100;
		}
		
// functions: 
// description: Rotates 2nd Profile Box on homepage		
function toggleClass( element, className ){
	element = $(element);
	var parent = element.parentNode;
	var children = parent.childNodes; // or just use element.siblings() with prototype
	for( x in children ){ // or $A(element.siblings).each(function(elem){do stuff});
		if(children[x]==element){
			try{Element.addClassName(children[x], className);}catch(err){}
		}else{
			try{Element.removeClassName(children[x], className);}catch(err){}
		}
	}
}
function removeChildClass( parent, className ){
	parent = $(parent);
	var returnElement;
	var children = parent.childNodes;
	for( x in children ){
		try{
			if(Element.hasClassName(children[x], className)){
				returnElement = children[x];
				Element.removeClassName(children[x], className);
			}
		}catch(err){}
	}
	return returnElement;
}

function toggleDisplay( element, className ){
	element = $(element);
	if(className!=''){
		var children = document.getElementsByClassName(className);
	}else{
		var parent = element.parentNode;
		var children = parent.childNodes;
	}
	for( x in children ){
		if(children[x]==element){
			Element.show(children[x]);
		}else{
			try{Element.hide(children[x]);}catch(err){}
		}
	}
}


function toggleBlind( element, className ){
	element = $(element);
	if(className!=''){
		var children = document.getElementsByClassName(className);
	}else{
		var parent = element.parentNode;
		var children = parent.childNodes;
	}
	for( x in children ){
		if(children[x]==element){
			new Effect.toggle(children[x], 'blind', {duration: 0.2});
		}else{
			try{
				if(Element.visible(children[x])){
					alert(children[x].id+' is visible');
				}
			}catch(err){}
		}
	}
}

	//----------------------------------------------
	//	tabSwitch()
	//	pass in the anchor element inside the tab
	//	You tabs should all be within a UL in the format below:
	//
	//	<ul>
	//		<li>
	//			<h2>
	//				<a onclick="tabSwitch(this);">tab</a>
	//			</h2>
	//			<div>
	//				content of the tab
	//			</div>
	//		</li>
	//	</ul>
	//
	//----------------------------------------------
	function tabSwitch(element)
	{
		// get the LI we want to set to current
		var li = element.parentNode.parentNode,
		
		// get the UL that contains all of the LIs
		ul = li.parentNode,
		
		// create an array containing all of the LIs
		liArray = ul.getElementsByTagName('li');
		
		//loop through the array of LIs
		for(x in liArray)
		{
			// the following is enclosed in a try to prevent it from throwing errors
			// if the html has been coded correctly
			try
			{
				// if the LI we are looping through is the one, set its class to 'current'
				liArray[x].className = (liArray[x] == li)?'current':'';
				
				// if the LI we are looping through is the one, display the div inside it
				liArray[x].getElementsByTagName('div')[0].style.display = (liArray[x] == li)?'block':'none';
				
			}catch(err){}
		}
	}
	
	
function tabSwitch2(boxID){
	
	var tab = $("maintabs_"+boxID);
	var box = $("mainbox_"+boxID);
	tab.className = "current";
	box.className = "current";
	box.style.display = "block";
	
	$A(tab.up().childElements()).without(tab).each(function(elem){elem.removeClassName("current")});
	$A(box.up().childElements()).without(box).each(function(elem){elem.removeClassName("current"); elem.hide()});
	
}
	
	
	
	
//BEGIN EXPAND | COLLAPSE - COOKIE FUNTIONS
function spin(name) {
//	alert(name);
	//console.log(name);
	var spinner = $(name + "");
	if(!spinner) return;
	//console.log(spinner);
	//spinner.innerHTML = 'close';
	//if(spinner.innerHTML == "view more"){
//		spinner.innerHTML = "close";
	//}else{
	//	spinner.innerHTML = "view more";
	//}
	var spinner_content = $( name+"_body" );
	if(!spinner_content) return;
	if ( !spinner_content.visible() ) {
		
		//spinner_content.className = 'shown';
		//spinner_content.style.display = 'block';
		//
		spinner.innerHTML = 'close';
		new Effect.BlindDown(spinner_content, {afterFinish:function(){
							//spinner_content.className = "shown";
							spinner.innerHTML = 'close';
							spinner_content.style.display = "block";
							
						}.bind(this)});
	}
	else {
		
		//spinner_content.className = 'hidden';
		//spinner_content.style.display = 'none';
		spinner.innerHTML = 'view more';
							
		new Effect.BlindUp(spinner_content, {afterFinish:function(){
							//spinner_content.className = "hidden";
							spinner_content.style.display = "none";
							spinner.innerHTML = 'view more';
							
						}.bind(this)});
	}
}


function spin2(name){
	//console.log(name);
	var spinner = $(name + "");
	if(!spinner) return;
	
	var spinner_content = $( name+"_body" );
	var spinner_mini = $(name+"_mini");
	//console.log(spinner_mini);
	if(!spinner_content) return;
	if(!spinner_mini){ 
		spin(name);
		return
	}
	if ( !spinner_content.visible() ) {
		
		spinner.innerHTML = 'close';
		
		new Effect.BlindUp(spinner_mini, {afterFinish:function(){
			//spinner_mini.className = "hidden";
			spinner_mini.style.display = "none";
			new Effect.BlindDown(spinner_content, {afterFinish:function(){
							//spinner_content.className = "shown";
							spinner.innerHTML = 'close';
							spinner_content.style.display = "block";
							
						}.bind(this)});
		}.bind(this)});
	}
	else {
		
		spinner.innerHTML = 'view more';
							
		new Effect.BlindUp(spinner_content, {afterFinish:function(){
							//spinner_content.className = "hidden";
							spinner_content.style.display = "none";
							spinner.innerHTML = 'view more';
							
							new Effect.BlindDown(spinner_mini, {afterFinish:function(){
									spinner_mini.style.display = "block";
									//spinner_mini.className = "shown";
							}.bind(this)});
							
						}.bind(this)});
	}


}



function getCookie2(name) {
	var cookieValue = document.cookie;
	var cookieStartsAt = cookieValue.indexOf(" " + name + "=");
	if (cookieStartsAt == -1) {
		cookieStartsAt = cookieValue.indexOf(name + "=");
	}
	if (cookieStartsAt == -1) {
		cookieValue = null ;
	}
	else {
		cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
		var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt); 
		if (cookieEndsAt == -1) {
			cookieEndsAt = cookieValue.length;
		}
		cookieValue = unescape(cookieValue.substring(cookieStartsAt,cookieEndsAt));
	}
	return cookieValue;
}

function setCookie( user_hide, value, expiredays ) {
	var exdate=new Date();
	if (getCookie2(user_hide) == '1') {
		exdate.setDate( exdate.getDate()+expiredays );
		document.cookie=user_hide+"=0"+(( expiredays==null ) ? "" : ";expires="+exdate.toGMTString());
	}
	else {
		exdate.setDate( exdate.getDate()+expiredays );
		document.cookie=user_hide+ "=" +escape( value )+(( expiredays==null ) ? "" : ";expires="+exdate.toGMTString());
	}
}


Event.observe(window, 'load', checkCookie);

function checkCookie() {
	var name=995;
	for (name=995;name<=1700;name++) {
		if (getCookie2(name)=='1') {
//		alert (name);
			spin2(name);
		}
	}
}



