var $j = jQuery.noConflict();

function fixSourceDestValue(src,dst, koef1, rounddown)
{
	var fromVal = $j(src).attr("value").replace(/,/, '.');
	$j(src).attr("value", fromVal);
	fromVal = parseFloat(fromVal);
	if (!isNaN(fromVal) && (fromVal != $j(src).attr("value")))
	{
		$j(src).attr("value",  fromVal);
	}
	if (!isNaN(fromVal) && (fromVal != 0))
	{
		if (rounddown)
			$j(dst).attr("value", Math.floor(fromVal*koef1*100)/100);
		else
			$j(dst).attr("value", Math.ceil(Math.floor(fromVal*koef1*10000)/100)/100);
	}
	else
	{
		$j(dst).attr("value", '');
	}
	if ($j(dst).attr("value")!='')
	{
		$j("div.calc span.submit").removeClass("hidden");
	}
	else
	{
		$j("div.calc span.submit").addClass("hidden");
	}
	return fromVal;
}


$j(document).ready(function(){
	$j('#order').click(function(){
		$j("div.calc span.submit").addClass("hidden");
		$j.post("?sec=calc", {direction:$j("#dir").attr("value"), wmz: $j("#wmz").attr("value")}, function(data)
				{
					if (data && data.lvl)
					{
						$j("div.calc span.submit").addClass("hidden");
						$j('#continue').toggleClass("hidden", $j('#agree').val() != "agree");
						$j('.calc ul a').each(function()
								{
									if ($j(this).parent().attr("class") != "help")
										$j(this).attr("disabled", "disabled");
								});
						$j("#natural").attr("disabled", "disabled");
						$j("#legal").attr("disabled", "disabled");
						$j("#lvl").attr("value", data.lvl);
						$j('.bill').css("display", "none");
						$j("#agree").val("");
						$j("#billform fieldset.personal > ul > li").hide();
						$j("#billform fieldset.personal ul li[tag=common]").show();
						$j("#billform fieldset.personal ul li[tag="+$j("#dir").val()+"]").show();
						$j("#billform fieldset.personal ul li[tag="+$j("#person").val()+"]").show();
						
						$j("#exchange").animate(
							{ width: "930px"},
							 500,
							function (){
								$j("#billform").fadeIn("medium");
							}
						);
					}
				}, "json");
		return false;
	});
	$j('.cancel').click(function(){
		$j("#confirmMessage").addClass("hide");
		$j('.bill').css("display", "none");
		$j("div.calc span.submit").removeClass("hidden");
		$j("#natural").removeAttr("disabled");
		$j("#legal").removeAttr("disabled");
		$j('.calc ul a').each(function()
				{
					if ($j(this).parent().attr("class") != "help")
						$j(this).removeAttr("disabled");
				});
		$j("#exchange").animate(
			{ width: "350px"},
			 500
		);
		$j("div.calc span.submit").removeClass("hidden");
		return false;
	});
	$j('.calc ul a').click(function(){
		if($j(this).parent().attr("class") != "help" && !$j(this).attr("disabled")){
		 	if($j(this).parent().attr("class") != "cur"){
				$j('.calc ul .cur').toggleClass("cur");
				$j(this).parent().toggleClass("cur");
				$j('.calc .buy').toggleClass("hide");
				$j('#dir').attr("value", $j(this).parent().attr("id"));
				$j('#wmz').keyup();
			}
			return false;
		}
	});
	
	function testCalcInput(e)
	{
		var reg = /\d|\.|,/;
		var c = String.fromCharCode(e.which);
		if (!reg.test(c)) return false;
	}
	$j('#wmz').keypress(function (e) {
		return testCalcInput(e);
	});
	$j('#lvl').keypress(function (e) {
		return testCalcInput(e);
	});
	$j('#wmz').keyup(function () {
		var rate = ($j('#dir').attr("value") == "buying") ? $j('#sell_rate').attr("value") : $j('#buy_rate').attr("value");
		fixSourceDestValue(this,$j("#lvl"), rate);
	});
	$j('#lvl').keyup(function () {
		var rate = ($j('#dir').attr("value") == "buying") ? $j('#sell_rate').attr("value") : $j('#buy_rate').attr("value");
		fixSourceDestValue(this,$j("#wmz"), 1.0/rate, true);
	});
	
	$j('.calc p label').click(function(){
		if($j(this).attr("class") != "selected" && !$j(this).attr("disabled")) $j('.calc p label').toggleClass("selected");
		$j('#person').attr("value", $j(this).attr("id"));
	});
	$j('.agree').click(function(){
		$j(this).toggleClass("selected");
		if($j('#agree').val() == "agree")
			{
				$j('#agree').val("");
			}
			else
				{
					$j('#agree').val("agree");
				}
		$j('#continue').toggleClass("hidden", $j('#agree').val() != "agree");
	});
	$j('.continue').click(function(){
		/* validate fields */
		var error = false;
		var errorList = [];
		//specific validates
		if ($j('#person').val() == 'natural')
		{
			if (!$j("#billform #name").val().match(/^\w+\s{0,1}(\w|\s)*$/i))
			{
				$j("#billform #name").css("border-color", "red");
				error = true;
				errorList.push('#name');
			}
			else
			{
				$j("#billform #name").css("border-color", "");
			}
			if (!$j("#billform #pid").val().match(/^[0-9]{6}-[0-9]{5}$/i))
			{
				$j("#billform #pid").css("border-color", "red");
				errorList.push('#pid');
				error = true;
			}
			else
			{
				$j("#billform #pid").css("border-color", "");
			}
			
		}
		else
		{
			if (!$j("#billform #rname").val().match(/^.+$/i))
			{
				$j("#billform #rname").css("border-color", "red");
				errorList.push('#rname');
				error = true;
			}
			else
			{
				$j("#billform #rname").css("border-color", "");
			}
			if (!$j("#billform #rid").val().match(/^(LV){0,1}[0-9]{11}$/i))
			{
				$j("#billform #rid").css("border-color", "red");
				errorList.push('#rid');
				error = true;
			}
			else
			{
				$j("#billform #rid").css("border-color", "");
			}
		}
		if (!$j("#billform #phone").val().match(/^[0-9()\+]{7,}$/i))
		{
			$j("#billform #phone").css("border-color", "red");
			errorList.push('#phone');
			error = true;
		}
		else
		{
			$j("#billform #phone").css("border-color", "");
		}
		if (!$j("#billform #email").val().match(/^\w+([\.-]?\w+)*@(((([a-z0-9]{2,})|([a-z0-9][-][a-z0-9]+))[\.][a-z0-9])|([a-z0-9]+[-]?))+[a-z0-9]+\.([a-z]{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/i))
		{
			$j("#billform #email").css("border-color", "red");
			errorList.push('#email');
			error = true;
		}
		else
		{
			$j("#billform #email").css("border-color", "");
		}
		if (!$j("#billform #wmzid").val().match(/^[a-z]{1}[0-9]{12}$/i))
		{
			$j("#billform #wmzid").css("border-color", "red");
			errorList.push('#wmzid');
			error = true;
		}
		else
		{
			$j("#billform #wmzid").css("border-color", "");
		}
		if (!$j("#billform #wmid").val().match(/^[0-9]{12}$/i))
		{
			$j("#billform #wmid").css("border-color", "red");
			errorList.push('#wmid');
			error = true;
		}
		else
		{
			$j("#billform #wmid").css("border-color", "");
		}

		if (!$j("#billform #bank").val().match(/^.{3,}$/i))
		{
			$j("#billform #bank").css("border-color", "red");
			errorList.push('#bank');
			error = true;
		}
		else
		{
			$j("#billform #bank").css("border-color", "");
		}
		if (!$j("#billform #bid").val().match(/^LV[0-9a-z]{19}$/i))
		{
			$j("#billform #bid").css("border-color", "red");
			errorList.push('#bid');
			error = true;
		}
		else
		{
			$j("#billform #bid").css("border-color", "");
		}

		if (error)
		{
			$j("#messageError ul li").hide();
			for(var i=0,imax=errorList.length; i < imax; i++)
			{
				$j("#messageError ul li[tag="+errorList[i]+"]").show();
			}
			$j("#messageError").removeClass('hide');
			$j("#messageError span.submit").click(function(){$j("#messageError").addClass('hide');});
			return;
		}
		$j("div.calc div[className*=message-error]").addClass('hide');
		var parent = $j(this).parent().parent();
		parent.fadeOut("medium", function(){
			//fill data
			if ($j('#person').val() == 'natural')
			{
				$j("#approve #name .input").html($j("#billform #name").val());
				$j("#approve #pid .input").html($j("#billform #pid").val());
			}
			else
			{
				$j("#approve #rname .input").html($j("#billform #rname").val());
				$j("#approve #rid .input").html($j("#billform #rid").val());
			}
			$j("#approve #phone .input").html($j("#billform #phone").val());
			$j("#approve #email .input").html($j("#billform #email").val());
			$j("#approve #wmzid .input").html($j("#billform #wmzid").val());
			$j("#approve #wmid .input").html($j("#billform #wmid").val());
			
			$j("#approve #comments_approve").text($j("#billform #comments").val());
			if ($j('#dir').val() == "buying")
			{	
				$j('#billform #account').children().each(function()
						{
							if ($j(this).attr("selected"))
								$j("#approve #account .input").html($j(this).text());
						});
			}

			$j("#approve #bank .input").html($j("#billform #bank").val());
			$j("#approve #bid .input").html($j("#billform #bid").val());				
			$j("#approve div.personal-info > ul > li").hide();
			$j("#approve div.personal-info ul li[tag=common]").show();
			$j("#approve div.personal-info ul li[tag="+$j("#dir").val()+"]").show();
			$j("#approve div.personal-info ul li[tag="+$j("#person").val()+"]").show();
			$j("#approve").fadeIn("medium");
			$j("#approve div.bill-wrap #approveCalc").click(submitOrder);
			$j("#approve div.bill-wrap #editData").click(function()
					{
						$j("#approve").fadeOut("medium", function()
								{
									$j('.continue').parent().parent().fadeIn("medium");
								});
					});
		});
	});
});
function submitOrder()
{
	// submit data
	var postData = {};
	if ($j("#dir").val() == "buying")
	{
		postData['exchange_id'] = 200;
		postData['bank_id'] = $j("#billform #account").val();
		postData['kurs'] = $j("#sell_rate").val();
	}
	else
	{
		postData['exchange_id'] = 199;
		postData['kurs'] = $j("#buy_rate").val();
	}
	
	if ($j('#person').val() == 'natural')
	{
		postData['name_surname'] = $j("#billform #name").val();
		postData['personal_code'] = $j("#billform #pid").val();
		postData['passport'] = 'fiz';
	}
	else
	{
		postData['name_surname'] = $j("#billform #rname").val();
		postData['personal_code'] = $j("#billform #rid").val();
		postData['passport'] = 'jur';
	}
	postData['phone'] = $j("#billform #phone").val();
	postData['mail'] = $j("#billform #email").val();
	postData['comments'] = $j("#billform #comments").val();
	postData['bank_no'] = $j("#billform #bank").val();
	postData['account'] = $j("#billform #bid").val();
	postData['wmz'] = $j("#billform #wmzid").val();
	postData['wmid'] = $j("#billform #wmid").val();
	postData['comments'] = $j("#billform #comments").val();
	postData['confirm'] = 1;
	postData['pay_summ'] = $j(($j("#dir").val() == "buying") ? "#wmz" : "#lvl").val();
	postData['order_summ'] = $j(($j("#dir").val() != "buying") ? "#wmz" : "#lvl").val();
	$j.post('?sec=change', postData, function(data)
			{
				if (parseInt(data.error))
				{
					$j("#messageError ul li").hide();
					$j("#messageError ul [tag=#customError]").html(data.errorText);
					$j("#messageError ul [tag=#customError]").show();
					$j("#messageError").removeClass('hide');
					$j("#messageError span.submit").click(function(){$j("#messageError").addClass('hide');});
				}
				else
				{
					$j("#orderid").text(data.orderid);
					$j("#confirmMessage p[tag=order]").toggleClass("hide", parseInt(data.orderbig) > 0);
					$j("#confirmMessage p[tag=orderbig]").toggleClass("hide", parseInt(data.orderbig) == 0);
					$j("#confirmMessage").removeClass("hide");
					$j("#confirmMessage span.submit").click(function(){$j(".cancel").click();});
				}
				}, 'json');
	
}