$(function () {

	$("#domainSubmit").click(function () {
		submitForm();
		return false; // Stop default behavior of browser

	});

	$("FORM.domains").submit(function () {

		return false;

	});

    $('#DomainName').keyup(function(e) {

  	  if(e.keyCode == 13) {
  	  	submitForm();
   	    e.preventDefault();
  	  }

  	  return false;

    });

    $("#offerteSubmit").click(function () {
    	$("#offerte").trigger('submit');
    })
    
    

	// Enable the enter key
    $("#offerte").keyup(function (e) {
    	if (e.keyCode == 13) $("#offerte").trigger('submit');
    });

    $("#domainSubmitOrder").click(function () {
    	$("#domainordersubmit").trigger('submit');
    });

    $("#factuursubmitbutton").click(function () {
    	$("#factuurform").trigger('submit');
    });

	// Enable the enter key
    $("#domainordersubmit").keyup(function (e) {
    	if (e.keyCode == 13) $("#domainordersubmit").trigger('submit');
    });

    $("#factuursubmitbutton").keyup(function (e) {
    	if (e.keyCode == 13) $("#factuursubmit").trigger('submit');
    });

    $("#webhostingsubmitbutton").click(function () {
    	$("#webhostingorderform").trigger('submit');
    })

    $("#sslsubmitbutton").click(function () {
    	$("#sslorderform").trigger('submit');
    })

    $("#serversubmitbutton").click(function () {
    	$("#serverorderform").trigger('submit');
    })

    $("#steleenvraagSubmit").click(function () {
    	$("#steleenvraag").trigger('submit');
    })



	// Enable the enter key ( Webhosting form )
    $("#webhostingorderform").keyup(function (e) {
    	if (e.keyCode == 13) $("#webhostingorderform").trigger('submit');
    });

	// Enable the enter key ( Webhosting form )
    $("#sslorderform").keyup(function (e) {
    	if (e.keyCode == 13) $("#sslorderform").trigger('submit');
    });

    // Enable the enter key ( Webhosting form )
    $("#serverorderform").keyup(function (e) {
    	if (e.keyCode == 13) $("#serverorderform").trigger('submit');
    });

    // Enable the enter key ( Question form )
    $("#steleenvraag").keyup(function (e) {
    	if (e.keyCode == 13) $("#steleenvraag").trigger('submit');
    });



	// Domainform hidable
	$(".hidable").hide();

	$("#freeRedirect").attr('checked',true);

    $("#freeRedirect").click(function () {
    	$(".hidable").hide();
    	$(".hidable INPUT").attr('disabled',true);
    });


    $(".checked").attr('checked',true);
    $(".buttonShowable").click(function () {
    	var id = $(this).attr('rel');
    	$(id).show();
    	$(id + " INPUT").attr('disabled',false);
    })
    
    $(".buttonHidable").click(function () {
    	var id = $(this).attr('rel');
    	$(id).hide();
    	$(id + " INPUT").attr('disabled',true);
    })
    
    $("#existingAccount").click(function () {
    	$(".hidable").show();
    	$(".hidable INPUT").attr('disabled',false);
    });

	// SSL form hidable
	$("#choiceJa").attr('checked',true);

    $("#choiceJa").click(function () {
    	$(".hidable").hide();
    	$(".hidable INPUT").attr('disabled',true);
    });

    $("#choiceNee").click(function () {
    	$(".hidable").show();
    	$(".hidable INPUT").attr('disabled',false);
    });

    // Webhosting & server hidable
	$("#choiceOverboeking").attr('checked',true);

    $("#choiceOverboeking").click(function () {
    	$(".hidable").hide();
    	$(".hidable INPUT").attr('disabled',true);
    });

    $("#choiceIncasso").click(function () {
    	$(".hidable").show();
    	$(".hidable INPUT").attr('disabled',false);
    });


    // Custom FAQ , hide/show item but not all, only current
    $(".faqitem .question").click(function () {
    	var rel = $(this).parent().attr('rel');
    	$(".faqitem[rel!=" + rel + "] .answer").hide();
    	$(this).parent().find(".answer").toggle();
    });


});


function orderDomainSubmitHandle() {

	$("#domainOrderSubmit").click(function () {
		var nrChecked =	$("FORM.domains input:checkbox:checked").length;
		if (nrChecked == 0) {
			alert('Er zijn geen domeinnamen geselecteerd.');
			return false;
		}
		else {
			$("#domainorder").trigger('submit');
			return false;
		}

	});

}

function toggleDomainCheckboxesHandle() {

	// Check all checkboxes to select all avaiable domains for order
    $('#OrderToggle').click( function () {

    	if ($(this).attr('checked') == true) {
			$("FORM.domains INPUT.orderoption").attr('checked',true);
		}
		else {
			$("FORM.domains INPUT.orderoption").attr('checked',false);
		}

    });

    $('#OrderToggleLink').click( function () {

    	if ($('#OrderToggle').attr('checked') == true) {
			$("FORM.domains INPUT.orderoption").attr('checked',false);
			$('#OrderToggle').attr('checked',false);
		}
		else {
			$("FORM.domains INPUT.orderoption").attr('checked',true);
			$('#OrderToggle').attr('checked',true);
		}

    });

}

function submitForm() {

	$(this).find("input").attr('readonly','readonly');
	$("#resultcheck").hide();
	setLoading();

	$("#domaincheck").ajaxSubmit({

		success: function (responseText, responseCode) {

			$("#resultcheck").html(responseText);
			$("#resultcheck").show();

			toggleDomainCheckboxesHandle();
			orderDomainSubmitHandle();

			$(this).find("input").attr('readonly','');
			setNotLoading();

			// Enable whois information buttons
			$('a.whois').click(function () {

				// Select the current whoised domain
				clearSelectedWhois();
				selectWhois($(this).parent().parent());

				// Add close button
				$("DIV.whoisinformation .close").click(function () {
					$("DIV.whoisinformation").hide();
					clearSelectedWhois(); // hide current whois information
				});

				$("DIV.whoisinformation").show();

				var Content = $('#' + $(this).attr("rel")).html();
				$("#whoisContent").html(Content);

				return false;

			});

		}

	});

	return false;

}

function clearSelectedWhois() {

	$("FORM.domains TR").removeClass('whoiseddomain');

}

function selectWhois(el) {
	el.addClass('whoiseddomain');
}

function setLoading() {
	$("#loader").show();
}

function setNotLoading() {
	$("#loader").hide();
}
