var loaded = 0;
var baseUrl = "http://www.screwfixwardrobes.com/";
//var baseUrl = "http://matt.screwfix.headserv.net/";
Effects = {};

Effects.fade = function(id, opacStart, opacEnd, duration, callback)
{
	Effects.changeOpacity(0, id);
	var speed = Math.round(duration/100);
	var timer = 0;

	if(opacStart > opacEnd)
	{
		for(var i=opacStart; i>=opacEnd; i--)
		{
			setTimeout("Effects.changeOpacity("+ i +", '"+ id +"', "+ opacEnd +", '"+ callback +")", (timer*speed));
			timer++;
		}
	}
	else if(opacStart < opacEnd)
	{
		for(var i=opacStart; i<=opacEnd; i++)
		{
			setTimeout("Effects.changeOpacity("+ i +", '"+ id +"', "+ opacEnd +", '"+ callback +"')", (timer*speed));
			timer++;
		}
	}
}

Effects.changeOpacity = function(opacity, id, endPoint, callback)
{

	var _style = document.getElementById(id).style;
    _style.opacity = (opacity / 100);
    _style.MozOpacity = (opacity / 100);
    _style.KhtmlOpacity = (opacity / 100);
    _style.filter = "alpha(opacity=" + opacity + ")";
	
	if(opacity == endPoint && callback != null)
	{
		eval(callback);
	}
}

Effects.onFaded = function()
{
	// Add callback code here
}

function doCoordsLeft() {
  var blaWidth = 759;
  var clientWidth = document.body.clientWidth;
  if (clientWidth < blaWidth) clientWidth = blaWidth;
  var leftCoord = (clientWidth / 2) - (blaWidth / 2)

  return leftCoord;
}
function page(prodUrl){
	//var myAjax = new Ajax(prodUrl, {method: 'get', update: $('ajaxhead')}).request();
	jQuery("#maincontent").load(prodUrl+" #ajaxOrion");
}
function doIt() {
  document.getElementById("products").style.left = doCoordsLeft() + 211;
  document.getElementById("faqs").style.left = doCoordsLeft() + 492;
  document.getElementById("aboutus").style.left = doCoordsLeft() + 90;
  document.getElementById("fitting").style.left = doCoordsLeft() + 335;
}
function videoShow(){
  window.open(baseUrl + "install/test.htm", "video", "scrollbars=no,menubar=no,statusbar=no,width=380,height=340");
}
//window.onresize = doIt;

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);


var switcher;
switcher = "1";
switcher2 = "1";
function MM_showHideLayers() { //v3.0
if((switcher == "1" || switcher2 == "1") && loaded == 1){
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}
}

function galPop(image, width, height){
  galPopImg = window.open('image.php?img=' + image, 'portGalImg_' + width + '_' + height, 'width=' + width + ',height=' + height + ',scrollbars=no,resizable=no');
  galPopImg.focus();
}

function upperCase(what){
  what.value = what.value.toUpperCase().replace(/([^0-9A-Z ])/g,"");
}

function numberOnly(what){
  what.value = what.value.replace(/([^0-9])/g,"");
}

function popDoors(num, isSplit){
  window.open('door_browser.php?num=' + num + '&split=' + isSplit, 'doorBrowser', 'width=600, height=600, scrollbars=yes, resizable=no');
}

function loadDoors(num, isSplit, doors){
	//alert("here");
	
	jQuery("#doorholder").load("door_browser_screw.php?num="+num+"&split="+isSplit+"&doors="+doors, function() { 
		checkout();																																																			 
	});
	jQuery("#doorholder").fadeIn("slow");
  //document.getElementById('doorholder').src = 'door_browser.php?num=' + num + '&split=' + isSplit + '&doors=' + doors;
}
function loadDoors2(num, isSplit, side, doors){
  //document.getElementById('doorholder2').src = 'door_browser_orion2.php?num=' + num + '&split=' + isSplit + '&side=' + side + '&doors=' + doors;
	jQuery("#doorholder").load("door_browser_screw_orion.php?num="+num+"&split="+isSplit+"&doors="+doors+"&side="+side, function() { 
		//checkout();																																																			 
	});
	jQuery("#doorholder").fadeIn("slow");
}



function canAjax(){
  var ro;
  var browser = navigator.appName;
  if(browser == 'Microsoft Internet Explorer'){
    ro = new ActiveXObject("Microsoft.XMLHTTP");
  }else{
    ro = new XMLHttpRequest();
  }
  return ro;
}

function pageFlick(urlname,pageid){
  if(canAjax()){
    $div='rangeGallery';
    var myAjax = new Ajax(urlname+'&aj=1&pager='+pageid, {method: 'get', update: $('rangeGallery')}).request(); //, onComplete: changeBack
  }
}

function newWin(){
  winwin = window.open(baseUrl + 'savebasket.php', 'basketSave', 'width=380,height=150,scrollbars=no,resizable=no,menubars=no');
  winwin.focus();
}

function popKey(){
  window.open(baseUrl + 'enter_key.php', 'keyLogin', 'width=380, height=150, scrollbars=no, resizable=no');
}
function popKeyApply(){
  //h=537
  window.open(baseUrl + 'apply_key.php', 'keyApply', 'width=500, height=215, scrollbars=yes, resizable=no');
}

function retrBask(){
	document.getElementById("emailcontainer").style.display = "";
}

function basketMe(id){
	window.open(baseUrl + 'viewbasket.php?id=' + id, 'basketWindow', '');
}

function popFeedback(){
	feedback = window.open(baseUrl + 'feedback.php?id=' + id, 'basketWindow', 'width=400,height=300');
	feedback.focus();	
}
function popBrochure(){
	brochure = window.open('/brochure.php', 'basketWindow', 'width=400,height=380, scrollbars=yes');
	brochure.focus();	
}
function redirect() {
	window.location='galaxy.php';
}
function Func1()
{
 //alert("Delayed 3 seconds");
 location.reload();
}

function Func1Delay()
{
setTimeout("Func1()", 100);
//location.reload();
//alert('i am first i am first ');
}

function getPrice(page, type, thumb, double, key){
var xmlhttp=false; //Clear our fetching variable
        try {
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
        } catch (e) {
                try {
                        xmlhttp = new
                        ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
            } catch (E) {
                xmlhttp = false;
                        }
        }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
        }
        var file = 'text.php?page='; //This is the path to the file we just finished making *
    xmlhttp.open('GET', file + page + '&type=' + type + '&thumblock=' + thumb + '&double=' + double + '&keylock=' + key, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
                var content = xmlhttp.responseText; //The content data which has been retrieved ***
                if( content ){ //Make sure there is something in the content variable
                      document.getElementById('glassPrice').innerHTML = '&pound; ' + content; //Change the inner content of your div to the newly retrieved content ****
                }
        }
        }
        xmlhttp.send(null) //Nullify the XMLHttpRequest
return;
}

function getPage(stage){
var xmlhttp=false; //Clear our fetching variable
        try {
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
        } catch (e) {
                try {
                        xmlhttp = new
                        ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
            } catch (E) {
                xmlhttp = false;
                        }
        }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
        }
        var file = 'glassAjax.php?stage='; //This is the path to the file we just finished making *
    xmlhttp.open('GET', file + stage, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
                var content = xmlhttp.responseText; //The content data which has been retrieved ***
                if( content ){ //Make sure there is something in the content variable
				//alert(content);
				//alert(image);
                     document.getElementById('stageAjax').innerHTML =  content; //Change the inner content of your div to the newly retrieved content ****
					
                }
        }
        }
        xmlhttp.send(null) //Nullify the XMLHttpRequest
return;
}


function returndoors(id){
	var myAjax = new Ajax('?doors='+id +'&stage=3&ajax=1', {method: 'get', update: $('ajaxhead')}).request(); onComplete: Func1Delay(); 
}
function returndoors_orion(id){
	var myAjax = new Ajax('?doors='+id +'&stage=2&ajax=1', {method: 'get', update: $('ajaxhead')}).request(); onComplete: Func1Delay(); 
}

function radioCheck(id) {
	if(document.getElementById(id).checked == true) {
		document.getElementById(id).checked = false;
	} else {
		document.getElementById(id).checked = true;
	}
}
//setInterval("showStage()", 500);
var minimum = 1500;
var maximum = 2100;
var minimumW = 500;
var maximumW = 1000;

function showStage() {
var height = document.getElementById('updateHeight').value;
var width = document.getElementById('updateWidth').value;
var gtype;
for (i=0;i<document.forms[0].glassType.length;i++) {
	if (document.forms[0].glassType[i].checked) {
		gtype = document.forms[0].glassType[i].value;
	}
}

if((height >= minimum && height <= maximum) && (width >= minimumW && width <= maximumW)){
	
	document.getElementById('stageTwo').style.display = "block";
	if (gtype) { 
		document.getElementById('stageThree').style.display = "block";
		document.getElementById('addtocart').style.display = "block";
	}
} else { 
	
	document.getElementById('stageTwo').style.display = "none";
	document.getElementById('stageThree').style.display = "none";
	document.getElementById('addtocart').style.display = "none";
}
}
function setColor(place, text) {
	var height = place.value;
	var width = place.value;
	if (text == "sizeDescription2") { 
		var val1 = minimum;
		var val2 = maximum;
		if(height >= minimum && height <= maximum) {
			//place.style.backgroundColor = "#B8FFB8";
			place.style.backgroundColor = "#DBFDDB";
			document.getElementById(text).innerHTML = "";
			calcPrice();
		} else {
			//place.style.backgroundColor = "#FFB8B8";
			place.style.backgroundColor = "#FEDAD8";
			document.getElementById(text).innerHTML = "Please input a value between " + val1 + " and " + val2;
		}

	} else {
		var val1 = minimumW;
		var val2 = maximumW;
		if(width >= minimumW && width <= maximumW) {
			//place.style.backgroundColor = "#B8FFB8";
			place.style.backgroundColor = "#DBFDDB";
			document.getElementById(text).innerHTML = "";
			calcPrice();
		} else {
			//place.style.backgroundColor = "#FFB8B8";
			place.style.backgroundColor = "#FEDAD8";
			document.getElementById(text).innerHTML = "Please input a value between " + val1 + " and " + val2;
		}
	}
}
function goBack() {

alert('go back!!');
}
function selected(type) {
	if (type == 2) {
		document.getElementById('obscure').style.border = "1px solid #CCCDCE";
		document.getElementById('clear').style.border = "none";
		document.getElementById('obscure').style.backgroundColor = "#F8F8F8";
		document.getElementById('clear').style.backgroundColor = "";
		calcPrice();
	}
	if (type == 3){
		document.getElementById('obscure').style.border = "none";
		document.getElementById('clear').style.border = "1px solid #CCCDCE";
		document.getElementById('obscure').style.backgroundColor = "";
		document.getElementById('clear').style.backgroundColor = "#F8F8F8";
		calcPrice();
	}
	document.getElementById('stageThree').style.display = "block";
	 showStage();
}
function calcPrice() {
var uwidth = document.getElementById('updateWidth').value;
var uheight = document.getElementById('updateHeight').value;
var uthumblock = document.getElementById('thumblock').checked;
var udouble = document.getElementById('double').checked;
var ukeylock = document.getElementById('keylock').checked;
var gtype;
for (i=0;i<document.forms[0].glassType.length;i++) {
	if (document.forms[0].glassType[i].checked) {
		gtype = document.forms[0].glassType[i].value;
	}
}
var sqrmtr = (uwidth/1000)*(uheight/1000);
getPrice(sqrmtr, gtype, uthumblock, udouble, ukeylock);

}
function calcPriceExtra(check, type) {
var uwidth = document.getElementById('updateWidth').value;
var uheight = document.getElementById('updateHeight').value
var sqrmtr = (uwidth/1000)*(uheight/1000);
if (document.getElementById('thumblock').checked == true) {
	
	priceextra = getPriceExtras(sqrmtr, type);
}

}
function chkFrm(){

  var ret = true;
	//alert(sizeDesc2() );
	//alert(sizeDesc() );
	if (jQuery("input[name=updateHeight]").val()) {
  if(sizeDesc() && sizeDesc2()){
    ret = true;
  }else{
   alert('Your dimensions don\'t match the minimum and maximum height/width');
	 ret = false;
  }
	}
  return ret;
}


function sizeDesc2(){
  var ret = false;
  var height = document.getElementById('updateHeight').value;
  var maximum = 2490;
	var minimum = 1200;
  if(height >= minimum && height <= maximum){
		ret = true;
  }
  else{
    ret = false;
  }
    return ret;
}


function sizeDesc(){
  var ret = false;
  var width = document.getElementById('updateWidth').value;

	var minimum = 600
  var door2 = 1800
  var door3 = 2090
  var door4 = 3110
  var door5 = 4140
  var door6 = 5000





  if(width >= minimum && width <= door2){
    //2 Doors
    output = "2-4 x doors | 1 x " + width + "mm track";
		ret = true;
  }
  else if(width >= (door2 + 1) && width <= door3){
    //3 doors
    output = "2-4 x doors | 1 x " + width + "mm track";
		ret = true;
  }
  else if(width >= (door3 + 1) && width <= door4){
    //4 doors
    output = "3-5 x doors | 1 x " + width + "mm track";
		ret = true;
  }
  else if(width >= (door4 + 1) && width <= door5){
    //5 doors
    output = "4-6 x doors | 1 x " + width + "mm track";
		ret = true;
  }
  else if(width >= (door5 + 1) && width <= door6){
    //6 doors
    output = "5-8 x doors | 1 x " + width + "mm track";
		ret = true;
  }
  else{
    //bork
    output = "Please input a width between " + minimum + "mm and " + door6 + "mm";
    ret = false;
  }
  if(!ret){
    document.getElementById('sizeDescription').innerHTML = output;
    document.getElementById('result1').innerHTML = '';
  }else{
    document.getElementById('result1').innerHTML = output;
  }
  return ret;


}






	jQuery(document).ready(function(){
		jQuery('a[rel*=ajaxbox]').facebox();
		checkout();
		

		

			jQuery("a.saved-quote").click(function() {
			var mySrc = jQuery(this).attr("href");
			//alert(mySrc);
			jQuery.facebox(function(test) { 
				jQuery.get(mySrc, function(data) { 
					jQuery.facebox(data);
				})
			})
			
				//});
			return false;
		});

	});
	function checkout() {
		//jQuery(document).unbind(facebox, function() { 
			jQuery('a[rel*=facebox]').facebox(); 
		//});
		jQuery("#galInner ul li a").click(function() {
			 var mySrc = jQuery(this).attr("href");
			 //alert("here");
			jQuery("#indexGal").load(mySrc+" #galInner", function() {
					checkout();									
				}).hide().fadeIn("slow");		
				//});
			return false;
		});
		jQuery("a.checkoutProcess").click(function() {
			var mySrc = jQuery(this).attr("href");
			jQuery("#ajaxProcess").html("<div align='center'><img style='margin-top: 200px;' src='images/newspinner.gif' /></div>");		
			jQuery("#maincontent").load(mySrc+" #ajaxProcess", function() {
					checkout();									
				}).hide().fadeIn("slow");		
				//});
			return false;
		});
		jQuery("input[type='radio'].galDoors").click(function() {
			var mySrc = jQuery(this).val();
			jQuery("#ajaxProcess").html("<div align='center'><img style='margin-top: 200px;' src='images/newspinner.gif' /></div>");		
			jQuery("#maincontent").load(baseUrl+"wardrobe-doors/galaxy/doors/"+mySrc+"/ #ajaxProcess", function() {
					checkout();									
				}).hide().fadeIn("slow");		
				//});
			return false;
		});
		jQuery("input[type='radio'].orionDoors").click(function() {
			var mySrc = jQuery(this).val();
			//alert("here");
			jQuery("#ajaxProcessOrion").html("<div align='center'><img style='margin-top: 200px;' src='images/newspinner.gif' /></div>");		
			jQuery("#maincontent").load(baseUrl+"room-divider/orion/doors/"+mySrc+"/ #ajaxProcessOrion", function() {
					checkout();									
				}).hide().fadeIn("slow");		
				//});
			return false;
		});

		jQuery("select.oriental").change(function() {
			//alert("here");
			var mySrc = (jQuery(this).val() == "y") ? "barYes": "barNo";
			var myId = jQuery(this).attr("name");
			jQuery("#ajaxProcess").html("<div align='center'><img style='margin-top: 200px;' src='images/newspinner.gif' /></div>");		
			jQuery("#maincontent").load(baseUrl+"wardrobe-doors/galaxy/"+mySrc+"/"+myId+"/ #ajaxProcess", function() {
					checkout();									
				}).hide().fadeIn("slow");		
			return false;
		});
		jQuery("input[type='checkbox']").click(function() {
			var mySrc = $(this).attr('checked')?"y":"n";
			if (jQuery(this).attr("name") == "doorFineline") {
				
				//alert(mySrc);
				jQuery("#ajaxProcess").html("<div align='center'><img style='margin-top: 200px;' src='images/newspinner.gif' /></div>");		
				jQuery("#maincontent").load(baseUrl+"wardrobe-doors/titan/?fineline="+mySrc+" #ajaxProcess", function() {
					checkout();									
				}).hide().fadeIn("slow");		
			//});
			//return false;
			}
			if (jQuery(this).attr("name") == "terms") {
				var display = (mySrc == "y")?"block":"none";
				//alert(display);
				$("#paymentProcess").css("display", display);
			}
		});
		jQuery("area").click(function() {
			var mySrc = jQuery(this).attr("href");
			jQuery("#ajaxProcess").html("<div align='center'><img style='margin-top: 200px;' src='images/newspinner.gif' /></div>");		
			jQuery("#maincontent").load(mySrc+" #ajaxProcess", function() {
					checkout();									
				}).hide().fadeIn("slow");		
				//});
			return false;
		});
		jQuery("#ajaxProcess form").submit(function() {
			var params = {};
			//alert("here");
			jQuery("input,select").each(function() {
				params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value; 
			});
			//var mySrc = jQuery(this).attr("href");
			if (chkFrm()) {
			jQuery("#ajaxProcess").html("<div align='center'><img style='margin-top: 200px;' src='images/newspinner.gif' /></div>");	
			
			jQuery("#maincontent").load(baseUrl+"wardrobe-doors/galaxy/ #ajaxProcess", params, function() {
					checkout();									
				}).hide().fadeIn("slow");
			}
			return false;
		});
		jQuery(".dAjaxProcess").click(function() {
			//alert("here");
			var mySrc = jQuery(this).attr("href");
			//var params = jQuery(this).serialize();
			jQuery.get(mySrc, function() {
			//alert(baseUrl+"wardrobe-doors/galaxy/ #ajaxProcess");
				jQuery("#doorholder").hide();
				jQuery("#maincontent").load(baseUrl+"wardrobe-doors/galaxy/ #ajaxProcess", function() {
					checkout();
				}).hide().fadeIn("slow");
			});
			//alert("here4");
			return false;
		});
		jQuery("input[type='text']").focus(function() {
			jQuery(".loz").hide();																				
			jQuery("#loz-"+this.name).fadeIn("slow");
		});
		jQuery("select#reducers").focus(function() {
			jQuery(".loz").hide();
			jQuery("#loz-"+this.name).fadeIn("slow");
		});
	}

