// JavaScript Document

function resetWhoisForm () {
	
	document.getElementById('domain_query_button').disabled = true;
	//mi devo ricordare si svuotare il DIV con i vecchi risultati se ci sono
	document.getElementById('yourDomain').innerHTML = "";
	document.getElementById('suggestDomains').innerHTML = "";
	document.getElementById('yourDomain_status').innerHTML = "";
	//rinascondo il box dei risultati dopo averlo svuotato
	document.getElementById('box_domain_result').style.display="none";
	//mostro il LOADING
	document.getElementById('box_loading').style.display="block";
	//nascondo la barra dei comandi
	//document.getElementById('nextStepDomainReg').style.display="none";
	return false;
}

function checkWhoisForm () {
	errors = '';
	if (document.getElementById("domain").value.length == 0) {
		errors+='- E\' inserire un valore nel campo dominio.\n';
	}
	
	if (errors){
			alert('Si sono verificati i seguenti errori:\n'+errors);
			return false;
		} else {
			dataLoading();
			return false;
		}
}

//FORM - INIZIO
/*$(document).ready(doWhoisPost);

function doWhoisPost () {
//$('#msgbox').toggle();
	//domain = document.getElementById("domain").value+'.'+document.getElementById("tld").value;
	
	$('#domain_query').submit( 
							  
		function () {
		$.ajax({
				type: 'POST',
				async: false,
				url: '/cgi-bin/domain_query.pl',
				//data: 'domain='+document.getElementById("domain").value+'.'+document.getElementById("tld").value,
				data: 'domain='+document.getElementById("domain").value,
				dataType: 'xml',
				success: onSuccess
			  });
		return false;
		}
		
	);

}*/

dataLoading=function() {
	resetWhoisForm();
	$.ajax({
				type: 'POST',
				//async: false,
				url: '/cgi-bin/domain_query.pl',
				//data: 'domain='+document.getElementById("domain").value+'.'+document.getElementById("tld").value,
				data: 'domain='+document.getElementById("domain").value,
				dataType: 'xml',
				success: onSuccess
			  });
		return false;
}
	
function onSuccess(xmlObj){
	
	var lista = xmlObj.getElementsByTagName('domain');
	//alert(lista.length)
	for (var i = 0; i < lista.length; i++) {
					
					property = lista[i];
					domain = property.getElementsByTagName('name')[0].firstChild.nodeValue;
					result = property.getElementsByTagName('result')[0].firstChild.nodeValue;

					//$('#dominio'+i+'_box').html(domain)
					createOutputDomain(i,domain,result)
	}
}
function createOutputDomain(id,domain,status) {
	
	if (status == 0) {
		status_label = "Dominio occupato"
		status_label_smart = "occupato"
		status_image = "ico-check-ko.gif";
		setStat = 'busy';
	} else if (status == 1) {
		status_label = "Dominio libero"
		status_label_smart = "libero"
		status_image = "ico-check-ok.gif";
		setStat = 'free';
	} else {
		status_label = "Dominio sconosciuto"
		status_image = "ico-check-ko.gif";
		status_label_smart = "sconosciuto"
		setStat = 'unknown';
	}
	//splitto la SELECT con il TLD e il prezzo del SOLO DOMINIO SELEZIONATO
	slected_tld = document.getElementById("tld").value
	var tldArray = slected_tld.split('#')
	var tld = tldArray[0];
	var domain_price = tldArray[1];
	
	//splitto il dominio intero per ricavare il TLD
	var domainArray = domain.split('.')
	var domain_name = domainArray[0];
	var domain_tld = domainArray[1];
	
	createDiv_suggestDomainList_1 = document.createElement("div");
	createDiv_suggestDomainList_1.setAttribute('id',"suggestDomainList_1");
	
	createDiv_suggestDomainList_2 = document.createElement("div");
	createDiv_suggestDomainList_2.setAttribute('id',"suggestDomainList_2");
	
	document.getElementById("suggestDomains").appendChild(createDiv_suggestDomainList_1);
	document.getElementById("suggestDomains").appendChild(createDiv_suggestDomainList_2);
	
	createDiv_domain_box_preferred = document.createElement("div");
	createDiv_domain_box_preferred.setAttribute('id',"tempDomain_"+domain+"_box");
	createDiv_domain_box_preferred.setAttribute('class',"tempDomain_box_preferred");
	createDiv_domain_box_preferred.setAttribute('className',"tempDomain_box_preferred"); //IE Fix
	
	createDiv_yourDomain = document.createElement("div");
	createDiv_yourDomain.setAttribute('id',"yourDomain_"+setStat);
	createDiv_yourDomain.setAttribute('class',"yourDomain_"+setStat);
	createDiv_yourDomain.setAttribute('className',"yourDomain_"+setStat); //IE Fix
	
	createDiv_domain_box = document.createElement("div");
	createDiv_domain_box.setAttribute('id',"tempDomain_"+domain+"_box");
	createDiv_domain_box.setAttribute('class',"tempDomain_box");
	createDiv_domain_box.setAttribute('className',"tempDomain_box"); //IE Fix
	
	createDiv_domain_check_container = document.createElement("div");
	createDiv_domain_check_container.setAttribute('id',"checkb_"+domain+"_container");
	createDiv_domain_check_container.setAttribute('class',"checkb_container");
	createDiv_domain_check_container.setAttribute('className',"checkb_container"); //IE Fix
	
	createDiv_domain_image = document.createElement("img");
	createDiv_domain_image.setAttribute('src',"../images/"+status_image);
	createDiv_domain_image.setAttribute('id',"checkb_"+domain+"_image");

	//HO DOVUTO TOGLIERE L'INPUT CAUSA BUG IEx //UPD: lo ripristino perche non userņ il JS ma un POST
	createDiv_checkbox = document.createElement("input");
	createDiv_checkbox.setAttribute('name', "options"+id);
	createDiv_checkbox.setAttribute('id', "dominio"+id+"_"+domain_name);
	createDiv_checkbox.setAttribute('type', "checkbox");
	createDiv_checkbox.setAttribute('value', domain);
	//UNA VOLTA STABILITO IL PRICING DEI DOMINI, POSSO RECUPERARLO DA QUI E AGGIUNGERLO ALL ARRAY
	//createDiv_checkbox.setAttribute('value', 'Registrazione dominio#dominio'+id+"1#"+domain_price+"#"+domain);
	createDiv_checkbox.setAttribute('onClick', "javascript:addToCart('Registrazione dominio',this.id,1,"+domain_price+",this.value,'canone mensile');");
	//createDiv_checkbox.setAttribute('onClick', "javascript:alert(this.value)");
	
	/*createDiv_checkbox = document.createElement("a");
	createDiv_checkbox.setAttribute('href', "javascript:addToCart('Registrazione dominio','dominio"+id+"',1,'32','"+domain+"');");
	createDiv_checkbox.setAttribute('id', 'dominio'+id);
	createDiv_checkbox.appendChild(document.createTextNode("Aggiungi"));*/
	
	createDiv_whois = document.createElement("a");
	createDiv_whois.setAttribute('href', "javascript:openPopup(\'/products/library/whois/?domain="+domain_name+"&tld="+domain_tld+"','450','700');");
	createDiv_whois.appendChild(document.createTextNode("Whois"));
	
	createDiv_transfer = document.createElement("a");
	createDiv_transfer.setAttribute('href', "javascript:addToCart('Trasferisci dominio','trasfer"+id+"_"+domain_name+"',1,"+domain_price+",'"+domain+"','canone mensile');");
	createDiv_transfer.appendChild(document.createTextNode("Trasferisci"));
	
	createDiv_domain_label = document.createElement("div");
	createDiv_domain_label.setAttribute('id',"tempDomain_"+domain+"_label");
	createDiv_domain_label.setAttribute('class',"tempDomain_label");
	createDiv_domain_label.setAttribute('className',"tempDomain_label");
	createDiv_domain_label.appendChild(document.createTextNode(domain));
	
	/*createDiv_domain_actions = document.createElement("div");
	createDiv_domain_actions.setAttribute('id',"tempDomain_"+domain+"_actions");
	createDiv_domain_actions.setAttribute('class',"tempDomain_actions");
	createDiv_domain_actions.setAttribute('className',"tempDomain_actions");*/
	
	createDiv_br = document.createElement("br");
	createDiv_br.setAttribute('class',"clear");
	
	
	if (tld == domain_tld) {
		//alert("entro qui")
		if (setStat == 'free') {
			yourDomain_label = 'Il dominio &egrave; '+status_label_smart+'!';
		} else if (setStat == 'busy')  {
			yourDomain_label = 'Il dominio &egrave; '+status_label_smart+'! <a href=javascript:openPopup("/products/library/whois/?domain='+domain_name+'&tld='+domain_tld+'","450","700")>(Dettagli)</a> Scegline un\'altro tra i disponibili.';
			//yourDomain_label = 'Il dominio &egrave; '+status_label_smart+'! Scegline un\'altro tra i disponibili..';
		} else {
			yourDomain_label = 'Si &egrave; verificato un errore. Riprovare..';
		}
		
		
		document.getElementById("yourDomain_status").innerHTML=yourDomain_label;
		//alert("DEBUG -> "+domain_tld)
		//popolo yourDomain con yourDomain FREE or BUSY
		document.getElementById("yourDomain").appendChild(createDiv_yourDomain);
		//document.getElementById("yourDomain").appendChild(createDiv_domain_box_preferred);
		document.getElementById("yourDomain_"+setStat).appendChild(createDiv_domain_box_preferred);
		//alert("PRICE -> "+domain_price)
	} else {
		//devo provare ad incolonnare i risultati
		var maxResultForColumn = 7;
		if (id< maxResultForColumn) {
			document.getElementById("suggestDomainList_1").appendChild(createDiv_domain_box);
		} else {
			document.getElementById("suggestDomainList_2").appendChild(createDiv_domain_box);
		}
		
	}
	
	
	document.getElementById("tempDomain_"+domain+"_box").appendChild(createDiv_domain_check_container);
	
	//document.getElementById("tempDomain_"+domain+"_box").appendChild(createDiv_domain_actions);
	if (status == 1) {
		if (tld == domain_tld) {
			//se č libero ed č quello che ho chiesto io allora spunto il CHECKBOX
			//createDiv_checkbox.setAttribute('checked', "checked");
		}
		//metto il checkbox solo sui domini liberi
		document.getElementById("checkb_"+domain+"_container").appendChild(createDiv_checkbox);
		//document.getElementById("checkb_"+domain+"_container").appendChild(createDiv_checkbox);
		//document.getElementById("tempDomain_"+domain+"_actions").appendChild(createDiv_checkbox);
	} else {
		document.getElementById("checkb_"+domain+"_container").appendChild(createDiv_domain_image);
	}
	
	document.getElementById("tempDomain_"+domain+"_box").appendChild(createDiv_domain_label);
	
	if (status == 0) {
	//se il dominio č occupato aggiungo il link per l'WHOIS e/o TRASFERIMENTO
	document.getElementById("tempDomain_"+domain+"_box").appendChild(createDiv_transfer);
	//document.getElementById("tempDomain_"+domain+"_box").appendChild(createDiv_whois);
	//mostro le note per i dominio BUSY
	document.getElementById('notes_domainBusy').style.display="block";
	//document.getElementById('notes_domainFree').style.display="none";
	
	} else {
		document.getElementById('notes_domainBusy').style.display="none";
		//document.getElementById('notes_domainFree').style.display="block";
	}
	
	document.getElementById("tempDomain_"+domain+"_box").appendChild(createDiv_br);
	
	document.getElementById('box_loading').style.display="none";
	document.getElementById('box_domain_result').style.display="block";
	//mostro la barra con i comandi // NECESSARIA?
	//document.getElementById('nextStepDomainReg').style.display="block";
	document.getElementById('domain_query_button').disabled = false;
	
}
