// $Author: ron $
// $Revision: 535 $
// $Date: 2010-09-01 12:50:37 +0200 (Wed, 01 Sep 2010) $

// this function pings the site to keep to session alive and valid
function ProlongSession()
{
	// touch the homepage without returning output, this prolongs the session
	TouchUrl('/global/admin/?no_output=true');
	
	// repeat this function every n minutes
	minutes = 5;
	setTimeout('ProlongSession()', minutes * 60 * 1000);
}

function getCookie( name )
{
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) 
	{
		return null;
	}

	if ( start == -1 ) return null;

	var end = document.cookie.indexOf( ";", len );

	if ( end == -1 ) end = document.cookie.length;

	return unescape( document.cookie.substring( len, end ) );

}


function deleteCookie( name, path, domain ) 
{
	
	if ( getCookie( name ) ) document.cookie = name + "=" +
	  ( ( path ) ? ";path=" + path : "") +
	  ( ( domain ) ? ";domain=" + domain : "" ) +
	  ";expires=Thu, 01-Jan-1970 00:00:01 GMT";

}

function setCookie( name, value, expires, path, domain, secure ) 
{

	var today = new Date();
	today.setTime( today.getTime() );
	
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie  = name+"="+escape( value ) +
	 ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
	 ( ( path ) ? ";path=" + path : "" ) +
	 ( ( domain ) ? ";domain=" + domain : "" ) +
	 ( ( secure ) ? ";secure" : "" );

}



/**
* @param sUrl, url for xml source file the graph is made on
* @param sDiv, the div id the graph needs to appear in
* @param iWidth, the width in pixels of the graph 
* @param iHeight, the height in pixels of the graph 
*/
function createGraph(sURL, sDiv, iWidth, iHeight, sScale)
{
	
	//alert('createGraph(' + sURL + ', ' + sDiv + ', ' + iWidth + ', ' + iHeight + ')');
	
	if(sScale == undefined)  sScale = 'noscale'; 
	if(iWidth == undefined)  iWidth  = 398;
	if(iHeight == undefined) iHeight = 375;

	document.getElementById(sDiv).innerHTML = AC_FL_RunContent(
		'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,45,0',
		'width', iWidth,
		'height', iHeight,
		'scale', sScale,
		'salign', 'TL',
		'bgcolor', '#ffffff',
		'wmode', 'opaque',
		'movie', '/global/interface/stats/charts',
		'src', '/global/interface/stats/charts',
		'FlashVars', 'library_path=/global/interface/stats/charts_library&xml_source='+sURL,
		'id', 'my_chart',
		'name', 'my_chart',
		'menu', 'true',
		'allowFullScreen', 'true',
		'allowScriptAccess','sameDomain',
		'quality', 'high',
		'align', 'middle',
		'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
		'play', 'true',
		'devicefont', 'false'
		); 
}

// embed an external url as a javascript head element
function EmbedScriptElement(url)
{
	var tag       = document.createElement("script");
	tag.type      = "text/javascript";
	tag.src       = url;
	document.getElementsByTagName("head")[0].appendChild(tag);
	window.status = '';
	return true;
}

function TouchUrl(url)
{
	var bogusImage = new Image();
	bogusImage.src = url;
}

function getElementsByClass(searchClass,node,tag) 
{ 
	var classElements = new Array(); 

	if ( node == null )  
		node = document; 

	if ( tag == null )  
		tag = '*'; 

	var els = node.getElementsByTagName(tag); 
	var elsLen = els.length; 
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"); 

	for (i = 0, j = 0; i < elsLen; i++) 
	{  
		if ( pattern.test(els[i].className) ) 
		{   
			classElements[j] = els[i];   
			j++;  
		} 
	} 
	
	return classElements;
}

//http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_substr_count/
function substr_count( haystack, needle, offset, length ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: substr_count('Kevin van Zonneveld', 'e');
    // *     returns 1: 3
    // *     example 2: substr_count('Kevin van Zonneveld', 'K', 1);
    // *     returns 2: 0
    // *     example 3: substr_count('Kevin van Zonneveld', 'Z', 0, 10);
    // *     returns 3: false
 
    var pos = 0, cnt = 0;
 
    if(isNaN(offset)) offset = 0;
    if(isNaN(length)) length = 0;
    offset--;
 
    while( (offset = haystack.indexOf(needle, offset+1)) != -1 ){
        if(length > 0 && (offset+needle.length) > length){
            return false;
        } else{
            cnt++;
        }
    }
 
    return cnt;
}

function HideElement(el)
{
	//if(debug) alert('HideElement(' + el + ')');
		
	if(document.getElementById(el))
		el = document.getElementById(el);

	el.style.display = 'none';

}

function ShowElement(el, displayType)
{
	//if(debug) alert('ShowElement(' + el + ', ' + displayType + ')');

	if(document.getElementById(el))
		el = document.getElementById(el);

	if(displayType == undefined )
	{
		el.style.display = 'block';
		return;
	}
		
	try 
	{	
		//alert('trying to display ' + el + ' like ' + displayType);
		el.style.display = displayType;
		return;
	}
	catch(error)
	{
		el.style.display = 'block';
		return;
	}
}

function StopPropagation(e)
{
	if (window.event) // if IE or Safari
		window.event.cancelBubble = true;
	else
		e.stopPropagation();
}

// popin for mailing_ROI
function showPopin(iMailingID)
{
	oPopIn = document.getElementById("popin");
	oPopIn.style.display='block';

	oFormID = document.getElementById("mailing_id");
	oFormID.value = iMailingID;

	iPopinWidth = oPopIn.clientWidth;
	iPopinHeight = oPopIn.clientHeight;

	iScreenWidth = document.body.offsetWidth;
	iScreenHeight = document.body.offsetHeight;

	oPopIn.style.top = ((iScreenHeight - (iPopinHeight * 2)) / 2)+'px';
	oPopIn.style.left = ((iScreenWidth - iPopinWidth) / 2)+'px';

	EmbedScriptElement("mailing_roi?act=getJSelements&mailing_id="+iMailingID);
}

function hidePopin()
{
	oPopIn = document.getElementById("popin");
	oPopIn.style.display='none';
}

function calcTotalProfit()
{
	iProfit = document.getElementById("opbrengsten_prijs").value.replace(",", ".") * document.getElementById("opbrengsten_aantal").value;
	document.getElementById("opbrengsten_totaal").value = iProfit;
}

function toggleMenuItem(className)
{
	var firstElement = $('.' + className + ' td:first');
	var element = $('.' + className + ' td');

	if(!firstElement.is(':hidden'))
	{
		$('.' + className + ' th').css('background-image','url(/global/interface/images/menu/leftmenu-closed.png)');
		element.hide();
	} else {
		$('.' + className + ' th').css('background-image','url(/global/interface/images/menu/leftmenu-open.png)');
		element.show();
	}
}

function toggle(id, checked)
{
	if(checked == true)
	{
		$('.' + id).show();
	}
	else
	{
		$('.' + id).hide();
	}
}

