var baseUrl = "http://www.pictureframesexpress.co.uk/";
var returntrue = true;
function doPaging(url){
  var myAjax = new Ajax(baseUrl + url, {method: 'get', update: $('pageLoad')}).request();
}

function popWhat(num){
  whatWin = window.open('what.php?id=' + num, 'whatWindow', 'width=600,height=400,scrollbars=yes,resizable=yes');
  whatWin.focus();
}

function updatePrices(elem, number, session, getprice, m){
  var value = document.getElementById(elem).value;
  var m = m.toLowerCase();
  var myAjax = new Ajax(baseUrl + 'picture-frames/customise/?frameTo=' + value + '&fid=' + number + '&sessionid='+ session + '&pricematch=' + getprice + '&m=' + m + '&ajax=1', {method: 'get', update: $('refreshFrames')}).request();
}

function deleteMount(url){
	//alert(url);
  if(confirm("Are you sure you wish to remove this product?")){
    window.location.href = url;
  }
}
function deleteDesigner(url){
	
  if(confirm("Are you sure you wish to remove this product?")){
    window.location.href = url;
  }
}
function removeBacking(type, size,oWid,oHei){
	var myWid = document.getElementById(oWid).value;
	var myHei = document.getElementById(oHei).value;

  var myAjax = new Ajax(baseUrl + 'picture-frame-mounts/customise/?type=' + type + '&size=' + size+'&'+oWid+'='+myWid+'&'+oHei+'='+myHei +'&remove=backing&ajax=1', {method: 'get', update: $('refreshFrames')}).request();
}

function updateOptions(elem, number, session, getprice, m, decision){
  var myAjax = new Ajax(baseUrl + 'picture-frames/customise/?elem=' + elem + '&fid=' + number + '&sessionid='+ session + '&pricematch=' + getprice + '&m=' + m + '&add=' + decision + '&ajax=1', {method: 'get', update: $('refreshFrames')}).request();
}

function yes(prodID, size, measurement){
  var myAjax = new Ajax(baseUrl + 'picture-frames/customise/?prodID=' + prodID + '&size=' + size + '&m=' + measurement + '&remove=yes&ajax=1', {method: 'get', update: $("refreshFrames")}).request();
}

function remMount(prodID, size, measurement){
  var myAjax = new Ajax(baseUrl + 'picture-frame-mounts/customise/?prodID=' + prodID + '&size=' + size + '&m=' + measurement + '&remove=yes&ajax=1', {method: 'get', update: $("refreshFrames")}).request();
}

function addOption(elem, number, session, getprice, price, decision){
  var myAjax = new Ajax(baseUrl + 'picture-frames/customise/?optionlabel=' + elem + '&fid=' + number + '&sessionid='+ session + '&pricematch=' + getprice + '&optionprice=' + price + '&add=' + decision + '&ajax=1', {method: 'get', update: $('refreshFrames')}).request();
}

function addMountOption(product,measure,size,backing,price,oWid,oHei){
	var myWid = document.getElementById(oWid).value;
	var myHei = document.getElementById(oHei).value;
	//alert (myHei);
  var myAjax = new Ajax(baseUrl + 'picture-frame-mounts/customise/?mount=' + product + '&type=' + measure + '&size='+size+'&backing='+backing+'&price='+price+'&'+oWid+'='+myWid+'&'+oHei+'='+myHei+'&ajax=1&update=mount', {method: 'get', update: $('refreshFrames')}).request();
}
function addMountOption2(product,measure,size,backing,price){
  var myAjax = new Ajax(baseUrl + 'picture-frame-mount-designer/?mount=' + product + '&type=' + measure + '&size='+size+'&backing='+backing+'&price='+price+'&ajax=1&update=mount', {method: 'get', update: $('refreshFrames')}).request();
}
function show(div){
  var divStatus = document.getElementById(div).style.display;
  if(divStatus == "none"){
    document.getElementById(div).style.display = "block";
  } else if(divStatus == "block"){
    document.getElementById(div).style.display = "none";
  }
}

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

function selectShape(productid, size, imperial, maximum, shape,oWid,oHei){
	var myWid = document.getElementById(oWid).value;
	var myHei = document.getElementById(oHei).value;

  var myAjax = new Ajax(baseUrl + 'picture-frame-mounts/customise/?prod=' + productid + '&type=' + imperial +'&size=' + size + '&shape=' + shape+'&'+oWid+'='+myWid+'&'+oHei+'='+myHei +'&act=shape&ajax=1', {method: 'get', update: $('refreshFrames')}).request();
}
function checkMountSizes(div, productid, size, imperial, maximum, minimum){
	//alert('blah');
  var element = div+'_'+size+'_'+imperial;
  var elemValue = document.getElementById(element).value;
  var elemValue = parseFloat(elemValue);
  var maximum = parseFloat(maximum);
  if(elemValue <= maximum){
    if(elemValue < 50){
      alert("This " + div + " is less than the minimum of " + 50 + ".\nThis has been reset to 50.");
			
      document.getElementById(element).value = 50;
			return false;
			//var myAjax = new Ajax(baseUrl + 'picture-frame-mounts/customise/?prod=' + productid + '&type=' + imperial +'&size=' + size + '&newSize=50&worh=' + div + '&act=update&ajax=1', {method: 'get', update: $('refreshFrames')}).request();
    }
  } else {
    alert("Your mounts " + div + " has exceed the " + maximum + "mm limit or it contains invalid characters. This has been reset to the maximum width of " + maximum + "mm.\n\nPlease update this height.");
    document.getElementById(element).value = 50;
		return false;
		//var myAjax = new Ajax(baseUrl + 'picture-frame-mounts/customise/?prod=' + productid + '&type=' + imperial +'&size=' + size + '&newSize=' + maximum + '&worh=' + div + '&act=update&ajax=1', {method: 'get', update: $('refreshFrames')}).request();
  }
}
function checkValidate(productid, size, imperial, maximumw, maximumh) {
	if (checkMountSizes('width', productid, size, imperial, maximumw) == false || checkMountSizes('height', productid, size, imperial, maximumh) == false) {
		return false;
	} else {
		return true;
	}
	
}
function mountSizes(div, productid, size, imperial, maximum, minimum){
	//alert('blah');
  var element = div+'_'+size+'_'+imperial;
  var elemValue = document.getElementById(element).value;
  var elemValue = parseFloat(elemValue);
  var maximum = parseFloat(maximum);
  if(elemValue <= maximum){
    if(elemValue < 50){
      alert("This value is less than the minimum of " + 50 + ".\nThis has been reset to 50.");
			
      document.getElementById(element).value = 50;
			//var myAjax = new Ajax(baseUrl + 'picture-frame-mounts/customise/?prod=' + productid + '&type=' + imperial +'&size=' + size + '&newSize=50&worh=' + div + '&act=update&ajax=1', {method: 'get', update: $('refreshFrames')}).request();
    } else {
			//alert("here");
			//if (elemValue == 0 && div == "height") {
			//	var widiv = element.replace("height", "width"); 
			//	if (document.getElementById(widiv).value != 0) {
			//		alert("Either both height and width have to be 0 or both have to be greater than 50, your width has been changed to 0");
			//		document.getElementById(widiv).value = 0;
			//	}
			//}
			//alert('elem is '+elemValue);
      //var myAjax = new Ajax(baseUrl + 'picture-frame-mounts/customise/?prod=' + productid + '&type=' + imperial +'&size=' + size + '&newSize=' + elemValue + '&worh=' + div + '&act=update&ajax=1', {method: 'get', update: $('refreshFrames')}).request();
    }
  } else {
    alert("Your mounts width has exceed the " + maximum + "mm limit or it contains invalid characters. This has been reset to the maximum width of " + maximum + "mm.\n\nPlease update this height.");
    document.getElementById(element).value = 50;
		//var myAjax = new Ajax(baseUrl + 'picture-frame-mounts/customise/?prod=' + productid + '&type=' + imperial +'&size=' + size + '&newSize=' + maximum + '&worh=' + div + '&act=update&ajax=1', {method: 'get', update: $('refreshFrames')}).request();
  }
}
function openingSize(div){
  var element = div;
  var elemValue = document.getElementById(element).checked;
	var elemHeight = element.replace("opening", "height");
	var elemWidth = element.replace("opening", "width");

	if(elemValue == 1){
			
  	document.getElementById(elemHeight).value = '0';
		document.getElementById(elemWidth).value = '0';
  } else {
  	document.getElementById(elemHeight).value = 50;
		document.getElementById(elemWidth).value = 50;
	}
} 
function deleteItem(product, size){
  if(confirm("Are you sure you want to delete this product?")){
    window.location.href = 'basket/?action=delete&type=normal&product=' + product + '&size=' + size;
  }
}

function showinfo(info){
  if(document.getElementById(info).style.display == "none"){
    document.getElementById(info).style.display = "block";
  } else {
    document.getElementById(info).style.display = "none";
  }
}

function addQuantity(va, id){
	if (va > '0') {
  	window.location.href = baseUrl + 'basket/?' + id + '&action=update&new=' + va;
	} else {
		alert('Cannot set the quantity to 0, if you dont want this product please use the delete button');
			window.location.href = baseUrl + 'basket/?add=designer&designer='+designer + '&quantity=1&rand=' + rand;
	}

}

function addQuantity2(va,id){
	if (va > '0') {
  	window.location.href = baseUrl + 'basket/?' + id + '&action=update&upd=mount&new=' + va;
	} else {
		alert('Cannot set the quantity to 0, if you dont want this product please use the delete button');
			window.location.href = baseUrl + 'basket/?add=designer&designer='+designer + '&quantity=1&rand=' + rand;
	}

}
function addQuantity3(va,id){
	if (va > '0') {
  	window.location.href = baseUrl + 'basket/?' + id + '&action=update&upd=misc&new=' + va;
	} else {
		alert('Cannot set the quantity to 0, if you dont want this product please use the delete button');
			window.location.href = baseUrl + 'basket/?add=designer&designer='+designer + '&quantity=1&rand=' + rand;
	}

}
function doDesigner(designer, quantity, rand){
  if(designer.length > 0 && !isNaN(quantity)){
		if (quantity > '0') {
    	window.location.href = baseUrl + 'basket/?add=designer&designer='+designer + '&quantity=' + quantity + '&rand=' + rand;
		} else {
			alert('Cannot set the quantity to 0, if you dont want this product please use the delete button');
			window.location.href = baseUrl + 'basket/?add=designer&designer='+designer + '&quantity=1&rand=' + rand;
		}
  }
}

function progress_bar(){
  var image_count = document.getElementsByTagName("img").length;
  var image_array = document.getElementsByTagName("img");
  for (var i = 0; i < image_count; i++){
    if(image_array[i].complete) {
      var new_image_count = image_count - 1;
      document.getElementById("imagecounter").innerHTML = i + ' of ' + new_image_count + ' pictures loaded<br />';
    }
  }
  var image_count = image_count;
}