/*
addEvent function found at http://www.scottandrew.com/weblog/articles/cbs-events
*/
function addEvent(obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}


/*
createElement function found at http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
*/
function createElement(element) {
	if (typeof document.createElementNS != 'undefined') {	
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}
	if (typeof document.createElement != 'undefined') {
		return document.createElement(element);
	}
	return false;
}


function getStyle(el, style) { 
	if(!document.getElementById) return;

	var value = el.style[toCamelCase(style)];

	if (!value) {
		if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") {		// DOM
			value = document.defaultView.getComputedStyle(el, "").getPropertyValue(style);
		} else if (el.currentStyle) {			// IE
			value = el.currentStyle[toCamelCase(style)];
		} else {	// Safari hack
			var foo = toCamelCase("client-"+style);
			value = el[foo];
		}
	}

	return value; 

} 


function setStyle(obj, style, value) { 
	obj.style[style] = value; 
} 


function toCamelCase( sInput ) {
	var oStringList = sInput.split('-');
	if(oStringList.length == 1) {
		return oStringList[0];
	}
	var ret = sInput.indexOf("-") == 0 ? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) : oStringList[0];
	for(var i = 1, len = oStringList.length; i < len; i++) {
		var s = oStringList[i];
		ret += s.charAt(0).toUpperCase() + s.substring(1);
	}
	return ret; 

} 


function popup(url, w, h) {
	var options = 'width=' + w + ',height=' + h + ',';
	options += 'resizable=yes,scrollbars=yes,status=no,';
	options += 'menubar=no,toolbar=no,location=no,directories=no';
	var newWin = window.open(url, 'newWin', options);
	newWin.focus();
}


function getStyleObject(objectId) {
	if (document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId).style;
	} else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId).style;
	} else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
	} else {
		return false;
	}
}


function toggle( el ) {
	var vis = toggleVis(el);
}


function toggleVis( el ) {
	var obj = document.getElementById(el);
	var style = getStyle(obj, 'display');
	if (style=='none') {
		setStyle(obj, 'display', 'block');
		return 1;
	} else {
		setStyle(obj, 'display', 'none');
		return 0;
	}
}


function toggleAdv(formname) {
	var vis = toggleVis('advsearch');
	toggle('search_adv'); 
	toggle('search_simple'); 
	var field = document.forms[formname].elements['search_adv'];
	field.value = vis;
}


function dl(file,loc,template) {
	if (
		(window.navigator.userAgent.indexOf("SV1") != -1 ) || 
		(window.navigator.userAgent.indexOf("MSIE 7.") != -1 )
	) {
		var options = 'width=0,height=0,resizable=no,scrollbars=no,' +
			'status=no,menubar=no,toolbar=no,location=no,directories=no';
		var dlWin = window.open(file, 'dlWin', options);
		loc = loc + '/dlp/';
	}
	if (template) {
		loc = loc + '?template=' + template;
	}
	location.href = loc;
}


function submit(formname) {
	document.forms[formname].submit();
}

function cfe(f,e) {
	var k;
	if (window.event) {
		k = window.event.keyCode;
	} else if (e) {
		k = e.which;
	} else return true;

	if (k==13) {
		f.form.submit();
		return false;
	} else return true;

}

function sws(w) {
	window.status = w;
	return true;
}

function cws() {
	window.status='';
	return true;
}

function contactForm(value) {
	var step2 = document.getElementById('step2');
	if (value=="domain") {
		document.location = 'http://helpcenter.tucows.com/Email.php';
	} else if (value==0) {
		setStyle(step2, 'display', 'none');
	} else {
		setStyle(step2, 'display', 'block');
	}
}

function insertTop(obj) {
	// Create the two div elements needed for the top of the box
	d=createElement("div");
	d.className="bt"; // The outer div needs a class name
    d2=createElement("div");
    d.appendChild(d2);
	obj.insertBefore(d,obj.firstChild);
}

function insertBottom(obj) {
	// Create the two div elements needed for the bottom of the box
	d=createElement("div");
	d.className="bb"; // The outer div needs a class name
    d2=createElement("div");
    d.appendChild(d2);
	obj.appendChild(d);
}

function initCB()
{
	// Find all div elements
	var divs = document.getElementsByTagName('div');
	var cbDivs = [];
	for (var i = 0; i < divs.length; i++) {
	// Find all div elements with cbb in their class attribute while allowing for multiple class names
		if (/\bcbb\b/.test(divs[i].className))
			cbDivs[cbDivs.length] = divs[i];
	}
	// Loop through the found div elements
	var thediv, outer, i1, i2;
	for (var i = 0; i < cbDivs.length; i++) {
	// Save the original outer div for later
		thediv = cbDivs[i];
	// 	Create a new div, give it the original div's class attribute, and replace 'cbb' with 'cb'
		outer = createElement('div');
		outer.className = thediv.className;
		outer.className = thediv.className.replace('cbb', 'cb');
	// Change the original div's class name and replace it with the new div
		thediv.className = 'i3';
		thediv.parentNode.replaceChild(outer, thediv);
	// Create two new div elements and insert them into the outermost div
		i1 = createElement('div');
		i1.className = 'i1';
		outer.appendChild(i1);
		i2 = createElement('div');
		i2.className = 'i2';
		i1.appendChild(i2);
	// Insert the original div
		i2.appendChild(thediv);
	// Insert the top and bottom divs
		insertTop(outer);
		insertBottom(outer);
	}
}

if(document.getElementById && document.createTextNode) {
//	addEvent(window, 'load', initCB);
}

function eqh() {
	if (arguments.length) {
		var b, h=0, maxh=0, i=0;
		for(i=0; i<arguments.length; i++) {
			b=document.getElementById(arguments[i]);
			h=b.offsetHeight;
			if (h=="") {
				h = b.height;
			}
			if (h=="") {
				return;
			}
			maxh = Math.max( maxh, h );
		}
	}
	for(i=0; i>arguments.length; i++) {
		b = document.getElementById(arguments[i]);
		setStyle( b, "height", maxh+"px" );
	}

}
function initHLI()
{
    // Find all li elements
    var lis = document.getElementsByTagName('li');
    var hlis = [];
    for (var i = 0; i < lis.length; i++) {
	    // Find all li elements with hli in their class attribute while allowing for multiple class names
        if (/\bhli\b/.test(lis[i].className))
            hlis[hlis.length] = lis[i];
	}

	// Loop through the found li elements,
	// find the first <a> tag inside the li to find the href, 
	// and attach a function to the li.onclick action to redirect there
    for (var i = 0; i < hlis.length; i++) {
		if (hlis[i].getElementsByTagName('a')[0].target && hlis[i].getElementsByTagName('a')[0].target=="_blank") {
			hlis[i].onclick = function() {
				var newWin = window.open(this.getElementsByTagName('a')[0].href, 'newWin');
				newWin.focus();
			}
			// and remove href from a tag to prevent double-clicks
			hlis[i].getElementsByTagName('a')[0].onclick = function() {
				return false;
			}
		} else {
			hlis[i].onclick = function() {
				location.href = this.getElementsByTagName('a')[0].href;
			}
		}
	}

}

if(document.getElementById && document.createTextNode) {
	addEvent(window, 'load', initHLI);
}

