var popup = {

	postmode: null,
	showing: false,
	
	show : function(form, id, handler, w, resp, validator, is_modal) {
		
		//if(this.showing) return;
   		
		var elid = id;
		var element = $('#'+id);
		var action = handler;
		var width = w;
		var val = validator==null?"":",\""+validator+"\"";		
		
		$.get(form,
           null,
           function(x){
           		
           		var popup_content =  "<form  onsubmit='popup.submitform(this,\""
									+ handler+"\",\""
									+ elid+"\",\""+resp+"\""
									+ val+");return false;'><div style='width:"+width
									+ ";position:absolute;'><div class='popup'><div style='float:right;'>"
									+ "<a href='javascript:popup.closeform(\""
									+ elid+"\");' class='close'>"
									+ "x</a></div><div style='clear:both;'></div>"
									+ x
									+ "</div></div></form>";
           	
           		$('#'+id).html(popup_content);
           		$('#'+id).jqm({ modal: is_modal }).jqmShow();
           }
    	);
		
    	this.showing = true;
	},
	
	closeform : function(id){
		$('#'+id).css('display', 'none');
		$('#'+id).jqm().jqmHide();
		this.showing = false;
	},
	
	submitform : function(frm,action,id,resp, val){

		if (val != null) {
			if(!eval(val))
				return false;
			
		}

		var params = '';
		var element = $('#'+id);
		
		for(i=0; i<frm.elements.length; i++){
			if(frm.elements[i].name!=''){
				params+= + i==0?'':'&';
				params+=frm.elements[i].name + "=" + encodeURIComponent(frm.elements[i].value);
			}
		}		
	
		var async_mode = true;
		if(this.postmode == false) {
			async_mode = false;
		}
		
		var self = this;
		var res = resp;
		$.ajax( {
			url: action, 
			type: 'post',
			data: params,
			async: async_mode,
			success :function (x) {

								result = jQuery.parseJSON(x);

								if(result.response == 'ok') {
									$('#'+id).css('display', 'none');
									self.showing = false;
									self.closeform(id);
								} else {
									self.showing = false;
									self.closeform(id);
									showError(result);
								}
								
								eval(""+res+"("+x+")");
							}
		});
	},
	
	getRequestObject : function() {
		var xmlHttp=null;
		try{
		    this.postmode = true;
			xmlHttp=new XMLHttpRequest();
		}catch (e){
		  // Internet Explorer
		  try{ 
			this.postmode = false;
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		  } catch (e){
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		  }
		return xmlHttp;		
	}	
}


