/* JavaScript part of Max-Hebergs (www.max-hebergs.net). **
** Created by Loic Percetti, copyright Max-Hebergs, all rights reserved. **
** Reproduction without written autorization is prohibited. */

var reg = /[^a-z0-9.-]/;
var checkednb, timer, process;
var selectdmn = '';
var extensions = new Array("com", "net", "org", "info", "name", "biz", "cc", "me", "tv", "mobi", "lu", "de", "fr", "be", "nl", "ch", "it", "se", "eu", "pl", "us", "asia", "in", "co.uk");

/* Generals functions */

Array.prototype.count = function() {
	return this.length;
};

Array.prototype.in_array = function(p_val) {
	for(i=0,l=this.length;i<l;i++) {
		if(this[i] == p_val)
			return true;
	}
	return false;
}

function getextension(str) {
	var i = 0;
	var pos = 0;
	var newstr = "";
	for (i=0;i<str.length;i++) {
		if (str.charAt(i) == ".") 
			pos = i;
	}
	if (pos == 0) 
		return "";
	if (pos > 0) {
		for (i=pos;i<str.length;i++)
			newstr = newstr+str.charAt(i);
		return newstr.toLowerCase().substr(1);
	}
}

function pregmatch(text) {
	m = reg.exec(text);
	while (m) {
		return true;
	}
	return false;
}

/* Specifics functions */

function hidder() {
	selectdmn = '';
	process++;
	clearTimeout(timer);
	document.getElementById('dmninfo').innerHTML = '';
	document.whois.dmn.value = document.whois.dmn.value.toLowerCase();
	document.whois.dmn.value = document.whois.dmn.value.replace(' ', '');
	if(document.whois.dmn.value.length < 2)
		document.getElementById('ext_list').style.display = 'none';
	else if(document.whois.dmn.value.length < 3 || pregmatch(document.whois.dmn.value))
		sub();
	else
		timer = setTimeout("sub()", 1100);

	process--;
	dmn_chk('DELETEALL');
}

function dmn_chk(type) {
	ido = 0; process++;
	while(extensions.count() > ido) {
		if(process > 1) {
			// break;
			process--;
			return false;
		}
		if(type != 'DELETEALL')
			dmn_process(extensions[ido]);
		else
			document.getElementById(extensions[ido]).innerHTML = '<input type="radio" name="ext" value="'+extensions[ido]+'" disabled="disabled" />.'+extensions[ido];
		ido++;	
	}
	process--;
}

function dmn_process(ext) {
	document.getElementById(ext).innerHTML = '<input type="radio" name="ext" value="'+ext+'" disabled="disabled" /><span style="color:#056ab1;">.'+ext+'</span>';
	var xhr; 
	try {
		xhr = new XMLHttpRequest();
	}          
	catch(e) {
		xhr = new ActiveXObject(Microsoft.XMLHTTP);
	}
	xhr.onreadystatechange = function() { 
		if(xhr.readyState == 4) {
			checkednb++;
			if(checkednb >= extensions.count()) {
				document.getElementById('loadstat').innerHTML = 'termin&eacute;e.';
				document.getElementById('loadbox').style.visibility = 'hidden';
			}
			if(xhr.status == 200) { // Si on recois les données (possible de else).
				if(xhr.responseText == "DISPONIBLE") {
					document.getElementById(ext).innerHTML = '<input type="radio" id="ext-'+ext+'" name="ext" value="'+ext+'" onchange="box_act(this.value);" /><label for="ext-'+ext+'" style="color:green;">.'+ext+'</label>';
					return true;
				}
				else if(xhr.responseText == "INDISPONIBLE") {
					document.getElementById(ext).innerHTML = '<input type="radio" name="ext" value="'+ext+'" onchange="box_act(this.value);" disabled="disabled" /><span style="color:red;">.'+ext+'</span>';
					return true;
				}
			} else
				return false;
		}
	}; 
	xhr.open("GET", "scripts/domaines-whois.php?dmn="+document.whois.dmn.value+"."+ext,true); 
	xhr.send(null);
}

function checking() {
	if(!extensions.in_array(getextension(document.whois.dmn.value))) {
		document.getElementById('dmninfo').innerHTML = '<br /><span style="color:red;">L\'extension de domaine sp&eacute;cifi&eacute;e n\'est pas disponible.</span>';
		return false;
	}
	document.getElementById('dmninfo').innerHTML = '<br /><span style="color:#056ab1;">La v&eacute;rification du domaine '+document.whois.dmn.value+' est en cours ..</span>';
	var xhr; 
	try {
		xhr = new XMLHttpRequest();
	}          
	catch(e) {
		xhr = new ActiveXObject(Microsoft.XMLHTTP);
	}
	xhr.onreadystatechange = function() { 
		if(xhr.readyState == 4) {
			if(xhr.status == 200) { // Si on recois les données (possible de else).
				if(xhr.responseText == "DISPONIBLE") {
					selectdmn = document.whois.dmn.value;
					document.getElementById('dmninfo').innerHTML = '<br /><span style="color:green;">Le nom de domaine '+document.whois.dmn.value+' est disponible.</span>';
					return true;
				}
				else if(xhr.responseText == "INDISPONIBLE") {
					document.getElementById('dmninfo').innerHTML = '<br /><span style="color:red;">Le nom de domaine '+document.whois.dmn.value+' n\'est pas disponible mais transf&eacute;rable.</span>';
					return true;
				}
			} else
				return false;
		}
	}; 
	xhr.open("GET", "scripts/domaines-whois.php?dmn="+document.whois.dmn.value,true); 
	xhr.send(null);
}

function box_act(ext) {
	selectdmn = document.getElementById('domainame').innerHTML+'.'+ext;
}

function sub(launcher) {
	if(launcher == 'but' && selectdmn.length > 2) {
		window.location = '?domaine='+selectdmn;
		return false;
	}
	if(launcher == 'but' && document.whois.dmn.value.length < 2) {
		alert('Vous devez spécifier un nom de domaine avant de pouvoir commander !');
		return false;
	}
	if(document.getElementById('domainame').innerHTML == document.whois.dmn.value) // anti-flood
		return false;
	selectdmn = '';
	document.getElementById('ext_list').style.display = 'none';
	if(document.whois.dmn.value.length < 3)
		document.getElementById('dmninfo').innerHTML = '<br /><span style="color:red;">Le nom de domaine <em>'+document.whois.dmn.value+'</em> est trop petit.</span>';
	else if(pregmatch(document.whois.dmn.value))
		document.getElementById('dmninfo').innerHTML = '<br /><span style="color:red;">Le nom de domaine <em>'+document.whois.dmn.value+'</em> est invalide.</span>';
	else {
		document.getElementById('domainame').innerHTML = document.whois.dmn.value;
		if(document.whois.dmn.value.match(/[.]/))
			checking();
		else {
			checkednb = 0;
			document.getElementById('ext_list').style.display = '';
			document.getElementById('loadstat').innerHTML = 'en cours ..';
			document.getElementById('loadbox').style.visibility = 'visible';
			dmn_chk();
		}
	}
}

function showdns(act) {
	if(act == 0)
		document.getElementById('dns').style.display = 'none';
	else
		document.getElementById('dns').style.display = '';
}

