/**
 * Basic Savana Script
 *
 * @author Martin Majlis (martin@m-core.net)
 */
function Pair(f, s)
{
	this.first = f;
	this.second = s;
}

function ajax(target, params, handler)
{
    var httpRequest;
    
    if(typeof window.ActiveXObject != 'undefined') {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } else {  
        httpRequest = new XMLHttpRequest();
    }
    
    httpRequest.open('POST', target, true);
    httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
    httpRequest.onreadystatechange = function () 
    {
       	processResponse(httpRequest, handler) 
    };
    
    arg = '';
    while (params.length > 0) { 
    	el = params.pop();
    	arg += el.first + "=" + el.second + "&";
    }
    httpRequest.send(arg);
}

function processResponse(httpRequest, handler)
{
	if (httpRequest.readyState == 4) {
    	if (httpRequest.status == 200) {
			handler(httpRequest.responseText);
		}
    } 
}

function priceCalculation()
{
	hpS = document.getElementById('host-prog');
	hp = hpS.options[hpS.selectedIndex].value;
	
	hdS = document.getElementById('host-dom');
	hd = hdS.options[hdS.selectedIndex].value;	
	
	hyS = document.getElementById('host-years');
	hy = hyS.options[hyS.selectedIndex].value;		
	
	var par = new Array();
	par.push(new Pair("prog", hp));
	par.push(new Pair("tld", hd));
	par.push(new Pair("years", hy));	

	ajax('http://www.savana.cz/script/priceCalculation.php', par, priceCalculationResult); 
}

function priceCalculationResult(txt)
{
	document.getElementById('host-price').value = txt;
	//document.getElementById('news').innerHTML = txt;
}

function isDomainFree()
{
	var par = new Array();
	name = document.getElementById('domain-name').value;

	tldS = document.getElementById('tld');
	tld = tldS.options[tldS.selectedIndex].value;	
	
	
	par.push(new Pair("name", name));
	par.push(new Pair("tld", tld));

	ajax('./script/whois.php', par, isDomainFreeResult);
}

function isDomainFreeResult(txt)
{
	//document.getElementById('news').innerHTML = txt;
	//return;
	if ( txt ) { 

		eval(txt);

		// predelat na DOM
		str = '<div id="domain-avail" onclick="this.parentNode.removeChild(this)">';
		while (domains.length > 0) {
			el = domains.pop();
			str += '<div>' + el.first + ': ';
			if (el.second == 1) { 
				str += 'volna';
			} else { 
				str += 'obsazena';
			}
		}
		str += '</div>';
		document.getElementById('news').innerHTML = str + "<hr />" + document.getElementById('news').innerHTML;		
		
	} 
}
