/* setVisibility(objId, sVisibility)
* Parameters:
* objId - the id of an element (case sensitive)
* sVisibility - "visible" | "inherit" | "none" (case insensitive)
*
* USAGE:
* Show div1:
* onclick='setVisibility("div1","visible")'
*
* Inherit the visibility of div2's parentNode:
* onblur='setVisibility("div2", "inherit")'
*
* Hide span3:
* onblur='setVisibility("span3", "hidden")'
*/


   function setVisibility(objId, sVisibility) {

        var obj = document.getElementById(objId);

         obj.style.visibility = sVisibility;
      }

   function setDisplay(objId, sVisibility) {

        var obj = document.getElementById(objId);

         obj.style.display = sVisibility;
      }

   function cbToggleDisplayOff(obj_checkbox, objId, toggleTo) {

        var obj = document.getElementById(objId);

   	  if(obj_checkbox.checked)
	  {
	    obj.style.display = 'none';
	  }
	  else
	  {
	    obj.style.display = toggleTo;
	  }
   }

   function cbToggleDisplayOn(obj_checkbox, objId, toggleTo) {

        var obj = document.getElementById(objId);

   	  if(obj_checkbox.checked)
	  {
	    obj.style.display = toggleTo;
	  }
	  else
	  {
  	    obj.style.display = 'none';
	  }
    }

    function reallyDeleteCustomer(id) {
        if (confirm('Are you sure you wish to this client?')) {
		      window.location = 'module.php@mod=inv_ship&action=delcustomer&id=.html
    {
        document.getElementById(formId).submit();
    }

    function submitCheckedForm(formId)
    {
        var theForm = document.getElementById(formId);

        if(theForm.onsubmit())
        {   //this check triggers the validations
            theForm.submit();
         }
    }
	
	function applyCheckedForm(formId)
    {
        var theForm = document.getElementById(formId);

        if(theForm.onsubmit())
        {   //this check triggers the validations
			newField = document.createElement("input");
			newField.type = "hidden";
			newField.name = "APPLY";
			newField.value = "true";
			
			theForm.appendChild(newField);
				
            theForm.submit();
         }
    }

    function deleteConfirm(theType,theLink)
    {
        if (window.confirm("Are you sure you want to delete this "+theType+"?")) {
            window.location=theLink;
        }
    }

    function restoreConfirm(theType,theLink)
    {
        if (window.confirm("Are you sure you restore the default settings for "+theType+"?")) {
            window.location=theLink;
        }
    }


/* EMAIL CAMPAIGN TOTAL SIZE 1 */
    var emailCampaignTotalSize = 0;

    function emailCampaignUpdateTotal(obj_checkbox,thesize) {


   	  if(obj_checkbox.checked)
	  {
	    emailCampaignTotalSize = emailCampaignTotalSize + thesize;
	  }
	  else
	  {
	    emailCampaignTotalSize = emailCampaignTotalSize - thesize;
	  }

	  writeEmailCampaignTotalSize();

    }

    function writeEmailCampaignTotalSize()
    {
        var obj = document.getElementById('emailCampaignSizeDiv');
	       obj.innerHTML = emailCampaignTotalSize+" kB";
    }

/* EMAIL CAMPAIGN TOTAL SIZE 1 */
    var emailCampaignTotalSize2 = 0;

    function emailCampaignUpdateTotal2(obj_checkbox,thesize) {


   	  if(obj_checkbox.checked)
	  {
	    emailCampaignTotalSize2 = emailCampaignTotalSize2 + thesize;
	  }
	  else
	  {
	    emailCampaignTotalSize2 = emailCampaignTotalSize2 - thesize;
	  }

	  writeEmailCampaignTotalSize2();

    }

    function writeEmailCampaignTotalSize2()
    {
        var obj = document.getElementById('emailCampaignSizeDiv2');
	       obj.innerHTML = emailCampaignTotalSize2+" kB";
    }


    function writeEmailCampaignTotalSizes()
    {
        writeEmailCampaignTotalSize();
        writeEmailCampaignTotalSize2();
    }

    function emailCampaignTotalSizeWarning(theForm)
    {
        if (emailCampaignTotalSize > 550 || emailCampaignTotalSize2 > 550) {
            var msg = '';

            msg = "The following size warnings have occured with the selected email campaigns:\n"

            if (emailCampaignTotalSize > 550 ) msg += " - The office email has a total size of "+emailCampaignTotalSize+" kB\n";
            if (emailCampaignTotalSize2 > 550 ) msg += " - The warehouse email has a total size of "+emailCampaignTotalSize2+" kB\n";
            msg += "\nDo you wish to continue?";

            if (!confirm(msg)) {
              return false;
            }
        }
        return validateForm( theForm, 0, 1, 0, 1, 8 );
    }

    var tabPane;

    function addSection(arrayOfForms)
    {
        if (typeof tabPane != "undefined" )
        {

            submitform(arrayOfForms[tabPane.getSelectedIndex()]);
        }
    }

    function hideLoading(){
    	load = document.getElementById('loadingDiv').style.display = 'none';
    }
    function showLoading(namespace) {
		place = document.getElementById(namespace);

		ie = false;
		if (document.all){
			w = place.offsetWidth;
			h = place.offsetHeight;
			ie = true;
		} else {
			w = place.clientWidth;
			h = place.clientHeight
		}

		x = findPosX(place);
		if (ie) x += w/16;
		else x += w/4;

		y = findPosY(place);
    	if (ie) y += h/8;
		else y += h/4;

		if(h<120) h=120;
    	load = document.getElementById('loadingDiv');

    	//alert(x + ', ' + y);

    	load.style.height = h/2 + 'px';
    	load.style.width = w/2 + 'px';
    	load.style.display = 'block';
    	load.style.left = x + 'px';
    	load.style.top = y + 'px';
    }
/*********************************
	HANDLE DYNAMIC JS INCLUSIONS
 *********************************/
/**
 * Holds a list of files inluded via include_once
 */
var included_files = new Array();
/**
 * Includes a js file once - use exactly like PHP include_once
 *
 * @param string script_filename - full path from directory used
 */
function include_once(script_filename) {
    if (!in_array(script_filename, included_files)) {
        included_files[included_files.length] = script_filename;
        include_dom(script_filename);
    }
}
/**
 * Adds a js file to the HEAD via DOM . Usually called by include_once
 *
 * @param string script_filename - full path from directory used
 */
function include_dom(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}


/*********************************************
	LINK BORDER REMOVAL
 *********************************************/
// removes the border from IE not FF
function removeFocusOnAllLinks(){
for(var i=0 ; i < document.links.length ; i++)
document.links.onfocus=blurLink;
}

function blurLink() {
if (this.blur) this.blur();
}

window.onload=removeFocusOnAllLinks;
/*********************************************/


/*********************************************
		FADING MSG BOX
 *********************************************/
var msg_currFadeValue = 100;
var msg_fadeStep = 0;
var msg_fadeTimer = 0;
var msg_mb = null
function fade(divID, msg, displayTime, fadeTime) {
	// set the msg text
	msg_mb = document.getElementById(divID);
	msg_mb.innerHTML = msg;
	
	//get the window size
	w = getWindowSize('w');
	h = getWindowSize('h');
	
		
	msg_mb.style.position = 'absolute';
	msg_mb.style.width = w/2 + 'px'; // half the window
	msg_mb.style.left = w/4 + 'px'; // a quarter of the way in
	msg_mb.style.top = '300px'; 
	msg_mb.style.padding = '10px 5px';
	
	setOpacity(msg_mb, 100)
	msg_mb.style.display = 'block';
	
	// the fader uses steps of 50 ms 
	// 100 is full opacity
	msg_fadeStep = 100/(fadeTime*1000/50);
	msg_fadeTimer = fadeTime;
	
	setTimeout('fadeTimer = window.setInterval(processFade, 50)', displayTime*1000) ; 
}

function processFade(){
	if  (msg_currFadeValue <= 0){
		//if we are totally invisible lets get out of here
		msg_mb.style.display = 'none';
		clearInterval(fadeTimer);
		return;
	}
	
	//reduce the opacity
	msg_currFadeValue -= msg_fadeStep;
	setOpacity(msg_mb, msg_currFadeValue)
	
}
function setOpacity(obj, value){
	if (obj.style.MozOpacity) // netscape
		obj.style.MozOpacity = value/100;
	else if (obj.filters && msg_mb.filters[0]){
		if (typeof obj.filters[0].opacity == "number") //if IE6+
			obj.filters[0].opacity = value;
		else //else if IE5.5-
			obj.style.filter = "alpha(opacity="+value+")";
	}
	else if (obj.style.KhtmlOpacity) //konqourer
		obj.style.KhtmlOpacity=value/100;
	else // firefox
		obj.style.opacity = value/100; //CSS3
}

/***************************************
		HELPER FUNCTIONS
****************************************/
/**
 * Finds the X position of the given element
 * 
 * @param html_element obj
 * @return int
 */
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

/**
 * Finds the Y position of the given element
 * 
 * @param html_element obj
 * @return int
 */
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
/**
 * Gets the current browser window width/height
 * 
 * @param char dim - 'w' for width, 'h' for height
 * @return int
 */
function getWindowSize(dim) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  if(dim == 'w') return myWidth;
  else return myHeight;
}
/**
 * simulates PHP in_array
 * 
 * @param mixed needle - item to search for
 * @param array haystack
 * @return bool
 */
function in_array(needle, haystack) {
    for (var i = 0; i < haystack.length; i++) {
        if (haystack[i] == needle) {
            return true;
        }
    }
    return false;
}
function checkAllSame(field, form){
	for (i = 0; i < form.length; i++)
	form[i].checked = field.checked ;
}

popImage_PositionX = 100;
popImage_PositionY = 100;

popImage_defaultWidth  = 1;
popImage_defaultHeight = 1;

var popImage_AutoClose = true;

if (parseInt(navigator.appVersion.charAt(0))>=4){
	var popImage_isNN=(navigator.appName=="Netscape")?1:0;
var popImage_isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}

var popImage_optNN='scrollbars=no,width='+popImage_defaultWidth+',height='+popImage_defaultHeight+',left='+popImage_PositionX+',top='+popImage_PositionY;
var popImage_optIE='scrollbars=no,width=150,height=100,left='+popImage_PositionX+',top='+popImage_PositionY;

function popImage(imageURL, imageTitle){
	if (popImage_isNN){
		imgWin=window.open('about%253Ablank.html','',popImage_optNN);
	}
	if (popImage_isIE){
		imgWin=window.open('about%253Ablank.html','',popImage_optIE);
	}
	with (imgWin.document){
		writeln('<html><head><title>'+imageTitle+'</title><style>body{margin:0px;padding:0px}</style>');
		writeln('<sc'+'ript>');
		writeln('var popImage_isNN,popImage_isIE;');
		writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
		writeln('popImage_isNN=(navigator.appName=="Netscape")?1:0;');
		writeln('popImage_isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
		writeln('function reSizeToImage(){');
		writeln('if (popImage_isIE){');
		writeln('window.resizeTo(100,100);');
		writeln('width=100-(document.body.clientWidth-document.images[0].width);');
		writeln('height=100-(document.body.clientHeight-document.images[0].height);');
		writeln('window.resizeTo(width,height);}');
		writeln('if (popImage_isNN){');       
		writeln('window.innerWidth=document.images[0].width;');
		writeln('window.innerHeight=document.images[0].height;}}');
		writeln('</sc'+'ript>');
		if (!popImage_AutoClose) 
			writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();self.focus()">')
		else 
			writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();self.focus()" onblur="self.close()">');
		writeln('<img name="image" src='+imageURL+' style="display:block"></body></html>');
		close();		
	}
}
function HTMLEncode(t) {
	var t = t.toString();
	var h = new Array("&","\"","<",">");
	var d = ""; var hl = h.length;
	var e = new Array("&amp;","&quot;","&lt;","&gt;")
	if (t) {
		for (var i=0; i<t.length; ++i) {
			var c = t.charAt(i);
			var r = 0;
			for (j=0; j<hl; ++j) {
				if (c == h[j]) {
					d += e[j]; r = 1; break;
				}
			}
			if (!r) d += c;
		};
	};
	return d;
};





