

/*--------------------------------------------------
 common.js

 Date: 10/19/2006
 Purpose: Common JavaScript functionality
/--------------------------------------------------*/





var _sendArticleUrl = "/article/send/default.asp";





/***************************************************
 printContent()

 Date: 12/12/2007
 Purpose: Open a printer friendly page
***************************************************/

function printContent( container ) {
	var html = new Array();
	var contents = $A( $$(".ContentContainer") )[0].innerHTML;
	var windowWidth = (screen.width * .75);
	var windowHeight = (screen.height * .75);
	var leftPosition = (screen.width / 2) - (windowWidth / 2);
	var topPosition = (screen.height / 2) - (windowHeight / 2) - (screen.height * .05);
	var properties = new Template( "left=#{left},top=#{top},width=#{width},height=#{height},scrollbars=yes" );
	printWindow = window.open( "about:blank", 
							   "PrintWindow", 
							   properties.evaluate( { 
									left: leftPosition, 
									top: topPosition,
									width: windowWidth,
									height: windowHeight
								} ) );
	html.push( "<html>" );
	html.push( "<head>" );
	html.push( "<title>Printer Friendly</title>" );
	html.push( "<link rel=\042stylesheet\042 type=\042text/css\042 href=\042/includes/css/style-screen.css\042 />" );
	html.push( "<style type=\042text/css\042 media=\042screen\042>" );
	html.push( "body" );
	html.push( "{" );
	html.push( "	margin: 0px;" );
	html.push( "	padding: 0px;" );
	html.push( "	background-color: #808080;" );
	html.push( "}" );
	html.push( "p" );
	html.push( "{" );
	html.push( "	margin: 5px;" );
	html.push( "	padding: 4px;" );
	html.push( "	background-color: #fff;" );
	html.push( "}" );
	html.push( ".Paper" );
	html.push( "{" );
	html.push( "	margin: 12px;" );
	html.push( "	padding: 25px;" );
	html.push( "	background-color: #fff;" );
	html.push( "	border-top: 1px solid #000;" );
	html.push( "	border-left: 1px solid #000;" );
	html.push( "	border-right: 2px solid #000;" );
	html.push( "	border-bottom: 2px solid #000;" );
	html.push( "}" );
	html.push( ".Print" );
	html.push( "{" );
	html.push( "	display: block;" );
	html.push( "	position: relative;" );
	html.push( "	width: 100%;" );
	html.push( "	height: auto;" );
	html.push( "	padding-left: 15px;" );
	html.push( "}" );
	html.push( ".Print input" );
	html.push( "{" );
	html.push( "	font-size: 1.0em;" );
	html.push( "	margin: 15px 8px 15px 0px;" );
	html.push( "	padding: 4px;" );
	html.push( "}" );
	html.push( "</style>" );
	html.push( "<style type=\042text/css\042 media=\042print\042>" );
	html.push( ".Print" );
	html.push( "{" );
	html.push( "	display: none;" );
	html.push( "}" );
	html.push( "</style>" );
	html.push( "</head>" );
	html.push( "<body style=\042text-align:left;\042>" );
	html.push( "<div class=\042Print\042>" );
	html.push( "<input type=\042button\042 value=\042Print this page\042 onclick=\042window.print()\042 />" );
	html.push( "<input type=\042button\042 value=\042Close\042 onclick=\042self.close()\042 />" );
	html.push( "</div>" );
	html.push( "<div class=\042Paper\042>" );
	html.push( "<p align=\042center\042><img src=\042/images/gfx_logos/logo_mic-sm.gif\042 alt=\042\042 border=\0420\042 /></p>" );
	html.push( stripJumpLinks( contents ) );
	html.push( "<p align=\042center\042>Making It Count &bull; www.makingitcount.com</p>" );
	html.push( "</div>" );
	html.push( "</body>" );
	html.push( "</html>" );
	printWindow.document.writeln( html.join( "\n" ) );
	printWindow.document.close();
}



/***************************************************
 stripJumpLinks()

 Date: 12/11/2008
 Purpose: Strip jump links
***************************************************/

function stripJumpLinks( content ) {
	var markers = [
		"<!-- Start Jump Links -->",
		"<!-- End Jump Links -->"
	];
	var position = [
		0,
		0
	];
	var chunks = [
		"",
		""
	];
	position[0] = content.indexOf( markers[0] ) + markers[0].length;
	position[1] = content.indexOf( markers[1] );
	chunks[0] = content.substring( 0, position[0] );
	chunks[1] = content.substring( position[1] );
	return chunks[0] + chunks[1];
}



/***************************************************
 emailContent()

 Date: 12/12/2007
 Purpose: Email a friend
***************************************************/

function emailContent( articleID ) {
	var windowWidth = 600;
	var windowHeight = screen.height * .60;
	if( windowHeight < 400 ) {
		windowHeight = 400;
	}
	var leftPosition = (screen.width / 2) - (windowWidth / 2);
	var topPosition = (screen.height / 2) - (windowHeight / 2) - (screen.height * .05);
	var properties = new Template( "left=#{left},top=#{top},width=#{width},height=#{height},scrollbars=yes,resizable=no,location=no,menubar=no" );
	emailWindow = window.open( _sendArticleUrl + "?id=" + articleID,
							   "EmailWindow", 
							   properties.evaluate( { 
										left: leftPosition, 
										top: topPosition,
										width: windowWidth,
										height: windowHeight
									} ) );
}



/*******************************************************************
 rateArticle()

 Date: 12/6/2008
 Purpose: Rate an article
********************************************************************/

function rateArticle( element ) {
	var id = element.id;
	_myRatingValue = parseInt( id.substring(id.length - 1) );
	// Set post parameters
	var pars = {
		ArticleID: _currentArticleID,
		RatingValue: _myRatingValue
	};
	// Set Ajax.Request options
	var options = {
		method: "post",
		generateUniqueUrl: true, 
		parameters: pars
	};
	// Run the request
	var myAjax = new Ajax.Request( _ratingUrl, options );
}



/*******************************************************************
 showStars()

 Date: 12/6/2008
 Purpose: Show rating stars
********************************************************************/

function showStars( event ) {
	var areaID = Event.element( event ).id;
	var ratingValue = areaID.substring( areaID.length - 1 );
	var toolTipID = "RatingToolTip" + ratingValue;
	$("RatingStars").src = _baseRatingStarPath + ratingValue + ".png";
	$(toolTipID).setStyle( { 
		left: ( Event.pointerX( event ) - 20 ) + "px",
		top: ( Event.pointerY( event ) - 40 ) + "px"
	 } );
	$(toolTipID).show();
}
		

/*******************************************************************
 hideStars()

 Date: 12/6/2008
 Purpose: Hide rating stars
********************************************************************/

function hideStars( event ) {
	if( _myRatingValue == 0 ) {
		$("RatingStars").src = _baseRatingStarPath + "0.png";
	} else {
		$("RatingStars").src = _baseRatingStarPath + _myRatingValue + ".png";
	}
	$$(".RatingToolTip").invoke("hide");
}



/***************************************************
 countChars( textboxID, counterID )

 Date: 10/18/2006
 Purpose: Count and limit the chars in a textbox. 
 Update a counter element with the remaining chars
***************************************************/

function countChars( textboxID, counterID ) {
	if( t = $( textboxID ) ) {
		var count = t.value.length;
		var max = t.getAttribute( "maxlength" ) * 1;
		if( count > max ) {
			t.value = t.value.substring( 0, max );
		} else {
			$( counterID ).innerHTML = '<br />characters left: ' + ( max - count );
		}
	}
}



/***************************************************
 toggleContainer()

 Date: 12/12/2007
 Purpose: Toggle container display based on condition.
 containers parameter can be a string or an array
***************************************************/

function toggleContainer( containers, condition ) {
	var c;
	// Convert containers to an array, if not one already
	containers = Object.isArray( containers ) ? containers : [ containers ];
	// To display or not to display
	containers.each( function( e ){ if( c = $( e ) ) { condition ? c.show() : c.hide(); } } );
}



/***************************************************
 attachRadioEventHandler()

 Date: 7/26/2007
 Purpose: Attach a handler to all buttons in a radio group
***************************************************/

function attachRadioEventHandler( baseName, eventName, handler ) {
	var i = 0;
	var element = new Object();
	while( element = $(baseName + "_" + i) ){
		Event.observe( element.id, eventName, handler );
		i++;
	}
}



/***************************************************
 categoryChange( categoryID, subcategoryID )

 Date: 10/18/2006
 Purpose: Disable/enable the subcategory box
***************************************************/

function categoryChange( categoryID, subcategoryID ) {
	if( e = $( categoryID ) ) {
		if( o = $( subcategoryID ) ) {
			if( e.options[e.selectedIndex].value == 7 ){
				Element.show( o );
			} else {
				Element.hide( o );
			}
		}
	}
}



/*******************************************************************
 getRadioValue( element )

 Date: 7/17/2007
 Purpose:  Get a radio field's value
***********************************************************/	

function getRadioValue( radio ) {
	var value = "";
	if( typeof( radio ) != "object" ) {
		if( ! ( radio = document.forms[0].elements[radio] ) ) return;
	}
	if( radio.length ){
		for( i = 0; i < radio.length; i++ ) {
			if( radio[i].checked ) {
				value = radio[i].value;
				break;
			}
		}
	}
	return value;
}



/***************************************************
 updateDate()

 Date: 9/22/2006
 Purpose: Update a Form class date field's value
***************************************************/

function updateDate( dateToUpdate, dateValueElement ) {
	// Get date field elements
	var dateToUpdate_Month = new Object();
	var dateToUpdate_Day = new Object();
	var dateToUpdate_Year = new Object();
	if( ! ( dateToUpdate_Month = $(dateToUpdate + "_Month") ) ) return;
	if( ! ( dateToUpdate_Day = $(dateToUpdate + "_Day") ) ) return;
	if( ! ( dateToUpdate_Year = $(dateToUpdate + "_Year") ) ) return;
	// Get new date value
	if( typeof( dateValueElement ) != "object" ) {
		// Try to create object
		if( ! ( dateValueElement = $(dateValueElement) ) ) return;
	}
	if( ! dateValueElement.value )  return;
	var date = dateValueElement.value;
	var newDate = new Date( date );
	var month = newDate.getMonth() + 1;
	var day = newDate.getDate();
	var year = newDate.getFullYear();
	setSelectBoxSelectedValue( dateToUpdate_Month, month.toString() );
	setSelectBoxSelectedValue( dateToUpdate_Day, day.toString() );
	setSelectBoxSelectedValue( dateToUpdate_Year, year.toString() );
}



/***************************************************
 setSelectBoxSelectedValue()

 Date: 9/22/2006
 Purpose: Set a select box's selected value
***************************************************/

function setSelectBoxSelectedValue( selectBox, newValue ) {
	if( typeof( selectBox ) != "object" ) {
		// Try to create object
		if( ! ( selectBox = $(selectBox) ) ) return;
	}
	if( ! selectBox.options ) return;
	for( i = 0; i < selectBox.options.length; i++ ) {
		if( selectBox.options[i].value == newValue ) {
			selectBox.options[i].selected = true;
		} else {
			selectBox.options[i].selected = false;
		}
	}
}



/***************************************************
 highlightRow()

 Date: 10/18/2006
 Purpose: Highlight a row (or not)
***************************************************/

function highlightRow( row, state ) {
	if( ! ( r = $(row) ) ) return;
	if( r.style ) {
		if( state ) {
			r.style.backgroundColor = _rowHighlightColor;
		} else {
			r.style.backgroundColor = "";
		}
	}
}






