$(document).ready(function(){
// validate the form when it is submitted
	$("#first").validate();
	$("#third").validate();
	$("#fourth").validate();
	$("#fifth").validate();
	$("#confreg").validate();
	$("#formZ").validate();
	$("#champ").validate();
	$("#confsuggestion").validate();

// Clone fields
var removeLink = ' <a class="remove" href="#" onclick="$(this).parent().slideUp(function(){ $(this).remove() }); return false">remove</a>';
$('a.add').relCopy({ append: removeLink});

var i = 1;
var input_name = null;
$('.dubl input').each(function () {
   var new_input_name = $(this).attr('name').substring(0, $(this).attr('name').indexOf('['));
   if (input_name !== new_input_name) {
       input_name = new_input_name;
       i = 1;
   }
   $(this).attr('name', input_name + '[' + i + ']');
   i++;
});
var i = 1;
$('.dubl .tshirt').each(function () {
   var new_input_name = $(this).attr('name').substring(0, $(this).attr('name').indexOf('['));
   if (input_name !== new_input_name) {
       input_name = new_input_name;
       i = 1;
   }
   $(this).attr('name', input_name + '[' + i + ']');
   i++;
});
var i = 1;
$('.dubl .tshirt2').each(function () {
   var new_input_name = $(this).attr('name').substring(0, $(this).attr('name').indexOf('['));
   if (input_name !== new_input_name) {
       input_name = new_input_name;
       i = 1;
   }
   $(this).attr('name', input_name + '[' + i + ']');
   i++;
});
$('.dubl label').each(function () {
   var new_input_name = $(this).attr('for').substring(0, $(this).attr('for').indexOf('['));
   if (input_name !== new_input_name) {
       input_name = new_input_name;
       i = 1;
   }
   $(this).attr('for', input_name + '[' + i + ']');
   i++;
});

var course_cs = $(".course1");
var course_le = $(".course2");

function validateCourse1(){
if(!$('#g:checked').length && !$('#m:checked').length && !$('#j:checked').length && !$('#k:checked').length && !$('#b:checked').length && !$('#d:checked').length) {
$('.chechErr').css("display","inline");
} else {
$('.chechErr').css("display","none");
}
}
function validateCourse2(){
var n;
for(n=2; n<11; n++) {
if(!$('#g'+n+':checked').length && !$('#m'+n+':checked').length && !$('#j'+n+':checked').length && !$('#k'+n+':checked').length) {
$('.chechErr'+n).css("display","inline");
} else {
$('.chechErr'+n).css("display","none");
}
}
}
course_cs.click(validateCourse1);
course_le.click(validateCourse1);
course_cs.click(validateCourse2);
course_le.click(validateCourse2);

function onRadioClick() {
    $(".notification_ok").hide();
    $(".notification_error").hide();
	$(".form_content1").show().removeClass("error");
	$(".form_content2").show().removeClass("error");
	$(".form_content3").show().removeClass("error");
	$('.loading').html('');
	$(".button").attr('disabled', false);
	$(".chechErr").css("display","none");
}

// First form submit
$("#first").submit(function(){

$('#a').val( $('#b').is(':checked') ? 'Yes' : 'No' );
$('#c').val( $('#d').is(':checked') ? 'Yes' : 'No' );

var str = $(this).serialize();
	$(validateCourse1())
	$(".button").attr('disabled', true);
	$('.loading').html('<img alt="." src="js/images/ajax-loader.gif" /> Please Wait...');
	$.ajax({
	type: "POST",
	url: "trainingform.php?value=Preregistration",
	data: str,
	success: function(msg){
    
$(".formnote").ajaxComplete(function(event, request, settings){

if(msg == 'OK') // Message Sent?
{
result = '<div class="notification_ok">Thank you for submitting your request. We hope that we will match your request in the nearest future by scheduling Zabbix Professional Training in your region at your preferred time. If your have any further comments or questions, we will be more that happy to hear it.</div>';
$(".form_content1").hide();
}
else if(msg == 'JP') // Message Sent JP?
{
result = '<div class="notification_ok">リクエストの送付ありがとうございます。できるだけ早くリクエストに合う時間、場所でZabbixプロフェッショナルトレーニングが開催できるよう調整します。その他ご質問やご意見がある場合はご連絡ください。</div>';
$(".form_content1").hide();
}
else
{
$('.loading').html('');
$(".button").attr('disabled', false);
$(".form_content1").show();
result = msg;
}
$(this).html(result);
});
}
});
return false;
});
// Second form submit
$("#third").submit(function(){

$('#h').val( $('#g').is(':checked') ? 'Yes' : 'No' );
$('#l').val( $('#m').is(':checked') ? 'Yes' : 'No' );

var n;
for(n=1; n<11; n++) {
    $('#h'+n).val( $('#g'+n).is(':checked') ? 'Yes' : 'No' );
}
for(n=1; n<11; n++) {
    $('#l'+n).val( $('#m'+n).is(':checked') ? 'Yes' : 'No' );
}

var str = $(this).serialize();
	$(validateCourse1() && validateCourse2())
	$(".button").attr('disabled', true);
	$('.loading').html('<img alt="." src="js/images/ajax-loader.gif" /> Please Wait...');
	$.ajax({
	type: "POST",
	url: "trainingform.php?value=Company",
	data: str,
	success: function(msg){

$(".formnote").ajaxComplete(function(event, request, settings){

if(msg == 'OK') // Message Sent?
{
result = '<div class="notification_ok">Thank you for registering for Zabbix Training course. We will contact you shortly to obtain any additional information required and to send you Proforma Invoice. Please do not hesitate to contact us if you have any questions or comments.</div>';
$(".form_content2").hide();
}
else if(msg == 'JP') // Message Sent JP?
{
result = '<div class="notification_ok">Zabbixトレーニングコースへのお申し込みありがとうございます。より詳細な確認と見積書の送付のため、折り返しご連絡致します。ご質問やご意見がございましたらお気軽にご連絡ください。</div>';
$(".form_content2").hide();
}
else
{
$('.loading').html('');
$(".button").attr('disabled', false);
$(".form_content2").show();
result = msg;
}
$(this).html(result);
});
}
});
return false;
});
// Third form submit
$("#fourth").submit(function(){

$('#v').val( $('#k').is(':checked') ? 'Yes' : 'No' );
$('#p').val( $('#j').is(':checked') ? 'Yes' : 'No' );
var n;
for(n=1; n<11; n++) {
    $('#p'+n).val( $('#j'+n).is(':checked') ? 'Yes' : 'No' );
}
for(n=1; n<11; n++) {
    $('#v'+n).val( $('#k'+n).is(':checked') ? 'Yes' : 'No' );
}

var str = $(this).serialize();
	$(validateCourse1() && validateCourse2())
	$(".button").attr('disabled', true);
	$('.loading').html('<img alt="." src="js/images/ajax-loader.gif" /> Please Wait...');
	$.ajax({
	type: "POST",
	url: "trainingform.php?value=Private",
	data: str,
	success: function(msg){
    
$(".formnote").ajaxComplete(function(event, request, settings){

if(msg == 'OK') // Message Sent?
{
result = '<div class="notification_ok">Thank you for registering for Zabbix Training course. We will contact you shortly to obtain any additional information required and to send you Proforma Invoice. Please do not hesitate to contact us if you have any questions or comments.</div>';
$(".form_content2").hide();
}
else if(msg == 'JP') // Message Sent JP?
{
result = '<div class="notification_ok">Zabbixトレーニングコースへのお申し込みありがとうございます。より詳細な確認と見積書の送付のため、折り返しご連絡致します。ご質問やご意見がございましたらお気軽にご連絡ください。</div>';
$(".form_content2").hide();
}
else
{
$('.loading').html('');
$(".button").attr('disabled', false);
$(".form_content2").show();
result = msg;
}
$(this).html(result);
});
}
});
return false;
});

// Fourth form submit
$("#fifth").submit(function(){

var str = $(this).serialize();
	$(".button").attr('disabled', true);
	$('.loading').html('<img alt="." src="js/images/ajax-loader.gif" /> Please Wait...');
	$.ajax({
	type: "POST",
	url: "trainingform.php?value=Suggestions",
	data: str,
	success: function(msg){
    
$(".formnote").ajaxComplete(function(event, request, settings){

if(msg == 'OK') // Message Sent?
{
result = '<div class="notification_ok">Thank you for sending us your feed-back on Zabbix professional training. We will try hard to implement all of you suggestions and take into account all of your comments.</div>';
$(".form_content3").hide();
}
else if(msg == 'JP') // Message Sent JP?
{
result = '<div class="notification_ok">Zabbixプロフェッショナルトレーニングのフィードバックをいただきありがとうございます。いただいたご提案やコメントはすべてコースに反映するように努力致します。</div>';
$(".form_content3").hide();
}
else
{
$('.loading').html('');
$(".button").attr('disabled', false);
$(".form_content3").show();
result = msg;
}
$(this).html(result);
});
}
});
return false;
});

// Conference suggestion form submit
$("#confsuggestion").submit(function(){

var str = $(this).serialize();
	$(".button").attr('disabled', true);
	$('.loading').html('<img alt="." src="js/images/ajax-loader.gif" /> Please Wait...');
	$.ajax({
	type: "POST",
	url: "trainingform.php?value=ConfSuggestions",
	data: str,
	success: function(msg){
    
$(".formnote").ajaxComplete(function(event, request, settings){

if(msg == 'OK') // Message Sent?
{
result = '<div class="notification_ok">Thank you for providing us your suggestions on Zabbix Conference 2012 organization improvement. Zabbix will take into consideration your ideas and experience!</div>';
$(".form_content3").hide();
}
else if(msg == 'JP') // Message Sent JP?
{
result = '<div class="notification_ok">Zabbixカンファレンス2012企画改善のご意見、誠にありがとうございました。 <br>頂きましたご意見とご体験を参考に企画を進めてまいります。</div>';
$(".form_content3").hide();
}
else
{
$('.loading').html('');
$(".button").attr('disabled', false);
$(".form_content3").show();
result = msg;
}
$(this).html(result);
});
}
});
return false;
});

//Hide div w/id extra
$('#Preregistration').attr('checked', false);
$('#Company').attr('checked', false);
$("#extra0").css("display","none");
$("#extra1").css("display","none");
$("#extra2").css("display","none");
$("#extra3").css("display","none");
$("#extra4").css("display","none");
$("#extra5").css("display","none");
$(".chechErr").css("display","none");
$("#first").css("display","block");
$("#second").css("display","block");
$("#third").css("display","block");
$("#fourth").css("display","block");
$("#fifth").css("display","block");
if(document.URL.indexOf("training_application_form.php?PreReg") >= 0){
	$("#extra0").fadeIn();
	//$("#formNav").css("display","none");
	//$("hr").css("display","none");
	$('#Preregistration').attr("checked","checked");
}
if(document.URL.indexOf("training_application_form.php?Registration") >= 0){
	$("#extra1").fadeIn();
	$("#extra4").fadeIn();
	//$("#formNav").css("display","none");
	//$("hr").css("display","none");
	$('#Registration').attr("checked","checked");
}
if(document.URL.indexOf("training_application_form.php?RegComp") >= 0){
	$("#extra2").fadeIn();
	$("#extra1").fadeIn();
	//$("#formNav").css("display","none");
	//$("hr").css("display","none");
	$('#Registration').attr("checked","checked");
}
if(document.URL.indexOf("training_application_form.php?RegPriv") >= 0){
	$("#extra3").fadeIn();
	$("#extra1").fadeIn();
	//$("#formNav").css("display","none");
	//$("hr").css("display","none");
	$('#Registration').attr("checked","checked");
}
if(document.URL.indexOf("training_application_form.php?Suggest") >= 0){
	$("#extra5").fadeIn();
	//$("#formNav").css("display","none");
	//$("hr").css("display","none");
	$('#Suggestions').attr("checked","checked");
}
if(document.URL.indexOf("turn_key_solution.php?PurchaseOrder") >= 0){
	$("#turnkey").fadeIn();
	$("#solution").css("display","none");
}
if(document.URL.indexOf("consulting.php?PurchaseOrder") >= 0){
	$("#consulting").fadeIn();
	$("#consult").css("display","none");
}
if(document.URL.indexOf("remote_troubleshooting.php?PurchaseOrder") >= 0){
	$("#remotet").fadeIn();
	$("#rem").css("display","none");
}
// Add onclick handler to checkbox w/id checkme
$("#Preregistration").click(function(){
if ($("#Preregistration").is(":checked"))
{
$("#extra1").css("display","none");
$("#extra2").css("display","none");
$("#extra3").css("display","none");
$("#extra4").css("display","none");
$("#extra5").css("display","none");
//$("#formNav").css("display","none");
$("#extra0").fadeIn();
onRadioClick();
}
else
{
$("#extra0").css("display","none");
}
});


$("#Registration").click(function(){
if ($("#Registration").is(":checked"))
{
$("#extra0").css("display","none");
$("#extra2").css("display","none");
$("#extra3").css("display","none");
$("#extra5").css("display","none");
//$("#formNav").css("display","none");
$("#Company").attr('checked', false);
$("#Private").attr('checked', false);
$("#extra1").fadeIn();
$("#extra4").fadeIn();
onRadioClick();
}
else
{
$("#extra4").css("display","none");
$("#extra1").css("display","none");
}
});

$("#Company").click(function(){
if ($("#Company").is(":checked"))
{
$("#extra0").css("display","none");
$("#extra3").css("display","none");
$("#extra4").css("display","none");
$("#extra5").css("display","none");
//$("#formNav").css("display","none");
$("#extra2").fadeIn();
onRadioClick();
}
else
{
$("#extra2").css("display","none");
}
});

$("#Private").click(function(){
if ($("#Private").is(":checked"))
{
$("#extra0").css("display","none");
$("#extra2").css("display","none");
$("#extra4").css("display","none");
$("#extra5").css("display","none");
//$("#formNav").css("display","none");
$("#extra3").fadeIn();
onRadioClick();
}
else
{
$("#extra3").css("display","none");
}
});

$("#Suggestions").click(function(){
if ($("#Suggestions").is(":checked"))
{
$("#extra0").css("display","none");
$("#extra1").css("display","none");
$("#extra2").css("display","none");
$("#extra3").css("display","none");
$("#extra4").css("display","none");
//$("#formNav").css("display","none");
onRadioClick();
$("#extra5").fadeIn();
}
else
{
$("#extra5").css("display","none");
}
});
// Registration form submit
$("#confreg").submit(function(){

var str = $(this).serialize();
	$(".button").attr('disabled', true);
	$('.loading').html('<img alt="." src="js/images/ajax-loader.gif" /> Please Wait...');
	$.ajax({
	type: "POST",
	url: "trainingform.php?value=Conference",
	data: str,
	success: function(msg){

$(".formnote").ajaxComplete(function(event, request, settings){

if(msg == 'OK') // Message Sent?
{
result = '<div class="notification_ok" style="max-width:100%;">Thank you for filling out Zabbix Conference 2012 pre-registration form. <br>Please do not hesitate to contact us if you have any questions or comments.</div>';
$(".form_content2").hide();
}
else if(msg == 'JP') // Message Sent JP?
{
result = '<div class="notification_ok">Zabbixカンファレンス2012登録フォームへのお申し込みありがとうございました。<br>ご意見やご質問がございましたらお気軽にご連絡ください。</div>';
$(".form_content2").hide();
}
else
{
$('.loading').html('');
$(".button").attr('disabled', false);
$(".form_content2").show();
result = msg;
}
$(this).html(result);
});
}
});
return false;
});
// Purchase Form
$("#formZ").submit(function(){

var str = $(this).serialize();
	$(".button").attr('disabled', true);
	$('.loading').html('<img alt="." src="js/images/ajax-loader.gif" /> Please Wait...');
	$.ajax({
	type: "POST",
	url: "trainingform.php?value=PurchaseForm",
	data: str,
	success: function(msg){

$(".formnote").ajaxComplete(function(event, request, settings){

if(msg == 'OK') // Message Sent?
{
result = '<div class="notification_ok" style="max-width:100%;">Thank you for filling out our Purchase Order form!</div>';
$(".form_content2").hide();
}
else if(msg == 'JP') // Message Sent JP?
{
result = '<div class="notification_ok">Thank you for filling out Purchase Order form!</div>';
$(".form_content2").hide();
}
else
{
$('.loading').html('');
$(".button").attr('disabled', false);
$(".form_content2").show();
result = msg;
}
$(this).html(result);
});
}
});
return false;
});
// Championship Form
$("#champ").submit(function(){

var str = $(this).serialize();
	$(".button").attr('disabled', true);
	$('.loading').html('<img alt="." src="js/images/ajax-loader.gif" /> Please Wait...');
	$.ajax({
	type: "POST",
	url: "trainingform.php?value=Championship",
	data: str,
	success: function(msg){

$(".formnote").ajaxComplete(function(event, request, settings){

if(msg == 'OK') // Message Sent?
{
result = '<div class="notification_ok" style="max-width:100%;">Thank you for filling out our Championship form!</div>';
$(".form_content2").hide();
}
else if(msg == 'JP') // Message Sent JP?
{
result = '<div class="notification_ok">Thank you for filling out our Championship form!</div>';
$(".form_content2").hide();
}
else
{
$('.loading').html('');
$(".button").attr('disabled', false);
$(".form_content2").show();
result = msg;
}
$(this).html(result);
});
}
});
return false;
});
});
function clear_form_elements(ele) {
	    $(ele).find(':input').each(function() {
	        switch(this.type) {
	            case 'select-multiple':
	            case 'select-one':
	            case 'text':
	            case 'textarea':
	                $(this).val('');
	                break;
	            case 'checkbox':
	            case 'radio':
	                this.checked = false;
	        }
	    });
};

