function decoderFunc(thisstring) {
	var returnstring = unescape(thisstring);
	var returnstring2 = returnstring.replace(/\+/g, " ");
	var returnstring3 = returnstring2.replace("\\", "");
	for(i=0; i < 10; i++) {
		returnstring3 = returnstring3.replace("\\", "");
	}
 
	return returnstring3;
}

//if ENTER key pressed on the page start the search
function checkCR(evt) {

    var evt  = (evt) ? evt : ((event) ? event : null);

    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);

    if ((evt.keyCode == 13) && (node.type=="text")) {
		doSearch();
	}	else if ((evt.keyCode == 13) && (node.type=="file")) {
    	doSearch();
	}
}

document.onkeypress = checkCR;


// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
var cf;
var fadesArray = new Array();

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
                                    'MSXML2.XMLHTTP.5.0',
                                    'MSXML2.XMLHTTP.4.0',
                                    'MSXML2.XMLHTTP.3.0',
                                    'MSXML2.XMLHTTP',
                                    'Microsoft.XMLHTTP');
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// read a file from the server
function showBrands(whichBrand) {
	
	//alert(0);
	var theBrandNum = whichBrand;
	//$("#pagetext").hide();

  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
	var theUrl = "processing/showbrands.php?manufact=" + whichBrand;
		//alert(theUrl);
    	// initiate reading a file from the server
    	xmlHttp.open("GET", theUrl, true);
    	xmlHttp.onreadystatechange = handleRequestStateChange;
    	xmlHttp.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
      //alert("Can't connect to server:\n" + e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleRequestStateChange() 
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        handleServerResponse();
      }
      catch(e)
      {
        // display error message
        //alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      //alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
    }
  }
}

 
// handles the response received from the server
function handleServerResponse()
{
	
	var maincontent = document.getElementById("maincontent");
	maincontent.innerHTML = "";
	//$("#pagetext").hide();
	//$("#breadcrumbs").hide();
	//alert("New");
	
	if (document.getElementById("frontpg")) {
		var featuredDiv = document.getElementById("frontpg");
		featuredDiv.innerHTML = "";
	}
  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  // obtain the XML's document element
  var xmlRoot = xmlResponse.documentElement;  
  // obtain arrays with manufacturer names and ids 
  var namesArray = xmlRoot.getElementsByTagName("thebrand");
  var idsArray = xmlRoot.getElementsByTagName("id");
  var thumbsArray = xmlRoot.getElementsByTagName("thumb");
  var dialsArray = xmlRoot.getElementsByTagName("dial");
  var thebrandname = xmlRoot.getAttribute("brandname");
  var brandescription = xmlRoot.getAttribute("desc");
  // generate HTML output
  var html = "";
  html += "<img src=\"images/bg_empty.gif\" alt=\"Swiss Watch Box\" style=\"margin-top:10px\" />";  
  html += "<div id=\"padded\">";  
  var mystring = "";
  var myDiv = document.getElementById("showwatches");
  var mainDiv = document.getElementById("pagetext");
  mainDiv.innerHTML = decoderFunc(brandescription);
   if (namesArray.length == 0) {
  	 html += "<br />Sorry, there are no watches by '" + decoderFunc(thebrandname) + "' at the moment.";
  } else {
	  
	  html += "<p class='headline'>"+thebrandname+"</p>";
	  // iterate through the arrays and create an HTML structure
	  for (var i=0; i < namesArray.length; i++) {
		html += "<div class=\"watchbox\">";
		html += "<a href=\"javascript:;;\" onclick=\"showWatch('" + decoderFunc(idsArray.item(i).firstChild.data) + "', 'brand');return false\">";
		html += "<img src=\"images/thumbs/" + decoderFunc(thumbsArray.item(i).firstChild.data) + "\" width=\"138\" height=\"131\" alt=\"" + decoderFunc(namesArray.item(i).firstChild.data) + "\" border=\"0\" />";
		html += "</a><br />";
		html += "<span class=\"watchname\"><a href=\"javascript:;;\" onclick=\"showWatch('" + decoderFunc(idsArray.item(i).firstChild.data) + "', 'brand');return false\">" + decoderFunc(namesArray.item(i).firstChild.data) + "</a></span><br />";
		html += "<span class=\"watchdial\">" + decoderFunc(dialsArray.item(i).firstChild.data) + "</span><br />";
		html += "</div>";	
		
			if (i%3===2&&i<(namesArray.length-1)){
				html += "<div class=\"line\" style=\"clear:both\"></div>";	
			}
		}
	}

  // display the HTML output
  	 html += "</div>";
     html += "<img src=\"images/bottom.gif\" alt=\"Swiss Watch Box\" />";  
 	 myDiv.innerHTML = html;
	var breadDiv = document.getElementById("breadcrumbs");
	breadDiv.innerHTML = "Brands | " + decoderFunc(thebrandname);
}

// read a file from the server
function showWatch(whichWatch, brandorcat) {
	
	//alert("Brand or cat is : " +brandorcat);
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
	var theUrl = "processing/showwatch.php?watchid=" + whichWatch + "&brandorcat=" + brandorcat;
    	// initiate reading a file from the server
    	xmlHttp.open("GET", theUrl, true);
    	xmlHttp.onreadystatechange = handleRequestStateChangeForWatch;
    	xmlHttp.send(null);
		
    }
    // display the error in case of failure
    catch (e)
    {
      //alert("Can't connect to server:\n" + e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleRequestStateChangeForWatch() 
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
	//alert(xmlHttp.status);
    if (xmlHttp.status == 200) 
    {handleServerResponseWatch();
      try
      {
        // do something with the response from the server
        
      }
      catch(e)
      {
        // display error message
        //alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttp.statusText);
    }
  }
}

// handles the response received from the server
function handleServerResponseWatch()
{
	var thebrandname = "";
	var mainDiv = document.getElementById("pagetext");
	var myDiv = document.getElementById("showwatches");
	//if (document.getElementById("featuredwatches")) {
		//featuredDiv = document.getElementById("featuredwatches");
		//featuredDiv.innerHTML = "";
	//}
	if (document.getElementById("maincontent")){
	var maincontent = document.getElementById("maincontent");
	maincontent.innerHTML = "";
	}
		
		
	if (document.getElementById("frontpg")) {
		var featuredDiv = document.getElementById("frontpg");
		featuredDiv.innerHTML = "";
	}
  	// read the message from the server
  	var xmlResponseWatch = xmlHttp.responseXML;
  	// obtain the XML's document element
  	var xmlRootWatch = xmlResponseWatch.documentElement;  
  	// obtain arrays with manufacturer names and ids 
  	var code = xmlRootWatch.getElementsByTagName("code");
  	var thebrand = xmlRootWatch.getElementsByTagName("brand");
  	var model = xmlRootWatch.getElementsByTagName("model");
	var ourprice = xmlRootWatch.getElementsByTagName("ourprice");
  	var retailprice = xmlRootWatch.getElementsByTagName("retailprice");
  	var movement = xmlRootWatch.getElementsByTagName("movement");
	var dial = xmlRootWatch.getElementsByTagName("dial");
	var thecase = xmlRootWatch.getElementsByTagName("case");
	var strap = xmlRootWatch.getElementsByTagName("strap");
	var watchcondition = xmlRootWatch.getElementsByTagName("watchcondition");
	var image1 = xmlRootWatch.getElementsByTagName("image1");
	var image2 = xmlRootWatch.getElementsByTagName("image2");
	var image3 = xmlRootWatch.getElementsByTagName("image3");
	var image4 = xmlRootWatch.getElementsByTagName("image4");
	var image5 = xmlRootWatch.getElementsByTagName("image5");
	var image6 = xmlRootWatch.getElementsByTagName("image6");

	var sold = xmlRootWatch.getElementsByTagName("sold");
	var seriestext = xmlRootWatch.getElementsByTagName("seriestext");
	var remarkstext = xmlRootWatch.getElementsByTagName("remarks");
	var brandorcat = xmlRootWatch.getAttribute("brandorcat");
	var thetitle = xmlRootWatch.getAttribute("thetitle");
	var theid = xmlRootWatch.getAttribute("theid");
	
	//alert(code+" | "+thebrand+" | "+model+" | "+ourprice+" | "+retailprice+" | "+movement+" | "+dial+" | "+thecase+" | "+strap+" | "+watchcondition+" | "+image1+" | "+image2+" | "+image3+" | "+image4+" | "+image5+" | "+image6+" | "+sold+" | "+seriestext+" | "+remarkstext+" | "+brandorcat+" | "+thetitle+" | "+theid);
	//alert("The title is " + thetitle);
  	// generate HTML output
	var html="";
  	html += "<img src=\"images/bg_empty.gif\" alt=\"Swiss Watch Box\" style=\"margin-top:10px\" />";  
	html += "<div id=\"padded\">";   
  	var mystring = "";

  	mainDiv.innerHTML = "";
	html += "<div class=\"watchheading\">";
	html += "<p class='headline'>" +  decoderFunc(thebrand.item(0).firstChild.data) + ":" + decoderFunc(model.item(0).firstChild.data) + "</p>";
	//html += "</div>";
	html += "<table class=\"maintable\"><tr><td>";
	html += "<ul class=\"watchpictable gallery clearfix\">";
	html += "<li><a href=\"images/"  + decoderFunc(image1.item(0).firstChild.data) +"\" rel=\"prettyPhoto[watches]\" ><img class=\"watchphoto\" width=\"138\" height=\"131\" src=\"images/thumbs/" + decoderFunc(image1.item(0).firstChild.data) + "\" /></a></li>";		
	html += "<li><a href=\"images/"  + decoderFunc(image2.item(0).firstChild.data) +"\" rel=\"prettyPhoto[watches]\" ><img class=\"watchphoto\"  width=\"138\" height=\"131\" src=\"images/thumbs/"  + decoderFunc(image2.item(0).firstChild.data) + "\" /></a></li>";			
	//html += "</tr>";
	
	var image3Data = decoderFunc(image3.item(0).firstChild.data);
	var image4Data = decoderFunc(image4.item(0).firstChild.data);
	var image5Data = decoderFunc(image5.item(0).firstChild.data);
	var image6Data = decoderFunc(image6.item(0).firstChild.data);
	
	//alert(decoderFunc(thebrand.item(0).firstChild.data)+"+"+thebrand.item(0).firstChild.data);
	
		
		if(image3Data != "placeholder.jpg"){
			html += "<li><a href=\"images/"  + decoderFunc(image3.item(0).firstChild.data) +"\" rel=\"prettyPhoto[watches]\" ><img class=\"watchphoto\" width=\"138\" height=\"131\" src=\"images/thumbs/" + image3.item(0).firstChild.data + "\" /></a></li>";			
		
		}
		if(image4Data != "placeholder.jpg"){
			html += "<li><a href=\"images/"  + decoderFunc(image4.item(0).firstChild.data) +"\" rel=\"prettyPhoto[watches]\" ><img class=\"watchphoto\"  width=\"138\" height=\"131\" src=\"images/thumbs/"  + image4.item(0).firstChild.data + "\" /></a></li>";
		}
		if(image5Data != "placeholder.jpg"){
			html += "<li><a href=\"images/"  + decoderFunc(image5.item(0).firstChild.data) +"\" rel=\"prettyPhoto[watches]\" ><img class=\"watchphoto\" width=\"138\" height=\"131\" src=\"images/thumbs/" + image5.item(0).firstChild.data + "\" /></a></li>";			
		
		}
		if(image6Data != "placeholder.jpg"){
			html += "<li><a href=\"images/"  + decoderFunc(image6.item(0).firstChild.data) +"\" rel=\"prettyPhoto[watches]\" ><img class=\"watchphoto\"  width=\"138\" height=\"131\" src=\"images/thumbs/"  + image6.item(0).firstChild.data + "\" /></a></li>";
		}
	
	html += "</ul>";
	html += "</td><td>";
  	html += "<table class=\"watchtable\">";
	html += "<tr><td class=\"leftcol\">";
	html += "<span>Code</span>";
	html += "</td></tr><tr><td>";
	html += decoderFunc(code.item(0).firstChild.data);
	html += "</td></tr>";
	html += "<tr><td class=\"leftcol\">";
	html += "<span>Brand</span>";
	html += "</td></tr><tr><td><strong>";
	html += decoderFunc(thebrand.item(0).firstChild.data);
	html += "</strong></td></tr>";
	html += "<tr><td class=\"leftcol\">";
	html += "<span>Series</span>";
	html += "</td></tr><tr><td><strong>";
	html += decoderFunc(seriestext.item(0).firstChild.data);
	html += "</strong></td></tr>";
	html += "<tr><td class=\"leftcol\">";
	html += "<span>Model</span>";
	html += "</td></tr><tr><td>";
	html += decoderFunc(model.item(0).firstChild.data);
	html += "</td></tr>";
	html += "<tr><td class=\"leftcol\">";
	
	var soldData = decoderFunc(sold.item(0).firstChild.data);
	if(soldData == "yes") {
		html += "<span>Our price</span>";
		html += "</td></tr><tr><td>";
		html += "<span style='color:red'><strong>** SOLD **</strong></span>";
	} else {
		html += "<span>Our price</span>";
		html += "</td></tr><tr><td><strong>";
		html += decoderFunc(ourprice.item(0).firstChild.data);
		html += "</strong>";
	}
	html += "</td></tr>";
	html += "<tr><td class=\"leftcol\">";
	html += "<span>Retail price</span>";
	html += "</td></tr><tr><td>";
	html += decoderFunc(retailprice.item(0).firstChild.data);
	html += "</td></tr>";
	html += "<tr><td class=\"leftcol\">";
	html += "<span>Movement/Complication</span>";
	html += "</td></tr><tr><td>";
	html += decoderFunc(movement.item(0).firstChild.data);
	html += "</td></tr>";
	html += "<tr><td class=\"leftcol\">";
	html += "<span>Case</span>";
	html += "</td></tr><tr><td>";
	html += decoderFunc(thecase.item(0).firstChild.data);
	html += "</td></tr>";
	html += "<tr><td class=\"leftcol\">";
	html += "<span>Dial</span>";
	html += "</td></tr><tr><td>";
	html += decoderFunc(dial.item(0).firstChild.data);
	html += "</td></tr>";
	html += "<tr><td class=\"leftcol\">";
	html += "<span>Strap</span>";
	html += "</td></tr><tr><td>";
	html += decoderFunc(strap.item(0).firstChild.data);
	html += "</td></tr>";
	html += "<tr><td class=\"leftcol\">";
	html += "<span>Condition</span>";
	html += "</td></tr><tr><td>";
	html += decoderFunc(watchcondition.item(0).firstChild.data);
	html += "</td></tr>";
	html += "<tr><td class=\"leftcol\">";
	html += "<span>SWB Remarks</span>";
	html += "</td></tr><tr><td>";
	html += decoderFunc(remarkstext.item(0).firstChild.data);
	html += "</td></tr>";
	html += "</table>";
	html += "</td></tr></table>";
	html += "<br clear=\"all\" />";
	html += "<div class=\"watchheading\" style=\"text-align:right;\">";
	if (brandorcat == "brand") {
		html += "<a href=\"javascript:;;\" class=\"linkleft\" onclick=\"showBrands('" + theid + "', 'brand');return false\"><img border=\"0\" src=\"images/cross.gif\" width=\"16\" height=\"17\" alt=\"Back to listing\" /> ::BACK TO " + decoderFunc(thetitle.toUpperCase()) + " LISTING::</a>";
	} else if (brandorcat == "cat") {
		html += "<a href=\"javascript:;;\" class=\"linkleft\" onclick=\"showCategories('" + theid + "', 'cat');return false\"><img border=\"0\" src=\"images/cross.gif\" width=\"16\" height=\"17\" alt=\"Back to listing\" /> ::BACK TO " + decoderFunc(thetitle.toUpperCase()) + " LISTING::</a>";
	} else {
		html += "<a href=\"index.php\" class=\"linkleft\"><img src=\"images/cross.gif\" width=\"16\" height=\"17\" alt=\"Back to home\" border=\"0\" /> ::BACK TO HOME::</a>";
	}
	html += "<a href=\"#theorder\" class=\"linkright\" onclick=\"showOrderForm();return false\" id=\"ordernow\"><img border=\"0\" src=\"images/cross.gif\" width=\"16\" height=\"17\" alt=\"Enquire this watch\" />::ENQUIRE THIS WATCH::</a>";
	html += "</div>";
	html += "<a nam\"theorder\"></a>";
	html += "<br clear=\"all\" />";
	html += "<div id=\"orderbox\">";
	html += "<p>Here you can express your interest in <strong>" + decoderFunc(model.item(0).firstChild.data) + "</strong> (" + decoderFunc(ourprice.item(0).firstChild.data) + ")</p>";
	html += "<form method=\"post\" action=\"orderform.php\" name=\"orderForm\">";
	html += "<p><label for=\"name\">Name:</label><input type=\"text\" name=\"name\" /></p>";
	html += "<p><label for=\"emailaddress\">Email:</label><input type=\"text\" name=\"emailaddress\" /></p>";
	html += "<p><label for=\"telephone\">Telephone:</label><input type=\"text\" name=\"telephone\" /></p>";
	html += "<input type=\"submit\" name=\"expressinterest\" value=\"Submit\" />";
	html += "<input type=\"hidden\" name=\"watchname\" value=\"" + decoderFunc(model.item(0).firstChild.data) + "\" />";
	html += "<input type=\"hidden\" name=\"watchcode\" value=\"" + decoderFunc(code.item(0).firstChild.data) + "\" />";
	html += "<input type=\"hidden\" name=\"watchprice\" value=\"" + decoderFunc(ourprice.item(0).firstChild.data) + "\" />";
	html += "</form>";
	html += "</div>";
	html += "<br clear=\"all\" />";
	// display the HTML output
	html += "</div></div>";  
	html += "<img src=\"images/bottom.gif\" alt=\"Swiss Watch Box\" />";  
 	myDiv.innerHTML = html;
	var breadDiv = document.getElementById("breadcrumbs");
	if (brandorcat == "brand") {
		breadDiv.innerHTML = "Brands | " + decoderFunc(thetitle);
	} else if (brandorcat == "cat") {
		breadDiv.innerHTML = "Category | " + decoderFunc(thetitle);
	} else if (brandorcat == "new") {
		breadDiv.innerHTML = "New product";
	} else {
		breadDiv.innerHTML = "Featured watch";
	}
	
	
	$(document).ready(function(){ // apply the lightbox (prettyphoto) once the page has been reloaded
 			 $("a[rel^='prettyPhoto']").prettyPhoto({
				 overlay_gallery: false,
				 default_width: 200,
				 default_height: 200
				});
	});

}

// read a file from the server
function showCategories(whichCategory) {
	//alert(2);

  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
	var theUrl2 = "processing/showcats.php?catid=" + whichCategory;
		//alert(theUrl);
    	// initiate reading a file from the server
    	xmlHttp.open("GET", theUrl2, true);
    	xmlHttp.onreadystatechange = handleRequestStateChangeForCat;
    	xmlHttp.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
      //alert("Can't connect to server:\n" + e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleRequestStateChangeForCat() 
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        handleServerResponseCat();
      }
      catch(e)
      {
        // display error message
        //alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      //alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
    }
  }
}

 
// handles the response received from the server
function handleServerResponseCat()
{
	if (document.getElementById("frontpg")) {
		featuredDiv = document.getElementById("frontpg");
		featuredDiv.innerHTML = "";
	}
  // read the message from the server
  var xmlResponseCat = xmlHttp.responseXML;
  // obtain the XML's document element
  xmlRootCat = xmlResponseCat.documentElement;  
  // obtain arrays with manufacturer names and ids 
  namesArrayCats = xmlRootCat.getElementsByTagName("thebrand");
  idsArray = xmlRootCat.getElementsByTagName("id");
  thumbsArray = xmlRootCat.getElementsByTagName("thumb");
  dialsArray = xmlRootCat.getElementsByTagName("dial");
  categoryname = xmlRootCat.getAttribute("categoryname");
  categorydescription = xmlRootCat.getAttribute("desc");
  //alert(namesArrayCats+"+"+idsArray+"+"+thumbsArray+"+"+dialsArray+"+"+categoryname+"+"+categorydescription);
  // generate HTML output
  html ="";
  html += "<div id=\"padded\">";  
  myDiv = document.getElementById("showwatches");
  mainDiv = document.getElementById("pagetext");
  mainDiv.innerHTML = decoderFunc(categorydescription);
  
  // iterate through the arrays and create an HTML structure
  if (namesArrayCats.length == 0) {
  	 html += "<br />Sorry, there are no watches in the '" + decoderFunc(categoryname) + "' category.";
  } else {
	  for (var i=0; i < namesArrayCats.length; i++) {
			html += "<div class=\"watchbox\">";
			html += "<a href=\"javascript:;;\" onclick=\"showWatch('" + decoderFunc(idsArray.item(i).firstChild.data) + "', 'cat');return false\">";
			html += "<img src=\"images/thumbs/" + decoderFunc(thumbsArray.item(i).firstChild.data) + "\" width=\"138\" height=\"131\" alt=\"" + decoderFunc(namesArrayCats.item(i).firstChild.data) + "\" border=\"0\" />";
			html += "</a><br />";
			html += "<span class=\"watchname\"><a href=\"javascript:;;\" onclick=\"showWatch('" + decoderFunc(idsArray.item(i).firstChild.data) + "', 'cat');return false\">" + decoderFunc(namesArrayCats.item(i).firstChild.data) + "</a></span><br />";
			html += "<span class=\"watchdial\">" + decoderFunc(dialsArray.item(i).firstChild.data) + "</span><br />";
			html += "</div>";		
		}
	}
  
  // display the HTML output
 	 myDiv.innerHTML = html;
	  html += "</div>"; 
	breadDiv = document.getElementById("breadcrumbs");
	breadDiv.innerHTML = "Categories | " + decoderFunc(categoryname);
}


function showOrderForm() {
	$("#orderbox").toggle();
	window.scrollTo(0, 1000);
}



