/************************************************************************************************************
Ajax form submit
Copyright (C) 2007  DTHMLGoodies.com, Alf Magne Kalleland

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

Alf Magne Kalleland, 2007
Owner of DHTMLgoodies.com


************************************************************************************************************/	

var DHTMLSuite = new Object();

DHTMLSuite.formUtil = function()
{
	
	
	
}

DHTMLSuite.getEl = function(elRef){
	if(typeof elRef=='string'){
		if(document.getElementById(elRef))return document.getElementById(elRef);
		if(document.forms[elRef])return document.forms[elRef];
		if(document[elRef])return document[elRef];
		if(window[elRef])return window[elRef];
	}
	return elRef;	// Return original ref.
	
}
	
DHTMLSuite.formUtil.prototype = 
{
	// {{{ getFamily
    /**
     *	Return an array of elements with the same name
     *	@param Object el - Reference to form element
     *	@param Object formRef - Reference to form
     *
     * @public
     */		
	getFamily : function(el,formRef)
	{
		var els = formRef.elements;
		var retArray = new Array();
		for(var no=0;no<els.length;no++){
			if(els[no].name == el.name)retArray[retArray.length] = els[no];
		}
		return retArray;		
	}
	// }}}
	
	,
	// {{{ hasFileInputs()
    /**
     *	Does the form has file inputs?
     *	@param Object formRef - Reference to form
     *
     * @public
     */		
	hasFileInputs : function(formRef)
	{
		var els = formRef.elements;
		for(var no=0;no<els.length;no++){
			if(els[no].tagName.toLowerCase()=='input' && els[no].type.toLowerCase()=='file')return true;	
		}
		return false;
	}
	// }}}
	,	
	// {{{ getValuesAsArray()
    /**
     *	Return value of form as associative array
     *	@param Object formRef - Reference to form
     *
     * @public
     */		
	getValuesAsArray : function(formRef)
	{
		var retArray = new Object();
		formRef = DHTMLSuite.getEl(formRef);
		var els = formRef.elements;
		for(var no=0;no<els.length;no++){
			if(els[no].disabled)continue;
			var tag = els[no].tagName.toLowerCase();
			switch(tag){
				case "input": 
					var type = els[no].type.toLowerCase();
					if(!type)type='text';
					switch(type){
						case "text":
						case "image":
						case "hidden":
						case "password":
							retArray[els[no].name] = els[no].value;
							break;
						case "checkbox":
							var boxes = this.getFamily(els[no],formRef);
							if(boxes.length>1){
								retArray[els[no].name] = new Array();
								for(var no2=0;no2<boxes.length;no2++){
									if(boxes[no2].checked){
										var index = retArray[els[no].name].length;
										retArray[els[no].name][index] = boxes[no2].value;
									}
								}								
							}else{
								if(els[no].checked)retArray[els[no].name] = els[no].value;
							}
							break;	
						case "radio":
							if(els[no].checked)retArray[els[no].name] = els[no].value;
							break;		
						
					}	
					break;	
				case "select":
					var string = '';			
					var mult = els[no].getAttribute('multiple');
					if(mult || mult===''){
						retArray[els[no].name] = new Array();
						for(var no2=0;no2<els[no].options.length;no2++){
							var index = retArray[els[no].name].length;
							if(els[no].options[no2].selected)retArray[els[no].name][index] = els[no].options[no2].value;	
						}
					}else{
						if (els[no].options.length==0) {
							retArray[els[no].name] = "";
						} else {
							retArray[els[no].name] = els[no].options[els[no].selectedIndex].value;
						}
					}
					break;	
				case "textarea":
					retArray[els[no].name] = els[no].value;
					break;					
			}			
		}
		return retArray;		
	}
	// }}}
	,	
	// {{{ getValue()
    /**
     *	Return value of form element
     *	@param Object formEl - Reference to form element
     *
     * @public
     */
	getValue : function(formEl)
	{
		switch(formEl.tagName.toLowerCase()){
			case "input":
			case "textarea": return formEl.value;
			case "select": return formEl.options[formEl.selectedIndex].value;			
		}
		return "";
	}
	// }}}
	,	
	// {{{ areEqual()
    /**
     *	Check if two form elements have the same value
     *	@param Object input1 - Reference to form element
     *	@param Object input2 - Reference to form element
     *
     * @public
     */	
	areEqual : function(input1,input2)
	{
		input1 = DHTMLSuite.getEl(input1);
		input2 = DHTMLSuite.getEl(input2);	
		if(this.getValue(input1)==this.getValue(input2))return true;
		return false;		
	}	
}



function __ajaxSubmitForm(formid,url,responseId)
{
	//this gets called on form submit 
	var formobj = DHTMLSuite.getEl(formid);
	var formUtil = new DHTMLSuite.formUtil();
	var els=formUtil.getValuesAsArray(formid);
	var pvar=new Array();
	var pval=new Array();
	var respobj = DHTMLSuite.getEl(responseId);
	
	respobj.innerHTML='<p>&nbsp;</p><img src="http://www.xxxylinks.com/images/waitimg.gif">Loading... please wait<p>&nbsp;</p><p>&nbsp;</p>';
	
	var ajaxIndex = dynamicContent_ajaxObjects.length;
	dynamicContent_ajaxObjects[ajaxIndex] = new sack();
	
	
	for(var prop in els){
	    //dynamicContent_ajaxObjects[ajaxIndex].setVar(prop,els[prop]);
	
		if(els[prop].constructor.toString().indexOf("Array")!= -1){
			for(var no=0;no<els[prop].length;no++){
				var name = prop + '[' + no + ']';
				if(prop.indexOf('[')>=0){ // The name of the form field is already indicating an array
					name = prop.replace('[','[' + no);	
				}
				dynamicContent_ajaxObjects[ajaxIndex].setVar(name,els[prop][no]);	
			}
		}else{
			dynamicContent_ajaxObjects[ajaxIndex].setVar(prop,els[prop]);			
		}		
	}		
	
	dynamicContent_ajaxObjects[ajaxIndex].requestFile = url;	// Specifying which file to get
	dynamicContent_ajaxObjects[ajaxIndex].onCompletion = function(){ ajax_showContent(responseId,ajaxIndex,url); };	// Specify function that will be executed after file has been found
	dynamicContent_ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function	

}

function __ajaxMultipartSubmitForm(formid,url,responseId)
{
	var formobj = DHTMLSuite.getEl(formid);
	var formUtil = new DHTMLSuite.formUtil();
	formobj.method = 'POST';
	formobj.action = url;	
	formobj.enctype = 'multipart/form-data';
	__createHiddenIframe(formobj,responseId);
	formobj.submit();
}
function __getPostIframeResponse(ifname,respid)
{
	var respcont=DHTMLSuite.getEl(respid);
	respcont.innerHTML = self.frames[ifname].document.body.innerHTML;	
}

function __createHiddenIframe(formobj,responseId)
{
	var div = document.createElement('DIV');
	document.body.appendChild(div);
	var d = new Date(); 
	var x=(d.getTime()-d.getMilliseconds())/1000;
	var iframeName = 'fuploadForm' + x;
	div.innerHTML = '<iframe style="visibility:hidden;width:5px;height:5px" id="' + iframeName + '" name="' + iframeName + '" onload="parent.__getPostIframeResponse(\''+iframeName+'\',\''+responseId+'\')"></iframe>'; 
	formobj.target = iframeName;	
}
