﻿function $m(theVar){
	return document.getElementById(theVar)
}
function remove(theVar){
	var theParent = theVar.parentNode;
	theParent.removeChild(theVar);
}
function addEvent(obj, evType, fn){
	if(obj.addEventListener)
	    obj.addEventListener(evType, fn, true)
	if(obj.attachEvent)
	    obj.attachEvent("on"+evType, fn)
}
function removeEvent(obj, type, fn){
	if(obj.detachEvent){
		obj.detachEvent('on'+type, fn);
	}else{
		obj.removeEventListener(type, fn, false);
	}
}
function isWebKit(){
	return RegExp(" AppleWebKit/").test(navigator.userAgent);
}

function ajaxUpload(form,url_action,id_element,html_show_loading,html_error_http) {
	var errorDisplay = document.getElementById('error-message');
	var videoId = document.getElementById('video-id-field').value;
	var userId = document.getElementById('user-id-field').value;
	var userName = document.getElementById('user-name-field').value;
	var guestEmail = null;
	var guestWebsite = null;
	var guestNotify = null;
	var guestSubscribe = null;
	
	if (!userName) {
		errorDisplay.innerHTML = 'You must <a href="/index.php?mode=login&amp;urldirect=' + window.location + '">login</a> or <a onclick="openLoginWindow();">register as a guest commenter</a> before you can post your image.';
		return false;
	}
	
	var imageUrlField = document.getElementById('image');
	var imageUploadInput = document.getElementById('filename');
	var imageTitleField = document.getElementById('title');
	var imageCommentField = document.getElementById('yugantcom');
	
	var imageUrlText = imageUrlField.value.replace('&amp;'," ").replace('&'," ");
	var imageUploadFile = imageUploadInput.value;
	var imageTitleText = imageTitleField.value;
	var imageCommentText = imageCommentField.value;
	
	if (!imageUrlText && !imageUploadFile) {
		errorDisplay.innerHTML = 'You must enter an image URL or select an image to upload.';
		imageUrlField.focus();
		return false;
	}
	
	if (!imageTitleText) {
		errorDisplay.innerHTML = 'You must enter a title for the image.';
		imageTitleField.focus();
		return false;
	}
	
	if(!imageCommentText || imageCommentText == 'Enter Comment') {
		errorDisplay.innerHTML = 'The comment field must not be blank';
		imageCommentField.focus();
		return false;
	}
	
	errorDisplay.innerHTML = '';
	var url = url_action+"&caption="+imageTitleText+"&userid="+userId+"&video="+videoId+"&image="+imageUrlText+"&com="+imageCommentText;
	
	if (!parseInt(userId)) {
		guestEmail = document.getElementById('guest-email-field').value;
		guestWebsite = document.getElementById('guest-website-field').value;
		guestNotify = document.getElementById('guest-notify-field').value;
		guestSubscribe = document.getElementById('guest-subscribe-field').value;
		
		url += '&email=' + guestEmail + '&website=' + guestWebsite + '&notify=' + guestNotify + '&subscribe=' + guestSubscribe;
	}
	
	var detectWebKit = isWebKit();
	
	form = (typeof(form)=="string") ? $m(form) : form;
	
	var erro="";
	if(form==null || typeof(form)=="undefined"){
		erro += "The form of 1st parameter does not exists.\n";
	}else if(form.nodeName.toLowerCase()!="form"){
		erro += "The form of 1st parameter its not a form.\n";
	}
	if($m(id_element)==null){
		erro += "The element of 3rd parameter does not exists.\n";
	}
	if(erro.length>0){
		alert("Error in call ajaxUpload:\n" + erro);
		return;
	}
	
	var iframe = document.createElement("iframe");
	iframe.setAttribute("id","ajax-temp");
	iframe.setAttribute("name","ajax-temp");
	iframe.setAttribute("width","0");
	iframe.setAttribute("height","0");
	iframe.setAttribute("border","0");
	iframe.setAttribute("style","width: 0; height: 0; border: none;");
	form.parentNode.appendChild(iframe);
	window.frames['ajax-temp'].name="ajax-temp";
	
	var doUpload = function(){
		removeEvent($m('ajax-temp'),"load", doUpload);
		
		imageTitleField.value = '';
		imageUrlField.value = '';
		imageCommentField.value = '';		
		document.getElementById('upload-image-preview').innerHTML='';
		
		updateCharacterLimit();
		showcat('show');
		
		var cross = "javascript: ";
		cross += "window.parent.$m('"+id_element+"').innerHTML = document.body.innerHTML; void(0);";
		$m(id_element).innerHTML = html_error_http;
		$m('ajax-temp').src = cross;
				
		if(detectWebKit) {
			remove($m('ajax-temp'));
		} else {
			setTimeout(function(){ remove($m('ajax-temp')); }, 250);
		}
	}

	addEvent($m('ajax-temp'),"load", doUpload);
	form.setAttribute("target","ajax-temp");
	form.setAttribute("action",url);
	form.setAttribute("method","post");
	form.setAttribute("enctype","multipart/form-data");
	form.setAttribute("encoding","multipart/form-data");
	if(html_show_loading.length > 0){
		$m(id_element).innerHTML = html_show_loading;
	}
	
	form.submit();
}
