﻿ // Author Aurimas Likas - aurimas@wow.lt
$(document).ready(function(){

	// skaiciuokle
	$("#boxes img").each(
		function( intIndex ){
			$( this ).bind(
				"click",
				function(){
					$("#boxes img").removeClass("box_on");
					$(this).toggleClass("box_on");
					var image_id = $(this).attr("id");
					$("#check_"+image_id).attr("checked", "checked"); 
				}
			) // end bind
		}
	) // end each
		
	$("input[@name='box']").click(
		function(){
			var select_image = $("input[@name='box']:checked").val();
			$("#boxes img").removeClass("box_on");
			$("#"+select_image).addClass("box_on");
		}
	);
	
	// bind #skaiciuokle_submit click event
	$("#skaiciuokle_submit").click(function(){
		var update_output = true;
		
		if ( $("#box_0201").hasClass("box_on") || $("#check_box_0201").attr("checked") == "checked" ){
			$("#pakuote").val("box_0201");
		}
		else if ( $("#box_0200").hasClass("box_on") ){
			$("#pakuote").val("box_0200");
		}
		else if ( $("#box_0409").hasClass("box_on") ){
			$("#pakuote").val("box_0409");
		}
		else if ( $("#box_0411").hasClass("box_on") ){
			$("#pakuote").val("box_0411");
		}
		else if ( $("#box_0301").hasClass("box_on") ){
			$("#pakuote").val("box_0301");
		}
		else{
			update_output = false;
			alert('Prašome pasirinkti pakuotės tipą (spauskite ant pakuotės paveikslėlio).');
		}
		
		if ( update_output && validate_inputs() ){
			$("#skaiciuokle").css("display", "none");
			$("#contact_data").show("slow");
		}
	})
	
	// ajax form params object
	var options = { 
		beforeSubmit:  validate_form,
		success:       show_response,
		resetForm:     true
	}; 
 
	// bind to the form's submit event 
	$('#skaiciuokle_form').submit(function() { 
		$(this).ajaxSubmit(options); 
		return false; 
	}); 
	
}) // end document ready

// pre-submit callback 
function validate_form(formData, jqForm, options) { 
	
	var asmuo = $("#contact_data #asmuo").val();
	var telefonas = $("#contact_data #telefonas").val();
	
	if (asmuo == ''){
		alert('Prašome įvesti kontaktinio asmens vardą');
		return false;
	}
	
	if ( telefonas.length < 5 || !is_number(telefonas) ){
		alert('Prašome įvesti tinkamą telefono numerį (tik skaičiai)');
		return false;
	}
	
	var email = $("#contact_data #email").val();
	if ( !validate_email(email) ){
		return false;
	}

    return true; 
} 
 
// post-submit callback 
function show_response(responseText, statusText)  { 
 
	if ( statusText == 'success' && responseText == '1' ){
		$("#contact_data").html('Jums buvo išsiųstas elektroninis laiškas Jūsų nurodytu adresu.');
	}
	else {
		alert('Įvyko nenumatyta klaida. Bandykite dar kartą.');
	}
} 
// VALIDATION FUNCTIONS
function validate_email(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

   if (reg.test(email) == false) {
      alert('Prašome įvesti tinkamą el. pašto adresą.');
      return false;
   }

   return true;
}

function validate_inputs(){
	var ilgis = Number( $("#ilgis").val() );
	var plotis = Number( $("#plotis").val() );
	var aukstis = Number( $("#aukstis").val() );
	var kiekis = Number( $("#kiekis").val() );
	
	if (ilgis > 0 && plotis > 0 && aukstis > 0 && kiekis > 0){
		return true;
	}
	else{
		alert('Prašome įveskite tinkamus pakuotės duomenis.');
		return false;
	}
}

function is_number(sText)
{
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	
	return IsNumber;
}

