var suf1 = "co.";
var suf2 = "uk";
var dmn = "thepatternboutique"; 

function decod(str)
{	
	str = str.replace("%92", "'");
	return unescape(str);
}

function pnteml(emlname,caption) {
  if (caption==undefined) {
   caption = emlname; 
  }
  mailstring= "<a href='javascript:emlaunch(\"" + emlname + "\");' title='Contact Address'>"+ caption +"</a>";
  document.write(mailstring);
}

function emlaunch(emlname) {
  var thsads = "ma" + "i" + "lto:" + emlname + "@" + dmn + "." + suf1 + suf2;
  document.location=thsads;
}

function getFlashMovie(movieName) {  
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName]; 
}

function formSend(aValue) {
	var text = aValue;
	getFlashMovie("custom").sendTextToFlash(text);
}

function getTextFromFlash(str) {
	
	var aValues = Array();
	aValues = str.split("¬");	
	
	var aValue = aValues[0];
	switch(aValue)
	{
		case "backToDesigns":
		var u = document.getElementById("backToDesignsURL").value;		
		if(u!=null) redirect(u);
		break;
		
		case "buyOnline":
			basketItemAdd(aValues[1]); // itemId		
		break;
	}
	return "ok";
}

function redirect(weburl)
{
	var path = window.location.pathname;
	var newLocation = window.location.host;
	var p = location.protocol;
	path = path.substring(0, path.lastIndexOf("/"));	
	// https
	newLocation = p + "//" + newLocation + path + "/" + weburl;	
	window.location.href = newLocation;
}

function ajaxObject(url, callbackFunction) {
  var that=this;      
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }

  this.update = function(passData,postMethod) { 
    if (that.updating) { return false; }
    that.AJAX = null;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();              
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      return false;                               
    } else {
      that.AJAX.onreadystatechange = function() {  
        if (that.AJAX.readyState==4) {             
          that.updating=false;                
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
          that.AJAX=null;                                         
        }                                                      
      }                                                        
      that.updating = new Date();                              
      if (/post/i.test(postMethod)) {
        var uri=urlCall+'?'+that.updating.getTime();
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-Length", passData.length);
        that.AJAX.send(passData);
      } else {
        var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
        that.AJAX.open("GET", uri, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function () { };
}

function basketItemAdd(itemId)
{
	var aobj = new ajaxObject("funcBasket.php", basketAJAXReturn);
	aobj.elementId = "";
	aobj.itemId = itemId;
	//aobj.elementId = "responseDIVId" + id;	//respond DIV id
	aobj.update("action=basketItemAdd&itemId="+itemId); 
}

function basketAJAXReturn(responseText, responseStatus)
{	
	if(responseStatus==200) 
	{
		if(this.elementId != "" && this.elementId != undefined) document.getElementById(this.elementId).innerHTML=responseText;	
		if(this.objId != "" && this.objId != undefined) document.getElementById(this.objId).value=decod(responseText);
		
		// run callback function...?
		var u = document.getElementById("basketURL").value;		
		if(u!=null) 
		{
			//append aRef i.e. a name=Anchor(itemId)
			u = u + "&aRef=" + this.itemId;
			redirect(u);
		}
			
	}
}

function changeLabelFont(lblId, obj)
{
	var fontId = obj.value;
	img = '_fonts/' + fontId + '.gif';
	document.getElementById("sampleImg"+lblId).getElementsByTagName("img")[0].src = img;
	//save
	var aobj = new ajaxObject("funcCustom.php", basketAJAXReturn);
	aobj.update("action=saveCustomFontOnly&labelId="+lblId+"&fontId="+fontId); 
}

function changeLabelText(lblId, obj)
{
	// save	
	var aobj = new ajaxObject("funcCustom.php", basketAJAXReturn);
	aobj.update("action=saveCustomTextOnly&labelId="+lblId+"&text="+encodeURIComponent(obj.value)); 
}

function setWording(statId, obj)
{
	var aobj = new ajaxObject("funcCustom.php", basketAJAXReturn);
	aobj.update("action=setWording&statId="+statId+"&text="+encodeURIComponent(obj.value)); 
}

function resetWording(statId)
{	
	var aobj = new ajaxObject("funcCustom.php", basketAJAXReturn);
	aobj.objId="wording"+statId;
	aobj.update("action=getWording&statId="+statId); 
}

var menuTimer = 0;
var menuObj;

function menuShowDD(i)
{	
	// hide any existing menu item...
	menuHideDD();
	menuObj = document.getElementById("ddMenu" + i);
	if (menuObj)
	{
		menuObj.style.visibility='visible';	
		menuTimer = 0;
	}
}

function menuHideDD()
{
	if(menuObj)	{menuObj.style.visibility='hidden';}
	menuKeepOpen();
}

function menuTimeOutDD()
{
	menuTimer = window.setTimeout(menuHideDD, 550);
}

function menuKeepOpen()
{
	if(menuTimer)
	{
		window.clearTimeout(menuTimer);
		menuTimer = null;
	}
}
document.onclick = menuHideDD; 
