var hs_arr = new Array();
var upload_running = 0;
var hs_current;
$(document).ready
( function()
  {
		  $('.my_hover').hover
				( function()
						{
						  var id = $(this).attr('id').slice(3);
								$("#id_" + id).attr('src','/global_stuff/triangle_selected.png');
						},
						function()
						{
						  var id = $(this).attr('id').slice(3);
								$("#id_" + id).attr('src','/global_stuff/triangle.png');
						}
		  );
    if($("#home_slideshow").length > 0)
				{
				  $("#home_slideshow").children().each
						( function()
						  {
						    hs_arr.push($(this).attr('id').substr(3));
										hs_current = 0;
								}
						);
				  setTimeout('home_slideshow()', 2000);
				  setInterval('home_slideshow()', 6000);
				}
  }
);
function next_tour_register()
{
  var registration = '';
		$(".next_tour_check").each
		( function(e)
		  {
      if($(this).is(":checked"))
					 {
        registration = registration + " " + $(this).attr("id");
					 }
				}
		);
  $.post
		( "ajax.php",
				{ task: "next_tour_register",
				  name: $("#next_tour_name").val(),
						mail: $("#next_tour_email").val(),
						phone: $("#next_tour_phone").val(),
						message: $("#next_tour_message").val(),
						registration: $.trim(registration)
				},
				function(data)
				{									
				  data = $.trim(data);
						if(data == 'ok')
						{
								alert("Thank you for registering. We will email you soon with more details.");
        reset_form();
						}
						else
						{
						  alert(data);
					 }
    }
		);
};

function reset_form()
{
  $(':input').each(function() 
  {
    var type = this.type;
		  var tag = this.tagName.toLowerCase();
		  if (type == 'text' || type == 'password' || tag == 'textarea')
		  {
		    this.value = "";
		  }
		  else if (type == 'checkbox' || type == 'radio')
		  {
		    this.checked = false;
		  }
		  else if (tag == 'select')
		  {
		    this.selectedIndex = -1;
		  }
		});
}
function reset_form_ii(form_id)
{
  $(':input').each(function() 
  {
    var type = this.type;
		  var tag = this.tagName.toLowerCase();
		  if (type == 'text' || type == 'password' || tag == 'textarea')
		  {
		    this.value = "";
		  }
		  else if (type == 'checkbox' || type == 'radio')
		  {
		    this.checked = false;
		  }
		  else if (tag == 'select')
		  {
		    this.selectedIndex = -1;
		  }
		});
}
function request_submit()
{
  $.post
		( "ajax.php",
				{ task: "request_submit",
				  name: $("#request_name").val(),
						mail: $("#request_email").val(),
						message: $("#request_message").val()
				},
				function(data)
				{									
				  data = $.trim(data);
						if(data == 'ok')
						{
								alert("Thank you. Your message has been sent. We will answer shortly.");
        reset_form();
						}
						else
						{
						  alert(data);
					 }
    }
		);
};
function request_corporate_partners_submit()
{
  $.post
		( "ajax.php",
				{ task: "request_corporate_partners_submit",
				  name: $("#request_name").val(),
						mail: $("#request_email").val(),
						phone: $("#request_phone").val(),
						message: $("#request_message").val()
				},
				function(data)
				{									
				  data = $.trim(data);
						if(data == 'ok')
						{
								alert("Thank you. Your message has been sent. We will answer shortly.");
        reset_form();
						}
						else
						{
						  alert(data);
					 }
    }
		);
};
function mailing_list_submit()
{
  $.post
		( "ajax.php",
				{ task: "mailing_list_submit",
				  name: $("#mailing_list_name").val(),
						mail: $("#mailing_list_email").val(),
						comment: $("#mailing_list_comment").val()
				},
				function(data)
				{									
				  data = $.trim(data);
						if(data == 'ok')
						{
								alert("Thank you for joining our mailing list.");
        reset_form();
						}
						else
						{
						  alert(data);
					 }
    }
		);
};
function gmt_register()
{
  var programs = '';
		$(":checkbox[@checked]").each
		( function()
		  {
		    programs += $(this).attr("id") + '|';
				}
		);
		programs = programs.substr(0, programs.length - 1);
  $.post
		( "ajax.php",
				{ task: "gmt_register",
				  registration_category: $("#registration_category").html(),
				  first_name: $("#gmt_first_name").val(),
				  last_name: $("#gmt_last_name").val(),
				  address: $("#gmt_address").val(),
				  city: $("#gmt_city").val(),
				  province: $("#gmt_province").val(),
				  country: $("#gmt_country").val(),
				  postal_code: $("#gmt_postal_code").val(),
				  email: $("#gmt_email").val(),
				  phone: $("#gmt_phone").val(),
				  children_names: $("#gmt_children_names").val(),
				  children_ages: $("#gmt_children_ages").val(),
				  comments: $("#gmt_comments").val(),
						programs: programs
				},
				function(data)
				{									
				  data = $.trim(data);
						if(data.substr(0,5) == 'ERROR')
						{
						  alert(data.substr(5));
						}
						else
						{
						  alert(data);
        reset_form();
						}
    }
		);
};
function gmt_register_pass(text_name, form_id)
{
  var programs = '';
		var gmt_agreed = 'no';
		$(":checkbox[@checked]").each
		( function()
		  {
				  var item = $.trim($(this).attr("id"));
						if(item == 'gmt_agreed')
						{
						  gmt_agreed = 'yes';
						}
						else
						{
						  item = item.substr(11);
						}
				}
		);
		$(".pass_program").each
		( function()
		  {
				  if($(this).val() != '0')
						{
						  var id = $(this).attr("id").substr(13);
		      programs += $("#pass_program_" + id).val() + ",";
						}
		  }
		);
		if(programs != '')
		{
    programs = programs.substr(0, programs.length - 1);
  }
		//
  var children_names = '';
		var children_ages = '';
		$(".child_name").each
		( function()
		  {
				  children_names += $.trim($(this).val()) + '{}';
				}
		);
		$(".child_age").each
		( function()
		  {
				  children_ages += $.trim($(this).val()) + '{}';
				}
		);
  $.post
		( "ajax.php",
				{ task: "gmt_register_pass",
				  first_name: $("#gmt_first_name").val(),
				  last_name: $("#gmt_last_name").val(),
				  address: $("#gmt_address").val(),
				  city: $("#gmt_city").val(),
				  province: $("#gmt_province").val(),
				  country: $("#gmt_country").val(),
				  postal_code: $("#gmt_postal_code").val(),
				  email: $("#gmt_email").val(),
				  phone: $("#gmt_phone").val(),
				  children_names: children_names,
				  children_ages: children_ages,
				  comments: $("#gmt_comments").val(),
						programs: programs,
						gmt_agreed: gmt_agreed
				},
				function(data)
				{									
				  data = $.trim(data);
						if(data.substr(0,5) == 'ERROR')
						{
						  alert(data.substr(5));
						}
						else
						{
	       location.href = "./" + text_name + "_feedback_" + form_id + ".html";
						}
    }
		);
}
function gmt_register_ii(text_name, form_id)
{
  var programs = '';
		var gmt_agreed = 'no';
		$("input:checked").each
		( function()
		  {
				  var item = $.trim($(this).attr("id"));
						if(item == 'gmt_agreed')
						{
						  gmt_agreed = 'yes';
						}
						else
						{
						  item = item.substr(11);
		      programs += item + ',';
						}
				}
		);
		programs = programs.substr(0, programs.length - 1);
		//
  var children_names = '';
		var children_ages = '';
		$(".child_name").each
		( function()
		  {
				  children_names += $.trim($(this).val()) + '{}';
				}
		);
		$(".child_age").each
		( function()
		  {
				  children_ages += $.trim($(this).val()) + '{}';
				}
		);
  $.post
		( "ajax.php",
				{ task: "gmt_register_ii",
				  first_name: $("#gmt_first_name").val(),
				  last_name: $("#gmt_last_name").val(),
				  address: $("#gmt_address").val(),
				  city: $("#gmt_city").val(),
				  province: $("#gmt_province").val(),
				  country: $("#gmt_country").val(),
				  postal_code: $("#gmt_postal_code").val(),
				  email: $("#gmt_email").val(),
				  phone: $("#gmt_phone").val(),
				  children_names: children_names,
				  children_ages: children_ages,
				  comments: $("#gmt_comments").val(),
						programs: programs,
						gmt_agreed: gmt_agreed
				},
				function(data)
				{									
				  data = $.trim(data);
						if(data.substr(0,5) == 'ERROR')
						{
						  alert(data.substr(5));
						}
						else
						{
	       location.href = "./" + text_name + "_feedback_" + form_id + ".html";
						}
    }
		);
}
function gmt_register_2()
{
  var programs = '';
		$("input:checked").each
		( function()
		  {
				  var id = $(this).attr("id");
						if(id.substr(7,1) == 2)
						{
		      programs += $(this).attr("id") + '|';
				  }
				}
		);
		programs = programs.substr(0, programs.length - 1);
  $.post
		( "ajax.php",
				{ task: "gmt_register",
				  first_name: $("#gmt_first_name_2").val(),
				  last_name: $("#gmt_last_name_2").val(),
				  address: $("#gmt_address_2").val(),
				  city: $("#gmt_city_2").val(),
				  province: $("#gmt_province_2").val(),
				  postal_code: $("#gmt_postal_code_2").val(),
				  email: $("#gmt_email_2").val(),
				  phone: $("#gmt_phone_2").val(),
				  children_names: $("#gmt_children_names_2").val(),
				  comments: $("#gmt_comments_2").val(),
						programs: programs
				},
				function(data)
				{									
				  data = $.trim(data);
						if(data.substr(0,5) == 'ERROR')
						{
						  alert(data.substr(5));
						}
						else
						{
						  alert(data);
        reset_form();
						}
    }
		);
};
function home_slideshow()
{
  var period = 1000;
	 $("#hs_" + hs_arr[hs_current]).hide();
  ++hs_current;
		hs_current = hs_current == hs_arr.length ? 0 : hs_current;
	 $("#hs_" + hs_arr[hs_current]).show();
		return;
	 $("#hs_" + hs_current).slideUp
		( period,
		  function()
				{
      ++hs_current;
		    hs_current = hs_current == hs_arr.length ? 0 : hs_current;
	     $("#hs_" + hs_arr[hs_current]).slideDown(period);
				}
		);
  
}
function show_form(target, checkboxes)
{
 $(":checkbox").attr("checked","");
 $(".gf").show();
	var arr = checkboxes.split(",");
	for(i=0; i<arr.length; ++i)
	{
	  $("#" + arr[i]).attr("checked", "checked");
	}
	location.href = target + ".html#form_anchor";
}

function show_form_ii(text_name, form_id, event_id)
{
  $(":checkbox").attr("checked","");
  $(".gf").show();
 	$("#form_event_" + event_id).attr("checked", "checked");
 	location.href = "#form_anchor";
}
//I may want to resize the black column

var win_popup = null;
function open_popup(url,width,height) 
{
  close_popup();
  var xMax = screen.width, yMax = screen.height;
		if (height == 0)
		{
		  height = screen.height;
		}
		if (width == 0)
		{
		  width = screen.width;
		}
		var xOffset = (xMax - width)/2, yOffset = (yMax - height)/2;
		win_popup = window.open(url,'win_popup','width='+width+',height='+height+',screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset);
}	
function close_popup()
{
		if (win_popup && win_popup.open && !win_popup.closed)
		{
				win_popup.close();
		}
		else
		{
				win_popup = window.open('cw.html','win_popup','width=10,height=10,screenX=10000,screenY=10000,top=5000,left=5000');
				win_popup.close();
		}
}
function add_child(iteration)
{
  $(".add_child_button").each
		( function()
		  {
				  if( $(this).attr("id") == "add_child_button_" + iteration )
						{
						  $(this).show();
						}
						else
						{
						  $(this).hide();
						}
				}
		);
	 $("#add_child_" + iteration).show();
}		
function galleries_register()
{
  $.post
		( "ajax.php",
				{ task: "galleries_register",
				  name: $("#register_name").val(),
			  	address: $("#register_address").val(),
			  	city: $("#register_city").val(),
			  	zip_code: $("#register_zip_code").val(),
			  	state: $("#register_state").val(),
			  	country: $("#register_country").val(),
			  	email: $("#register_email").val(),
				  phone: $("#register_phone").val(),
						terms_agreed: $("#terms_agreed").attr('checked') ? 'yes' : 'no'
				},
				function(data)
				{									
						eval(data);
    }
		);
}
function image_uploader()
{
  $.post
		( "ajax.php",
				{ task: "upload_file_cards_key"
				},
				function(data)
				{
				  cards_key = $.trim(data);
		    new AjaxUpload
    		( 'upload_button',
		      { action: 'ajax.php',
				      name: 'file',
						    data: { task : 'upload_file_cards',
						            cards_key: $.trim(data)
						          },
						    onSubmit: function(file, extension)
					    	{
						      if(upload_running == 1)
							    	{
								      alert('A file is already been uploaded. Only one at a time!');
									    	return false;
						    		}
		        		upload_running = 1;
					    	  //$("#upload_button").hide();
						      //$("#upload_feedback").hide();
						      $("#uploading_button").show();
						    },
						    onComplete: function(file, response)
					    	{
						      eval(response);
						      upload_running = 0;
						      $("#uploading_button").hide();
					    	}
			    	}
	    	);
    }
		);
}
function cards_order()
{
  $.post
		( "ajax.php",
				{ task: "cards_order",
				  design: $('input[name=design]:checked').val(),
						color_scheme: $("#color_scheme").val(),
						custom_design: $("#custom_design").val(),
				  name_card: $("#name_card").val(),
						child_1: $("#child_1").val(),
						child_2: $("#child_2").val(),
						child_3: $("#child_3").val(),
						child_4: $("#child_4").val(),
				  status: $('input[name=status]:checked').val(),
						status_other: $("#status_other").val(),
      cards_email: $("#cards_email").val(),
						cards_phone: $("#cards_phone").val(),
						cards_mobile: $("#cards_mobile").val(),
						cards_website: $("#cards_website").val(),
						cards_address: $("#cards_address").val(),
				  shipping_name: $("#shipping_name").val(),
			  	shipping_address: $("#shipping_address").val(),
			  	shipping_city: $("#shipping_city").val(),
			  	shipping_zip_code: $("#shipping_zip_code").val(),
			  	shipping_state: $("#shipping_state").val(),
			  	shipping_country: $("#shipping_country").val(),
			  	shipping_email: $("#shipping_email").val(),
				  shipping_phone: $("#shipping_phone").val(),
						terms_agreed: $("#terms_agreed").attr('checked') ? 'yes' : 'no',
						cards_key: cards_key,
						images_uploaded: $("#uploaded_images").html()
				},
				function(data)
				{									
						eval(data);
    }
		);
}
