var working = true;
var Ajax = (window.XMLHttpRequest) ? 
	new XMLHttpRequest() : ((window.ActiveXObject) ? 
	new ActiveXObject("Microsoft.XMLHTTP") : null);

function LoadProgressBar(o) {
	if (!Ajax) {
		alert("Uploading.... progress bar not working, missing feature");
		return "";
	}
	document.getElementById('MWblock').style.display = 'block';
	setTimeout('StartProgress()', 2000);
}

function StartProgress() {
	Ajax.open("GET", 'adv.php?sid='+sid+'&a='+Math.random(), true);
	Ajax.send(null);
	Ajax.onreadystatechange = function () {
		 if (Ajax.readyState == 4) {
			if (Ajax.status == 200) {
				var text = Ajax.responseText;
				text = text.replace(/[^0-9]+/,'');
				if (!text) text = 0;
				var Xadv = parseInt(text);
				setAdvance(Xadv);
				if (Xadv==100) working = false;
			}
			if (working) setTimeout('StartProgress()', 750);
		 }
	}
}

function setAdvance(Adp) {
	if (Adp < 0) Adp = 0;
	if (Adp > 100) Adp = 100;
	var Ado = (240 - Math.round((240 / 100) * parseInt(Adp))) * -1;
	var bar = document.getElementById('MWImageFile');
		bar.style.backgroundPosition = Ado+"px";
		bar.alt = Adp;
	document.getElementById('MWText').innerHTML = Adp+"%";
}

function AddUploadBoxes(placeholderid, copyfromid) {
	var placeholder = document.getElementById(placeholderid);
	var copyfrom = document.getElementById(copyfromid);
	if (fields >= max_upload_field) return;
	var newdiv = document.createElement('div');
	newdiv.innerHTML = copyfrom.innerHTML;
	newdiv.className = copyfrom.className;
	newdiv.id = "comment_"+fields;
	newdiv.getElementsByTagName("a")[1].setAttribute("href", "javascript:RemoveUploadBox("+fields+");");
	placeholder.parentNode.insertBefore(newdiv, placeholder);
	fields++;
}
function RemoveUploadBox(n) {
	document.getElementById("comment_"+n).parentNode.removeChild(document.getElementById("comment_"+n));
	// reenumerate the nodes, otherwise we might run into id conflicts
	children = document.getElementById("uploadBoxes").childNodes;
	var boxNumber = 0;
	for (i=0; i<children.length; i++) {
		if (children[i].nodeType == 1) { // if it's an element node
			//alert("Yay!\nbox: "+boxNumber+"\ni: "+i+"/"+children.length+"\nid: "+children[i].id+"\n("+children[i]+")");
			if (children[i].id!="FileUploadTemplate"  && children[i].id!="placeholder") {
				children[i].id = "comment_"+boxNumber;
				children[i].getElementsByTagName("a")[1].setAttribute("href", "javascript:RemoveUploadBox("+boxNumber+");");
			}
			boxNumber++;
		} else {
			//alert("Noooo!\ni: "+i+"/"+children.length+"\nnodeType: "+children[i].nodeType+" ("+children[i]+")")
		}
	}
	fields--;
}
function check() {
	var error = "";
	if (alert_recipient_mail=='1' &&
		!document.getElementById("recipient_email").value.match(/.+@.+\..+/)) {
		error+="*- Recipient email is required\n";
	}
	if (alert_my_email=='1' &&
		document.getElementById("email") &&
		!document.getElementById("email").value.match(/.+@.+\..+/)) {
		error+="*- Insert your email in My Email\n";
	}
	if (alert_verify=='1' &&
		document.getElementById("captcha") &&
		document.getElementById("captcha").value.length != 6) {
		error+="*- Captcha must be 6 characters.\n";
	}
	if (alert_description == '1') {
		var check = false;
		for (var a=0; a<= max_upload_field; a++) {
			if (document.getElementById("comment_"+a)) {
				var s = document.getElementById("comment_"+a);
				if (s.childNodes.length) {
					for (var j=0; j <= s.childNodes.length; j++) {
						if (s.childNodes[j] &&
							s.childNodes[j].type=='textarea') {
							if (!s.childNodes[j].value) {
								error += "*- Insert a comment with your file\n";
								check=true;
								break;
							}
						}
					}
					if (check) break;
				}
			}
		}
		if (!check &&
			 document.getElementById("comment") &&
			!document.getElementById("comment").value){
			error += "*- Insert a comment with your file\n";
		}
	}
	if (!document.getElementById("tos").checked) {
		error+="*- You must accept our TOS before continuing\n";
	}
	if (error) {
		alert("We found the following errors:\n\n"+error);
		return false;
	}
	window.scroll(0, 0);
	return true;
}