//*************** functions for global using ********/
// initial screen size
function setScreenSize() {
	avlWidth=screen.availWidth;
	avlHeight=screen.availHeight;
	if (avlWidth>=1024) {
		window.resizeTo(1024,800);
	} else {
		window.resizeTo(avlWidth,avlHeight);
	}
}
sizeChangeCode="Schriftgr&ouml;&szlig;e: <img class=\"buttonSmall\" id=\"button_small\" onclick=\"change_size\('small'\);\" src=\"images/9/size_small.gif\" width=\"14\" height=\"14\" border=\"0\" alt=\"\"><img class=\"buttonLarge\" id=\"button_large\" onclick=\"change_size\('large'\);\" src=\"images/9/size_large.gif\" width=\"14\" height=\"14\" border=\"0\" />";

//  font size changing
function change_size(whatstyle,firsttime) {
 document.getElementById('small').disabled=true;
 document.getElementById('large').disabled=true;
 document.getElementById(whatstyle).disabled=false;
 eraseCookie('fontsize');
 createCookie('fontsize',whatstyle,'1');
// if (firsttime!='first_time') {set_size_button(whatstyle);}
}

function set_size_button(new_style) {

 /*document.getElementById('button_small').style.backgroundImage="url(<%img_buttonBackground%>)";
 document.getElementById('button_large').style.backgroundImage="url(<%img_buttonBackground%>)";
 document.getElementById('button_'+new_style).style.backgroundImage="url(<%img_buttonBackgroundX%>)";
 */

}

var current_style;
function set_size_first_time() {
 if (!readCookie('fontsize')) {
   document.getElementById('small').disabled=false;
   current_style='small';
 } else {
   current_style=readCookie('fontsize')
   change_size(current_style,'first_time');
 }
}
set_size_first_time();

// Cookie handling

function createCookie(name,value,hours)
{
 if (hours)
 {
  var date = new Date();
  date.setTime(date.getTime()+(hours*60*60*1000));
  var expires = "; expires="+date.toGMTString();
 }
 else var expires = "";
 document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
 var nameEQ = name + "=";
 var ca = document.cookie.split(';');
 for(var i=0;i < ca.length;i++)
 {
  var c = ca[i];
  while (c.charAt(0)==' ') c = c.substring(1,c.length);
  if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
 }
 return null;
}
function eraseCookie(name)
{
 createCookie(name,"",-1);
}

function ppup(width,height){
	popup=window.open('','popup','scrollbars=yes,width='+width+',height='+height+',locationbar=0,menubar=0,resizable=0,status=0');
	popup.focus();
}

function checkEmail(email) {
	var usr = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
	var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
	var regex = "^"+usr+"\@"+domain+"$";
	var myrxp = new RegExp(regex);
	var check = (myrxp.test(email));
	if (check!=true){
		alert("Bitte geben Sie eine gültige Email-Adresse ein.");
		return false;
	}
	return true;
}
//*************** end of global functions ********/

function checkSelectValue(control, value)
{	
	for(i=0; i<control.options.length; i++)
	{
		if(control.options[i].value==value)
		{
			control.options[i].selected=true;
			break;
		}
	}
}

function checkCategory(control,id)
{	
	if(control.options[control.selectedIndex].text=='Handwerk') 
	{		
		setClassName(id, 'red')
	}
	else
	{
		setClassName(id, 'white')
	}
}

function setClassName(id, className)
{
	document.getElementById(id).className=className;
}

function completeDate(control)
{
	control.value=control.value.replace(/\s/g,'');
	// vereinfachte Datumseingabe: 101104 => 10.11.2004
	if (control.value.indexOf(".") == -1) // Angabe darf noch keinen Punkt enthalten
	{
		if ((control.value.length==6) || (control.value.length==8)) // entweder 101104 oder 10112004
		{
			var dd = control.value.substr(0,2);
			if(parseInt(dd,10)==0)
			{
				dd="01";
			}
			var mm = control.value.substr(2,2);
			if(parseInt(mm,10)==0)
			{
				mm="01";
			}
			var yy = control.value.substr(4,4);
			//alert (dd + " " + mm + "  " + yy);
			
			if (isNaN(yy) == false)
			{
				if (yy.length == 2)
				{
					if (parseInt(yy,10) < 20)
					{
						yy = parseInt(2000+parseInt(yy,10),10);					
					}
					else
					{
						yy = parseInt(1900+parseInt(yy,10),10);
					}
				}
				control.value=dd+"."+mm+"."+yy;
			}
		}
	}
	else if(control.value.match(/\./g).length==2)  // im  Datum müssen zwei '.' enthalten sein	
	{				
		var datum=control.value.split(".");
		var dd=datum[0];
		var mm=datum[1];
		var yy=datum[2];
		if(dd.length<2)
		{
			dd="0"+dd;
		}
		if(parseInt(dd,10)==0)
		{
			dd="01";
		}
		if(mm.length<2)
		{
			mm="0"+mm;
		}
		if(parseInt(mm,10)==0)
		{
			mm="01";
		}
		if (isNaN(yy) == false)
		{
			if (yy.length <= 2)
			{
				if (parseInt(yy,10) < 20)
				{
					yy = parseInt(2000+parseInt(yy,10),10);					
				}
				else
				{
					yy = parseInt(1900+parseInt(yy,10),10);
				}
			}
			control.value=dd+"."+mm+"."+yy;
		}		
	}
	if(control.value.length!=10)
	{
		control.focus();
	}
}

function charCounter( countElement, inLength, max )
{	
	countElement.value = (max-inLength);
}

function copySelectedText(from, to, filter)
{
	if(form.options[from.selectedIndex].value != filter)
	{
		to.value=from.options[from.selectedIndex].text;	
	}
	else
	{
		to.value='';
	}	
}
