var DOM = 0, MS = 0, NS = 0, OP = 0, isIE = 0, isMac = 0, isSafari = 0, isKonqueror = 0;
var drpObj = null;
var flag = 0, flag2 = 0;

function checkBrowser() {    	 	 
	 if(document.getElementById) DOM = 1;
	 else if (window.opera) OP = 1;
	 else if(document.all && !OP) MS = 1;
	 else if(window.netscape && window.screen && !DOM && !OP) NS = 1;
	 
	 var agt = navigator.userAgent.toLowerCase();  	 
	 if(agt.indexOf("msie") != -1) isIE = 1;
	 else if(agt.indexOf("safari") != -1) isSafari = 1;
	 else if(agt.indexOf("konqueror") != -1) isKonqueror = 1;
}

function setClass(elName, clName) {
    if(DOM) {
        var el = document.getElementById(elName);
        if(isIE) el.className = clName;
        else el.className = clName;    
    } else if (MS) {
        document.all[elName].classname = clName;
    }
}


function isEnter(e) {
    
    var keynum;

    if(window.event) {
      keynum = e.keyCode;
    } else if(e.which) {
      keynum = e.which;
    }

    if(keynum == 13) {
        return true;
    } else {
        return false;   
    }   
}

function clearInput(obj,def) {
  if(!def || obj.value == def) {
      obj.value = "";      
  }
  obj.style.color = "#000000";
}

function openDoc(sTarget, sWinName, sWidth, sHeight) {           
    var sProps = "width=" + sWidth + ",height=" + sHeight + ",left=0,top=0,scrollbars=yes";
    winRef = window.open(sTarget, sWinName, sProps);
    winRef.focus();
}

function openWin(sTarget, sProps) {                    
   winRef = window.open(sTarget, "new", sProps);
   winRef.focus();        
}

function doFreeCall(sAction, iDialogId, iRecptId, sAreaCode, sLocalCode) {
    
    var sUrl = "/servlet/sip/call?action=" + sAction + "&dialogId=" + iDialogId + "&recptId=" + iRecptId;
    
    if(sAreaCode && sLocalCode) {
       sUrl += "&areacode="+sAreaCode + "&localcode=" + sLocalCode;    
    }
   
    winRef = window.open(sUrl, "new", "width=735,height=350,left=0,top=0,resizable=no,scrollbars=yes,location=no,menubar=no");
    winRef.focus(); 
}

function setSelectIndex(sel, val) {
     for (i=0;i<sel.options.length;i++) { 	
        if (sel.options[i].value == val) {
           sel.selectedIndex = i;
           break;
        }
     }
}

function changeImage(obj,path) {obj.src=""+path;}

function storeCookie( name, value, expires, path) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );
        
    if ( expires ) {
    	expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    
    var str = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
    ( ( path ) ? ";path=" + path : "" );
   
    document.cookie = str;
}

function fetchCookie( 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 dropCookie( name, path) {
    if ( fetchCookie( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function getFormData(frm,disable) {
    
    var str = "";
	var el = frm.elements;
	for(i = 0; i < el.length; i++) {
				
		if(el[i].type == "text" || el[i].type == "textarea" || el[i].type == "hidden"  || el[i].type == "password") {
		   if(el[i].value.length > 0) {	   	
		   	  str = str + el[i].name + "=" +  encodeURIComponent(el[i].value) + "&";		   	  
		   }
		   if(disable) el[i].disabled = true;
		} else if(el[i].type == "checkbox" || el[i].type == "radio") {
		   if(el[i].checked) {
		     str = str + el[i].name + "=" + el[i].value + "&";		     
		   }
		   if(disable) el[i].disabled = true;
		} else if(el[i].type == "select-one") {
		  if(el[i].value.length > 0) {
		   	  str = str + el[i].name + "=" + el[i].value + "&";		   	  
		   }
		   if(disable) el[i].disabled = true;
		}		
	}
	
	return str;
}

function encode_utf8(rohtext) {	
	rohtext = rohtext.replace(/\r\n/g,"\n");
	var utftext = "";
	for(var n=0; n<rohtext.length; n++)	{		
		var c=rohtext.charCodeAt(n);		
		if (c<128) 
		  utftext += String.fromCharCode(c);	
		else if((c>127) && (c<2048)) {
			utftext += String.fromCharCode((c>>6)|192);
			utftext += String.fromCharCode((c&63)|128);
		} else {
			utftext += String.fromCharCode((c>>12)|224);
			utftext += String.fromCharCode(((c>>6)&63)|128);
			utftext += String.fromCharCode((c&63)|128);
		}
    }
	return utftext;
}

function decode_utf8(utftext) {
  var plaintext = ""; var i=0; var c=c1=c2=0;  
  while(i<utftext.length){
    c = utftext.charCodeAt(i);
    if (c<128) {
      plaintext += String.fromCharCode(c);
      i++;
    } else if((c>191) && (c<224)) {
      c2 = utftext.charCodeAt(i+1);
      plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
      i+=2;
    } else {
      c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
      plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
      i+=3;
    }
  }
  return plaintext;
}


// SUBNAVI //
function attachDocEvent() {
  document.onmousedown = function() {
    if(flag2 == 1 && drpObj.className != "ssd") {
        drpObj.className = drpObj.className.replace("over", "");
        if(flag == 0) {
            drpObj = null;
        }
        flag = 0;
    }
  }
}

function startDrpNav(obj) {
    
    if(flag == 0 && obj != drpObj) {
        obj.className="over";
        drpObj = obj;
        flag2 = 1;
        flag = 1;
    } else if(flag == 0 && obj == drpObj) {
        flag2 = 0;
        flag = 2;   
    } else if(flag == 1 && obj == drpObj) {
        flag2 = 1;
    } else if(flag == 2 && obj == drpObj) {
        obj.className="over";
        drpObj = obj;
        flag2 = 1;
    }  else if(flag == 2 && obj != drpObj) {
        obj.className="over";
        drpObj = obj;
        flag2 = 1;
    }
} 

function removeOver() {
  drpObj.className = drpObj.className.replace("over", "");
}

attachDocEvent();
checkBrowser();


function switch_list(cat) {
	
	var switcher = document.getElementById('plus_'+cat);
	var sublist = document.getElementById('cat_'+cat);
	
	if (sublist.style.display == 'none') {
		switcher.className = 'plus';
		sublist.style.display = 'block';
	} else {
		switcher.className = 'plus plus_minus';
		sublist.style.display = 'none';
	}
	
}

function switch_list_gar(cat) {
	
	var switcher = document.getElementById('plus_'+cat);
	var sublist = document.getElementById('cat_'+cat);
	
	if (switcher.className == 'plus') {
		switcher.className = 'plus plus_minus';
		sublist.style.display = 'block';
	} else {
		switcher.className = 'plus';
		sublist.style.display = 'none';
	}
	
}

function checkBox(id) {
	var id = document.getElementById(id);
	if (id.checked) {
		return true;
	}
}

function toggleClass(id, cssClass, input_id) {
	
	var id = document.getElementById(id);
	if (id.className == cssClass) {
		id.className = '';
	} else if (checkBox(input_id)) {
		id.className = '';
	} else {
		id.className = cssClass;
	}
	
}

// function fur Karte bei Branchen

function hide_layers(this_id) {
	var m = document.getElementById('myBody').childNodes;
    for (i=0; i<m.length; i++) {
    	if (m.item(i).id!=null && m.item(i).id!='' && m.item(i).id!=this_id) {
	    	var item = m.item(i).id;
				var i_id = document.getElementById(item);
	    	i_id.style.display='none';
    	}
    }
}

function switch_profile_edit(list) {
	if (list=='list1') {
		document.getElementById('list1').style.display='none';
		document.getElementById('list2').style.display='block';
	} else {
		document.getElementById('list1').style.display='block';
		document.getElementById('list2').style.display='none';
	}
}
