function selectRadio(theform,nr,id)
{
	//alert('coupon form: ' + theform.motive[nr].value);
	if (!theform.motivepath[nr].disabled)
	{
		theform.motivepath[nr].checked = true;
		theform.motiveid.value = id;
	}
}


function changeElementStatus(theform,status)
{
	//screen thru every element in the form, and hunt down "submit" and "reset"
	for (i=0;i<theform.length;i++)
	{
		var tempobj = theform.elements[i];
		//if (tempobj.type.toLowerCase() == "radio" && tempobj.name == "motiveid")
		if (tempobj.type.toLowerCase() != "submit")
		{
			//alert(tempobj.type.toLowerCase()+' / '+tempobj.name+' / '+tempobj.value);
			tempobj.disabled = status;
		}
	}
}


function getSelectedAmount(theform)
{
	for (i=0;i<theform.length;i++)
	{
		var tempobj = theform.elements[i];
		var amount = '';
		if (tempobj.type.toLowerCase() == "radio" && tempobj.name == "value[amount]")
		{
			if (tempobj.checked)
			{
				amount = tempobj.value; break;
			}
		}
	}
	return amount;
}


function getSelectedMotive(theform)
{
	for (i=0;i<theform.length;i++)
	{
		var tempobj = theform.elements[i];
		var motive = '';
		if (tempobj.type.toLowerCase() == "radio" && tempobj.name == "motivepath")
		{
			if (tempobj.checked)
			{
				motive = tempobj.value; break;
			}
		}
	}
	return motive;
}


function jsAddSlashes(text)
{
	var strInputCode = text;
	newtext = strInputCode.replace(/\"/g, "&#x93;");
	newtext = newtext.replace(/\'/g, "&#x91;");
	return newtext;
}


function getText(textString)
{
	//return escape(textString);
	return (jsAddSlashes(textString));
}


function WriteLayer(ID,parentID,sText)
{
	if (document.layers)
	{
		var oLayer;
		if (parentID)
		{
			oLayer = eval('document.' + parentID + '.document.' + ID + '.document');
		}
		else
		{
			oLayer = document.layers[ID].document;
		}
		oLayer.open();
		oLayer.write(sText);
		oLayer.close();
	}
	else if (parseInt(navigator.appVersion)>=5&&navigator.appName=="Netscape")
	{
		document.getElementById(ID).innerHTML = sText;
	}
	else if (document.all) document.all[ID].innerHTML = sText
}


function closeFlashPreview()
{
	if (document.getElementById('layerFlash'))
	{
		document.getElementById('layerFlash').style.visibility = 'hidden';
	}
	if (document.getElementById('layerForm'))
	{
		document.getElementById('layerForm').style.visibility = 'visible';
	}
	//var bilder = document.getElementById('').src;
	//alert("BILDPATH : "+bilder);
	changeElementStatus(document.formcoupon,false);
}


function checkLength()
{
	
	//if (document.formcoupon.body.value.length>300)
	//{
	//	document.formcoupon.body.value = document.formcoupon.body.value.substr(0, 300);
	//}
	//WriteLayer('freeText','owntext_text_textarea',(300-document.formcoupon.body.value.length));	

	return zeilen_checken(document.formcoupon.body, 80, 560, 7);

/*
	var zeilen = document.formcoupon.body.value.split('\n'); 	
	if (zeilen.length > 7 )
	{
		zeilen.splice(7, zeilen.length);
		var inlineText = zeilen.join('\n');
		//alert(inlineText.length);
		if (is.ie || is.op3) 
		{
			//alert ("IE and OPERA");
			document.formcoupon.body.value = inlineText.slice(0,inlineText.length-1);
		}
		else
		{
			//alert ("NO IE");
			document.formcoupon.body.value = inlineText.slice(0,inlineText.length);
		}
		alert("Sie dürfen maximal 7 Zeilen eingeben!");	
		//alert(document.formcoupon.body.value);
	}
	else
	{
		//WriteLayer('freeRows','owntext_text_textarea',"&nbsp;");			
	}
*/
}

// text 		den zu pruefende TextArea
// zpz 			Zeichen pro Zeile
// max_zeichen 			Maximale Zeichen pro Zeile
// max_zeile 	Maximale Zeile 
function zeilen_checken(text, zpz, max_zeichen, max_zeilen)
{
	var x=0, y=zpz;  
	//alert(text.value.length);
    var restZeichen = max_zeichen - text.value.length;  

	while (x + zpz - 1 < text.value.length) {
		
		var suberg = text.value.substr(x,y + 2).search(/\n.+/);
		if (suberg == -1){ 
	    	if (text.value.substr(x + y - 2,1) == " "){ 
	      		text.value = text.value.substr(0,x + y - 2 + 1) + "\n" + text.value.substr(x + y - 2 + 1,text.value.length - (x + y - 2 + 1));
	      		x = x + zpz;
	    	}else{
	      		for(var i = x + y - 2; i >= x; i--){
	        		if (text.value.substr(i,1) == " "){
	          			text.value = text.value.substr(0,i + 1) + "\n" + text.value.substr(i + 1,text.value.length - (i + 1));  
	          			x = i - y + 2 + zpz;   
	          			break;
	        		}else if (i == x){
		  				text.value = text.value.substr(0,i + zpz - 2 + 1) + "\n" + text.value.substr(i + zpz - 2 + 1,text.value.length - ( i + zpz - 2 + 1));  
	          			x = i + zpz - 2 - y + 2 + zpz; 
	          			break;
					}
	      		}
	    	}
	  	}else {
	    	x = x + suberg + 1;
	  	}
	}

    var zeilen = text.value.split('\n'); 
	
	
		
	if(zeilen.length > max_zeilen) { 

        zeilen.splice( max_zeilen, zeilen.length); 
        var inlineText = zeilen.join('\n');
		if (is.ie || is.op3){
			//alert ("IE oder OPERA");
			text.value  = inlineText.slice(0,inlineText.length-1);
		}
		else{
			//alert ("NO IE");
			text.value  = inlineText.slice(0,inlineText.length);
		}         
        alert("Sie dürfen maximal " +max_zeilen+ " Zeilen eingeben!");	
         
         
         
    } 
    else 
    { 
        // rv_counter.value = max_zeichen - text.value.length; 
	 	//zeil_counter.value = max_zeilen - zeilen.length;
    } 
		WriteLayer('freeText','owntext_text_textarea',restZeichen +" Zeichen und "+(max_zeilen-zeilen.length)+" Zeilen " );
}




function textAreaRows()
{
	var zeilen = document.formcoupon.body.value.split('\n'); 	
	if (zeilen.length > 7)
	{
		alert("Maximal 7 Zeilen");
		return false;
	}
	return true;
}


function removeHTMLTags(htmltext)
{
	var strInputCode = htmltext;
	// This line is optional, it replaces escaped brackets with real ones, i.e. < is replaced with < and > is replaced with >
	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1) { return (p1 == "lt")? "<" : ">"; });
	strInputCode2 = strInputCode.replace(/\r/g, function (strMatch, p1) { return "" });
	var strTagStrippedText = strInputCode2.replace(/<\/?[^>]+(>|$)/g, "");
	return strTagStrippedText;
}


function WinOpen(fileName,width,height){
    fileWin = window.open(fileName,'fileWindow','width='+width+',height='+height+',scrollbars=no,location=no,resizable=yes,toolbar=no,menubar=no');
    fileWin.focus()
}

