var rootDir = '/';
var fixedPrice = 0;

function newCaptcha(){
	document.getElementById('captchaIMG').src = rootDir + 'includes/captcha.php?' + Math.round(Math.random()*100000);
}

function addRow(tableId, value, pos, cols, addParam, th){
	th = (th == true) ? 'h' : 'd';
	addParam = (addParam == null) ? '' : addParam;
	
	var ele = document.getElementById(tableId);
	if(pos == -1)
		pos = ele.rows.length;
	else
		pos = eval(pos);
	var newRow = ele.insertRow(pos);
	newRow.innerHTML = '<t'+ th + addParam +'>' + value + '</t'+ th +'>';
}

function addDomain(){
	var name = document.getElementById('domainName');
	var tld = document.getElementById('domainTld');
	if(name.value != '' && tld.selectedIndex > 0){
		return checkDomain(1);
	}
	else{
		alert('Please insert first your desired Domain!');
		return false;
	}
}

function insertDomain(){
	var ele = document.getElementById('orderedDomains');
	var name = document.getElementById('domainName');
	var tld = document.getElementById('domainTld');
	var temp = ele.innerHTML.split(/\<br\>/);
	
	var domain = 'http://' + name.value + tld.options[ tld.selectedIndex ].innerHTML;
	
	ele.innerHTML += domain + '<input type="submit" name="delete" value="' + (temp.length-1) + '" class="deleteDomain" onclick="return deleteDomain(' + (temp.length-1) + ');" /><input type="hidden" name="domain[' + (temp.length-1) + '][domainName]" value="' + name.value + '" /><input type="hidden" name="domain[' + (temp.length-1) + '][tld]" value="' + tld.value + '" /><br />';
	
	var price = parseFloat(tld.options[tld.selectedIndex].title);
	fixedPrice = price;
	
	name.value = '';
	tld.selectedIndex = 0;
	document.getElementById('avail').innerHTML = '';
}

function deleteDomain(index){
	var ele = document.getElementById('orderedDomains');
	var domains = ele.innerHTML.split(/\<br\>/);
	domains[index] = '';
	ele.innerHTML = domains.join('');
}

/*
function addDomain(){
	var value = document.getElementById('domainBase').innerHTML;
	var ele = document.getElementById('products');
	var pos = ele.rows.length-2;
	var id = pos - 3;
	
	value = '<div class="contact">' + value.replace(/([a-z]+)(\[)?0(\])?/ig, '$1$2' + id + '$3') + '</div>' + 
	'<a href="javascript:void(0);" onclick="checkDomain(' + id + ')">Available?</a> <span id="avail' + id + '">Unknown</span> </td>' + 
	'<td id="domainPrice' + id + '" class="price">0.00 &euro; /<br /> Year</td>';
	
	addRow('products', value, pos);
}*/

function updateDomainPrice(ele){
	var q = document.getElementById('domainPrice');
	var price = parseFloat(ele.options[ele.selectedIndex].title);
	
	price += fixedPrice;
	
	q.innerHTML =  price.toFixed(2) + ' &euro; /<br /> ' + year;
	updatePrice();
}

function updatePrice(){
	var hosting = document.getElementById('hostPrice').innerHTML;
	hosting = hosting.substring(0, hosting.indexOf(' '));
	hosting = parseFloat(hosting);
	
	var domains = document.getElementById('domainPrice');
	var domainPrice = 0.00;
	
	domains = domains.innerHTML;
	domainPrice += parseFloat(domains.substring(0, domains.indexOf(' ')));
	
	var totalPrice = hosting * 12.00 + domainPrice;
	document.getElementById('totalPrice').innerHTML = totalPrice.toFixed(2) + ' &euro; / ' + year;
}

function verifyForm(form){
	var i, j;
	for(i = 0; i < form.elements.length; i++){
		if(form.elements[i].value == '' && form.elements[i].id != 'fax' && form.elements[i].id != 'company' && form.elements[i].id.indexOf('2') == -1 && form.elements[i].id != 'password' && form.elements[i].id != 'cpassword'){
			var q = document.getElementsByTagName('label');
			var friendlyName;
			for(j = 0; j < q.length; j++)
				if(q[j].htmlFor == form.elements[i].id)
					friendlyName = q[j].innerHTML.substr(0, q[j].innerHTML.length);
			alert('Please fill in a value for: ' + friendlyName);
			return false;
		}
	}
	return true;
}

function toggleClient(id, id2){
	var q = document.getElementById(id); 
	q.style.display = 'block';
	q.style.visibility = 'visible';
	q = document.getElementById(id2); 
	q.style.display = 'none';
	q.style.visibility = 'hidden';
	return false;
}
