function costCalc(price,vat,count)
{
    costValue=price*count;
    if (document.getElementById('region').value==0)
    {
        document.getElementById('cost').innerHTML='&pound;'+Number(costValue).toFixed(2);
        document.getElementById('vat').innerHTML='N/A';
        document.getElementById('total').innerHTML='N/A';
        document.getElementById('submit_button').style.visibility = 'hidden';
        return 1;
    }

	if (isNaN(count) || count=='' || parseInt(count)<1)
	{
		document.getElementById('error').innerHTML='Wrong quantity';
		document.getElementById('cost').innerHTML='';
    	document.getElementById('vat').innerHTML='';
    	document.getElementById('total').innerHTML='';
    	document.getElementById('submit_button').style.visibility = 'hidden';
    	return 1;
	}

	count=parseInt(count);

	costValue=price*count;
    vatValue= (document.getElementById('region').value==3) ? 0 : vat*count;
    totalValue=costValue+vatValue;
	if (costValue>500)
	{
		document.getElementById('cost').innerHTML='&pound;'+Number(costValue).toFixed(2);
		document.getElementById('vat').innerHTML='';
    	document.getElementById('total').innerHTML='<font color="red" size="1">Due to shipping insurance constraints this order cannot be sent using the shipping options available online. Please email your enquiry to info@airbornecricket.com and we will give supply a quote by return. Apologies, airbornecricket.com</font>';
    	document.getElementById('submit_button').style.visibility = 'hidden';
    	return 1;
	}

    linkName = new String(document.getElementById('status').getAttribute("href"));
	linkArray = linkName.split("=");
	document.getElementById('submit_button').style.visibility = 'visible';
    document.getElementById('cost').innerHTML='&pound;'+Number(costValue).toFixed(2);
    document.getElementById('vat').innerHTML= (document.getElementById('region').value==3)  ? 'N/A' : '&pound;'+Number(vatValue).toFixed(2);
    document.getElementById('total').innerHTML='&pound;'+Number(totalValue).toFixed(2);
    document.getElementById('status').setAttribute("href", "http://www.xe.com/pca/input.php?Amount="+Number(totalValue).toFixed(2)+"&From=GBP&ToSelect="+linkArray[3]);

}

function editCostCalc(price,vat,count)
{

	if (isNaN(count) || count=='' || parseInt(count)<1)
	{
		document.getElementById('cost').innerHTML='<font color="red">Wrong quantity</font>';
    	document.getElementById('vat').innerHTML='';
    	document.getElementById('total').innerHTML='';
    	document.getElementById('submit_button').style.visibility = 'hidden';
    	return 1;
	}

	count=parseInt(count);

	costValue=price*count;
    vatValue= vat*count;
    totalValue=costValue+vatValue;
	if (costValue>500)
	{
		document.getElementById('cost').innerHTML='&pound;'+Number(costValue).toFixed(2);
		document.getElementById('vat').innerHTML='';
    	document.getElementById('total').innerHTML='<font color="red" size="1">Due to shipping insurance constraints this order cannot be sent using the shipping options available online. Please email your enquiry to info@airbornecricket.com and we will give supply a quote by return. Apologies, airbornecricket.com</font>';
    	document.getElementById('submit_button').style.visibility = 'hidden';
    	return 1;
	}

	document.getElementById('submit_button').style.visibility = 'visible';
    document.getElementById('cost').innerHTML='&pound;'+Number(costValue).toFixed(2);
    document.getElementById('vat').innerHTML= '&pound;'+Number(vatValue).toFixed(2);
    document.getElementById('total').innerHTML='&pound;'+Number(totalValue).toFixed(2);
}

function changeCountry()
{
    select=document.getElementById('region');
    option=select.options[select.selectedIndex];
    document.getElementById('country_field').value=option.text;
}

function showRegForm(selector)
{
    if (selector.checked)
    {
        document.getElementById('password').style.visibility = 'visible';
        document.getElementById('news').style.visibility = 'visible';
        setElementVisibility(document.getElementById('password'), true);
        setElementVisibility(document.getElementById('news'), true);
    }
    else
    {
        document.getElementById('password').style.visibility = 'hidden';
        document.getElementById('news').style.visibility = 'hidden';
    }
}

function getXmlHttp()
{
	var xmlhttp;
 	try
 	{
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	}
 	catch (e)
 	{
 		try
 		{
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
 			xmlhttp = false;
		}
 	}
 	if (!xmlhttp && typeof XMLHttpRequest!='undefined')
 	{
 		xmlhttp = new XMLHttpRequest();
 	}
 	return xmlhttp;
}

function changeLink()
{
    var req = getXmlHttp()
    
    select=document.getElementById("region");
    option=select.options[select.selectedIndex];
    input=option.text;
    var total = document.getElementById('total').innerHTML;
    
    req.open("GET", "http://www.airbornecricket.com/changelink.php?c="+input+"&t="+total, true);  // задать адрес подключения

    var statusElem = document.getElementById('status');

    req.onreadystatechange = function()
    {  // onreadystatechange активируется при получении ответа сервера
    	if (req.readyState == 4)
    	{ // если запрос закончил выполняться
            if(req.status == 200)
            { // если статус 200 (ОК) - выдать ответ пользователю
            	linkName = new String(req.responseText);
				linkArray = linkName.split(" ");
				statusElem.innerHTML = "<u>convert to "+linkArray[1]+"</u>"; // показать статус (Not Found, ОК..)
				statusElem.setAttribute("href", "http://www.xe.com/pca/input.php?Amount="+linkArray[2]+"&From=GBP&ToSelect="+linkArray[0]);
            }
            // тут можно добавить else с обработкой ошибок запроса
        }

    }

    // объект запроса подготовлен: указан адрес и создана функция onreadystatechange
    // для обработки ответа сервера

    req.send(null);  // отослать запрос

    statusElem.innerHTML = "Waiting...";
}
