function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			if (oldonload)
			{
				oldonload();
			}
			func();
		}
	}
}

function externalLinks()
{
	if (!document.getElementsByTagName) 
		return;

	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";                                               
	}
}

addLoadEvent (externalLinks);


var color_norm = "#8091c7";
var color_fail = "#8091c7";

var last_fail = '';
var error = 'error';

var err_fields = new Array();
var err_email = new Array();

err_fields['lt'] = 'Prašome užpildyti visus laukelius, pažymėtus žvaigždute (*).';
err_fields['en'] = 'Please fill all required fields (*).';
err_fields['ru'] = 'Заполните пожалуйста все поля, помеченные звёздочкой (*).';

err_email['lt'] = 'Neteisingas el. pašto adresas.';
err_email['en'] = 'Wrong e-mail.';
err_email['ru'] = 'Введён неверный адрес эл. почты.';

function validateContacts()
{
	var state = false;              
	state = fieldsCheck('message', 'phone', 'name');
	if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
	return state;
}

function validateOrder()
{
	var state = false;              
	state = fieldsCheck('datetime', 'place', 'phone', 'person', 'company');
	if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
	
	if (state == true)
	{
		state = checkOrder();
	}
	
	if (state == true)
	{
		state = confirmOrder();
	}
	return state;
}

function validateOrder1()
{
	var state = false;
	state = fieldsCheck('type', 'phone', 'address', 'city', 'company_code', 'company');
	if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
	return state;
}

function validateOrder2()
{
	var state = false;              
	state = fieldsCheck('mobile', 'address', 'city', 'hobby', 'identity', 'surname', 'name');
	if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
	return state;
}

function validateCareer()
{
	var state = false;              
	state = fieldsCheck('message', 'cv', 'position', 'name');
	if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
	return state;
}

function fieldsCheck()
{
	var state = true;
	document.getElementById(error).style.display = 'block';
    for (var n = 0; n < arguments.length; n++)
	{
		document.getElementById(arguments[n] + '_l').style.color = color_norm;
	}
    for (var n = 0; n < arguments.length; n++)
	{
		if (arguments[n] == 'email')
		{
			if (emailCheck(document.getElementById('email').value) == false)
			{
				last_fail = arguments[n];
				document.getElementById(arguments[n] + '_l').style.color = color_fail;
				state = false;
				document.getElementById(error).innerHTML = err_email[lang];
				document.location.replace('#error');
				fadeIn(error, 0);
			}
		}
		else if (document.getElementById(arguments[n]).value == 0)
		{
			last_fail = arguments[n];
			document.getElementById(arguments[n] + '_l').style.color = color_fail;
			state = false;
			document.getElementById(error).innerHTML = err_fields[lang];
			document.location.replace('#error');
			fadeIn(error, 0);
		}
	}
	return state; 
}

function emailCheck(emailStr)
{
	if (emailStr == "")
	{
		return false;
	}
    var checkTLD = 1;
	var knownDomsPat = /^(com|net|org|lt|ru|ro|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray = emailStr.match(emailPat);

	if (matchArray == null)
	{
		return false;
	}

	var user = matchArray[1];
	var domain = matchArray[2];

	for (i = 0; i < user.length; i++)
	{
		if (user.charCodeAt(i) > 127)
		{
			return false;
		}
	}

	for (i = 0; i < domain.length; i++)
	{
		if (domain.charCodeAt(i) > 127)
		{
			return false;
		}
	}

	if (user.match(userPat) == null)
	{
		return false;
	}

	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null)
    {
		for (i = 1; i <= 4; i++)
		{
			if (IPArray[i] > 255)
			{
				return false;
			}
		}
		return true;
    }

	var atomPat = new RegExp("^" + atom + "$");
	var domArr = domain.split(".");
	var len = domArr.length;
	for (i = 0; i < len; i++)
	{
		if (domArr[i].search(atomPat) == -1)
		{
			return false;
		}
	}

	if (checkTLD && domArr[domArr.length-1].length != 2 && domArr[domArr.length-1].search(knownDomsPat) == -1)
	{
		return false;
	}

	if (len < 2)
	{
		return false;
	}
}

function fadeIn(objId, opacity) 
{
    if (document.getElementById)
	{
		if (opacity <= 100)
        {
			document.getElementById(objId).style.MozOpacity = opacity / 100;
			document.getElementById(objId).style.filter = "alpha(opacity:" + opacity + ")";
			opacity += 10;
			window.setTimeout("fadeIn('" + objId + "'," + opacity + ")", 100);
        }
	}
}

function fadeOut(objId, opacity)
{
	if (document.getElementById)
	{
		if (opacity >= 0)
        {
			document.getElementById(objId).style.MozOpacity = opacity / 100;
			document.getElementById(objId).style.filter = "alpha(opacity:" + opacity + ")";
			opacity -= 10;
			window.setTimeout("fadeOut('" + objId + "'," + opacity + ")", 100);
		}
	}
}

function count(id)
{
	var field = document.getElementById('amount['+id+']').value;
	var price = document.getElementById('price['+id+']').value;
	field = field.replace(',', '.');
	field = field.replace(' ', '');
	price = price.replace(',', '.');
	price = price.replace(' ', '');
	
	if (isNaN(field))
	{
		document.getElementById('total['+id+']').value = '0';
	}
	else
	{
		document.getElementById('total['+id+']').value = Math.round(parseFloat(field*price));
		document.getElementById('type['+id+']').checked = 'checked';
		
		var sum = 0;
		var amount;
		var multiple;
		for (var i = 1; i < 11; i++)
		{
			if (document.getElementById('type['+i+']').checked)
			{
				amount = document.getElementById('amount['+i+']').value;
				multiple = document.getElementById('price['+i+']').value;
				amount = amount.replace(',', '.');
				amount = amount.replace(' ', '');
				multiple = multiple.replace(',', '.');
				multiple = multiple.replace(' ', '');
				if (!isNaN(amount))
				{
					sum += Math.round(parseFloat(amount*multiple));
				}
			}
		}
		document.getElementById('sum').innerHTML = sum;
	}
}

function checkOrder()
{
	for (var i = 1; i < 11; i++)
	{
		if (document.getElementById('type['+i+']').checked)
		{
			amount = document.getElementById('amount['+i+']').value;
			amount = amount.replace(',', '.');
			amount = amount.replace(' ', '');
			if (!isNaN(amount))
			{
				return true;
			}
		}
	}
	
	last_fail = 'type[1]';
	document.getElementById(error).innerHTML = err_fields[lang];
	fadeIn(error, 0);
	document.location.replace('#error');
	
	return false;
}

function confirmOrder()
{
	var info = '';
	info += document.getElementById('company_l').innerHTML + " " + document.getElementById('company').value + "\n";
	info += document.getElementById('person_l').innerHTML + " " + document.getElementById('person').value + "\n";
	info += document.getElementById('address_l').innerHTML + " " + document.getElementById('address').value + "\n";
	info += document.getElementById('phone_l').innerHTML + " " + document.getElementById('phone').value + "\n";
	info += document.getElementById('code_l').innerHTML + " " + document.getElementById('code').value + "\n";
	info += document.getElementById('vat_l').innerHTML + " " + document.getElementById('vat').value + "\n";
	info += document.getElementById('bank_l').innerHTML + " " + document.getElementById('bank').value + "\n";
	info += document.getElementById('account_l').innerHTML + " " + document.getElementById('account').value + "\n";
	info += document.getElementById('place_l').innerHTML + " " + document.getElementById('place').value + "\n";
	info += document.getElementById('datetime_l').innerHTML + " " + document.getElementById('datetime').value + "\n";
	
	if (confirm("Patikrinkite ar visi duomenys teisingai užpildyti: \n" + info))
	{
		return true;
	}
	return false;
}

