
function xml2html(p_xmlNode, p_htmlNode){
	for (var i=0; i < p_xmlNode.childNodes.length; i++){
		var currentXmlNode=p_xmlNode.childNodes[i];
		if(currentXmlNode.nodeType==3){
			var textNode=document.createTextNode(currentXmlNode.nodeValue);
			p_htmlNode.appendChild(textNode);
		}
		else {
			var tagName=currentXmlNode.tagName;
			var currentHtmlNode=document.createElement(tagName);
			p_htmlNode.appendChild(currentHtmlNode);
			// recursion
			xml2html(currentXmlNode, currentHtmlNode);
		}
	}
}

// This function make an a ajax request to show user an
// additional info for the order
showAdditionalInfo = function(aID, briefInfoDivId){
	// hide the link and make next element visible
	$('#'+aID).hide().next().show();
	// hide the brief div and make next div with full information visible
	$('#'+briefInfoDivId).hide().next().show();
	return false;
}

showAdditionalInfoBeforeSend = function(XMLHttpRequest, order_id)
{
	$('#aiw_'+order_id).show();
	$('#additional_info_'+order_id).hide();
}

showAdditionalInfoError = function(data, status, order_id)
{
	alert('Error while processing request! Please, try again later.');
	$('#additional_info_'+order_id).show();
}

showAdditionalInfoComplete = function(data, status, order_id)
{
	$('#aiw_'+order_id).hide();
}

showAdditionalInfoSuccess = function(data, status, order_id)
{
	var information=$j('information', data);
	$('#additional_info_full_'+order_id).clone(data);
	$('#additional_info_full_'+order_id).attr('is_loaded','true');
	additionalInfoDoAfterLoad(order_id);
}

hideAdditionalInfo = function(aID, briefInfoDivId){
	$('#'+aID).hide().prev().show();
	$('#'+briefInfoDivId).show().next().hide();
}

additionalInfoDoAfterLoad = function(order_id){
	$('#additional_info_'+order_id).hide();
	$('#additional_info_full_'+order_id).show();

	$('#more_'+order_id).hide();
	$('#less_'+order_id).show();
}

function showHideContactStatusSwitcher(p){
	var c=$('#contact_'+p);
	c=$('ul.contactStatusSwitcher',c);
	if(c.css('display')=='none'){c.show();}else{c.hide();}
}

function onActivateResponse(p){onR(p,'newCounter');}
function onRejectResponse(p){onR(p,'rejectedCounter');}
function onDeactivateResponse(p){onR(p,'inactiveCounter');}
function onMakePerspectiveResponse(p){onR(p,'perspectiveCounter');}
function onMakeWorkedResponse(p){var w=onR(p,'workedCounter');
	if(w.text()=='1')w.parent().wrapAll('<a href="#"></a>');
}

function onR(p,s){
var l=$('errorList',p);
if(l.length){var e=$('error',l);alert(e.attr('message'));return;}
l=$('contact',p);
if(!l.length){alert('unknown error');return;}
var c=$('#contact_'+l.attr('contactID'));
c.hide();
l=$('#contactFilterMenu');
c=$('span.'+s,l);
c.text(parseInt(c.text())+1);
return c;
}

function activateContact(p){$.post('/contacts/activate',{contact_id:p},onActivateResponse,'xml');}
function rejectContact(p){$.post('/contacts/reject',{contact_id:p},onRejectResponse,'xml');}
function deactivateContact(p){$.post('/contacts/deactivate',{contact_id:p},onDeactivateResponse,'xml');}
function makePerspectiveContact(p){$.post('/contacts/makePerspective',{contact_id:p},onMakePerspectiveResponse,'xml');}
function makeWorkedContact(p){$.post('/contacts/makeWorked',{contact_id:p},onMakeWorkedResponse,'xml');}

function showHideRoleSwitcher(){
	var r=$('#roleSwitcher');
	if(r.css('display')=='none'){r.show();}else{r.hide();}
}

validateRegistrationForm = function(form){
	if( checkElementsByEForm(form, false) ){
		if( trim(form.elements['password'].value) != trim(form.elements['password2'].value) ){
			alert(document.getElementById('errPwd').innerHTML);
		}else {
			if( document.getElementById('oferta').checked ){
				form.submit();
			}else {
				alert(document.getElementById('errOferta').innerHTML);
			}
		}
	}
}

function askfor(l,m){if(confirm(m)){window.location.href=l;}}

