var imgsize_fixed = new Array();
imgsize_fixed[1] = true;

function fill_menus(level) {
	new_selected = document.getElementById("id_level_"+level).value;
	for (a=level+1; a<=4; a++) {
		select_start = "<select onchange=\"fill_menus("+a+");\" name=\"level_"+a+"\" id=\"id_level_"+a+"\">";
		if (children_of[document.getElementById("id_level_"+(a-1)).value]) {
			document.getElementById("id_selectbox_"+a).innerHTML = select_start + children_of[document.getElementById("id_level_"+(a-1)).value] + "</select>";
		}
		else document.getElementById("id_selectbox_"+a).innerHTML = select_start + "<option value='-1'>&larr; Select a parent</option>" + "</select>";
	}
}

function make_urlname(a) {
	dispnameraw = document.getElementById('id_new_c_dispname_'+a).value;
	dispname = dispnameraw.replace(/[\-\/\s]+/g, "-");
	dispname = dispname.replace(/(ä|Ä|á|Á|à|À)/g, "a");
	dispname = dispname.replace(/(ë|Ë|é|É|è|È)/g, "e");
	dispname = dispname.replace(/(ï|Ï|í|Í|ì|Ì)/g, "i");
	dispname = dispname.replace(/(ö|Ö|ó|Ó|ò|Ò)/g, "o");
	dispname = dispname.replace(/(ü|Ü|ú|Ú|ù|Ù)/g, "u");
	dispname = dispname.replace(/[^a-zA-Z\d\-]/g, "");
	dispname = dispname.toLowerCase();
	document.getElementById('id_new_c_urlname_'+a).value = dispname;
	document.getElementById('id_new_c_sorttext_'+a).value = dispnameraw;
}

function show_hide1(a) {
	b = document.getElementById('block_'+a);
	if (b.style.display == "block") b.style.display = "none"; else b.style.display = "block"; 
}

function show_hide2() {
	if (document.getElementById("id_writerlink").checked == true) document.getElementById("writerurl_part").style.display = "block";
	else document.getElementById("writerurl_part").style.display = "none";
}

function word_count() {
	articleText = document.getElementById("id_articletext").value;
	articleText = articleText.replace(/\s+/g, " ");
	articleText = articleText.replace(/^\s/g, "");
	articleText = articleText.replace(/\s$/g, "");
	if (articleText == "") wordCount = 0;
	else {
		words = articleText.split(" ");
		wordCount = words.length;
	}
	wc_percent = Math.round(wordCount / req_wordCount * 100, 1);
	document.getElementById("wordcount").innerHTML = wordCount + "/" + req_wordCount + " (" + wc_percent + "%)";
}

function format_it(the_tag) {
	the_area = document.getElementById('id_articletext');
	if (document.selection) {
		str = document.selection.createRange().text 
		document.selection.createRange().text = "<" + the_tag + ">" + str + "</" + the_tag + ">";
	}
	else {
		var startPos = the_area.selectionStart;
		var endPos = the_area.selectionEnd;
		var str = the_area.value.substring(startPos, endPos);
		the_area.value = the_area.value.substring(0, startPos) + "<" + the_tag + ">" + str + "</" + the_tag + ">" + the_area.value.substring(endPos, the_area.value.length);
		the_area.selectionStart = startPos + 3;
		the_area.selectionEnd = endPos + 3;
	}
	the_area.focus();
}

function disptoggle_all (baseid, which, extra) {
	
	for (a=1; a<=stripcount; a++) {
		if (a == which && document.getElementById(baseid + '_' + a)) {
			document.getElementById(baseid + '_' + a).style.display = "block";
			if (extra.substring(0,3) == "tab") document.getElementById(extra+"_"+a).className = "tabnow";
		}
		else if (document.getElementById(baseid + '_' + a)) {
			document.getElementById(baseid + '_' + a).style.display = "none";
			if (extra.substring(0,3) == "tab") document.getElementById(extra+"_"+a).className = "";
		}
	}
	if (baseid == 'itemstrip' && !imgsize_fixed[which]) {
		fix_imgsizes();
		imgsize_fixed[which] = true;
	}
}

function fix_imgsizes() {
	if (num_pageimgs) {
		for (a=1; a<=num_pageimgs; a++) {
			if (document.getElementById('img_'+a)) {
				img = document.getElementById('img_'+a);
				imgwidth = img.offsetWidth;
				imgheight = img.offsetHeight;
				if (imgwidth > 90 || imgheight > 90) {
					if (imgwidth >= imgheight) imgfactor = Math.round(100*(80/imgwidth))/100;
					else imgfactor = Math.round(100*(80/imgheight))/100;
					newimgwidth = Math.round(imgwidth*imgfactor);
					newimgheight = Math.round(imgheight*imgfactor);
					img.width = newimgwidth;
					img.height = newimgheight;
					if (document.getElementById('imgsee_'+a)) document.getElementById('imgsee_'+a).style.display = "block";
				}
			}
		}
	}
}

function seelarger(url) {
	if (url) imagewindow = window.open("http://www.megaministore.com/seelarger.php?url="+url, "Image", "height=300; width=300; scrollbars=1; resizable=1;");
}

function getforsale(disc_id) {
	forsale = document.getElementById("block_forsale_"+disc_id);
	forsale.innerHTML = "<p class='ctr mt_big mb_big light'>Getting listings...</p>";

	var dataString = 'getforsale=1&disc_id='+disc_id;
	$.ajax({
		type: "POST",
		url: "http://www.megaministore.com/quickedit.php",
		data: dataString,
		success: function(response) {
			forsale.innerHTML = response;
//			document.getElementById("blurb|" + info).innerHTML = newvalue.replace(/\n/g, '<br />');
//			document.getElementById("editblurb|" + info).style.display = "none";
		}
	});
}
