
/*************************** Initialization *****************************/

var root = location.protocol + '//' + location.host + '/';

var opera = (navigator.userAgent.indexOf('Opera') >= 0)? true : false;
var ie    = (document.all && !opera)? true : false;
var dom   = (document.getElementById)? true : false;

var clWidth = (ie) ? 'document.body.clientWidth' : 'window.innerWidth';
var clHeight = (ie) ? 'document.body.clientHeight' : 'window.innerHeight';

//not used 
//window.onclick = HideAllWins;




function gotoUrl( url ){
	
	if( url != "" ){
		
    	document.location = url;
    }
}


function reloadWindow(){
	
	document.location.reload();
}


function Redirect( url ){
	
	window.location.href = url;
}

/**
 * A dollar sign defines jQuery, if used !
 * 
 * @param v
 * @return
 */
function $$( v ) { 
	
	if( document.getElementById(v) ){
	
		return( document.getElementById(v) )
	} 

	return false;
}

function ShowImg( id ){
	
	 url = 'viewimg.php';
	 ow('/viewimg.php?id='+id+'&sw='+clWidth+'&sh='+clHeight, 'display', 280, 120, true);
}

function ShowItem( id, type, mess ){
	
	if( confirm( mess ) ){
	
	 	url = 'viewitem.php';
	 	ow('/viewitem.php?id='+id+'&type='+type+'&sw='+clWidth+'&sh='+clHeight, 'display', 280, 120, true);	
	}
}

/**
 * 
 * @param url
 * @param string name
 * @param int w
 * @param int h
 * @param boolean center
 * @param boolean scrollbars
 * @return
 */
function ow(url, name, w, h, center, scrollbars) {
	
    if(url) url = unescape(url);
    
    eval('if(typeof win_'+name+' != \'undefined\') win = win_'+name+'; else win = false;');
    
    if(!win || win.closed) {
    	
        xy = '';
        
        if(center) {
        	
            x  = (screen.availWidth-w)/2;
            y  = (screen.availHeight-h)/2;
            xy = ',top='+y+',left='+x;
        }
        
        win = open(url, name, 'toolbar=0,status=0,resizable=0,scrollbars='+(scrollbars?1:0)+',width='+w+',height='+h+xy);
        eval('win_'+name+' = win;');
    }
    else
    {
        win.focus();
        
        if( url && win.location.pathname + win.location.search != url){
        	
        	win.location = url;
        }
    }
}

function wclose() {
	
	window.close(); 
}


function DigitsOnly( evt, obj, point ) {
	
	    var keycode;
	 
	    if (evt)
	        ;
	    else if (window.event)
	        evt = window.event;
	    else if (event)
	        evt = event;
	    else
	        return true;
	 
	    if (evt.charCode)
	        keycode = evt.charCode;
	    else if (evt.keyCode)
	        keycode = evt.keyCode;
	    else if (evt.which)
	        keycode = evt.which;
	    else
	        keycode = 0;
	        
	 
		if ((keycode == null) || ( keycode == 8 ) || ( keycode == 9 ) ){
		
			return true;        
		}
		
		if( point && ( keycode == 46 ) ){
			
			return true;
		}
	        
	    if( keycode >= 48 && keycode <= 57 ){
	    	
	    	return true;
	    	
	    }
	    
	    return false;
}

function showLoading(){
	

	pos = getWindowSize();
	
	x = pos[0]/2 - 5;
	y = pos[1]/2 - 10;
	
	SetElementPositionX('load', x );
	SetElementPositionY('load', y );
	
	SetElementDisplay('load', 'block');
}	
	
function shoeLoadingPosition( obj ){
	
	x = findPosX( obj ) + 130;
	y = findPosY( obj );
	
	SetElementPositionX('load', x );
	SetElementPositionY('load', y );
	
	SetElementDisplay('load', 'block');	
}


function hideLoading(){

	SetElementDisplay('load', 'none');
}	


function hideMessagesDiv(){
	
	SetElementDisplay('messages_div', 'none');
}

//seo urls
function ChangeLang(ln){
	
	//domain
	var url = root;
	
	//url like this /ln/[string/]?action=1&id=2
	var path_query = location.pathname + location.search;
	path_query = path_query.substr(4);	
	

	
	if( ln ){
		
		url = url + ln + '/' + path_query;
		Redirect(url);
	}

}


function ChangeCurr( obj, params ){
	
	if( params == undefined || params == '' ){
		
		params = '';
	}
	
	url = 'index.php?' + params;
	
	if( obj && obj.id && obj.value ){
		
		url += obj.id + '=' + obj.value;
		Redirect( url );
	}	
}


function RedirectParent( url ){
	
	window.parent.location.href = url;
}

function GoBack(){
	
	history.back();
}

function RemoveElement(id){
	
	if( document.getElementById(id) )
		document.getElementById(id).parentNode.removeChild( document.getElementById(id) );
}

function ShowMessage(message){
	
	UnblockAll();
	hideLoading();
	
	alert( message );
}

function limitText(limitField, limitNum) {

	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} 
	
}

function UpdateElementContent( elId, val ){ 
	
	if( document.getElementById(elId) ){
		
		document.getElementById(elId).innerHTML = val;
	}
}

function UpdateAjaxContent( elId ){ 
	
	if( content && $$(elId) ){
		
		$$(elId).innerHTML = content;
	}
}

function UpdateAjaxContentSecond( elId ){ 
	
	if( content_second && $$(elId) ){
		
		$$(elId).innerHTML = content_second;
	}
}


function UpdateImageSrc( imgId, val ){ 
	
	if( document.getElementById(imgId) ){
		
		document.getElementById(imgId).src = val;
	}
}


function UpdateElementValue( elId, val ){ 
	
	if( document.getElementById(elId) ){
		
		document.getElementById(elId).value = val;
	}
}

function AppendElement(elId, childId){
	
	if( document.getElementById(elId) && document.getElementById(childId) ){
		
		document.getElementById(elId).appendChild(document.getElementById(childId));
	}
}

function AddContentToHiddenDiv(){
	
	if( document.getElementById('hiddenDiv') ){
		
		document.getElementById('hiddenDiv').innerHTML = content;
	}
}

function SetElementDisplay( elId, style ){
	
	if( document.getElementById(elId) ){
		
		document.getElementById(elId).style.display = style;
	}
}

function SetElementPositionX( elId, x ){
	
	if( document.getElementById(elId) ){
		
		document.getElementById(elId).style.left = x + 'px';
	}	
}

function SetElementPositionY( elId, y ){
	
	if( document.getElementById(elId) ){
		
		document.getElementById(elId).style.top = y + 'px';
	}	
}

function GetElementValueById( elId ){
	
	if( document.getElementById(elId) && 
		( document.getElementById(elId).type == 'input' || document.getElementById(elId).type == 'select-one' || document.getElementById(elId).type == 'select-multiple' ) ){
		
		return document.getElementById(elId).value;
	}	
	
	return '';
}

function GetRadioValueByName( elName ){
	
	if( document.getElementsByName(elName) ){ 
		
		e = document.getElementsByName(elName);
		
		for (var i=0; i < e.length; i++ ){
			
		   	 if ( e[i].checked ){
		   	  	
		      	return e[i].value;
		     }
	    }		
	}
	
	return '';
}


function GetCheckboxStatusById( elId ){
	
	if( document.getElementById(elId) ){
		
		if( document.getElementById(elId).checked == true ){
			
			return 1;
		}
		else{
			return 0;
		}
	}	
}



function UpdateReportContent( htmlId, active, statusId, activeVal ){
	
		var width;
	
		if( document.getElementById(htmlId) && document.getElementById(htmlId).parentNode ){
			
			width = document.getElementById(htmlId).parentNode.offsetWidth;
		}
	
		UpdateElementContent( htmlId, active );
		UpdateElementContent( statusId, activeVal );
		
		if( document.getElementById(htmlId) && document.getElementById(htmlId).parentNode ){
			
			document.getElementById(htmlId).parentNode.style.width = width - 10 + 'px';
		}		
	
}


function SetParentElementSize( elId, width, height){
	
	if( document.getElementById(elId) && document.getElementById(elId).parentNode ){
		
		el = document.getElementById(elId).parentNode;
		
		if( width != '' ){
		
			el.style.width = width;	
		}

		if( height != '' ){
		
			el.style.height = height;	
		}		
		
	}
}


function changeColor(obj, color){
	
	if( obj && color != ''){
		
		obj.style.color = color;
	}
}

function changeBgColor(obj, color){
	
	if( obj && color != ''){
		
		obj.style.backgroundColor = color;
	}
}

function SetElementHref( elId, val ){ 
	
	if( document.getElementById(elId) ){
		
		document.getElementById(elId).href = val;
	}
}


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;
   }


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;
	}
	
	
function getScrollXY() {
	
  var scrOfX = 0, scrOfY = 0;
  
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}	


function getWindowSize() {

	var myWidth = 0, myHeight = 0;
	
	var scrollCords = getScrollXY();
  
	if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth - 17;
	    myHeight = window.innerHeight - 17;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth - 2;
	    myHeight = document.documentElement.clientHeight - 2;
	 } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth - 2;
	    myHeight = document.body.clientHeight - 2;
	 }
	 
	 myWidth += scrollCords[0];
	 myHeight += scrollCords[1];
	
	 return [ myWidth, myHeight ];
}


/*
function ShowLoading(){
	
	SetElementDisplay('loading', 'block');
	
	var loading = document.getElementById( 'loading' );
	var hider = document.getElementById( 'hider' );

	if( hider && loading ){
		
			hider.style.top = 250 + 'px';
			hider.style.left = 400 + 'px';
			hider.style.width = loading.offsetWidth + 'px';
			hider.style.height = loading.offsetHeight + 'px';
			hider.style.display = 'block';	
	}
}
*/

function AddComboValues(inputId, selectId){

	var inp = document.getElementById( inputId );
	var sel = document.getElementById( selectId );

	var newOption = new Option(inp.value, inp.value);
	sel[sel.length] = newOption;
}




function HideAllWins(){
	
	if( $$('contact_div') ){
		
		$$('contact_div').style.display = 'none';
	}
}


function printPage( type ){
    
	window.open('/print.php?type='+type,'printer','width=600,height=800,scrollbars=yes');
}

function printWeeklyMenu( categoryid, week ){
    
	window.open('/print.php?type=weekly&categoryid='+categoryid+'&week='+week,'printer','width=600,height=800,scrollbars=yes');
}

function printDailyMenu( day ){
    
	window.open('/print.php?type=daily&day='+day,'printer','width=600,height=800,scrollbars=yes');
}

function loadContent( id ){
	
	if( window.opener.document.getElementById(id) ){
	
		var content = window.opener.document.getElementById(id).innerHTML;

		document.getElementById("menus").innerHTML = content;
		
		window.print();	
	}	
	
}

/*function noact() {
	return false;
}*/


