﻿var this_domain = document.domain;
if (this_domain.indexOf("192.") > -1) 
	this_domain = this_domain + "/dirs/propertydir";
else if(this_domain.indexOf("imzee.") > -1) 
	this_domain = this_domain + "/sites";
	
this_domain = "http://"+this_domain;

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,to_load){
	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);
		var cross = "javascript: ";
		cross += "rr = document.body.innerHTML;\
							if(rr.indexOf('Error') > -1) { } \
							else \
							{\
							 	if(parent.document.getElementById('tr_btn_"+to_load+"'))\
							 		parent.document.getElementById('tr_btn_"+to_load+"').style.display = '';\
								if(parent.document.getElementById('add_remove_"+(to_load-1)+"'))\
							 		parent.document.getElementById('add_remove_"+(to_load-1)+"').innerHTML = \"<input type=button value=Remove name=img_reomve onClick=\\\"remove_image("+parseInt(to_load-1)+")\\\">\";\
							}\
							window.parent.$m('"+id_element+"').innerHTML = rr; 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);
        }
    }
	old_action = form.action;
	addEvent($m('ajax-temp'),"load", doUpload);
	form.setAttribute("target","ajax-temp");
	form.setAttribute("action",url_action);
	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();
	form.setAttribute("action",old_action);
	form.setAttribute("target","");
}

function remove_image(id)
{
	if(document.getElementById('ad_images_'+id))
		document.getElementById('ad_images_'+id).innerHTML = '<img src="../Images/show_images.php?img=pnf.jpg&amp;dir=1"/>';
	ajaxpostpage(this_domain+'/classifieds/custom/remove_uploads.php?type=img&id='+id, 'add_remove_'+id);
}
