// IE workaround
function getElementsByName_iefix(tag, name) {
	var option = "name";
	if(arguments[2]) option=arguments[2];

	var elem = document.getElementsByTagName(tag);
	var arr = new Array();
	for(i = 0,iarr = 0; i < elem.length; i++) {
		att = elem[i].getAttribute(option);
		if(att == name) {
			arr[iarr] = elem[i];
			iarr++;
		}
	}
	return arr;
}

function toggle_div(div_name) {	
	var closed = false;
	now_close = arguments[1];
	var retval = arguments[2];
	var all_divs = getElementsByName_iefix("div", div_name);	
	for(var j = 0 ; j < all_divs.length ; j++) {
		if(all_divs[j].style) {
			if(all_divs[j].style.display == 'block' || now_close) {
				all_divs[j].style.display = 'none';
				closed = true; // From now on, every div will be closed
			} else all_divs[j].style.display = 'block';
		}
	}
	if(retval) return closed;
}


/**
 * This Function toggles the visibility of Div Layers.
 * The Div Layers have to be generated with the Perl Functions
 * &start_inner_section(); and &end_inner_section();
 * 
 * If the function gets several with a colon delimited values,
 * the first one will be toggled and all the others will be closed.
 * 
 * @param  name  The name(s) of the layer(s) to toggle
 */
function toggle_menu(name) {
	
	var names = name.split(":");
	var arrow_path = "/images/arrow";
	var now_close = false; // Is set to true, after the first toggle
	
	for( var i = 0 ; i < names.length ; i++ ) {
		var arrow_id = "img_div_" + names[i];
		var div_name = "div_" + names[i];
		var closed = toggle_div(div_name, now_close, 1);
		
		// Change the button accordingly
		if(closed) document.getElementById(arrow_id).src = arrow_path + "/right.png";
		else document.getElementById(arrow_id).src = arrow_path + "/down.png";
		
		now_close = true;
	}
}

/**
 * This Function is used to show the active menu_option within the main menu. 
 * 
 * @param  e_div  The "this" value from the clicked link
 */
function unbold(e_div) {
	var all_menu_options = getElementsByName_iefix("a", 'menu_option');
	
	// Make every "menu_option" normal...
	for(var i = 0 ; i < all_menu_options.length ; i++) {
		if(all_menu_options[i].style) all_menu_options[i].style.color = '';
	}

	// And make the clicked one green
	if(e_div) e_div.style.color = '#92ab55';
}

function print_comments() {
	var id = 0;
	var count = 0;
	// alert("-"+arguments[0]+"-");
	if(arguments[0]) id = arguments[0];
	if(arguments[1]) count = arguments[1];
	
	var e_div_comments = document.getElementById("div_comments");
	if(e_div_comments) {
		var comments = load_url(0, "pages/comments.php?id="+id 
											  +"&count="+count 
											  +"&start="+document.getElementById("more_count").innerHTML);
		e_div_comments.innerHTML = e_div_comments.innerHTML + comments;
		if(comments.match(/<!-- END OF COMMENTS -->$/))
			document.getElementById('more_comments_button').style.display = 'none';
		++document.getElementById("more_count").innerHTML;
	}
}

function get_value(id) {
	var e_id = document.getElementById(id);
	var e_value = "";
	if(e_id) e_value = e_id.value;

	return e_value;
}

function form_submit(count) {
	var name = get_value("name");
	var website = get_value("website");
	var entry = get_value("entry");
	var commenton = get_value("commenton");
	var captcha = get_value("recaptcha_response_field");
	var challenge = get_value("recaptcha_challenge_field");
	var result;
	
	if(name == "name") name = "";
	if(website == "[website]") website = "";
	if(entry == "Text - (No HTML formating tags are allowed)") entry = "";
	
	if(name == "") { alert("Name has to be filled in."); return; }
	if(entry == "") { alert("Some text has to be entered."); return; }
	if(captcha == "") { alert("The captcha code has to be given."); return; }
	
	result = load_url(0, "pages/post.php" + 
					"?name=" + encodeURIComponent(name) +
					"&website=" + encodeURIComponent(website) +
					"&entry=" + encodeURIComponent(entry) +
					"&commenton=" + commenton +
					"&captcha=" + captcha +
					"&challenge=" + challenge );
	
	Recaptcha.reload();
	if(result == "1") { alert("Wrong captcha text, please try again."); return; }
	if(result == "2") { alert("Couldn't post the data into the database."); return; }
	if(result == "3") { alert("Couldn't send mail."); return; }
	// The post process has been successful
	
	toggle_menu("comment");
	document.getElementById("div_comments").innerHTML = "";
	document.getElementById("more_count").innerHTML = "0";
	print_comments(commenton, count);
}
/** * This Function requests an [url] from the server *  * @param  url  The url to request */function load_url(div_id, url) {	if(url) {
		if (window.XMLHttpRequest) http=new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari		else http=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5	
		// This set's up the parameters that are handed through
		// first of, convert the GET-Parameters
		var params = document.location.search.substr(1,document.location.search.length);
		if(params) params += "&"; // If parameters are there, append a &
		params += "l=1"; // This parameter is always passed
		// read out some parameters from the given url
		if(url.split("?")[1]) params += "&" + url.split("?")[1];
		
		http.open("POST",url,false);
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");		http.send(params);
		if(div_id) document.getElementById(div_id).innerHTML=http.responseText;
		else {
			return http.responseText;
		}
	} else document.getElementById(div_id).innerHTML="";}

function show_captcha() {
	Recaptcha.create("6LdoNgwAAAAAAP0Lx7X3fIG7mKIfn7R4dmmO74O-", "", {
		theme: "custom",
		lang: "en",
		custom_theme_widget: 'recaptcha_widget'
	  	});
}
