function getIpFromHost(hostname, comp) {
	createRequestHandle();
	httphandler.open("POST", __SITE_URL+"/includes/getipfromhost.php", true);
	httphandler.onreadystatechange = function() {
		if (httphandler.readyState == 4 && httphandler.status == 200) {
			response = httphandler.responseText;
			$('#'+comp).val(response.substr(11));
		}   
	}
	params = "hostname=" + hostname;
	httphandler.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httphandler.setRequestHeader("Content-length", params.length);
	httphandler.send(params);            
}

var httphandler = false;
var response = "";
function createRequestHandle() { 
	if (!httphandler && typeof XMLHttpRequest!='undefined') {
	    httphandler = new XMLHttpRequest();
	}
}

function check_domain(){
	var domain = '';
	
	if($('#domain_results tr').length > 1){
		$('#domain_results tbody tr').remove();	
	}
	
	$('#domain').css('backgroundImage','');
	
	if($('#domain').val().substr(0,4) == 'www.'){
		domain = $('#domain').val().substr(4);
	}else{
		domain = $('#domain').val();
	}
	
	var regex = /^[-.a-zA-Z0-9]*$/;
	if (regex.test(domain)) {
		// valid domain		
	}else{
		$('#domain').css({'background':'url('+__SITE_URL+'/style/images/small_error.png) no-repeat top right'});
		return
	}
	
	if(domain.length >= 2){
		$('#domainname_result').html('<strong class="black">' + domain + '</strong>');
		$('#domainname_results_text').css('display','block');
		$('#domain_results_tr').show();		
	}else{
		$('#domain').css({'background':'url('+__SITE_URL+'/style/images/small_error.png) no-repeat top right'});
		return;
	}

	if(domain.indexOf('.') != -1){
		var tld = domain.substr(domain.indexOf('.')+1);
		domain = domain.substr(0,domain.indexOf('.'));
	}
	
	if(tld != undefined && in_array_product_tld(tld,product_tlds)){
		var tlds = {"ProductTld": tld};
	}else{
		var tlds = product_tlds;
	}
	
	$(tlds).each( function(index,data){
		$.post(__SITE_URL+"/includes/whois.php", { domain: domain, tld: data.ProductTld },
			function(result){
				if(data.ProductTld != ''){
					if(result == 'available' || result == 'unavailable'){
						if(result == 'available'){
							text = lang_free;
							linktext = lang_order;
						}else{
							text = lang_taken;
							linktext = lang_transfer;
						}

						$('#domain_results > tbody:last').append('<tr><td class="w_200">' + domain + '.' + data.ProductTld + '</td><td class="w_60">' + text + '</td><td class="w_60"><a href="' + __SITE_URL + '/bestelformulier.php?domain=' + domain + '&tld=' + data.ProductTld + '">' + linktext + '</a></td></tr>');
					}else{
						$('#domain_results').after('<tr><td class="w_200">Server error:' + data.ProductTld + '</td><td><img src="'+__SITE_URL+'/style/images/small_error.png" /></td></tr>');
					}
				}
			});
	});
	
}

function get_token(id){
	$.get(__SITE_URL+"/"+__DOMAIN_URL+"/"+__GET_TOKEN_URL, { id: id },
		function(data){
			$('#token_value').html(data);
		}
	);	
}

function in_array_product_tld (needle, haystack) {
	for (key in haystack) {
		if (haystack[key].ProductTld == needle) {
			return true;
		}
	}
}

$(function(){
	$('#check_dns').click(function(){
		if(this.checked === false){ 
			$('#DNS1_col').css('visibility','visible'); 
			$('#DNS2_col').css('visibility','visible');
			$('#DNS3_col').css('visibility','visible');
			
			getIpFromHost($('#DNS1').val(), 'DNS1IP'); 
			setTimeout ("getIpFromHost($('#DNS2').val(), 'DNS2IP');", 200);
			setTimeout ("getIpFromHost($('#DNS3').val(), 'DNS3IP');", 400);
		}else{ 
			$('#DNS1_col').css('visibility','hidden'); 
			$('#DNS2_col').css('visibility','hidden'); 
			$('#DNS3_col').css('visibility','hidden'); 
		}
	});
});
