$(document).ready(function(){
		
	$(document).pngFix();
	
	$(".rates01-tr").mouseover(function() {
		$(this).attr("class", "rates01-tr-a");
		if($.browser.msie && $.browser.version=="6.0") $(this).children().children().css("width","60px");
	});
	$(".rates01-tr").mouseleave(function() {
		$(this).attr("class", "rates01-tr");
		if($.browser.msie && $.browser.version=="6.0") $(this).children().children().css("width", "auto");
	});
	
	if($.browser.msie) {
		if($("#sbody").height()<$("#smenu").height())
			$("#sbody").css("height",($("#smenu").height()+100)+"px");
	}
	
	/*$("#firstname, #lastname, #email, #password1, #password2, #country, #company_vat, #voucher").tooltip({
		events: { input: "focus,mouseout" },
		delay: 0,
		tipClass: "re-tooltip",
		position: "top right",
		offset: [-5, 15],
		effect: "fade",
		opacity: 0.8
	});*/
	
	$("#number, #dtooltip, #message").focus(function() {
		$("#"+($(this).attr("id"))+"_tooltip").fadeIn(600);
	});
	$("#number, #dtooltip, #message").blur(function() {
		$("#"+($(this).attr("id"))+"_tooltip").fadeOut(600);
	});
	
	$("#firstname, #lastname, #email, #phonenumber, #password1, #password2, #country, #voucher, #company_vat").focus(function(e){
		$("#re-tooltip").css({"margin-left":tooltip[$(this).attr("alt")][0], "margin-top":tooltip[$(this).attr("alt")][1]});
		$("#re-tooltip").fadeIn(600);
		if($(this).attr("id")=="phonenumber" && check_for_russia($(this).attr("value"))) {
			$("#re-tooltip-text").html(text_phonenumber_russia);
		}
		else if($(this).attr("id")=="phonenumber" && check_for_countries($(this).attr("value"))) {
			$("#re-tooltip-text").html(text_phonenumber_country);
		}
		else {
			$("#re-tooltip-text").html(window["text_" + $(this).attr("id")]);
		}
	});
	$("#firstname, #lastname, #email, #phonenumber, #password1, #password2, #country, #voucher, #company_vat").blur(function(e){
		$("#re-tooltip").css("display","none");
		$("#re-tooltip-text").html("");
	});
		
	$("#phonenumber").keyup(function(e){
		if(check_for_russia($(this).attr("value"))) {
			$("#re-tooltip-text").html(text_phonenumber_russia);
		}
		else if(check_for_countries($(this).attr("value"))) {
			$("#re-tooltip-text").html(text_phonenumber_country);
		}
		else {
			$("#re-tooltip-text").html(window["text_" + $(this).attr("id")]);
		}
	});
	
});

function openIds(array) {
	for(i=0; i<array.length; i++)
		document.getElementById(array[i]).style.display='';
}

function changeClasses(array) {
	for(i=0; i<array.length; i++)
		document.getElementById(array[i]).className='op-opened';
}

function checkMail2(opmail, optext, lang) {
	var template = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z])+$/;
	temp = opmail.value;
	if (template.test(temp)) {
		
	}
	else {
		if(lang=="de")
			alert("Falsches E-mail Format.");
		if(lang=="en")
			alert("Invalid email format.");
		if(lang=="ru")
			alert("Неверный формат эл. почты.");
		opmail.focus();
		return false;
	}
	if(optext.value!='') {
		return true;
	}
	else {
		if(lang=="de")
			alert("Einen Kommentar schreiben.");
		if(lang=="en")
			alert("Write your opinion/comment.");
		if(lang=="ru")
			alert("Напишите отзыв/комментарий.");
		optext.focus();
		return false;
	}
	
	return true;
}

function checkMail(f, lang) {
	var template = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z])+$/;
	temp = f.mail.value;
	if (template.test(temp)) {
		
	}
	else {
		alert("Neteisingas elektroninio pašto adresas.");
		f.mail.focus();
		return false;
	}
	if(f.text.value!='') {
		return true;
	}
	else {
		alert("Parašykite klausimą.");
		f.text.focus();
		return false;
	}
}

function check_for_russia(str) {
	if(str.length>4 && str.substring(0,2)=='+7') {
		return true;
	}
	else {
		return false;
	}
}

function check_for_countries(str) {
	if(str.length>4 && (str.substring(0,4)=='+370' || str.substring(0,3)=='+49' || str.substring(0,3)=='+43' || str.substring(0,3)=='+32' || str.substring(0,3)=='+44' || str.substring(0,3)=='+49' || str.substring(0,3)=='+30' || str.substring(0,3)=='+97' || str.substring(0,3)=='+34' || str.substring(0,4)=='+371' || str.substring(0,3)=='+48' || str.substring(0,2)=='+1' || str.substring(0,3)=='+35' || str.substring(0,3)=='+33' || str.substring(0,3)=='+39' || str.substring(0,4)=='+420' || str.substring(0,3)=='+41')) {
		return false;
	}
	else if(str.length>4) {
		return true;
	}
}

fetch_unix_timestamp = function()
{
	return parseInt(new Date().getTime().toString().substring(0, 10))
}

function generate_phone_input() {
	var rand_no = Math.ceil(100*Math.random());
	timestamp = fetch_unix_timestamp();
	timestamp = timestamp + '' + rand_no;
	$('#phone_container').append('<div id="phone_new' + timestamp + '" class="phone-pad"><table border="0" cellpadding="0" cellspacing="0"><tr><td><input id="phonenew' + timestamp + '" name="phonenew' + timestamp + '" type="text" class="user-field-phone"  value="" onclick="$(\'#tooltip_edit_phone\').fadeIn(500); $(\'#tooltip_edit_phone\').css(\'display\', \'block\'); return false;" onblur="$(\'#tooltip_edit_phone\').fadeOut(500,function() { $(\'#tooltip_edit_phone\').css(\'display\', \'none\'); return false; });" /></td><td class="plus-minus-pad"><input type="submit" name="removephone-java" value="" class="removephone" onclick="return clear_phone(\'phone_new' + timestamp + '\', \'phonenew' + timestamp + '\');" /></td></tr></table></div>');
	return false;
}

function generate_phone_input2(selects) {
	var rand_no = Math.ceil(100*Math.random());
	timestamp = fetch_unix_timestamp();
	timestamp = timestamp + '' + rand_no;
	var option = "";
	
	for(i=0; i<selects.length; i++) {
		option = option + '<option value="' + selects[i] + '">' + selects[i] + '</option>';
	}
	
	$("#us_new_number").append('<table id="table_' + timestamp + '" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td align="left"><input type="submit" value="" class="us-minus" onclick="$(\'#table_' + timestamp + '\').remove();return false;" /></td><td align="left" class="us-td"><input name="phone_new_' + timestamp + '" type="text" value="+" class="us-textfield-phone" /></td><td align="center" class="us-td"><input name="name_new_' + timestamp + '" type="text" value="" class="us-textfield" /></td><td align="center" class="us-td"><input name="group_new_' + timestamp + '" type="text" value="" class="us-textfield" /></td><td align="center" class="us-td"><input name="desc_new_' + timestamp + '" type="text" value="" class="us-textfield" /></td></tr></table>');
	
	return false;
}

function manageSubmit(url) {
	if(document.getElementById('model').value!=-1) {
		document.location.href = url + '/index.php?id=' + document.getElementById('model').value;
	}
	else {
		if(document.getElementById('brand').value!=-1) 
			document.location.href = url + '/index.php?id=' + document.getElementById('brand').value;
		else return false;
	}
}

function show_op(id, id2) {
	if(document.getElementById(id).style.display=='none') {
		document.getElementById(id).style.display='';
		document.getElementById(id2).className='op-opened';
	}
	else {
		document.getElementById(id).style.display='none';
		document.getElementById(id2).className='op-closed';
	}
}

function openfaq(id, id2) {
	//alert(faq_o, id);
	if(faq_o!=0 && faq_o!=id && q_o!=0) {
		document.getElementById(faq_o).style.display = 'none';
		document.getElementById(q_o).className='faq-closed';
	}
	if(document.getElementById(id).style.display=='none') {
		document.getElementById(id).style.display='';
		document.getElementById(id2).className='faq-opened';
	}
	else {
		document.getElementById(id).style.display='none';
		document.getElementById(id2).className='faq-closed';
	}
	
	/*if(document.getElementById(id2).className=='faq-closed')
		document.getElementById(id2).className='faq-opened';
	else document.getElementById(id2).className='faq-closed';*/
	faq_o = id;
	q_o = id2;
}

function checkboxes(form, id) {
	var c=0;
	var inputs = document.getElementById(form).getElementsByTagName("input");
	for(var i=0; i<inputs.length; i++){
		if(inputs[i].type == 'checkbox') 
			if(inputs[i].checked==false)
				c = 1;
		}
	if(c==0) {
		document.getElementById(id).disabled=false;
	}
	else {
		document.getElementById(id).disabled=true;
	}
}

function settozero(id, id2, url, id3) {	
	if(document.getElementById(id).disabled==true) {
		document.getElementById(id).disabled=false;
	}
	else {
		document.getElementById(id).disabled=true;
	}
	useAjax(url, id3);
	document.getElementById(id).value = '';
	document.getElementById(id2).options[0].selected = true;
	document.getElementById('step2phoneimg_bg').style.display = 'none';
	$("#internet_box").html('<input name="gsm" type="hidden" value="none" />');
}

function check_register(f) {
	var template = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z])+$/;
	if(f.first_name.value=="" || f.last_name.value=="" || f.userid.value=="" || f.pass.value=="" || f.phone.value=="") {
		alert('Please, fill in all fields.');
		return false;
	}
	else if (template.test(f.userid.value)) {
		return true;
	}
	else {
		alert('User ID must be a valid email!');
		return false;
	}
}

function writeChartPieId(src, id) {
	document.write('<embed type="application/x-shockwave-flash" src="' + src + '" id="' + id + '" name="' + id + '" quality="high" allowscriptaccess="always" flashvars="chartWidth=400&amp;chartHeight=350&amp;debugMode=0&amp;DOMId=' + id + '&amp;registerWithJS=0&amp;scaleMode=noScale&amp;lang=EN" width="500" height="350">');
}

function writeChartPie(src) {
	document.write('<embed type="application/x-shockwave-flash" src="' + src + '" id="ChartId2" name="ChartId2" quality="high" allowscriptaccess="always" flashvars="chartWidth=400&amp;chartHeight=350&amp;debugMode=0&amp;DOMId=ChartId&amp;registerWithJS=0&amp;scaleMode=noScale&amp;lang=EN" width="500" height="350">');
}

function writeChart3d(src) {
	document.write('<embed type="application/x-shockwave-flash" src="' + src + '" id="ChartId" name="ChartId" quality="high" allowscriptaccess="always" flashvars="chartWidth=600&amp;chartHeight=350&amp;debugMode=0&amp;DOMId=ChartId&amp;registerWithJS=0&amp;scaleMode=noScale&amp;lang=EN" width="600" height="350">');
}

function writeFlash(movieLocation,width,height){
  document.write('<object id="flashmast" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'"><param name="allowScriptAccess" value="always" /><param name="allowNetworking" value="all"><param name="movie" value="'+movieLocation+'" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><embed id="flashembed" src="'+movieLocation+'" quality="high" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="'+width+'" height="'+height+'" wmode="transparent"></embed></object>');
}

function writeFlashVar(movieLocation,width,height,var1){
  document.write('<object id="flashmast" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'"><param name=FlashVars value="var1='+var1+'"><param name="allowNetworking" value="all" /><param name="allowScriptAccess" value="always" /><param name="movie" value="'+movieLocation+'" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><embed id="flashembed" src="'+movieLocation+'" FlashVars="var1='+var1+'" quality="high" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="'+width+'" height="'+height+'"></embed></object>');  
}

function writeFlashVars(movieLocation,width,height,var1,var2){
  document.write('<object id="flashmast" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'"><param name=FlashVars value="var1='+var1+'&var2='+var2+'"><param name="allowNetworking" value="all"><param name="allowScriptAccess" value="always"><param name="movie" value="'+movieLocation+'"><param name="quality" value="high"><param name="wmode" value="transparent" /><embed id="flashembed" src="'+movieLocation+'" FlashVars="var1='+var1+'&var2='+var2+'" quality="high" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="'+width+'" height="'+height+'"></embed></object>');  
}

function clearfield(id) {
	if(document.getElementById(id).value=="e-mail" || document.getElementById(id).value=="password")
		document.getElementById(id).value = "";
	return false;
}

function confirm_act(text) {
	if(confirm(text)) { return true; }
	else { return false; }
}

function show_dual(id, id2) {
	if(document.getElementById(id).style.display=='none') {
		document.getElementById(id).style.display='';
		return false;
	}
	else if(document.getElementById(id2).style.display=='none') {
		document.getElementById(id2).style.display='';
		return false;
	}
	return false;
}

function show_single(id) {
	if(document.getElementById(id).style.display=='none')
		document.getElementById(id).style.display='';
	return false;
}

function show(id) {
	if(document.getElementById(id).style.display=='none')
		document.getElementById(id).style.display='';
	else document.getElementById(id).style.display='none';
	return false;
}

function clear_phone(id, id2) {
	document.getElementById(id).style.display='none';
	document.getElementById(id2).value='';
	return false;
}

function showsub(id) {
	if(cat_opened!=id && cat_opened!='') {
		if(document.getElementById(cat_opened).style.display=='')
			document.getElementById(cat_opened).style.display='none';
	}
	cat_opened = id;
	if(document.getElementById(id).style.display=='none')
		document.getElementById(id).style.display='';
	else document.getElementById(id).style.display='none';
}

function checkAjax(){
	var xmlHttp;
	try{xmlHttp=new XMLHttpRequest();}
	catch(e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser doesn't support AJAX!");return false;}}}
	return xmlHttp;
}
function getRatesByCountryId(id){
	if(id > 0){
		var xmlHttp = checkAjax();
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4 && xmlHttp.status==200){
				var xmlDoc = xmlHttp.responseXML.documentElement;
				var dataCount = xmlDoc.getElementsByTagName('rate').length;

				if(dataCount > 0){
					var rate_id = xmlDoc.getElementsByTagName("id")[0].childNodes[0].nodeValue;
					var rate_destination = xmlDoc.getElementsByTagName("destination")[0].childNodes[0].nodeValue;
					var rate_landline = xmlDoc.getElementsByTagName("landline")[0].childNodes[0].nodeValue;
					var rate_mobile = xmlDoc.getElementsByTagName("mobile")[0].childNodes[0].nodeValue;

					document.getElementById('rate_destination').innerHTML = rate_destination;
					document.getElementById('rate_landline').innerHTML = rate_landline;
					document.getElementById('rate_mobile').innerHTML = rate_mobile;
				}
				else{
					document.getElementById('rate_destination').innerHTML = '---';
					document.getElementById('rate_landline').innerHTML = '000';
					document.getElementById('rate_mobile').innerHTML = '000';
				}
			}
		}
		xmlHttp.open("GET","client/js/getRatesByCountryId.php?id="+id,true);
		xmlHttp.send(null);
	}
}
function checkIsCountryInEU(country){
	if(document.getElementById('country').value=='USA') {
		document.getElementById('state').style.display = '';
	}
	else {
		document.getElementById('state').style.display = 'none';
	}
	
	if(document.getElementById('is_company2').checked){
		var EU = new Array();
		EU[0] = new Array();
		EU[0]['code'] = 'AT'; EU[0]['name'] = 'Austria';

		EU[1] = new Array();
		EU[1]['code'] = 'BE'; EU[1]['name'] = 'Belgium';

		EU[2] = new Array();
		EU[2]['code'] = 'BG'; EU[2]['name'] = 'Bulgaria';

		EU[3] = new Array();
		EU[3]['code'] = 'CY'; EU[3]['name'] = 'Cyprus';

		EU[4] = new Array();
		EU[4]['code'] = 'CZ'; EU[4]['name'] = 'Czech Republic';

		EU[5] = new Array();
		EU[5]['code'] = 'DE'; EU[5]['name'] = 'Germany';

		EU[6] = new Array();
		EU[6]['code'] = 'DK'; EU[6]['name'] = 'Denmark';

		EU[7] = new Array();
		EU[7]['code'] = 'EE'; EU[7]['name'] = 'Estonia';

		EU[8] = new Array();
		EU[8]['code'] = 'ES'; EU[8]['name'] = 'Spain';

		EU[9] = new Array();
		EU[9]['code'] = 'FI'; EU[9]['name'] = 'Finland';

		EU[10] = new Array();
		EU[10]['code'] = 'FR'; EU[10]['name'] = 'France';

		EU[11] = new Array();
		EU[11]['code'] = 'GB'; EU[11]['name'] = 'United Kingdom';

		EU[12] = new Array();
		EU[12]['code'] = 'GR'; EU[12]['name'] = 'Greece';

		EU[13] = new Array();
		EU[13]['code'] = 'HU'; EU[13]['name'] = 'Hungary';

		EU[14] = new Array();
		EU[14]['code'] = 'IE'; EU[14]['name'] = 'Ireland';

		EU[15] = new Array();
		EU[15]['code'] = 'IT'; EU[15]['name'] = 'Italy';

		EU[16] = new Array();
		EU[16]['code'] = 'LT'; EU[16]['name'] = 'Lithuania';

		EU[17] = new Array();
		EU[17]['code'] = 'LU'; EU[17]['name'] = 'Luxembourg';

		EU[18] = new Array();
		EU[18]['code'] = 'LV'; EU[18]['name'] = 'Latvia';

		EU[19] = new Array();
		EU[19]['code'] = 'MT'; EU[19]['name'] = 'Malta';

		EU[20] = new Array();
		EU[20]['code'] = 'NL'; EU[20]['name'] = 'Netherlands';

		EU[21] = new Array();
		EU[21]['code'] = 'PL'; EU[21]['name'] = 'Poland';

		EU[22] = new Array();
		EU[22]['code'] = 'PT'; EU[22]['name'] = 'Portugal';

		EU[23] = new Array();
		EU[23]['code'] = 'RO'; EU[23]['name'] = 'Romania';

		EU[24] = new Array();
		EU[24]['code'] = 'SE'; EU[24]['name'] = 'Sweden';

		EU[25] = new Array();
		EU[25]['code'] = 'SI'; EU[25]['name'] = 'Slovenia';

		EU[26] = new Array();
		EU[26]['code'] = 'SK'; EU[26]['name'] = 'Slovak Republic';
		
		EU[27] = new Array();
		EU[27]['code'] = 'GB'; EU[27]['name'] = 'England';
		
		EU[28] = new Array();
		EU[28]['code'] = 'GB'; EU[28]['name'] = 'UK';

		for(var i=0; i<EU.length; i++){
			if(EU[i]['name'] == country){
				document.getElementById('company_vat-box').style.display = 'block';
				document.getElementById('company_vat').value = EU[i]['code'];
				return;
			}
		}
		document.getElementById('company_vat-box').style.display = 'none';
		document.getElementById('company_vat').value = '';
	}
	
}

function show_req() {
	//document.getElementById('step1_country').style.display = '';
	document.getElementById('step1_city_box').style.display = '';
	document.getElementById('step1_address_box').style.display = '';
	document.getElementById('step1_zipcode_box').style.display = '';
	document.getElementById('step1_divider').style.display = '';
}

function hide_req() {
	//document.getElementById('step1_country').style.display = 'none';
	document.getElementById('step1_city_box').style.display = 'none';
	document.getElementById('step1_city').value = '';
	document.getElementById('step1_address_box').style.display = 'none';
	document.getElementById('step1_address').value = '';
	document.getElementById('step1_zipcode_box').style.display = 'none';
	document.getElementById('step1_zipcode').value = '';
	document.getElementById('step1_company_name').value = '';
	document.getElementById('company_vat').value = '';
	document.getElementById('step1_divider').style.display = 'none';
}

function checkIsOther(value, id) {
	if(value==60) {
		document.getElementById(id).style.display = '';
	}
	else {
		document.getElementById(id).value = '';
		document.getElementById(id).style.display = 'none';
	}
}

function checkIfUSA(value, id) {
	if(value=="USA") {
		document.getElementById(id).style.display = '';
	}
	else {
		document.getElementById(id).style.display = 'none';
	}
}
