function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function BeginSearch(e) {

  if (e.value == 'Search') {
    e.value = '';
  }

}

/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopupSizeGuide").css({
			"opacity": "0.7"
		});
		$("#backgroundPopupSizeGuide").fadeIn("slow");
		$("#popupSizeGuide").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopupSizeGuide").fadeOut("slow");
		$("#popupSizeGuide").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupSizeGuide").height();
	var popupWidth = $("#popupSizeGuide").width();

	//centering
	$("#popupSizeGuide").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6

	$("#backgroundPopupSizeGuide").css({
		"height": windowHeight
	});

}

//centering popup
function movePopup(){

	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupSizeGuide").height();
	var popupWidth = $("#popupSizeGuide").width();

	$("#popupSizeGuide").css({
		"position": "absolute",
		"top": 0-(popupHeight/2),
		"left": popupWidth/2
	});
	//only need force for IE6

	$("#backgroundPopupSizeGuide").css({
		"height": windowHeight
	});

}

$(document).ready(function(){

// IE7 bug fix
$(function() {

	if (getInternetExplorerVersion() == 7) {
	  $('#searchbar').css('zIndex', -1);
	  $('#breadcrumb').css('zIndex', -1);
	  $('#content').css('zIndex', -1);
	  $('#carousel').css('zIndex', -1);

	  $('#menuContainer').css('zIndex', 1000);
	  $('#menu').css('zIndex', 1001);
	  $('.real').css('zIndex', 1002);
	  $('.home').css('zIndex', 1003);
	  $('.toplevel').css('zIndex', 1003);
	  $('.menulink').css('zIndex', 1004);
	  $('.sub').css('zIndex', 1005);
	  $('.submenuitem').css('zIndex', 1006);
	  $('.submenuitem_col1').css('zIndex', 1006);
	  $('.tshirtitem').css('zIndex', 1006);
	  $('.sublink').css('zIndex', 1007);
	  $('.imagecont').css('zIndex', 1008);
	  $('.menuimage').css('zIndex', 1009);

	  // IE7 bug fix
//$(function() {
//	var zIndexNumber = 1000;
//	$('div').each(function() {
//		$(this).css('zIndex', zIndexNumber);
//		zIndexNumber -= 10;
//	});
//});
	}
});




//LOADING POPUP
//Click the button event!
$("#downloadsizeguide").click(function(){
	//centering with css
	movePopup();
	//load popup
	loadPopup();
});

//CLOSING POPUP
//Click the x event!
$("#popupSizeGuideClose").click(function(){
	disablePopup();
});
//Click out event!
$("#backgroundPopupSizeGuide").click(function(){
	disablePopup();
});
//Press Escape event!
$(document).keypress(function(e){
	if(e.keyCode==27 && popupStatus==1){
		disablePopup();
	}
});

/****************** Carousel ***************************/
var imagewidth = 964;
var imagecount = 0;

try {
  imagecount = document.forms['carouselform'].carouselcount.value;
} catch (err) {}

$("#carouselscroll").css('width', (imagewidth*imagecount)*2);
$("#carouselscroll1").css('width', imagewidth*imagecount);
$("#carouselscroll2").css('width', imagewidth*imagecount);
$("#carouselscroll2").css('left', imagewidth*imagecount);

var carouselscrollstate = 0;
var cvCls    = '#carouselscroll1';
var ciCls  = '#carouselscroll2';
var ctCls = ciCls;

$("#leftarrow").click(function() {

if (carouselscrollstate > 0) {
  $("#carouselscroll").animate({
      left: '+=964px'
      }, "slow", "swing");
      carouselscrollstate--;

    if (carouselscrollstate == imagecount-1) {
      ctCls = cvCls;
      cvCls = ciCls;
      ciCls = ctCls;
    }

} else {
    position = $(ciCls).position();
    newleft = position.left - ((imagewidth*imagecount)*2);
    $(ciCls).css('left', newleft);
    ctCls = cvCls;
    cvCls = ciCls;
    ciCls = ctCls;

    $("#carouselscroll").animate({
    left: '+=964px'
    }, "slow", "swing", function() {
       position = $(ciCls).position();
       newleft = position.left - ((imagewidth*imagecount)*2);
      $(ciCls).css('left', newleft);
    });
    carouselscrollstate = (imagecount*2)-1;

}

});

$("#rightarrow").click(function() {

if (carouselscrollstate < (imagecount*2)-1) {
 $("#carouselscroll").animate({
    left: '-=964px'
    }, "slow", "swing");
 carouselscrollstate++;

 if (carouselscrollstate == imagecount) {
    ctCls = cvCls;
    cvCls = ciCls;
    ciCls = ctCls;
 }

} else {

    position = $(ciCls).position();
    newleft = position.left + ((imagewidth*imagecount)*2);
    $(ciCls).css('left', newleft);
    ctCls = cvCls;
    cvCls = ciCls;
    ciCls = ctCls;

    $("#carouselscroll").animate({
    left: '-=964px'
    }, "slow", "swing", function() {
       position = $(ciCls).position();
      newleft = position.left + ((imagewidth*imagecount)*2);
      $(ciCls).css('left', newleft);
    });
    carouselscrollstate = 0;

}

});



/******************  Bestsellers ***********************/
var scrollstate = 0;
var scrollstatevert = 0;
var scrollstaterec = 1;

var visibleCls    = '#scroll1';
var invisibleCls  = '#scroll2';
var tmpCls;
width = $(visibleCls).width();

$("#leftlink").click(function() {

if (scrollstate == 0) {
  position = $(invisibleCls).position();
  left = position.left;
  newleft = left-(width*2);
  $(invisibleCls).css('left', newleft);
}

$("#scroll").animate({
      left: '+=876px'
      }, "slow", "swing");
  scrollstate = 0;

tmpCls = invisibleCls;
invisibleCls = visibleCls;
visibleCls = tmpCls;

scrollstate = 0;

});


$("#rightlink").click(function() {

if (scrollstate == 1) {
  position = $(invisibleCls).position();
  left = position.left;
  newleft = left+(width*2);

  $(invisibleCls).css('left', newleft);
}

$("#scroll").animate({
      left: '-=876px'
      }, "slow", "swing");
  scrollstate = 0;

tmpCls = invisibleCls;
invisibleCls = visibleCls;
visibleCls = tmpCls;

scrollstate = 1;

});

var vCls    = '#scrollvert1';
var iCls  = '#scrollvert2';
var tCls = iCls;

$("#leftlinkvert").click(function() {
if (scrollstatevert > 0) {
  $("#scrollvert").animate({
      top: '+=352px'
      }, "slow", "swing");
      scrollstatevert--;

    if (scrollstatevert == 3) {
      tCls = vCls;
      vCls = iCls;
      iCls = tCls;
    }

} else {
  position = $(iCls).position();
    newtop = position.top - 2816;
    $(iCls).css('top', newtop);
    tCls = vCls;
    vCls = iCls;
    iCls = tCls;

    $("#scrollvert").animate({
    top: '+=352px'
    }, "slow", "swing", function() {
       position = $(iCls).position();
      newtop = position.top - 2816;
      $(iCls).css('top', newtop);
    });
    scrollstatevert = 7;

}
});

$("#rightlinkvert").click(function() {
if (scrollstatevert < 7) {
 $("#scrollvert").animate({
    top: '-=352px'
    }, "slow", "swing");
 scrollstatevert++;

 if (scrollstatevert == 4) {
    tCls = vCls;
    vCls = iCls;
    iCls = tCls;
 }

} else {

    position = $(iCls).position();
    newtop = position.top + 2816;
    $(iCls).css('top', newtop);
    tCls = vCls;
    vCls = iCls;
    iCls = tCls;

    $("#scrollvert").animate({
    top: '-=352px'
    }, "slow", "swing", function() {
       position = $(iCls).position();
      newtop = position.top + 2816;
      $(iCls).css('top', newtop);
    });
    scrollstatevert = 0;


}
});


// Recently Viewed

$("#leftlinkrec").click(function() {
if (scrollstaterec > 1) {
  $("#scrollrec").animate({
      left: '+=622px'
      }, "slow", "swing");
  scrollstaterec--;
}
});

$("#rightlinkrec").click(function() {

boxcount = document.getElementById('scrollform').boxcount.value;
if (scrollstaterec < 3 && boxcount > 4*scrollstaterec) {
 $("#scrollrec").animate({
    left: '-=622px'
    }, "slow", "swing");
 scrollstaterec++;
}
});


/**********************  tell a friend ***************/


// hides the slickbox as soon as the DOM is ready
 // (a little sooner than page load)
  $('#tellafriendform').hide();
 // shows the slickbox on clicking the noted link
  $('#slick-show').click(function() {
    if ($('#slick-show').is('.button')) {
      $('#tellafriendform').show('slow');
      $('#slick-show').removeClass('button');
      $('#slick-show').addClass('inverse');
      $('#emailaddresses').get(0).contentEditable = true;
      $('#custommessage').get(0).contentEditable = true;
      $('#emailaddress').get(0).contentEditable = true;
    } else {
      $('#tellafriendform').hide('slow');
      $('#slick-show').removeClass('inverse');
      $('#slick-show').addClass('button');
    }
    return false;
  });
 // hides the slickbox on clicking the noted link
  $('#slick-hide').click(function() {
    $('#tellafriendform').hide('fast');
    return false;
  });

 // toggles the slickbox on clicking the noted link
  $('#slick-toggle').click(function() {
    $('#tellafriendform').toggle(400);
    return false;
  });

var imageurl = '/mpl/blocks/categoryimage.mas?categoryurl=';

// menu hover

$('.submenuitem').mouseenter(function() {
  /*if ( $('> .subsub', this).css('width') == '0px') {
    $('> .subsub', this).animate( { "width": "+=200px" }, "fast" );
  }*/
  href = $('> .sublink', this).attr('href');
  li = $(this).siblings('.imagecont');
  $('> .menuimage', li).attr('src', imageurl+href);
});

$('.submenuitem').mouseleave(function() {
  /*if ( $('> .subsub', this).css('width') == '200px') {
    $('> .subsub', this).animate( { "width": "-=200px" }, "fast" );
    $('> .subsub', this).css('width', '0px');
  }
  */
});

$('.submenuitem_col1').mouseenter(function() {
  /*if ( $('> .subsub', this).css('width') == '0px') {
    $('> .subsub', this).animate( { "width": "+=200px" }, "fast" );
  }*/
  href = $('> .sublink', this).attr('href');
   li = $(this).siblings('.imagecont');
  $('> .menuimage', li).attr('src', imageurl+href);

});


$('.tshirtitem').mouseenter(function() {
  /*if ( $('> .subsub', this).css('width') == '0px') {
    $('> .subsub', this).animate( { "width": "+=200px" }, "fast" );
  }*/
  href = $('> .sublink', this).attr('href');
   li = $(this).siblings('.imagecont');
  $('> .menuimage', li).attr('src', imageurl+href);

});





$('.menulink').mouseenter(function() {
    //$(this).addClass('menuhover');


    if ($(this).siblings('.sub').css('display') != 'block') {
      $('.sub').slideUp('fast');
      $('.menulink').stop(true);
      $('.menulink').fadeTo(0, 0, function() { $(this).removeClass('menuhover'); });
      $('.menulink').fadeTo(0, 1);
    }
    $(this).siblings('.sub').slideDown(200);
    $(this).fadeTo(0, 0, function() { $(this).addClass('menuhover'); });
    $(this).fadeTo(200, 1);

    cancel_timer();


});

$('.menulink').mouseleave(function() {
   if ($(this).siblings('.sub').css('display') != 'block') {
    $(this).stop(true);
    $(this).fadeTo(0, 0, function() { $(this).removeClass('menuhover'); });
    $(this).fadeTo(0, 1);
   }
  my_timer();
   //$(this).siblings('.sub').slideUp('fast');
});


$('.sub').mouseenter(function() {
    cancel_timer();
});

timeout = 500;
closetimer = 0;

function cancel_timer() {
  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}
}

function my_timer()
{
  closetimer = window.setTimeout(close, timeout);
}


function close() {
  $('.sub').slideUp('fast');
  $('.menulink').stop(true);
  $('.menulink').fadeTo(0, 0, function() { $(this).removeClass('menuhover'); });
  $('.menulink').fadeTo(0, 1);
}

$('.sub').mouseleave(function() {
  // $(this).slideUp('fast');
  my_timer();
});


$('.sub', this).hide();


  initialRating = 0;

  try
  {
   var xmlhttp = new XMLHttpRequest();
  }
  catch(err1)
  {
   var ieXmlHttpVersions = new Array();
   ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.7.0";
   ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.6.0";
   ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.5.0";
   ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.4.0";
   ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.3.0";
   ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp";
   ieXmlHttpVersions[ieXmlHttpVersions.length] = "Microsoft.XMLHttp";

   var i;
   for (i=0; i < ieXmlHttpVersions.length; i++)
   {
    try
    {
     var xmlhttp = new ActiveXObject(ieXmlHttpVersions[i]);

     break;
    }
    catch (err2)
    {

    }
   }
  }

  if (typeof xmlhttp == "undefined") {

  } else {

     xmlhttp.open("POST", "/cgi-bin/ws/rating.pl", true);
     xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
     xmlhttp.onreadystatechange=function() {
       if (xmlhttp.readyState==4 && xmlhttp.status==200) {

	initialRating = xmlhttp.responseText;
	 	//  Ratings
	starCount = 4;
	$('#example-2').ratings(starCount,initialRating).bind('ratingchanged', function(event, data) {
	   $('#example-rating-2').text(data.rating + '/' + starCount);
	 });
       }
     }
  }
  try {
  xmlhttp.send('productSKU='+document.getElementById('updateform').productSKU.value);
  } catch (err) { }



});


jQuery.fn.ratings = function(stars, initialRating) {

  //Save  the jQuery object for later use.
  var elements = this;


  var voted = 0;
  //Go through each object in the selector and create a ratings control.
  return this.each(function() {

    if (voted == 1) {
      return false;
    }
    //Make sure intialRating is set.
    if(!initialRating)
      initialRating = 0;


    // get initial ratings via ajax


    //Save the current element for later use.
    var containerElement = this;

    //grab the jQuery object for the current container div
    var container = jQuery(this);

    //Create an array of stars so they can be referenced again.
    var starsCollection = Array();

    //Save the initial rating.
    containerElement.rating = initialRating;

    //Set the container div's overflow to auto.  This ensure it will grow to
    //hold all of its children.
    container.css('overflow', 'auto');

    //create each star
    for(var starIdx = 0; starIdx < stars; starIdx++) {

      //Create a div to hold the star.
      var starElement = document.createElement('div');

      //Get a jQuery object for this star.
      var star = jQuery(starElement);

      //Store the rating that represents this star.
      starElement.rating = starIdx + 1;

      //Add the style.
      star.addClass('jquery-ratings-star');

      //Add the full css class if the star is beneath the initial rating.
      if(starIdx < initialRating) {
        star.addClass('jquery-ratings-full');
      }

      //add the star to the container
      container.append(star);
      starsCollection.push(star);

      //hook up the click event
      star.click(function() {
	if (voted ==1 ) {
	    return;
	}
        //set the containers rating
   //     containerElement.rating = this.rating;

        //When clicked, fire the 'ratingchanged' event handler.
        //Pass the rating through as the data argument.
        elements.triggerHandler("ratingchanged", {rating: this.rating});
	voted = 1;

	try
	{
	 var xmlhttp = new XMLHttpRequest();
	}
	catch(err1)
	{
	 var ieXmlHttpVersions = new Array();
	 ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.7.0";
	 ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.6.0";
	 ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.5.0";
	 ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.4.0";
	 ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.3.0";
	 ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp";
	 ieXmlHttpVersions[ieXmlHttpVersions.length] = "Microsoft.XMLHttp";

	 var i;
	 for (i=0; i < ieXmlHttpVersions.length; i++)
	 {
	  try
	  {
	   var xmlhttp = new ActiveXObject(ieXmlHttpVersions[i]);

	   break;
	  }
	  catch (err2)
	  {

	  }
	 }
	}

	if (typeof xmlhttp == "undefined") {

	} else {

	   xmlhttp.open("POST", "/cgi-bin/ws/updaterating.pl", true);
	   xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	   xmlhttp.onreadystatechange=function() {
	     if (xmlhttp.readyState==4 && xmlhttp.status==200) {

		initialRating = xmlhttp.responseText;
		 //Unhighlight unselected stars.
		for(var index = 0; index < stars ; index++) {
		  starsCollection[index].removeClass('jquery-ratings-user');
		  if(index < initialRating) {
		    starsCollection[index].addClass('jquery-ratings-full');
		  } else {
		    starsCollection[index].removeClass('jquery-ratings-full');
		    starsCollection[index].addClass('jquery-ratings-empty');
		  }
		}
		      //  Ratings
	      	try
		{
		 var xmlhttp2 = new XMLHttpRequest();
		}
		catch(err1)
		{
		 var ieXmlHttpVersions = new Array();
		 ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.7.0";
		 ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.6.0";
		 ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.5.0";
		 ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.4.0";
		 ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.3.0";
		 ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp";
		 ieXmlHttpVersions[ieXmlHttpVersions.length] = "Microsoft.XMLHttp";

		 var i;
		 for (i=0; i < ieXmlHttpVersions.length; i++)
		 {
		  try
		  {
		   var xmlhttp2 = new ActiveXObject(ieXmlHttpVersions[i]);

		   break;
		  }
		  catch (err2)
		  {

		  }
		 }
		}

		if (typeof xmlhttp2 == "undefined") {

		} else {
		  xmlhttp2.open("POST", "/cgi-bin/ws/ratinginfo.pl", true);
		  xmlhttp2.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		  xmlhttp2.onreadystatechange=function() {
		  if (xmlhttp2.readyState==4 && xmlhttp.status==200) {
		      $('#ratingtotal').html(xmlhttp2.responseText);
		  }
	       }
	     }
	         xmlhttp2.send('productSKU='+document.forms['updateform'].productSKU.value);

	     }
	   }
	}

	xmlhttp.send('productSKU='+document.forms['updateform'].productSKU.value+'&rating='+this.rating);



      });

      star.mouseenter(function() {

	if (voted ==1 ) {
	    return;
	}
        //Highlight selected stars.
        for(var index = 0; index < this.rating; index++) {
          starsCollection[index].addClass('jquery-ratings-user');
        }
        //Unhighlight unselected stars.
        for(var index = this.rating; index < stars; index++) {
          starsCollection[index].removeClass('jquery-ratings-user');
        }
      });

      container.mouseleave(function() {

	if (voted ==1 ) {
	    return;
	}
	//Highlight selected stars.
        for(var index = 0; index < containerElement.rating; index++) {
          starsCollection[index].removeClass('jquery-ratings-user');
	  if(index < initialRating) {
	    starsCollection[index].addClass('jquery-ratings-full');
	  }
        }
        //Unhighlight unselected stars.
        for(var index = containerElement.rating; index < stars ; index++) {
	  starsCollection[index].removeClass('jquery-ratings-user');
	  if(index < initialRating) {
	    starsCollection[index].addClass('jquery-ratings-full');
	  }
        }
      });
    }
  });
};


function SubmitEmails(e)
{
 var event = e || window.event;

 document.getElementById('emailform').addresses.value = document.getElementById('emailaddresses').innerHTML.replace(/ /gi, ";");
 document.getElementById('emailform').custom.value = document.getElementById('custommessage').innerHTML;
 document.getElementById('emailform').email.value = document.getElementById('emailaddress').innerHTML;
 document.getElementById('emailaddresses').contentEditable = false;
 document.getElementById('custommessage').contentEditable = false;
 document.getElementById('emailaddress').contentEditable = false;

 document.getElementById('emailaddresses').style.backgroundColor = '#ccc';
 document.getElementById('custommessage').style.backgroundColor = '#ccc';
 document.getElementById('emailaddress').style.backgroundColor = '#ccc';
 document.getElementById('tellafriendform').style.backgroundColor = '#ccc';
 document.getElementById('emailaddresses').style.color = '#bbb';
 document.getElementById('custommessage').style.color = '#bbb';
 document.getElementById('emailaddress').style.color = '#bbb';
 document.getElementById('tellafriendform').style.color = '#bbb';
 //document.getElementById('inactivebutton').style.zIndex = 2;
 document.getElementById('emailaddresses').style.borderColor = '#bbb';
 document.getElementById('custommessage').style.borderColor = '#bbb';
 document.getElementById('emailaddress').style.borderColor = '#bbb';

 try
 {
  var xmlhttp = new XMLHttpRequest();
 }
 catch(err1)
 {
  var ieXmlHttpVersions = new Array();
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.7.0";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.6.0";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.5.0";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.4.0";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.3.0";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "Microsoft.XMLHttp";

  var i;
  for (i=0; i < ieXmlHttpVersions.length; i++)
  {
   try
   {
    var xmlhttp = new ActiveXObject(ieXmlHttpVersions[i]);

    break;
   }
   catch (err2)
   {

   }
  }
 }

 if (typeof xmlhttp == "undefined") {
  document.getElementById('emailform').submit();
 } else {

    xmlhttp.open("POST", "/cgi-bin/ws/tellafriend.pl", true);
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4 && xmlhttp.status==200) {

        document.getElementById('emailaddresses').contentEditable = true;
        document.getElementById('custommessage').contentEditable = true;
        document.getElementById('emailaddress').contentEditable = true;

        document.getElementById('emailform').custom.value = '';
        document.getElementById('emailform').addresses.value = '';
        document.getElementById('emailform').email.value = '';
        document.getElementById('emailaddresses').style.backgroundColor = '#fff';
        document.getElementById('custommessage').style.backgroundColor = '#fff';
        document.getElementById('emailaddress').style.backgroundColor = '#fff';
        document.getElementById('tellafriendform').style.backgroundColor = '#fff';
        document.getElementById('emailaddresses').style.color = '#666';
        document.getElementById('custommessage').style.color = '#666';
        document.getElementById('emailaddress').style.color = '#666';
        document.getElementById('tellafriendform').style.color = '#666';
        //document.getElementById('inactivebutton').style.zIndex = 0;
        document.getElementById('emailaddresses').style.borderColor = '#CCDEF1';
        document.getElementById('custommessage').style.borderColor = '#CCDEF1';
        document.getElementById('emailaddress').style.borderColor = '#CCDEF1';

        result = xmlhttp.responseText;

        if (result == 1) {
          document.getElementById('custommessage').innerHTML = 'Hi!  I saw this great product on NFLZONEUK.co.uk and thought of you.';
          document.getElementById('result').innerHTML = 'Emails sent!';
          document.getElementById('emailaddresses').innerHTML = '';
          document.getElementById('emailaddress').innerHTML = '';
        } else if (result == 2) {
          document.getElementById('result').innerHTML = 'Your friend&#146;s email was invalid';
	   event.preventDefault();
        } else if (result == 3) {
          document.getElementById('result').innerHTML = 'Your email was invalid';
        } else if (result == 4) {
          document.getElementById('result').innerHTML = 'There was a problem';
        }

	return false;
      }

      return false;
    }




  sendValue = 'addresses='+document.getElementById('emailform').addresses.value;
  sendValue += '&sku='+document.getElementById('emailform').SKU.value;
  sendValue += '&custom='+document.getElementById('emailform').custom.value;
  sendValue += '&email='+document.getElementById('emailform').email.value;



  xmlhttp.send(sendValue);

 }


  return false;

}


function checkSelectionsSpecial(sku,url) {

        var productform = document.forms['check_'+sku];

	if (productform.hasOptions.value==1) {
	  productform.action = url;
	}

        productform.submit();
}

function recSelectionsSpecial(sku,url) {

        var productform = document.forms['rec_'+sku];

	if (productform.hasOptions.value==1) {
	  productform.action = url;
	}

        productform.submit();
}

function vertSelectionsSpecial(sku,url) {

        var productform = document.forms['vert_'+sku];

	if (productform.hasOptions.value==1) {
	  productform.action = url;
	}

        productform.submit();
}



function copyAddress() {

  var form = document.forms['billingDetailsForm'];

	form.dcountry.selectedIndex = form.country.selectedIndex;
	form.dstate.selectedIndex = form.state.selectedIndex;
	shipCountryEvent();

	form.dtitle.selectedIndex = form.title.selectedIndex;
	form.dfirstname.value = form.firstname.value;
	form.dlastname.value = form.lastname.value;
	form.daddress1.value = form.address1.value;
	form.daddress2.value = form.address2.value;
	form.daddress3.value = form.address3.value;
	form.dtowncity.value = form.towncity.value;
	form.dcounty.value = form.county.value;
	form.dpostcode.value = form.postcode.value ;

	form.dtelephone.value = form.telephone.value;
	form.demail.value = form.email.value;

}


function checkSelectionsAndBuy() {

	var retVal = true;

	var productform = document.forms['updateform'];

	var count = productform.optionsetcount.value;

	for (var i=1; i <= count; i++) {

		var value = eval('productform.optionSet'+i+'.value');
		var title = eval('productform.optionSet'+i+'.title');

		if (value == '') {
			alert("Please select a " + title);
			retVal = false;
		}
	}

	if (retVal) {
		document.forms['updateform'].submit();
	}

	return false;
}

function billCountryEvent() {

	var countryISO = $("#country").val();

	if (countryISO == 'US') {
		if ($("#postcode").val() == 'AA1 1AA')
		{
			$("#postcode").val('')
		}
		$("#postcode_container").show();
		$("#state_container").show();
		$("#county_container").hide();
		$("#postcode_text").html('Zip Code');
	}
	else if (countryISO == 'IE') {
		$("#state_container").hide();
		$("#county_container").show();
		$("#postcode_text").html('Postcode');
		$("#postcode_container").hide();
		$("#postcode").val('AA1 1AA');
	}
	else {

		if ($("#postcode").val() == 'AA1 1AA')
		{
			$("#postcode").val('')
		}
		$("#state_container").hide();
		$("#county_container").show();
		$("#postcode_container").show();
		$("#postcode_text").html('Postcode');
	}

}

function shipCountryEvent() {

	var countryISO = $("#dcountry").val();

	if (countryISO == 'US') {
		if ($("#dpostcode").val() == 'AA1 1AA')
		{
			$("#dpostcode").val('')
		}
		$("#dpostcode_container").show();
		$("#dstate_container").show();
		$("#dcounty_container").hide();
		$("#dpostcode_text").html('Zip Code');
	}
	else if (countryISO == 'IE') {
		$("#dpostcode_text").html('Postcode');
		$("#dpostcode_container").hide();
		$("#dpostcode").val('AA1 1AA');
		$("#dstate_container").hide();
		$("#dcounty_container").show();
	}
	else {
		if ($("#dpostcode").val() == 'AA1 1AA')
		{
			$("#dpostcode").val('')
		}
		$("#dstate_container").hide();
		$("#dcounty_container").show();
		$("#dpostcode_container").show();
		$("#dpostcode_text").html('Postcode');
	}
}

function change_usstate() {

	$("#county").val($("#state option:selected").text())

}

function change_usdstate() {

	$("#dcounty").val($("#dstate option:selected").text())

}

