var ImageRotator = Class.create();


ImageRotator.prototype = {
  // Arguments:
  //   images - A list of the ids of elements to rotate
  //   duration - the length of time an element to should show for
  initialize: function(args) {
    this.images = args['images'];
    this.duration = args['duration'] || 7000
  },
  
  start: function() {
    this.show(imageRotator.images[0]);

    setTimeout("imageRotator.crossFade(0)", this.duration);
  },

  show: function(image) {
    new Effect.Appear(image);
  },

  hide: function(image) {
    new Effect.Fade(image);
  },

  crossFade: function(current_index) {

    if(current_index+1 == imageRotator.images.length) {
      next_index = 0
    }
    else {
      next_index = current_index + 1
    }

    this.hide(imageRotator.images[current_index]);
    this.show(imageRotator.images[next_index]);
    setTimeout("imageRotator.crossFade("+next_index+")", this.duration);
  }
}


function getFlashMovie(options)
{
  
  movie = options.movie
  width = options.movieWidth
  height = options.movieHeight
  altImage = options.altImage
  altLink = options.altLink
  altText = options.altText
  flashVersion = options.flashVersion
  variables = options.variables
  bgColor = options.bgColor
  
  if (!options.flashVersion){flashVersion = 6;}
  
  if (altLink == false){altLink = "/"}
  
  if (!width){width = 787}
  if (!height){height = 244}

  if (movie)
  {
    // Version check based upon the values entered above in "Globals"
    var hasReqestedVersion = DetectFlashVer(flashVersion, 0, 0);

    // Check to see if the version meets the requirements for playback
    if (hasReqestedVersion) {
    	// if we've detected an acceptable version
    	// embed the Flash Content SWF when all tests are passed
    	AC_FL_RunContent(
    				"src", "../media/images/content/" + movie,
    				"width", width,
    				"height", height,
    				"align", "",
    				"id", "detectionExample",
    				"quality", "high",
    				"bgcolor", bgColor,
    				"name", movie,
    				"allowScriptAccess","sameDomain",
    				"type", "application/x-shockwave-flash",
    				'codebase', 'http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab',
    				"pluginspage", "http://www.adobe.com/go/getflashplayer",
    				"FlashVars", variables
    	);
    } else {  // flash is too old or we can't detect the plugin
    	var alternateContent = '<a href="../../../javascripts/' + altLink + '"><img src="../../../javascripts/' + altImage + '" alt="' + altText + '"  /></a>';
    	document.write(alternateContent);  // insert non-flash content
    }
    
  } // if movie
}

function toggleslide(div)
{
  
  if (document.getElementById(div).style.display == "none")
  {
     new Effect.SlideDown(div, {duration: 0.5});
  }
  else
  {
     new Effect.SlideUp(div, {duration: 0.5});
  }
  
}

function slidedown(div)
{
  
  if (document.getElementById(div).style.display == "none")
  {
     new Effect.SlideDown(div, {duration: 0.5});
  }
  else
  {
     new Effect.SlideUp(div, {duration: 0.5});
  }
  
}


function toggleOptician(div, which)
{
  store = "store" + which
  other = "other" + which
  div = div + which
  if (document.getElementById(div).style.display == "none")
  {
    if (div == store)
    {
    
      if (document.getElementById(other).style.display != "none")
      {
        new Effect.SlideUp(other, {duration: 0.3});
        new Effect.SlideDown(store, {delay: 0.3, duration: 0.4});
      }
      else
      {
        new Effect.SlideDown(store, {duration: 0.4});
      }
    }
    else
    {
    
      if (document.getElementById(store).style.display != "none")
      {
        new Effect.SlideUp(store, {duration: 0.3});
        new Effect.SlideDown(other, {delay: 0.3, duration: 0.4});
      }
      else
      {
        new Effect.SlideDown(other, {duration: 0.4});
      }
    }
  }
}



function slide(options)
{
	var tabs = new Array('tab_onlineSimple', 'tab_fax', 'tab_telephone', 'tab_inStore', 'tab_noprescription');
	
	// turn all tabs off
	for(var i = 0; i < tabs.length; i++)
	{
        tab = document.getElementById(tabs[i]);
        tab.className = 'tab off';
        
        box = document.getElementById(options.up[i]);
		if (box.style.display != "none")
		{
			new Effect.SlideUp(options.up[i], {duration: 0.4});
		}
	}

	// turn selected tab on
	tab = document.getElementById('tab_' + options.down);
	tab.className = 'tab on';
	
	// show the copy we want to show
	new Effect.SlideDown(options.down, { delay: 0.2, duration: 0.6 });
}




// function for creating drop down boxes that increment from specified start to end values
function createDropDown(options)
{
  //if there is no incremental value then set it to 1
  if (!options.increment){options.increment = 1};

  document.write("<select name='" + options.name + "' id='" + options.name + "' class='" + options.cssClass + "'>");
  var prefix = ""
  
  //loop with increment
  for (var x = options.startValue; x <= options.endValue; x = x + options.increment)
    {
  
  
      // set prefix if includePlus is true
      if (options.includePlus == true && x > 0){prefix = "+"}else{prefix = ""}
      
      if (!options.selectedValue){options.selectedValue = 0};
      
      //check to see if the current value is the same as the value to be selected
      if (x == options.selectedValue || prefix + x == options.selectedValue)
      {
        //alert("found");
        var selectedText = "selected='selected'"
        var foundSelected = 1
      }
      else
      {
        var selectedText = ""
      }
      
      
      
      //check for alternating values with a loop
      if (options.alternateLabels && x != 0)
      {
        for (var i = 0; i < options.alternateLabels.length; i ++ )
          {
            searchFor = x.toFixed(options.precision) + " " + options.alternateLabels[i];
            if (searchFor == options.selectedValue || searchFor == prefix + options.selectedValue){var selectedText = "selected='selected'"}else{selectedText = ""};
            
            document.write("<option value='" + prefix + x.toFixed(options.precision) + " " + options.alternateLabels[i] + "' " + selectedText + ">");
            document.write(prefix + x.toFixed(options.precision) + " " + options.alternateLabels[i] ); 
          }
       }
       else if (x != 0)
       {
         document.write("<option value='" + prefix + x.toFixed(options.precision) + "' " + selectedText + ">");
         document.write(prefix + x.toFixed(options.precision));
       }
       else if (x == 0 && options.zeroValues)
       {
         for (var a = 0; a < options.zeroValues.length; a ++ )
         {
           document.write("<option value='" + options.zeroValues[a] + "' " + selectedText + ">");
           document.write(options.zeroValues[a]);
         }
       }

        
      // close option tag
      document.write("</option>");
  
    }
    
    //check that the selected value is there. if it's been missed add it now
    if (foundSelected != 1)
    {
     
      if (options.selectedValue == 0){options.selectedValue = options.zeroValues[0]}
      document.write("<option value='" + options.selectedValue + "' selected='selected' >");
      document.write(options.selectedValue);
      document.write("</option>");
    }
    
    // close select tag 
    document.write("</select>");
  
}


function popupV2(options)
{
  url = options.url
  customWidth = options.width
  customHeight = options.height
  topOffset = options.topOffset
  scrollbars = options.scrollbars
  
  //get widths and heights of the visitors screen
  var leftPos = (screen.width - customWidth) / 2;
  var topPos = ((screen.height - customHeight) / 2) + topOffset;
  
  
	newWindow = window.open(url, "popup", "location=0, status=0, resizable=1, scrollbars=" + scrollbars + ", width="+ customWidth + ", height=" + customHeight + ", top=" + topPos + ", left=" + leftPos);
	newWindow.window.focus();
}



function popup(url, customWidth, customHeight)
{
  
  //get widths and heights of the visitors screen
  var leftPos = (screen.width - customWidth) / 2;
  var topPos = ((screen.height - customHeight) / 2);
  
  
	newWindow = window.open(url, "popup", "location=0, status=0, resizable=1, scrollbars=1, width="+ customWidth + ", height=" + customHeight + ", top=" + topPos + ", left=" + leftPos);
	newWindow.window.focus();
}


function windowClose()
{
		window.close();
}



function getFilename(full_path)
{
	var URL = unescape(full_path);
	var xstart = URL.lastIndexOf("/") + 1;
	var xend = URL.length;
	var prefixName = URL.substring(xstart,xend);
}



function checkDate( val ) {
val = "" + val;
var a_val = val.split( "/" );
if ( a_val.length != 3 )
a_val = val.split( "-" );
if ( a_val.length != 3 )
throw "Please enter the date of birth as numbers only in the format dd/mm/yyyy, eg 31/07/2006" ;
var dt = new Date( a_val[2], a_val[1] - 1, a_val[0] );
if ( "" + dt.getDate() == "NaN" || "" + dt.getMonth() == "NaN" || "" + dt.getFullYear() == "NaN" )
throw val + ": Please enter the date of birth as numbers only in the format dd/mm/yyyy, eg 31/07/2006";
if ( dt.getDate() < 1 || dt.getDate() > 31 )
throw val + ": Day of month should be between 1 and 31";
if ( dt.getMonth() < 0 || dt.getMonth() > 11 )
throw val + ": Month " + m + " should be between 1 and 12";
if ( dt.getFullYear() < ( new Date().getFullYear() - 200 ) || dt.getFullYear() > new Date().getFullYear() )
throw val + ": Invalid year - provide the year as a 4 digit number - e.g. 2006";
val = "" + dt.getDate() + "/" + ( dt.getMonth() + 1 ) + "/" + dt.getFullYear();
return val;
}


function check_form(form) {

if(form.name.value == "") {
alert("Please enter your name.")
form.name.focus();
return false;
}

if(form.postcode.value == "") {
alert("Please enter your postcode.")
form.postcode.focus();
return false;
}

if(form.telephone.value == "") {
alert("Please enter your daytime telephone number.")
form.telephone.focus();
return false;
}

if(form.dob.value == "" || form.dob.value == "dd/mm/yyyy" ) {
alert("Please enter your date of birth.")
form.dob.focus();
return false;
}
try {
form.dob.value = checkDate( form.dob.value  );
}
catch( dterr ) {
alert( dterr );
form.dob.focus();
return false;
}


if(form.agreeprivacypolicy.checked == false) {
alert("Please agree to the terms of our privacy policy.")
form.agreeprivacypolicy.focus();
return false;

}
return true;

}






 function validate_form ( )
  {
  valid = true;
  
  if ( document.form1.searchvalue.value == "" )
  {
  alert ( "Please fill in the 'Search' box." );
  valid = false;
  }
  
  if ( document.form1.searchvalue.value == "Search" )
  {
  alert ( "Please fill in the 'Search' box." );
  valid = false;
  }
  
  return valid;
  }
  
 
  function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

function captionizeImages() {
  if (!document.getElementsByTagName) return false;
  if (!document.createElement) return false;
  var images = document.getElementsByTagName("img");
  if (images.length < 1) return false; 
  for (var i=0; i<images.length; i++) {
    if (images[i].className.indexOf("captioned") != -1) {
      var title = images[i].getAttribute("alt");
      var divCaption = document.createElement("div");
      divCaption.className="caption";
      var divCaption_text = document.createTextNode(title);
      divCaption.appendChild(divCaption_text);
      var divContainer = document.createElement("div");
      divContainer.className="imgcontainer";
      images[i].parentNode.insertBefore(divContainer,images[i]);
      divContainer.appendChild(images[i]);
      insertAfter(divCaption,images[i]);
    }
  }
}

function clearText(thefield){
		if (thefield.defaultValue==thefield.value)
		thefield.value = ""
}