/*
** Colour Inc Ltd.  © 2008
** 20080826
** Dan Richards - Orpheus Software Ltd
*/

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.01
  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 && d.getElementById) x=d.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 PageQuery(q) {
  if(q.length > 1) this.q = q.substring(1, q.length);
  else this.q = null;
  
  this.keyValuePairs = new Array();
  if(q) {
    for(var i=0; i < this.q.split("&").length; i++) {
      this.keyValuePairs[i] = this.q.split("&")[i];
    }
  }
  
  this.getKeyValuePairs = function() { return this.keyValuePairs; }
  
  this.getValue = function(s) {
    for(var j=0; j < this.keyValuePairs.length; j++) {
      if(this.keyValuePairs[j].split("=")[0] == s)
        return this.keyValuePairs[j].split("=")[1];
    }
    return false;
  }
  
  this.getKey = function(v) {
    for(var k=0; k < this.keyValuePairs.length; k++) {
      if(this.keyValuePairs[k].split("=")[1] == v)
        return this.keyValuePairs[k].split("=")[0];
    }
    return false;
  }

  this.getParameters = function() {
    var a = new Array(this.getLength());
    for(var j=0; j < this.keyValuePairs.length; j++) {
      a[j] = this.keyValuePairs[j].split("=")[0];
    }
    return a;
  }
  this.getLength = function() { return this.keyValuePairs.length; } 
}

function queryString(key) {
  var page = new PageQuery(window.location.search); 
  return unescape(page.getValue(key)); 
}

function queryStringType(val) {
  var page = new PageQuery(window.location.search); 
  return unescape(page.getKey(val)); 
}


function focusSearch() {
  var s=document.forms['lnav'].search;
  s.focus();
}


function searchEnter(e) {
  if (!e) e = window.event;
  if (e.keyCode == 13) {
    e.cancelBubble = true;
    if (e.returnValue) e.returnValue = false;
    if (e.stopPropagation) e.stopPropagation();
    submitSearch(); 
    return false;
  } else {
    return true;
  }
}

function submitSearch() {
  var s=document.forms['nav'].search.value.toLowerCase();
  if (s == '') {
    alert("Showing all images, as no search terms were entered...");
  } else {
    var delim = Array(',','+','.','/','#','(',')',';',':');
    for (var i=0; i < delim.length; i++) {
      ns = s.replace(delim[i],' ');
    }
    var words = ns.split(' ');
    out = '';
    for (i=0; i < words.length; i++) {
      w = words[i].toLowerCase();
      if (w.length > 0 && w!='and' && w!='by') {
        out = out + '+' + w;
      }
    }

    out = out.substring(1);
    filter(out);
  }
}  

/*
// version that allows multiple search terms together
function filter(crit, t) {
  var qs=window.location.search;
  if (qs.length == 0 || qs.substr(0,5)=="?img=") { // only criteria
    newqs = "?"+t+"="+crit;
  } else {
    var ex=queryString(t);
    if (ex!='false') {  // replace existing criteria of this type
      newqs = qs.replace(t+'='+ex, t+'='+crit);
    } else {  // add criteria
      newqs = qs+"&"+t+"="+crit;
    }
    if (t != 'p') {
      var p=queryString('p');
      if (p!='false') {  // reset to page 1
        newqs = newqs.replace('p='+p, 'p=1');
      }
    }
  }
//  var loc=document.forms['nav'].action;
  window.location = 'gallery.php'+newqs;
}
*/

function filter(crit, t) {
  var page=window.location.toString();
  var qs=window.location.search;	//.toLowerCase();
  // keep existing search for paging changes
  if (t=="p" || t=="per") {
    if (t=="p") {
      var p=queryString('p');
      if (p=='false') {
        if (qs == "") {
          qs = "?p="+crit;
        } else {
          qs = qs + "&p="+crit;
        }
      } else {
        qs = qs.replace('p='+p, 'p='+crit);
      }
    } else {  // reset to page 1
      if (p!='false') {
        qs = qs.replace('p='+p, 'p=1');
      }
    }
          
    if (t=="per") {
      var pp=queryString('per');
      if (pp=='false') {
        if (qs == "") {
          qs = "?per="+crit;
        } else {
          qs = qs + "&per="+crit;
        }
      } else {
        qs = qs.replace('per='+pp, 'per='+crit);
      }
    }
  } else {  // replace existing search
    qs = "?s="+crit;
  }
  if (page.indexOf('favourites.php')>0) {
  	page = page.substr(0,page.indexOf('?'));
  	window.location = page+qs;
  } else {
  	window.location = 'gallery.php'+qs;
  }
}

function removeFilter(filt, t) {
  var newqs="";

  if (filt == "") {
    var per=queryString('per');
    if (per!='false') {  // keep the per_page setting
      newqs = "?per="+per;
    }
  } else {
    var qs=window.location.search.toLowerCase();
    var f = filt.toLowerCase();
    var kvp = t+"="+f;

    if (qs == "") {
      newqs = kvp;
    } else {
      qs = qs.substr(1); // strip leading ?
      var aQS = qs.split("&");
      for (var i=0; i < aQS.length; i++) {
        aKVP = aQS[i].split("=");
        if (t == 's') { // handle multiple search words
          aW = aKVP[1].split("+");
          if (aW.length > 1) {
            w = "";
            for (var j=0; j < aW.length; j++) {
              if (aW[j] != f) { // keep existing words
                w = w + aW[j] + "+";
              }
            }
            newqs = newqs + "s=" + w.substr(0, w.length-1) + "&"; // strip trailing +
          }
        }
        // keep existing kvp's
        if (t != aKVP[0]) {
          newqs = newqs + aQS[i] + "&";
        }
      }
      newqs = newqs.substr(0, newqs.length-1); // strip trailing &
      if (newqs != "") {
        newqs = "?"+newqs;
        if (t != 'p') { // reset to page 1
          var p=queryString('p');
          if (p!='false') {  // reset to page 1
            newqs = newqs.replace('p='+p, 'p=1');
          }
        }
      }
    }
  }

  var loc=document.forms['nav'].action;
  window.location = loc+newqs;
}

function updMatchAny() {
  var qs = window.location.search.toLowerCase();
  var a=queryString('a');
  if (a=='false') {
    qs = qs + "&a=y";
    var loc=document.forms['nav'].action;
    window.location = loc+qs;
  } else {
    removeFilter('y','a');
  }
}

function updSoundex() {
  var qs = window.location.search.toLowerCase();
  var x=queryString('x');
  if (x=='false') {
    qs = qs + "&x=y";
    var loc=document.forms['nav'].action;
    window.location = loc+qs;
  } else {
    removeFilter('y','x');
  }
}

function updPage(newpage) {
  var f = document.forms['nav'];
  f.thispage.value = newpage;
  f.submit();
}

function updMedia(newMedia) {
  var loc = document.forms['nav'].action;
  var qs = window.location.search.toLowerCase();
  var kvp = "";
  if (newMedia != 'canvas') {
    kvp = "&m="+newMedia;
  }
  var m = queryString('m');
  if (m != 'false') {
    newqs = qs.replace('&m='+m, kvp);
  } else if (kvp != "") {
    newqs = qs+'&m='+newMedia;
  }
  window.location = loc+newqs;
}

function customHandleEnter(e) {
  if (!e) e = window.event;
  if (e.keyCode == 13) {
    e.cancelBubble = true;
    if (e.returnValue) e.returnValue = false;
    if (e.stopPropagation) e.stopPropagation();
    updCustom(); 
    return false;
  } else {
    return true;
  }
}

function updCustom() {
  var f = document.forms['nav'];
  var c = f.custom_s.value;
  var qs = window.location.search.toLowerCase();
  var kvp = "";
  if (c == "" || !isNumeric(c)) {
    alert("Please enter a valid number for the short side of your custom picture (in centimetres)");
    f.custom_s.focus();
    return;
  }
  var mx = parseInt(f.maxShort.value);
  if (parseInt(c) > mx) {
    alert("The maximum measurement for the short side of your custom picture is "+mx+"cm");
    f.custom_s.focus();
    return;
  }    
  if (parseInt(c) < 20) {
    alert("We're happy to print your picture this small, but please note that a minimum price may apply");
    f.custom_s.focus();
  }    
  var z = queryString('z');
  if (z != 'false') {
    newqs = qs.replace('&z='+z, '&z='+c);
  } else {
    newqs = qs+'&z='+c;
  }
  window.location = f.action+newqs;  
}

function isDefined(v) {
  return (typeof(window[v]) == "undefined")?  false: true;
}

function popup(file) {
	popupWin = window.open(file, 'popup', 'scrollbars=yes,status=1,menubar=yes,resizable=yes,width=760,height=480');
  myTimer = setTimeout("popupWin.focus()",1000);
}

function bigimg(img_no) {
	new_win = window.open("","new_win","toolbar=0,scrollbars=0,location=0,resizable=1,width=300,height=300,left=40,top=40");
	new_win.document.open();
	new_win.document.writeln('<html><head><title>GalleryInc - '+img_no+'</title></head>');
	new_win.document.writeln('<body onLoad="resizePage();" bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" onClick="self.close()" onBlur="self.close()">');
  new_win.document.writeln('<img src="preview.php?i='+img_no+'" id=imgID alt="Click to close">');
//	new_win.document.writeln('<img src="'+img_name+'" id=imgID alt="Click to close">');
	new_win.document.writeln('<script>function resizePage() { img = document.images[0]; window.resizeTo(img.width+10,img.height+31) } </\script>');
	new_win.document.writeln('</body></html>');
	new_win.document.close();
	new_win.focus();
}
	
function addToBasket(i, m, l, s) {
  var loc="basket.php?p=a&i="+i+"&l="+l+"&m="+m+"&s="+s;
  window.location = loc;
}


function delFromBasket(id) {
  var loc="basket.php?p=d&id="+id;
  window.location = loc;
}


function updBasket(id, qty) {
 var f=document.forms[0];
 var o=eval("f.qty" + id);
 if ( !isNumeric(o.value) ) {
   alert("Please enter a valid number");
   o.focus();
   return;
 }
  var loc="basket.php?p=u&id="+id+"&q="+o.value;
  window.location = loc;
}

function editBasket() {
 location = "basket.php";
}

function checkout() {
 var f=document.forms[0];
 document.getElementById('checkoutcontainer').innerHTML = '<img src="img/continue_disabled.gif" width="122" height="27" border="0" />';
 f.action = "checkout.php";
 f.submit();
}

function submitCheckout() {
  var f=document.forms[0];
  var m=new Array("name","address","town","postcode","email");
  
  for (var i=0; i<5; i++) { 
    o = eval("f." + m[i]);
    if ( o.value == "" ) {
      alert("Please enter your " + m[i]);
      o.focus();
      return;
    }
  }
  if (f.delivery.selectedIndex == 0) {
    alert("Please select a delivery option.");
    f.delivery.focus();
    return;
  } else {
    if (f.delivery.selectedIndex > 2 && f.country.value == "") {
      alert("Please enter the country.");
      f.country.focus();
      return;
    }
  }
  document.getElementById('checkoutcontainer').innerHTML = '<img src="img/continue_disabled.gif" width="122" height="27" border="0" />';
  f.submit();
}

function submitCheckout2() {
  var f=document.forms[0];
  //var m=new Array("cctype","ccnumber","month","year","securitycode");
  var m=new Array("name","address","town","postcode","email");

  for (var i=0; i<5; i++) { 
    o = eval("f." + m[i]);
    if ( o.value == "" ) {
      alert("Please enter your billing " + m[i]);
      o.focus();
      return;
    }
  }
  document.getElementById('checkoutcontainer').innerHTML = '<img src="img/continue_disabled.gif" width="122" height="27" border="0" />';
  f.submit();
}

function updMand() {
  var f=document.forms[0];
  var m=new Array("name","address","town","postcode","email");

  for (var i=0; i<5; i++) { 
    o = eval("f." + m[i]);
    if ( o.value != "" ) {
      o.className = "optfield";
    } else {
      o.className = "mandfield";
    }
  }
}

function updDel() {
  var f=document.forms[0];
  var price=parseFloat(f.amount.value);
  if (f.delivery.selectedIndex > 0) {
    price = eval(parseFloat(f.amount.value) + parseFloat(f.delivery.value));
    switch(f.delivery.selectedIndex) {
      case 1:
        f.country.value = "UK";
        break;
      case 2:
        f.country.value = "UK";
        break;
    }
  }
  document.getElementById('total_price').innerHTML = price.toFixed(2);
}

function ismaxlength(obj) {
  var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
  if (obj.getAttribute && obj.value.length>mlength)
  obj.value=obj.value.substring(0,mlength)
}

function handleEnter(e, nextControl) {
  if (!e) e = window.event;
  if (e.keyCode == 13) {
    e.cancelBubble = true;
    if (e.returnValue) e.returnValue = false;
    if (e.stopPropagation) e.stopPropagation();
    var f=document.forms[0];  
    var o = eval("f." + nextControl);
    o.focus();
    return false;
  } else {
    return true;
  }
}


function isNumeric(entry){
  var validChar='0123456789';
  var strlen=entry.length;
  if (strlen < 1) {
    return false;
  }
  for (i=0; i<strlen; i++){
    if( validChar.indexOf( entry.charAt(i) ) < 0 ) {
      return false;
    }
  }  
  return true;
}

var firsttime;
function validator(theForm) {
  if (firsttime == "Y") {
    alert("Please wait your payment is being processed.");
    return (false);
  }
  firsttime = "Y";
  return (true);
}



