/*
------------------------------------------------------------------------------------------------------------------------------------------------
COL Contact form functions
------------------------------------------------------------------------------------------------------------------------------------------------	
*/

function populatelist(elementid,kind,itemid)
{
	var colcode = document.getElementById('companycode').value;
	var colurl = 'colcontact.php?gotourl='+document.getElementById('colurl').value+'web/svc.php';

	var getinfo = 'svc_id=contactform&showxmlheader=no';
	var postinfo = 'companycode='+colcode+'&cf_action=getlist&cf_kind='+kind+'&cf_itemid='+itemid+'&cf_elementid='+elementid;	
	ajaxReq(colurl+'&'+getinfo,postinfo,'populatelist_data');	
}


function populatelist_data(response)
{
   	var xmldoc = xmlFix(response);
   	var	xmldata = xml2array(xmldoc);  
   	   		
   	if (!xmldata['colweb']) return;
   	   		 
   	if (xmldata['colweb']['listinfo']['cf_elementid'])
   	{
   		obj = document.getElementById(xmldata['colweb']['listinfo']['cf_elementid']);
  		if (!obj) return; 
   	}  
   	
   	if (xmldata['colweb']['list'])
	   	if (xmldata['colweb']['list']['item'])
	   	{     
	   		var list = xmldata['colweb']['list']['item'];
	   		   		
	   		for (i in list)
	   		{
	   			var opt = document.createElement("OPTION");
	   			opt.text = list[i]['title']; 
	   			opt.value = list[i]['value'];
	   			if (opt.text=="undefined") opt.text="";
				obj.options.add(opt);
	   		}   	
	   	}   	
}





/*
------------------------------------------------------------------------------------------------------------------------------------------------
AJAX functions
------------------------------------------------------------------------------------------------------------------------------------------------	
*/

	/* FUNCTION: Ajax Request via POST.
		PARAMS:		strURL:			target URL
					strSubmit:		POST string, URL format ('&param=value' etc.);
					strResultFunc:	NAME (string) of function to be executed on reply	
	*/
	function ajaxReq(strURL, strSubmit, strResultFunc) {
		var xmlHttpReq = false;
		// Mozilla/Safari/IE7
		if (window.XMLHttpRequest) 
		{			
			xmlHttpReq = new XMLHttpRequest();
			if (xmlHttpReq.overrideMimeType)
				xmlHttpReq.overrideMimeType('text/xml'); // this would break IE7..
		}
		// <=IE6
		else if (window.ActiveXObject) 
		{		
			xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
		xmlHttpReq.open('POST', strURL, true);
		xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlHttpReq.onreadystatechange = function() {
			if (xmlHttpReq.readyState == 4) {
				if (xmlHttpReq.status == 200) {
					if (strResultFunc) { // Run result function
						if (typeof strResultFunc == "string")
						eval(strResultFunc+'(xmlHttpReq);');
						else
							eval(strResultFunc(xmlHttpReq));
					}
					else // if no result function was specified, simply return true.
						return true;
				}
			}
        }
       	xmlHttpReq.send(strSubmit);
	}

		
	/*	FUNCTION:	XML document fixer for IE6-. IE doesn't read responseXML, this should fix that.
		 			Drop your xmlHTTPReq elements here and the function would return XML objects.
		
		PARAMS: 	Request:	XmlHTTPRequest object
	*/
	function xmlFix(request) 
	{
		// for standard compliant browsers
		if (document.implementation && document.implementation.createDocument) {
			return request.responseXML;
		}
		//Internet explorer
		else if (window.ActiveXObject){
		// Search and destroy (or at least rename) older nodes. 
		// This is To avoid multiple nodes (getElementById problem)
			var oldung = document.getElementById('_formjAjaxReturnXML');
			if (oldung) oldung.id = 'obsolete';
		// Now the real deal happens
			testandoAppend = document.createElement('xml');
			testandoAppend.setAttribute('innerHTML',request.responseText);
			testandoAppend.setAttribute('id','_formjAjaxReturnXML');
			document.body.appendChild(testandoAppend);
			xmlDoc = document.getElementById('_formjAjaxReturnXML');
			return xmlDoc;
		}
		else {
		//If the browser doesn't support xml
			alert('Your browser can\'t handle XML documents, please contact support.');
		}
	}
		
	
	//////////////////////////////////// xml2array() ////////////////////////////////////////
	//See http://www.openjs.com/scripts/xml_parser/
	var not_whitespace = new RegExp(/[^\s]/);//This can be given inside the funciton - I made it a global variable to make the script a little bit faster.
	var parent_count;
	//Process the xml data
	function xml2array(xmlDoc,parent_count) 
	{
		var arr;
		var parent = "";
		parent_count = parent_count || new Object;
	
		var attribute_inside = 0; /*:CONFIG: Value - 1 or 0
		*	If 1, Value and Attribute will be shown inside the tag - like this...
		*	For the XML string...
		*	<guid isPermaLink="true">http://www.bin-co.com/</guid>
		*	The resulting array will be...
		*	array['guid']['value'] = "http://www.bin-co.com/";
		*	array['guid']['attribute_isPermaLink'] = "true";
		*	
		*	If 0, the value will be inside the tag but the attribute will be outside - like this...	
		*	For the same XML String the resulting array will be...
		*	array['guid'] = "http://www.bin-co.com/";
		*	array['attribute_guid_isPermaLink'] = "true";
		*/
		
		if(xmlDoc.nodeName && xmlDoc.nodeName.charAt(0) != "#") 
		{			
			if(xmlDoc.childNodes.length > 1) { //If its a parent
				arr = new Object;
				parent = xmlDoc.nodeName;
				
			}
		}
		var value = xmlDoc.nodeValue;
		if(xmlDoc.parentNode && xmlDoc.parentNode.nodeName && value) 
		{
			if(not_whitespace.test(value)) {//If its a child
				arr = new Object;
				arr[xmlDoc.parentNode.nodeName] = value;
			}
		}
	
	

		if(xmlDoc.childNodes.length) {
			if(xmlDoc.childNodes.length == 1) { //Just one item in this tag.
				arr = xml2array(xmlDoc.childNodes[0],parent_count); //:RECURSION:
			} else { //If there is more than one childNodes, go thru them one by one and get their results.
				var index = 0;
	
				for(var i=0; i<xmlDoc.childNodes.length; i++) {//Go thru all the child nodes.
					var temp = xml2array(xmlDoc.childNodes[i],parent_count); //:RECURSION:
					if(temp) {
						var assoc = false;
						var arr_count = 0;
						for(key in temp) {
							if(isNaN(key)) assoc = true;
							arr_count++;
							if(arr_count>2) break;//We just need to know wether it is a single value array or not
						}
						
						if(assoc && arr_count == 1) {
							if (typeof arr === "undefined")
								continue;
							
							if(arr[key]) { 	//If another element exists with the same tag name before,						
											//		put it in a numeric array.
								//Find out how many time this parent made its appearance
								if(!parent_count || !parent_count[key]) {
									parent_count[key] = 0;
	
									var temp_arr = arr[key];
									arr[key] = new Object;
									arr[key][0] = temp_arr;
								}
								parent_count[key]++;
								arr[key][parent_count[key]] = temp[key]; //Members of of a numeric array
							} else {							
								parent_count[key] = 0;
								arr[key] = temp[key];
								if(xmlDoc.childNodes[i].attributes)
								if(xmlDoc.childNodes[i].attributes.length) {
									for(var j=0; j<xmlDoc.childNodes[i].attributes.length; j++) {
										var nname = xmlDoc.childNodes[i].attributes[j].nodeName;
										if(nname) {
											/* Value and Attribute inside the tag */
											if(attribute_inside) {
												var temp_arr = arr[key];
												arr[key] = new Object;
												arr[key]['value'] = temp_arr;
												arr[key]['attribute_'+nname] = xmlDoc.childNodes[i].attributes[j].nodeValue;
											} else {
											/* Value in the tag and Attribute otside the tag(in parent) */
												arr['attribute_' + key + '_' + nname] = xmlDoc.childNodes[i].attributes[j].nodeValue;
											}
										}
									} //End of 'for(var j=0; j<xmlDoc. ...'  
								} //End of 'if(xmlDoc.childNodes[i] ...'  
							}
						} else {
							arr[index] = temp;
							index++;
						}
					} //End of 'if(temp) {'
				} //End of 'for(var i=0; i<xmlDoc. ...'
			}
		}
	
		if(parent && arr) {
			var temp = arr;
			arr = new Object;
			
			arr[parent] = temp;
		}
		return arr;
	}	
		
		
	function dump(arr,level) 
	{
		var dumped_text = "";
		if(!level) level = 0;
		
		//The padding given at the beginning of the line.
		var level_padding = "";
		for(var j=0;j<level+1;j++) level_padding += "    ";
		
		if(typeof(arr) == 'object') { //Array/Hashes/Objects
		 for(var item in arr) {
		  var value = arr[item];
		 
		  if(typeof(value) == 'object') { //If it is an array,
		   dumped_text += level_padding + "'" + item + "' ...\n";
		   dumped_text += dump(value,level+1);
		  } else {
		   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
		  }
		 }
		} else { //Stings/Chars/Numbers etc.
		 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
		} 
		return dumped_text;  
	} 	
		