
function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
}

function showMap(){
	curY=(screen.height/2)-165;
	curX=(screen.width/2)-150;
	attributes="width=300, height=330, top="+curY+", left="+curX+", resizable=no, location=no, menubar=no, toolsbar=no, directories=no";
	linkUrl="showMap.php";
	window.open(linkUrl, 'showMap', attributes);		
}

function showPic(picID){
	curY=(screen.height/2)-263;
	curX=(screen.width/2)-275;
	attributes="width=550, height=526, top="+curY+", left="+curX+", resizable=no, location=no, menubar=no, toolsbar=no, directories=no";
	linkUrl="showPicture.php?curPicture="+picID;
	window.open(linkUrl, 'showPicture', attributes);		
}

function showShipmentZones(){
	curY=(screen.height/2)-263;
	curX=(screen.width/2)-275;
	attributes="width=550, height=526, top="+curY+", left="+curX+", resizable=no, location=no, menubar=no, toolsbar=no, directories=no";
	linkUrl="showZones.php";
	window.open(linkUrl, 'showZones', attributes);		
}

function showVid(picID){
	curY=(screen.height/2)-210;
	curX=(screen.width/2)-275;
	attributes="width=550, height=420, top="+curY+", left="+curX+", resizable=no, location=no, menubar=no, toolsbar=no, directories=no";
	linkUrl="showVideo.php?curVid="+picID;
	window.open(linkUrl, 'showVideo', attributes);		
}


function changeDesign(designNum)
{
	if (designNum != 'default')
		curImage="images/templates/boxDesign_design"+designNum+".gif";
	else
		curImage="images/templates/boxDesign_defDesign.gif";
		
	document.getElementById('designerDiv_templateBack').style.backgroundImage='url('+curImage+')';
	switch (designNum)
	{
		case "1":
		case 1:
		curColor='#D52326';
		break;
		case "2":
		case 2:
		curColor='#03A41E';
		break
		case "3":
		case 3:
		curColor='#009C9E';
		break
		case "4":
		case 4:
		curColor='#FF7800';
		break;	
		default:
		curColor='#5E1409';
		break;
	}
	document.getElementById('designTextTd').style.color = curColor;
}

function ChangeText(text_value)
{
	document.getElementById('designTextTd').innerHTML = text_value;
}
function ChangePicture(new_path, w, h, mar)
{
	var src_pic = document.getElementById('designPic');
	src_pic.src = new_path;
	src_pic.width = w;
	src_pic.height = h;
	src_pic.style.margin = mar;
}

function boxDesignForm_submit(subType)
{
	switch (subType)
	{
		case 1:
			document.shopCart.submit();
		break;
		case 2:
		if (document.shopCart.upload_fileName.value=='')
		{
			alert ("נא להעלות את קובץ התמונה שלכם לאריזה");
			return false;
		}
		else if (document.shopCart.curDesign.value == 'default')
		{
			alert ("נא בחרו רקע מתאימה לאריזה");
			return false;
		}
		else
		{
			checkNext = confirm ("?האם להתקדם לשלב הבא");
			if (checkNext)
			{
				document.shopCart.action = 'boxDesigner_stage2.php';
				document.shopCart.submit();
			}
			else
			{
				return false;
			}
		}
	}
}

function getHTTPObject() 
{
	 if ( window.XMLHttpRequest ) 
	     	xmlhttp = new XMLHttpRequest();
	  else if ( window.ActiveXObject )
	   	 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  return xmlhttp;
}

function LoadXML(url)
{
	var xmlHttp = getHTTPObject();
	xmlHttp.open("GET",url, false);
	xmlHttp.send(null);
	return xmlHttp.responseXML.documentElement;
}

function GetArtType(type_art)
{
	var url = "xmlIcons.php?type="+type_art;
	var xml = LoadXML(url);
	var icons = xml.getElementsByTagName('icon');
	var select_icon = document.getElementById('icon_types').options;
	select_icon.length = 1;
	for(var i=0, index = 1; i<icons.length;i++)
	{
		var icon_text = icons[i].firstChild.data;
		select_icon.length++;
		select_icon[index].value = icon_text;
		select_icon[index].text = icon_text;
		index++;
	}
}

function showTopic(topicID){
	// close all topics 
	nextOpen=(document.getElementById('topic_'+topicID).style.display=="none")?true:false;
	for (i=0;i < totalTopics; i++){
		document.getElementById('topic_'+i).style.display="none";
		document.getElementById('arrowButton_'+i).src='images/smallArrow.jpg';
	}	
	if (nextOpen){
		document.getElementById('topic_'+topicID).style.display="inline";
		document.getElementById('arrowButton_'+topicID).src='images/smallArrow_down.jpg';
	}    
}


/*----------------------------------------------------------------------------------------------------
New menu functions - Liran Oz 20/7/2008
----------------------------------------------------------------------------------------------------*/
//menu globals
var verticalMenuWidth = 150;
var horizontalMenuWidth = 150;
var mouseOutPadding = 15;

var bBox = null;
function BoundingBox(obj, left, top, width, height, mousePad)
{
    this.left = left;
    this.top = top;
    this.height = height;
    this.width = width;
    this.padding = mousePad;
    var menuObj = obj;
    
    this.IsOut = function(mouseX, mouseY)
    {
        //check if inside boudaries        
        if (mouseX >= this.left - this.padding && mouseX <= this.left + this.width + this.padding && mouseY >= this.top - this.padding && mouseY <= this.top + this.padding + this.height)
            return false;
        return true;
    }
    
    this.getLastItem = function()
    {
        return menuObj;
    }
    
    this.deactivate = function()
    {
        menuObj.style.display = 'none';
        menuObj.parentNode.setAttribute('className', '');
        menuObj.parentNode.setAttribute('class', '');
    }
    
}

function findPos(obj)
{
    var curleft = curtop = 0;

    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);

        return [curleft,curtop];
    }
}

//http://www.quirksmode.org
function mouseMove(e) {
    var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY)     {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY)     {
        posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
    
    if (bBox) {
        if (bBox.IsOut(posx, posy)) {
            bBox.deactivate();
            bBox = null;
        }
    }
}

function GetSubUL(obj)
{
    if (!obj || !obj.getElementsByTagName)
        return false;
    var ul = obj.getElementsByTagName('ul');
    if (!ul.length)
        return false;
    return ul[0];
}

function OpenVerticalMenu(obj)
{
    if (bBox) {
        var item = bBox.getLastItem();
        if (item.parentNode == obj)     //same one
            return;
    
        //ther's a menu already open, close it and continue
        var item = bBox.getLastItem();
        bBox.deactivate(); 
        bBox = null;
    }
    
    var ulObj = GetSubUL(obj);
    if (!ulObj || !obj.parentNode.offsetWidth)
        return false;
    
    //find out where to display it.
    var containerWidth = obj.parentNode.offsetWidth;
    var itemPos = findPos(obj);

    ulObj.style.top = itemPos[1] + 'px';
    ulObj.style.left = (itemPos[0] - verticalMenuWidth) + 'px';
    ulObj.style.width = verticalMenuWidth + 'px';
    
    obj.setAttribute('className', 'onFocus');
    obj.setAttribute('class', 'onFocus');
    ulObj.style.display = 'block';
    
    //set a new bounding box
    bBox = new BoundingBox(ulObj, itemPos[0] - verticalMenuWidth, itemPos[1], verticalMenuWidth + containerWidth, ulObj.offsetHeight, mouseOutPadding);
}

function OpenHorizontalMenu(obj)
{
    if (bBox) {
        var item = bBox.getLastItem();
        if (item.parentNode == obj)     //same one
            return;
        //ther's a menu already open, close it and continue
        var item = bBox.getLastItem();
        bBox.deactivate(); 
        bBox = null;
    }
    
    var ulObj = GetSubUL(obj);
    if (!ulObj || !obj.parentNode.offsetWidth)
        return false;
    
    //find out where to display it.
    var containerWidth = obj.offsetWidth;
    var containerHeight = obj.offsetHeight;
    var itemPos = findPos(obj);    
    
    ulObj.style.top = (itemPos[1] + containerHeight) + 'px';
    ulObj.style.left = (itemPos[0] - (horizontalMenuWidth - containerWidth)) + 'px';
    ulObj.style.width = horizontalMenuWidth + 'px';
    
    obj.setAttribute('className', 'onFocus');
    obj.setAttribute('class', 'onFocus');
    ulObj.style.display = 'block';
    //set a new bounding box
    bBox = new BoundingBox(ulObj, itemPos[0] - (horizontalMenuWidth - containerWidth), itemPos[1] + containerHeight, horizontalMenuWidth, ulObj.offsetHeight, mouseOutPadding);
}

function CloseOpenedMenu()
{
    if (bBox) {
        var item = bBox.getLastItem();
        //ther's a menu already open, close it and continue
        var item = bBox.getLastItem();
        bBox.deactivate(); 
        bBox = null;
    }
}
                                           

//Hook mouse listener
document.onmousemove = mouseMove;