var Tage_pro_Monat = new Array (31,28,31,30,31,30,31,31,30,31,30,31);
var WochenTage     = new Array ("Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag","Sonntag");
var lastUser = 0;
var NoReset = true;

function BrowserType() {
	if(document.ids)x='nc4';
	else if( document.all && !document.getElementById )x='ie4';
	else if( window.opera && !document.createElement )x='op5';
	else if( window.opera && window.getComputedStyle )  {
	          if(document.createRange)x='op8';
	            else if(window.navigate)x='op7.5';
	                             else x='op7.2';                   }
	else if( window.opera && document.compatMode )x='op7';
	else if( window.opera && document.releaseEvents )x='op6';
	else if( document.contains && !window.opera )x='kq3';
	else if( window.pkcs11&&window.XML)x='f15';
	else if( window.getSelection && window.atob )x='nn7';
	else if( window.getSelection && !document.compatMode )x='nn6';
	else if( window.clipboardData && document.compatMode )
	  x=window.XMLHttpRequest? 'IE7' : 'IE6';
	else if( window.clipboardData ){x='ie5';
	     if( !document.createDocumentFragment ) x+='.5';
	     if( document.doctype && !window.print ) x+='m';}
	else if( document.getElementById && !document.all ) x='op4';
	else if( document.images && !document.all ) x='nn3';
	else if(document.clientWidth&&!window.RegExp)x='kq2';
	else x='???';
	return x;
}

function ist_Schaltjahr(jahr) {
   if (jahr % 4 == 0)
      if (jahr < 1582)
         return 1;
      else if (!(jahr % 100 == 0))
              return 1;
           else if (jahr % 400 == 0)
                   return 1;
   return 0;
}

function current_year(year) {
  if (year < 1900)
     year = year + 1900;
  return year;
}

function addMonth(initialString,wert) {
  var dat = new Date();
  var l_dd   = parseInt(dat.getDate());
  var l_mm   = parseInt(dat.getMonth()+1);
  var l_jahr = parseInt(current_year(dat.getYear()));
  var multiplicator = Math.round(wert / Math.abs(wert));

  if (initialString!='') {
  	if (initialString.substr(0, 1)=='0')
  		l_dd = parseInt(initialString.substr(1, 1));
  	else
  		l_dd = parseInt(initialString.substr(0, 2));
  	if (initialString.substr(3, 1)=='0')	
  		l_mm = parseInt(initialString.substr(4, 1));
  	else
  		l_mm = parseInt(initialString.substr(3, 2));
  	l_jahr = parseInt(initialString.substr(6, 4));
  }

	l_jahr += Math.floor((l_mm - 1 + wert) / 12);
	l_mm   = (l_mm + wert) % 12;
	l_mm	 = (l_mm==0) ? 12 : l_mm;
 
  Tage_pro_Monat[1] = 28 + ist_Schaltjahr(l_jahr);
  if (l_dd > Tage_pro_Monat[l_mm-1])
     l_dd = Tage_pro_Monat[l_mm-1];

	result=(l_dd <= 9 ? "0"+String(l_dd) : String(l_dd)) + "." + 
		(l_mm <= 9 ? "0"+String(l_mm) : String(l_mm)) + "." + 
		 l_jahr;
  return result;
}

function WriteinDiv(id,text) {
	bt=BrowserType();
	if (bt=='IE7' || bt=='IE6' || bt=='f15')
		document.getElementById(id).innerText = text;
	else if (bt=='f15') {
		with (document[id].document) {
  			open();
  			write(text);
  			close();
		}
	}
}

function WriteHTMLinDiv(id,text) {
	bt=BrowserType();
	if (bt=='IE7' || bt=='IE6' || bt=='f15')
		document.getElementById(id).innerHTML = text;
	else if (bt=='f15') {
		with (document[id].document) {
  			open();
  			write(text);
  			close();
		}
	}
}

function UpdateRecordValues(user_id) {
	if (NoReset && lastUser>0 && lastUser!=user_id) {
		NoReset=false;
		document.forms["form_" + lastUser].reset();
		UpdateRecordValues(lastUser);
		NoReset=true;
	}
	lastUser=user_id;
	if (document.getElementById('div_Value_'+user_id)) {
		Value=parseInt(document.getElementById('CB_Duration_'+user_id).value);
		ExpiredDB='';
		if (document.getElementById('input_expiredDB_'+user_id))
			ExpiredDB=document.getElementById('input_expiredDB_'+user_id).value;
		if (Value>0) {
			WriteHTMLinDiv('div_Value_'+user_id,(Math.round(Value*2 * (100-Value*1.2)/100)).toFixed(2)+' &euro;');
			WriteHTMLinDiv('div_Expired_'+user_id,addMonth(ExpiredDB,Value));
		} else {
			WriteHTMLinDiv('div_Value_'+user_id,'');
			WriteHTMLinDiv('div_Expired_'+user_id,ExpiredDB);
		}
		if (document.getElementById('div_link_activate_'+user_id)) {
			if (Value>0)
				WriteHTMLinDiv('div_link_activate_'+user_id,
							   '<a id="link_activate_' + user_id + '" class="edit" href="javascript:Execute_Update(' + user_id + ')">Freischalten</a>');
				//document.getElementById('link_activate_'+user_id).firstChild.nodeValue="Update";
			else
				WriteHTMLinDiv('div_link_activate_'+user_id,'');
				//document.getElementById('link_activate_'+user_id).firstChild.nodeValue="Aktualisieren";
		}
	}
}

function Execute_Update(user_id) {
	expired=document.getElementById('input_expiredDB_'+user_id).value;
	month=parseInt(document.getElementById('CB_Duration_'+user_id).value);
	expired=addMonth(expired,month);
	document.getElementById('input_expiredDB_'+user_id).value=expired;
	document.forms["form_" + user_id].submit();
}

function setOptimalImageSize(ImageObject) {
	if (ImageObject) {
		width  = ImageObject.width;
		height = ImageObject.height;
		
		ImageObject.removeAttribute("height", 0);
		ImageObject.removeAttribute("width", 0);
	
		org_width  = ImageObject.offsetWidth;
		org_height = ImageObject.offsetHeight;
		
		rat = org_width / org_height;
		
		new_width  = height * rat;
		new_height = height;
		if (new_width>width) {
			new_height = width / rat;
			new_width=width;
		}
		
		ImageObject.setAttribute("height", Math.round(new_height));
		ImageObject.setAttribute("width", Math.round(new_width)); 
		
		if (new_width<width) {
			ImageObject.hspace=Math.round((width-new_width) / 2);
		}
		if (new_height<height){
			ImageObject.vspace=Math.round((height-new_height) / 2);
		}
	}
}
