// JavaScript Document
(function($){
  $.ifixpng("../images/rien.gif");
  $(function(){
    $("#twitter a,#facebook a,#youtube a,#banner_1 a").attr("target","_blank");
    
    var ie6 = $.browser.msie && $.browser.version == 6;
    var iPhone = navigator.userAgent.search(/iPhone|iPod/)!=-1;
    
    if(iPhone) {
      $("#home object").replaceWith("<video width=\"803\" height=\"535\" poster=\"../images/video_iphone.jpg\" src=\"../images/SWTrailer.m4v\"></video>");
    }
/*
    $("#banner_1").hover(function(){
      var $this = $(this),
          $img = $this.find("img"),
          img = $img[0];
      if(ie6)
        $img.iunfixpng().removeAttr("style");
      if($this.is(".en"))
        img.src="../images/comingsoon.png";
      if($this.is(".es"))
        img.src="../images/comingsoon_es.png";
      if($this.is(".fr"))
        img.src="../images/comingsoon_fr.png";
      if(ie6)
        $img.ifixpng();
    },function() {
      var $this = $(this),
          $img = $this.find("img"),
          img = $img[0];
      if(ie6)
        $img.iunfixpng().removeAttr("style");
      img.src="../images/banner_apple.png";
      if(ie6)
        $img.ifixpng();      
    });
*/
    if(ie6) {
      $("img,#twitter a,#facebook a,#youtube a").ifixpng();
      var menu = $("#m_menu_1 a,#m_menu_2 a,#m_menu_3 a,#m_menu_4 a");
      var updatebg = function() {
        menu.iunfixpng().removeAttr("style").ifixpng();
      };
      menu.hover(
        function(){
          window.setTimeout(updatebg,0);
        },function(){
          window.setTimeout(updatebg,0);
        }
      );
      menu.ifixpng();
      menu.click(function(){
        window.location = $(this).attr("href");
        return false;
      });
    }

    $("#container.contact").each(function(){
      var radio_check = $(this).find(":radio,:checkbox"); 
      var radio = radio_check.filter(":radio"); 
      radio_check.each(function(){
        var id = this.id;
        var span = $("<span class='check_image' id='check_"+id+"'></span>");
        $(this).addClass("imaged").before(span);
        if(this.checked) {
          span.removeClass("check_image").addClass("check_image_checked");
        };
        span.click(function(){
          var input = $(this).next();
          var is_radio = input.is(":radio");
          if(input[0].checked && !is_radio) {
            input[0].checked = false;
            span.removeClass("check_image_checked").addClass("check_image");
          } else {
            input[0].checked = true;
            if(is_radio)
              radio.prev().removeClass("check_image_checked").addClass("check_image");
            span.removeClass("check_image").addClass("check_image_checked");
          };
        });
        $(this).next().click(function(){
          var input = $(this).prev();
          var is_radio = input.is(":radio");
          var span = input.prev();
          if(input[0].checked && !is_radio) {
            span.removeClass("check_image_checked").addClass("check_image");
          } else {
            if(is_radio)
              radio.prev().removeClass("check_image_checked").addClass("check_image");
            span.removeClass("check_image").addClass("check_image_checked");
          };
        })
      })
    });

    if($.fn.selectbox && !iPhone)
      $("#country").selectbox();
    if(ie6) {
      var drop_down = $("div.jquery-selectbox-moreButton");
      var updatedrop = function() {
        drop_down.iunfixpng().removeAttr("style").ifixpng();
      };
      drop_down.hover(
        function() {
          window.setTimeout(updatedrop,0);
        },function() {
          window.setTimeout(updatedrop,0);
        }
      );
      drop_down.ifixpng();
    }
    $("#contact .en #submit").hover(function(){
      $(this).css("backgroundImage","url(../images/submit_selected.gif)");    
    },function(){
      $(this).css("backgroundImage","url(../images/submit.gif)");
    });      
    $("#contact .en #reset").hover(function(){
      $(this).css("backgroundImage","url(../images/reset_selected.gif)");    
    },function(){
      $(this).css("backgroundImage","url(../images/reset.gif)");
    });      
    $("#contact .es #submit").hover(function(){
      $(this).css("backgroundImage","url(../images/submit_es_on.gif)");    
    },function(){
      $(this).css("backgroundImage","url(../images/submit_es.gif)");
    });      
    $("#contact .es #reset").hover(function(){
      $(this).css("backgroundImage","url(../images/reset_es_on.gif)");    
    },function(){
      $(this).css("backgroundImage","url(../images/reset_es.gif)");
    });      
    $("#contact .fr #submit").hover(function(){
      $(this).css("backgroundImage","url(../images/submit_fr_on.gif)");    
    },function(){
      $(this).css("backgroundImage","url(../images/submit_fr.gif)");
    });      
    $("#contact .fr #reset").hover(function(){
      $(this).css("backgroundImage","url(../images/reset_fr_on.gif)");    
    },function(){
      $(this).css("backgroundImage","url(../images/reset_fr.gif)");
    });

    var image_paths_en = [
      "../images/menu_01_en_hover.png",
      "../images/menu_02_en_hover.png",
      "../images/menu_03_en_hover.png",
      "../images/menu_04_en_hover.png",
      "../images/submit_selected.gif",
      "../images/reset_selected.gif",
      "../images/dropdown_arrow_on.png",
      "../images/comingsoon.png"
    ];
    var image_paths_fr = [
      "../images/menu_01_fr_hover.png",
      "../images/menu_02_fr_hover.png",
      "../images/menu_03_fr_hover.png",
      "../images/menu_04_fr_hover.png",
      "../images/submit_fr_on.gif",
      "../images/reset_fr_on.gif",
      "../images/dropdown_arrow_on.png",
      "../images/comingsoon_fr.png"
    ];
    var image_paths_es = [
      "../images/menu_01_es_hover.png",
      "../images/menu_02_es_hover.png",
      "../images/menu_03_es_hover.png",
      "../images/menu_04_es_hover.png",
      "../images/submit_es_on.gif",
      "../images/reset_es_on.gif",
      "../images/dropdown_arrow_on.png",
      "../images/comingsoon_es.png"
    ];
    var images = [];
    var image_paths = $("#m_menu").is(".en")?image_paths_en:($("#m_menu").is(".fr")?image_paths_fr:image_paths_es);
    $.each(image_paths,function(){
      images.push($("<img />").attr("src",this));
    });
    
  });  
})(jQuery)

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
};

function echeck(str)
{

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr || str.lastIndexOf(dot) == str.length-1){
		    return false
		}
		
		if (str.length-1-str.lastIndexOf(dot) < 2){
			return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
}

function is_numeric(strString)
{
var strValidChars = "0123456789";
var strChar;
var blnResult = true;

if (strString.length == 0) return false;

//  test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++)
   {
   strChar = strString.charAt(i);
   if (strValidChars.indexOf(strChar) == -1)
      {
      blnResult = false;
      }
   }
return blnResult;
}

 
function validate() {
	document.getElementById("first_name").parentNode.className=""
	document.getElementById("last_name").parentNode.className="";
	document.getElementById("email").parentNode.className="";
	document.getElementById("postcode").parentNode.className="";
	document.getElementById("dd").parentNode.className="birth";
	document.getElementById("country").parentNode.parentNode.className="";

	var ret = true;	
	if ("" == document.getElementById("first_name").value.trim())
	{
		document.getElementById("first_name").parentNode.className="error";
		ret = false;
	}
	if ("" == document.getElementById("last_name").value.trim())
	{
		document.getElementById("last_name").parentNode.className="error";
		ret = false;
	}

	if ("" == document.getElementById("email").value.trim() || !echeck(document.getElementById("email").value.trim()))
	{
		document.getElementById("email").parentNode.className="error";
		ret = false;
	}

	if ("" == document.getElementById("postcode").value.trim())
	{
		document.getElementById("postcode").parentNode.className="error";
		ret = false;
	}

	if (
			"" == document.getElementById("dd").value.trim() ||
			!is_numeric(document.getElementById("dd").value.trim()) ||
			"" == document.getElementById("mm").value.trim() ||
			!is_numeric(document.getElementById("mm").value.trim()) ||
			"" == document.getElementById("yyyy").value.trim() ||
			!is_numeric(document.getElementById("yyyy").value.trim())
		)
	{
		document.getElementById("dd").parentNode.className="birth error";
		ret = false;
	}

	if (0 == document.getElementById("country").selectedIndex)
	{
		document.getElementById("country").parentNode.parentNode.className="error";
		ret = false;
	}
	
	return ret;
}