jQuery(document).ready(function() {
	
	var errorInd1, errorInd2, errorInd3;
	var alreadyClickedSon,alreadyClickedDont=0;
	function nuPorositProcessS1(){
		var vendbanimiHolder = $("#s1-vendbanimi").val(),
			//tornadoHolder = $("input[name='s1-tornadoresiver']:checked").val(),
			resiveriHolder = $("input[name='s1-resiveri']:checked").val();

		if (vendbanimiHolder.length==0){
			$("#s1-vendbanimi-container").glow("#FFF", 1500);
			//.highlightFade({color:'#FFF',speed:1500,iterator:'exponential'})
			errorInd1 = 1;
		}else{
			$("#s1-vendbanimi-container span").fadeOut(500);
			errorInd1 = 0;
		}
/*		if (vendbanimiHolder == "USA"){
			if (!tornadoHolder) {
				$("#s1-tornadoresiver-container").glow("#FFF", 1500);
				errorInd2 = 1;
			}else{
				errorInd2 = 0;
			}
		}
		else{
				errorInd2 = 0;
		}
*/		if (!resiveriHolder) {
			$("#nuPorosit-resiveri-container").glow("#FFF", 1500);
			errorInd3 = 1;
		}else{
			errorInd3 = 0;
		}

		if (errorInd1 == 0 && errorInd3 == 0) {
			// No errors found, update adjacent div
			if ($(this).val()=="Ri-kalkulo"){
				$("#s3-orderFormContainer").slideUp("500");
			}else{
				$(this).val("Ri-kalkulo");
			}
			
			//$("#s1-vendbanimi").addClass("alreadyCheckedOnce");
			alreadyClickedSon = 1;
			alreadyClickedDont = 0;
			//$("input[name='s1-tornadoresiver']").addClass("alreadyCheckedOnce");
			//$("input[name='s1-resiveri']").addClass("alreadyCheckedOnce");
			
			
			if (domainLocation){
				var loadUrl = "http://diasporatv."+domainLocation+"/wp-content/themes/diaspora/porosit-gen.php";
			}else if (domainLocation == "com"){
				var loadUrl = "http://diasporatv.com/wp-content/themes/diaspora/porosit-gen.php";
			}
			$("#s2-results").fadeOut("500",function(){
				$("#s2-results").load(loadUrl,"vendbanimi="+vendbanimiHolder+"&resiveri="+resiveriHolder, function(){
					$("#s2-results").fadeIn("500");
					$("#s2-results input#s2-porosit-button").click(nuPorositShowS3);
				});
			});
		}
	}

	function nuPorositShowS3(){
		// Update order form
		$("#s3-orderFormContainer").slideUp("500",nuPorositUpdateS3);
		return false;
	}
	
	function nuPorositUpdateS3(){
		// Update order form hidden values
		$("#s3resiverPrice").val($("#s2-results #s2resiverPrice").val());
		$("#s3resiverPriceCurr").val($("#s2-results #s2resiverPriceCurr").val());
		$("#s3resiverLloji").val($("#s2-results #s2resiverLloji").val());
		$("#s3parapagimi").val($("#s2-results #s2parapagimi").val());
		$("#s3parapagimiCurr").val($("#s2-results #s2parapagimiCurr").val());
		$("#s3totalPayment").val($("#s2-results #s2totalPayment").val());
		$("#s3totalPaymentCurr").val($("#s2-results #s2totalPaymentCurr").val());
		$("#s3vendbanimi").val($("#s2-results #s2vendbanimi").val());
		$("#s3vendbanimiCurr").val($("#s2-results #s2vendbanimiCurr").val());
		$("#s3postimi").val($("#s2-results #s2postimi").val());
		$("#s3postimiCurr").val($("#s2-results #s2postimiCurr").val());
		$("#s3postimiLloji").val($("#s2-results #s2postimiLloji").val());
		$("#s3-adresaNe").text($("#s2-results #s2vendbanimi").val());
		$("#s3-telNe").text($("#s2-results #s2vendbanimiTel").val());
		$("#s3-orderFormContainer").slideDown("500");
	}

	$("input#s1-submit").click(nuPorositProcessS1);

	$("input[name='s1-tornadoresiver']").click(function(){
			if ($("input[name='s1-tornadoresiver']:checked").val()==1){
				$('#s1-resiveri-sd').attr('checked', false);
				$('#s1-resiveri-sd').attr('disabled', true);
			}else{
				$('#s1-resiveri-sd').removeAttr('disabled');
			}
		}
	);

	$("#s1-vendbanimi").change(function(){
		$("#s3-orderFormContainer").slideUp("500", function(){
			// Check if country is US to hide Tornado Question
			var vendbanimiHolder = $("#s1-vendbanimi").val();
			/*if (vendbanimiHolder == "USA"){
				//$("#s1-tornadoresiver-container").css("display","block");
				$('#s1-resiveri-sd').removeAttr('disabled');
			}else{
				$("#s1-tornadoresiver-container").css("display","none");

						$('#s1-resiveri-sd').attr('checked', false);
						$('#s1-resiveri-sd').attr('disabled', true);
			}*/
			
			
			if (alreadyClickedSon==1 && alreadyClickedDont==0){
				$("#s2-results").fadeOut("500", function(){
					$("#s2-results").html('<img src="/wp-content/themes/diaspora/img/porosit-intro-img.gif" title="Ta bėjmė njė llogari - Plotėsoni formularin nė anėn e majt" style="margin-top: 10px;" height="133" width="328">');
					$("#s2-results").fadeIn("500");
				});
				alreadyClickedDont = 1;
			}
		});
	});

	$("input[name='s1-resiveri']").click(function(){
		$("#s3-orderFormContainer").slideUp("500", function(){
			if (alreadyClickedSon==1 && alreadyClickedDont==0){
				$("#s2-results").fadeOut("500", function(){
					$("#s2-results").html('<img src="/wp-content/themes/diaspora/img/porosit-intro-img.gif" title="Ta bėjmė njė llogari - Plotėsoni formularin nė anėn e majt" style="margin-top: 10px;" height="133" width="328">');
					$("#s2-results").fadeIn("500");
				});
				alreadyClickedDont = 1;
			}
		});
	});
});

