/********** AJAX **********/

function ajaxObject() {
	// To initialize: var myObject = new ajaxObject();
	// To use: myObject.get(url, variables, callback);
	// Variables: first=Franklin&middle=Delano&last=Roosevelt
	var http = false;
	if(navigator.appName == "Microsoft Internet Explorer") {
		http = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		http = new XMLHttpRequest();
	}
	this.get = function(url, variables, callback) {
		http.open("GET", url+"?"+variables, true);
		http.onreadystatechange = function() {
			if(http.readyState == 4 && http.status == 200) {
				callback(http.responseText);
			}
		}
		http.send(null);
	}
	this.post = function(url, variables, callback) {
		http.open("POST", url, true);
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", variables.length);
		http.setRequestHeader("Connection", "close");
		http.onreadystatechange = function() {
			if(http.readyState == 4 && http.status == 200) {
				callback(http.responseText);
			}
		}
		http.send(variables);
	}
}

var ajax = new ajaxObject();

/********** MENU / MOUSEOVERS **********/

// Menu button array
var array_menu = new Array(
	"nav-bar-about",
	"nav-bar-know-center",
	"nav-bar-careers",
	"nav-bar-co-venture",
	"nav-bar-global-citizenship",
	"nav-bar-store");

// Nav-bar button mouseover
function over(element_id) {
	var data_div = "tier-2-" + element_id;
	button = "nav-bar-" + element_id;
	if (element_id != "nav-bar-1") {
		setHTML("tier-2", getHTML(data_div));
		for (link in array_menu) {
			if (array_menu[link] != button) {
				replace_bg(array_menu[link], "url('/website/images/nav-bar-bg.png')"); 
				color(button, "#990a1b");
			}
		}
	} 
	replace_bg(button, "#f8f8f8");
	show_hide("tier-2", "show");
}

// Hide tier-2 menu
function hide_menu() {
	show_hide("tier-2", "hide");
	for (link in array_menu) {
		replace_bg(array_menu[link], "url('/website/images/nav-bar-bg.png')");
		color(array_menu[link], "#303030");
	}
}

// Mouseover top link
function toplink_over(element_id) {
	replace_bg(element_id, '#b70b18');
}

// Mouseout top link
function toplink_out(element_id) {
	replace_bg(element_id, 'none');
}

/**********	SHOW / HIDE	**********/

// Show / Hide element
function show_hide(element_id, state) {
	var target = document.getElementById(element_id);
	if (target == null) return;
	switch (state) {
	case "hide":
		target.style.display = "none";
		break;
	case "show":
		target.style.display = "block";
		break;
	default:
		if (target.style.display == "" || target.style.display == "block") {
			target.style.display = "none";
		} else {
			target.style.display = "block";
		}
		break;
	}
}

// Toggle showhide (show/hide)
function toggle_showhide(element_id) {
	var arrow = "img_" + element_id;
	var paragraph = document.getElementById(element_id);
	if (paragraph.style.display == "" || paragraph.style.display == "block") {
		document[arrow].src = "/website/images/arrow-right.png";
		paragraph.style.display = "none";
	} else {
		document[arrow].src = "/website/images/arrow-down.png";
		paragraph.style.display = "block";
	}
}

// Toggle Blog Feed
function toggleFeed(which) {
	switch(which) {
	case "blog":
		var linkName = "Blog";
		break;
	case "events":
		var linkName = "Events";
		break;
	default: break;
	}
	var feed = document.getElementById(which + "-contents");
	if (feed.style.display == "" || feed.style.display == "block") {
		feed.style.display = "none";
		setHTML("toggle-" + which, "Show " + linkName);
	} else {
		feed.style.display = "block";
		setHTML("toggle-" + which, "Hide " + linkName);
	}
}

/********** STYLE COLORS **********/

// Change background color
function replace_bg(element_id, color){
	switch(color) {
	case "right-normal":
		document.getElementById(element_id).style.background = "#f6f6f6";
		break;
	case "right-hover":
		document.getElementById(element_id).style.background = "#ffffff";
		break;
	case "nav-hover":
		document.getElementById(element_id).style.background = "#ffffff";
		break;
	case "nav-normal":
		document.getElementById(element_id).style.background = url("/website/images/nav-bar-bg.png");
		break;
	default:
		document.getElementById(element_id).style.background = color;
		break;
	}
}

function color(element_id, color) {
	document.getElementById(element_id).style.color = color;
}

/********** CONTENTS / INNER HTML **********/

// Replace contents of element
function replace_contents(element_id, contents) {
	setHTML(element_id, contents);
}

function getHTML(element_id) {
	element = document.getElementById(element_id);
	if (element.tagName == "DIV" || element.tagName == "SPAN") {
		return element.innerHTML;
	} else if (element.tagName == "INPUT") {
		return element.value;
	} else {
		return element.innerHTML;
	}
}

function setHTML(element_id, html) {
	element = document.getElementById(element_id);
	if (element.tagName == "DIV" || element.tagName == "SPAN") {
		element.innerHTML = html;
	} else if (element.tagName == "INPUT") {
		element.value = html;
	} else {
		element.innerHTML = html;
	}
}

function getListItems(listid) {
	var list = document.getElementById(listid);
	var items = list.getElementsByTagName("li");
	return items;
}

// Update App-list content
function updateAppList(content) {
	var appList = document.getElementById('app-list');
	appList.innerHTML = content;
}

/********** STRINGS & TRIMMING **********/

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function trim_editor(str) {
	str = str.replace(/[\r\n\t]*/g, "");
	str = str.replace(/[\s]*<p>[\s]*/g, "<p>");
	str = str.replace(/[\s]*<\/p>[\s]*/g, "</p>");
	str = str.replace(/[\s]*<span>[\s]*/g, "<span>");
	str = str.replace(/[\s]*<\/span>[\s]*/g, "</span>");
	return str;
}

/********** POPUPS **********/

function showPopup(type, msg, title) {
	show_hide("dim-background", "show");
	switch (type) {
		case "contact":
			show_hide("form-wrapper", "show");
			show_hide("contact-form", "show");
			break;
		case "country":
			show_hide("form-wrapper-thin", "show");
			show_hide("popup-country", "show");
			break;
		case "register":
			show_hide("form-wrapper", "show");
			show_hide("register-form", "show");
			break;
		case "login":
			show_hide("form-wrapper", "show");
			show_hide("login-form", "show");
			break;
		case "share":
			show_hide("form-wrapper", "show");
			show_hide("share-form", "show");
			break;
		case "settings":
			show_hide("form-wrapper", "show");
			show_hide("settings-form", "show");
			break;
		case "blog":
			show_hide("form-wrapper", "show");
			show_hide("blog-form", "show");
			break;
		case "solutions":
			show_hide("form-wrapper", "show");
			show_hide("popup-solutions", "show");
			break;
		case "member-firms":
			show_hide("form-wrapper-thin", "show");
			show_hide("popup-member-firms", "show");
			break;
		case "global-firms":
			show_hide("form-wrapper", "show");
			show_hide("popup-global-firms", "show");
			break;
		case "alert":
			show_hide("form-wrapper", "show");
			setHTML("popup-alert-title", title);
			setHTML("popup-alert-contents", msg);
			show_hide("popup-alert", "show");
			break;
		case "save":
			show_hide("form-wrapper", "show");
			if (msg != "" && msg != null) {
				setHTML("popup-save-contents", msg);
			}
			show_hide("popup-save", "show");
			break;
	}
}

function hidePopup(type) {
	show_hide("dim-background", "hide");
	switch (type) {
		case "contact":
			show_hide("form-wrapper", "hide");
			show_hide("contact-form", "hide");
			break;
		case "country":
			show_hide("form-wrapper-thin", "hide");
			show_hide("popup-country", "hide");
			break;
		case "register":
			show_hide("form-wrapper", "hide");
			show_hide("register-form", "hide");
			break;
		case "login":
			show_hide("form-wrapper", "hide");
			show_hide("login-form", "hide");
			break;
		case "share":
			show_hide("form-wrapper", "hide");
			show_hide("share-form", "hide");
			break;
		case "settings":
			show_hide("form-wrapper", "hide");
			show_hide("settings-form", "hide");
			break;
		case "blog":
			show_hide("form-wrapper", "hide");
			show_hide("blog-form", "hide");
			break;
		case "news":
			show_hide("form-wrapper", "hide");
			show_hide("news-form", "hide");
			break;
		case "solutions":
			show_hide("form-wrapper", "hide");
			show_hide("popup-solutions", "hide");
			break;
		case "member-firms":
			show_hide("form-wrapper-thin", "hide");
			show_hide("popup-member-firms", "hide");
			break;
		case "global-firms":
			show_hide("form-wrapper", "hide");
			show_hide("popup-global-firms", "hide");
			break;
		case "alert":
			show_hide("form-wrapper", "hide");
			setHTML("popup-alert-contents", "");
			show_hide("popup-alert", "hide");
			break;
		case "save":
			show_hide("form-wrapper", "hide");
			setHTML("popup-save-contents", "");
			show_hide("popup-save", "hide");
			break;
	}
}

/********** ETC. **********/

// Redirect browser to URL
function redirect(url) {
	base = "http://www.go-4d.com/";
	if (url.substring(0,7) != "http://") {
		url = base + url;
	}
	window.location = url; 
}

// Dropdown links
function drop_redirect(menuObj) {
   var i = menuObj.selectedIndex;
	redirect(menuObj.options[i].value);
}

// Check parent element
function checkParent(clicked, subject) {
	while (clicked.parentNode) {
		if (clicked == subject) {
			return false;
		}
		clicked = clicked.parentNode;
	}
	return true;
}

// Get Elements by Class
function getElementsByClass(searchClass, node, tag) {
  var classElements = new Array();
  if (node == null) node = document;
  if (tag == null) tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if (pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}
