/* common.js */

function replaceUriSegment(position, str) {
	var parts = location.href.split('/');
	parts[position+2] = str;
	location.href = parts.join('/');
}

function enableFormElement(fe) {
	var item = document.getElementById(fe);
	item.setAttribute('disabled', '');
}

function disableFormElement(fe) {
	var item = document.getElementById(fe);
	item.setAttribute('disabled', 'disabled');
}

function popUp(uri, x, y, mode) {
	if(!x) {
		x=800;
	}

	if(!y) {
		y=600;
	}

	var opts;
	if(!mode || mode == 'dialog') {
		var opts = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width='+x+',height='+y+',bgcolor=#cccccc';
	} else if(mode == 'normal') {
		var opts = 'toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width='+x+',height='+y+',bgcolor=#cccccc';
	}

	if(window.open(uri,'PopUpWindow',opts)) {
		return false;
	} else {
		return true;
	}
}

function playYouTubeClip(clip) {
	var yt = document.getElementById('embedded_youtube');
	var content = '<object width="402" height="319">';
	content += '<param value="' + clip + '" name="movie"/>';
	content += '<param value="transparent" name="wmode"/>';
	content += '<embed width="402" height="319" wmode="transparent" type="application/x-shockwave-flash" src="' + clip + '"/>';
	content += '</object>';

	yt.innerHTML = content;
	return false;

}

// Message Box Hide/Close function
function hideDomNode(box) {
	var msgBox = document.getElementById(box);
	if(msgBox) {
		msgBox.className = 'hidden';
		return false;
	} else {
		return true;
	}
}

function displayToggle(domNode, toggleAnchor) {
	var dn = document.getElementById(domNode);
	var ta = document.getElementById(toggleAnchor);
	if (dn.className && dn.className.indexOf('hidden')>=0) {
		dn.className = '';
		ta.firstChild.nodeValue = 'hide';
	} else {
		dn.className = 'hidden';
		ta.firstChild.nodeValue = 'show';
	}
	return false;
}

function toggleVisibility(dn) {
	if (dn.className && dn.className.indexOf('hidden')>=0) {
		dn.className = dn.className.replace(/hidden/, '');
	} else {
		dn.className = dn.className + ' hidden';
		dn.className = dn.className.replace(/\\s{2,}/, ' ');
	}
	return false;
}

function allowScroll(nodeId) {
	var node = document.getElementById(nodeId);
	if(node.style && document.createElement) {
		node.style.overflow = 'auto';
	}
}

function disallowScroll(nodeId) {
	var node = document.getElementById(nodeId);
	if(node.style && document.createElement) {
		var msgBox = document.createElement('p');
		msgBox.className = 'tip';
		var msg = document.createTextNode('Click anywhere in this box to activate scrolling.');
		msgBox.appendChild(msg);
		node.insertBefore(msgBox, node.firstChild);
		node.style.overflow = 'hidden';
	}
}


// Page Initialization (for body onload event)
function initializePage() {
	/*
	Begin Form initialization
	*/

	// Check if forms.js is loaded
	var scripts = document.getElementsByTagName('script');
	if(scripts.length>0) {
		for(var i=0; i<scripts.length; i++) {
			if(scripts[i].src.toLowerCase().indexOf('forms.js')>=0) {
				// Resest the 'Loading...' state and styles
				hideLoading();  // found in forms.js

				// Create the onsubmit event
				var forms = document.forms;
				for (var k=0; k<forms.length; k++) {
					forms[k].onsubmit = function() { return validateForm(this.id) };   // found in forms.js
				}
			}
		}
	}
	
	/*
	End Form initialization
	*/
	
	/*
	Begin External Link initialization
	*/
	var links = document.getElementsByTagName('a');
	var domain = window.location.hostname;

	for(var i=1; i<links.length; i++) {
		if(!links[i].href.match(domain) && !links[i].href.match('mailto') && !links[i].href.match('javascript')) {
			links[i].target = "_blank";
			links[i].title += " (New Window)";
		}
	}
	/*
	End External Link initialization
	*/
	
	/*
	Begin Table Row Link initialization
	*/
	// Find outside links
	var tableRows = document.getElementsByTagName('tr');

	for(var i=0; i<tableRows.length; i++) {
		var anchors = tableRows[i].getElementsByTagName('a')
		if(anchors.length==1) {
			$(tableRows[i]).addEvent('click', function(){window.location.href=this.getElementsByTagName('a')[0].href; return false;} );
			$(tableRows[i].addClass('clickable'));
		}
	}
	/*
	End Table Row Link initialization
	*/
	
	/*
	Begin Form Field Empty initialization
	*/
	// Find form fields
	var toEmpty = new Array('search_string', 'prefill_email');
	var toEmptyValues = new Array('Search the site...', 'Enter your email...');

	for(var i=0; i<toEmpty.length; i++) {
		if(document.getElementById(toEmpty[i])) {
			var field = document.getElementById(toEmpty[i]);
			field.valueToEmpty = toEmptyValues[i]

			$(field).addEvent('focus', function() {
				if(this.value == this.valueToEmpty) {
					this.value = '';
				}
			} );
		
			$(field).addEvent('blur', function() {
				if(this.value == '') {
					this.value = this.valueToEmpty;
				}
			} );
		}
	}
	/*
	End Form Field Empty initialization
	*/
}


// Page Clean-up (for onUnload event)
function cleanUpPage() {}

// Cookie Functions
function createCookie(name,value,days) {
	if(days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}
}

function readCookie(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 eraseCookie(name) {
	createCookie(name,"",-1);
}

function learnBy(selectElement) {
	var key = selectElement.options[selectElement.selectedIndex].text;
	key = key.replace(" (Co-contributor)", "");
	key = key.toLowerCase();
	key = key.replace(" ", "-");
	switch(selectElement.name) {
		case "subject":
			if (key == "webinars") window.location = "/courses";
			else if (key == "advocates") window.location = "/advocates";
			else window.location = "/video/subject/" + key;
			break;
		case "product":
			window.location = "/video/manufacturer/" + key;
			break;
		case "author":
			window.location = "/video/author/" + key;
			break;
	}
}

function emailLink(name, server) {
	document.write("<a href='mailto:" + name + "@" + server + "'>" + name + "@" + server);
}

