/*
 * runOnLoad.js: portable registration for onload event handlers.
 * 
 * This module defines a single runOnLoad() function for portably registering
 * functions that can be safely invoked only when the document is fully loaded
 * and the DOM is available.
 *
 * Functions registered with runOnLoad() will not be passed any arguments when
 * invoked. They will not be invoked as a method of any meaningful object, and
 * the this keyword should not be used.  Functions registered with runOnLoad()
 * will be invoked in the order in which they were registered.  There is no
 * way to deregister a function once it has been passed to runOnLoad().
 *
 * In old browsers that do not support addEventListener() or attachEvent(),
 * this function relies on the DOM Level 0 window.onload property and will not
 * work correctly when used in documents that set the onload attribute
 * of their <body> or <frameset> tags.
 */
function runOnLoad(f) {
    if (runOnLoad.loaded) f();    // If already loaded, just invoke f() now.
    else runOnLoad.funcs.push(f); // Otherwise, store it for later
}

runOnLoad.funcs = []; // The array of functions to call when the document loads
runOnLoad.loaded = false; // The functions have not been run yet.

// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run() more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad() to register another function.
runOnLoad.run = function() {
    if (runOnLoad.loaded) return;  // If we've already run, do nothing

    for(var i = 0; i < runOnLoad.funcs.length; i++) {
        try { runOnLoad.funcs[i](); }
        catch(e) { /* An exception in one function shouldn't stop the rest */ }
    }
    
    runOnLoad.loaded = true; // Remember that we've already run once.
    delete runOnLoad.funcs;  // But don't remember the functions themselves.
    delete runOnLoad.run;    // And forget about this function too!
};

// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
    window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
else window.onload = runOnLoad.run;


$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#eeeeee"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	  var name = $("input#name").val();
		if (name == "") {
      $("label#name_error").show();
      $("input#name").focus();
      return false;
    }
		var email = $("input#email").val();
		var foundAt = email.indexOf("@",0)
		if (foundAt < 1) {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }
		var phone1 = $("input#phone1").val();
		if (phone1 == "") {
      $("label#phone_error").show();
      $("input#phone1").focus();
      return false;
    }
		var subject = $("input#subject").val();
		if (subject == "") {
      $("label#subject_error").show();
      $("input#subject").focus();
      return false;
      }
		var massage = $("textarea#massage").val();
		if (massage == "") {
      $("label#massage_error").show();
      $("textarea#massage").focus();
      return false;
      }
		var kapcsa = $("input#kapcsa").val();
		var kapcsab = $("input#kapcsab").val();
		var nyelv = $("input#nyelv").val();
		if ((kapcsab == "") || (kapcsab != kapcsa)) {
      $("label#kapcsab_error").show();
      $("input#kapcsab").focus();
      return false;
      }
		var dataString = 'name='+ name + '&email=' + email + '&phone1=' + phone1 + '&subject=' + subject  + '&massage=' + massage; 
		//alert (dataString);return false;
	if (nyelv == 'hu'){	
		$.ajax({
      type: "POST",
      url: "includes/process.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<h2>Üzenetét elküldtük!</h2>")
        .append("<p>Hamarosan jelentkezünk.</p>")
		.hide()
        .fadeIn(1500, function() {
          $('#message').append("<img id='checkmark' src='interface/check.png' />");
        });
      }
     });
     }
     else if (nyelv == 'ro'){	
		$.ajax({
      type: "POST",
      url: "includes/process.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<h2>Mesajul a fost trimis!</h2>")
        .append("<p>Te vom contacta în curând.</p>")
		.hide()
        .fadeIn(1500, function() {
          $('#message').append("<img id='checkmark' src='interface/check.png' />");
        });
      }
     });
     }
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});

function backToTop() {
    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;

    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }

    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }

    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;

    var x = Math.max(x1, Math.max(x2, x3));
    var y = Math.max(y1, Math.max(y2, y3));

    window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

    if (x > 0 || y > 0) {
        window.setTimeout("backToTop()", 25);
    }
}