/**
 * $Header: /iscoliosis.com/js/main.js 2     7/16/08 4:13p Robinp2 $
 *
 * @name	main.js
 * @package	iScoliosis.com
 * @author	Phil Robinson <phil.robinson@medtronic.com>
 * @version	$Revision: 2 $
 * @updated	$Modtime: 7/16/08 4:13p $
 */


// creates application namespace 
YAHOO.namespace('app');
YAHOO.app = function() {
	/**
	 * private properties & methods
	 */

	// YUI niceties & shortcuts
	var Dom = YAHOO.util.Dom;
	var Event = YAHOO.util.Event;
	var lang = YAHOO.lang;
	var ua = YAHOO.env.ua;
	var $ = Dom.get;

	// text sizer cookie properties
	var cookie = jimAuld.utils.cookies;		// assumes Jim Auldridge's cookie library; located at /shared/javascript/cookies.min.js
	var textSizeCookieName = 'text-size';
	var textSizeCookie = cookie.get(textSizeCookieName);
	var textSizeDefault = 'normal';

	// headline rotator
	var headlineRotator = {
		elHeadline: null,	// headline element
		interval:	5,		// timeout interval in seconds
		timer:		null,	// holds the id of the timeout 
		lastIndex:	null,	// index of the last headline displayed
		headlines:	[
			{url: '/findadoctor.html',			text: 'Find A Doctor'},
			{url: '/patient.html',				text: 'Read Patient Stories'},
			{url: '/articles-brace_care.html',	text: 'Taking Care of Your Brace'},
			{url: '/symptoms-screening.html',	text: 'Check Your Child for Scoliosis'},
			{url: '/articles-exercise.html',	text: 'Exercises for Scoliosis'} 
		],

		init: function(el) {
			var self = this;
			this.elHeadline = el;
			el.innerHTML = '';
			this.loadHeadline();
			this.timer = window.setInterval(function() { self.loadHeadline(); }, (this.interval * 1000));
		},

		loadHeadline: function() {
			// get a random headline, but not the last one
			do {
				var index = Math.floor(Math.random() * this.headlines.length);
			} while (index == this.lastIndex);

			this.lastIndex = index;
			this.elHeadline.innerHTML = '<a href="'+this.headlines[index]['url']+'">'+this.headlines[index]['text']+'</a>';
		}
	};


	/**
	 * toggleSearch will clear the "Search" text when the user clicks in the
	 * search input and reinsert it if the user blurs the input. 
	 * 
	 * @param {Object} el
	 */
	var toggleSearch = function(el) {
		var el = $(el);
		var val = el.defaultValue;

		var doFocus = function() {
			if (el.value == val) {
				el.value = '';
			}
		};

		var doBlur = function() {
			if (el.value == '') {
				el.value = val;
			}
		};

		// attach the search input events
		Event.on(el, 'focus', doFocus);
		Event.on(el, 'blur', doBlur);
	};


	/**
	 * resizeText "resizes" the text on the page by setting a class on the body
	 * that corresponds with the link the user has clicked.
	 * 
	 * @param {Object} e
	 */
	var resizeText = function(e) {
		Event.preventDefault(e);

		var which = Event.getTarget(e);
		var newTextSize = which.id;
		var oldTextSize = (newTextSize != 'normal') ? 'normal' : 'big';

		// add text size class to body
		Dom.replaceClass(document.body, oldTextSize, newTextSize);

		// set text size cookie
		cookie.set(textSizeCookieName, newTextSize, 24000);
	};


	/**
	 * loadPatientSelector uses SWFObject to add the Flash-based Patient Stories
	 * selector to the index page. It is dependent on /shared/javascript/swfobject.js.
	 */
	var loadPatientSelector = function() {
		var so = new SWFObject(
			'/flash/section-patient.swf',
			'flash',
			'520',
			'201',
			'8',
			'#fff'
		);
		so.write('section-patient');
	};




	/**
	 * public properties, methods
	 */
	return {
		init: function() {
			// check for text size cookie
			if (textSizeCookie != '' && textSizeCookie != 'normal') {
				Dom.replaceClass(document.body, 'normal', textSizeCookie);
			}

			// load Flash Patient Stories selector on index page
			Event.onAvailable('section-patient', loadPatientSelector);

			// add headlines rotator to index page
			Event.onAvailable('module-headlines', headlineRotator.init, $('headline'), headlineRotator, true);

			// add handler to clear/populate search form input 
			Event.onAvailable('search', toggleSearch, 'search');

			// add text resizer handlers
			Event.on('text-resizer', 'click', resizeText);

			// attach onclick event to print icon link  
			Event.on('link-print', 'click', function(e, el) {
				Event.stopEvent(e);
				window.print();
			});

			/**
			 * insert a "separator" DIV after #article-header on content pages
			 * in IE6 to combat a bug where an absolutely positioned element
			 * disappears if it has a floated sibling:
			 * 
			 * http://www.brunildo.org/test/IE_raf3.html
			 */
			if (document.body.id != 'index') {
				Event.onAvailable('article-header', function() {
					if (ua.ie >= 6) {
						var div = document.createElement('div');
						div.id = 'separator';
						Dom.setStyle(div, 'clear', 'both');
						Dom.setStyle(div, 'height', '1px');
						Dom.setStyle(div, 'lineHeight', '1px');
						var result = Dom.insertAfter(div, this);
					}
				});
			}
		}
	}
}();


// init the app when DOM is ready
YAHOO.util.Event.onDOMReady(YAHOO.app.init, YAHOO.app, true);

function popINTER(path) {
        myWin = window.open('','myWindow','scrollbars=yes,directories=no,status=no,resizable=yes,toolbar=no,copyhistory=no,location=no,menubar=no,width=340,height=255,top=15,left=15')
        myWin.location.href = path;
}

function interstitial(storyurl){
        window.open(storyurl,'mywindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=300,height=400');
}

function checkgender(showit){
	if(document.survey.q2[3].checked || document.survey.q2[4].checked || document.survey.q2[5].checked){
		document.survey.q2b[0].checked=false;
		document.survey.q2b[1].checked=false;
		document.survey.q2b[2].checked=false;
			if(showit){alert("The period question is for girls only.");}
		}
}
function validate() 
{
//load variables
err="";

// error check to see if filled out correctly

	if(!document.survey.q1[0].checked && !document.survey.q1[1].checked && !document.survey.q1[2].checked && !document.survey.q1[3].checked && !document.survey.q1[4].checked && !document.survey.q1[5].checked) {
	err+="Please choose your scoliosis condition for question one.\n";
	}
	
// on q2, if a boy, do not allow checking of any q2b radio buttons
	if(!document.survey.q2[0].checked && !document.survey.q2[1].checked && !document.survey.q2[2].checked && !document.survey.q2[3].checked && !document.survey.q2[4].checked && !document.survey.q2[5].checked) {
	err+="Please choose your gender and age for question two.\n";
	}

	
	if(document.survey.q2[0].checked || document.survey.q2[1].checked ||document.survey.q2[2].checked){
		if(!document.survey.q2b[0].checked && !document.survey.q2b[1].checked && !document.survey.q2b[2].checked) {
		err+="Please choose your period condition for question two.\n";
		}
	}
	
	if(!document.survey.q3[0].checked && !document.survey.q3[1].checked && !document.survey.q3[2].checked && !document.survey.q3[3].checked && !document.survey.q3[4].checked && !document.survey.q3[5].checked) {
	err+="Please choose your type of scoliosis for question three.\n";
	}
	
	if(!document.survey.q5[0].checked && !document.survey.q5[1].checked && !document.survey.q5[2].checked) {
	err+="Please choose where the apex of your curve is for question six.\n";
	}
	
	if(!document.survey.q6[0].checked && !document.survey.q6[1].checked) {
	err+="Please choose if your doctor has told you you have spinal anomalies for question seven.\n";
	}
	
	if(!document.survey.q7[0].checked && !document.survey.q7[1].checked && !document.survey.q7[2].checked && !document.survey.q7[3].checked && !document.survey.q7[4].checked) {
	err+="Please choose number of degrees of your curvature for question eight.\n";
	}

//if errors, show the error alert, otherwise submit the form	
if(err>""){
	alert(err);
	}else{
	document.survey.submit();
	}
}

