/***** Globals *****/
var holdNav;
var popWin;


/***** Onload *****/

window.onload = init;

function init() {
	//setNavEvents();
	//getElem('navigation').style.visibility = 'visible';
}

/***** API *****/

// Get element
function getElem(id) {
	var obj = (typeof id == 'string') ? document.getElementById(id) : id;
	return obj;
}

// Create element
function createElem(elem) {
	return document.createElement(elem);
}

// Set event attribute
function setAttr(obj, handler, theFunction) {
	if (document.all) {
		obj[handler] = new Function(theFunction);
	} else {
		obj.setAttribute(handler, theFunction);
	}
}

// Adjust height of iframe to fit content
function adjustFrame() {
	var obj = document.getElementsByTagName('iframe')[0];

	if (obj) {
		// W3C syntax
		if (obj.contentDocument && obj.contentDocument.body.offsetHeight) {
			obj.style.height = obj.contentDocument.body.offsetHeight + 'px';
		}
		// IE syntax
		else if (obj.Document && obj.Document.body.scrollHeight) {
			obj.style.height = obj.Document.body.offsetHeight + 'px';
		}
	}

	// Set frame content style
	var frame = window.frames[0];
	var frame_body = frame.document.getElementsByTagName('body')[0]
	frame_body.className = 'external';
	frame_body.firstChild.className =
		(typeof classname != 'undefined') ? classname : '';
}

// Get query name/value
function getQuery() {
	var i, qArray, qVal;
	var q = new Array();

	if (location.search) {
		qArray = location.search.substr(1).split('&');
		for (i = 0; i < qArray.length; i++) {
			qVal = qArray[i].split('=');
			q[qVal[0]] = unescape(qVal[1]);
		}
	}
	return q;
}

/*** Special functions ***/

// Product Guide select menu
function showGroup(obj) {
	var i, display;
	var objLength = obj.length;
	var objIndex = obj.selectedIndex;

	for (i = 0; i < objLength; i++) {
		if (i == objIndex) {
			display = 'block';
			obj[i].setAttribute('selected', 'selected');
		} else {
			display = 'none';
			obj[i].removeAttribute('selected');
		}
		getElem('group_' + i).style.display = display;
	}
}

// Apply style sheet to print page
function printStyle() {
	var q = getQuery();
	if (q.print) {
		document.write('<link rel="stylesheet" type="text/css" href="../../css/print.css"/>');
	}
}

// Open window
function openWin(filename, width, height, show) {
	var attr = '';

	// close window if already open
	if (popWin && !popWin.closed) popWin.close();

	// open window
	attr += 'width=' + width + ',';
	attr += 'height=' + height + ',';
	attr += (show) ?
		'resizable=yes,location=yes,menubar=yes,scrollbars=yes' :
		'resizable=no,location=no,menubar=no,scrollbars=no';
	popWin = window.open(filename, 'popWin', attr);
	popWin.focus();
}

/***** Builds *****/

// iFrame
function buildFrame(id, params, bgcolor, width, height, scroll, refresh) {
	var src, iframe;
	scroll = (scroll) ? 'auto' : 'no';

	if (id) {
		language = id.indexOf('language=fr') > 0 ? 'fr' : 'en';
		survey = id.indexOf(',') < 0 ? id : id.substring(0, id.indexOf(','))

		switch (survey) {
			case '101':
				src = 'http://content.dll1.com/SCJohnson/FHJ/Register/Entry.aspx';
				break;
			case '90':
				src = 'http://content.dll1.com/SCJohnson/FHJ/AskDiane.aspx';
				break;
			case '93':
				src = 'http://content.dll1.com/SCJohnson/FHJ/UpdateProfile/SendConfirmationEmail.aspx';
				break;
			case '97':
				src = 'http://content.dll1.com/SCJohnson/FHJ/UpdateProfile/ContactInformation.aspx';
				break;
			case '99':
				src = 'http://content.dll1.com/SCJohnson/FHJ/UpdateProfile/ProductPreferences.aspx';
				break;
			case '100':
				src = 'http://content.dll1.com/SCJohnson/FHJ/UpdateProfile/MembershipStatus.aspx';
				break;
			case '102':
				src = 'http://scjohnson.dll1.com/rightathomefamilysavings/en/coupons.aspx';
				break;
		}
		
		src += (src.indexOf("?") > 0 ? '&' : '?') + params;
		src += (language) ? '&l=' + language : '';
		src += (bgcolor) ? '&bgcolor=' + bgcolor : '';
		
	}

	iframe = (refresh) ? getElem('iframe') : createElem('iframe');
	iframe.setAttribute('id', 'iframe');
	iframe.setAttribute('frameBorder', '0');
	iframe.setAttribute('scrolling', scroll);
	if (src) iframe.setAttribute('src', src);
	if (width) iframe.style.width = width + 'px';
	if (height) iframe.style.height = height + 'px';

	//setAttr(iframe, 'onload', 'adjustFrame()');
	if (refresh) getElem('iframe_container').innerHTML = '';
	getElem('iframe_container').appendChild(iframe);
}
