/*
	@description: display a bigger version of the images that are used as a thumbnail in articles
	@author:		k.kuplen@netconomy.net
*/
function displayBigImage(popupImgSrc, imgWidth, imgHeight, srcObject, textObj) {


	// image div that is filled with the image src
	var displayElement = document.getElementById("popupImage");
	
	// div element to be filled with text belonging to the image
	var displayTextElement = document.getElementById("popupImageText")
	var displayTextElementContent = document.getElementById("popupImageTextContent");
	
	// empty previous texts;
	for (var i=0; i < displayTextElementContent.childNodes.length; i++) {
		displayTextElementContent.removeChild(displayTextElementContent.childNodes[i])
	}
	
	
	// hide element if visible
	displayElement.parentNode.style.display = "none";
	
	// setting styles attributes for the image
	displayElement.src = popupImgSrc;
	displayElement.style.width = imgWidth;
	displayElement.style.height = imgHeight;
	
	var topPos   = 0;
	var leftPos  = 0;
	var maxRight = 999;
	
	// handling subtext for image
	var tmpText = "";
		
	tmpText = textObj.innerHTML
	
	var textHeight = 0;
	// check if its a valid text to display - that is other things than whitespace
	if (tmpText.replace(/\s/g,"") == "" ) {
		tmpText = ""
	} else {
		tmpText = tmpText.replace(/\r|\n|\t/g, ""); // strip unneeded whitespace
	}
	
	
	var myExpression = /<p>/;
	var paragraphs = myExpression.exec(tmpText) ;
	var numParagraphs = 0;
	if (paragraphs != null) {
		numParagraphs =paragraphs.length
	}

	
	// guess a matching height based on the length of the text 
	if (tmpText.length > 0) {
		textHeight = ( ((Math.ceil(tmpText.length / 30) + numParagraphs) * 12)  + 10  );
	}
	
	displayTextElement.style.height =  textHeight + "px"
	
	var widthHack = 0;
	// IE 5.5 does padding and spacing just "different" than other browsers
	if( navigator.appVersion.indexOf("MSIE 5")>-1) {
		widthHack = 8;
	} else {
		widthHack = -9;
	}
	
	
	// IE depending positioning
	if (document.all) {
	
//		if ( ((srcObject.parentNode.offsetTop) - imgHeight  ) > 0) {
			// IE's offsetTop property is not commulative 
			// and does not add up with partentNode.offsetTop , so use nasty hacking
			topPos = ( (srcObject.offsetTop  + 60 ) - imgHeight ) ;
//		}
	} else if (document.getElementById) {
		if ( ( srcObject.offsetTop  - imgHeight  ) > 0 ) {
			topPos = (srcObject.offsetTop - imgHeight ) - 15 ;
		}
	}
	

	// alert("topPos: " + topPos)
	
	displayTextElement.style.width = (imgWidth + widthHack)+ "px"
	
	
	
	displayTextElementContent.innerHTML = textObj.innerHTML;

	
	// hiding span element that contains the text if empty
	if (tmpText.length > 0) {
		displayTextElement.firstChild.style.display="inline";
	} else {
		displayTextElement.firstChild.style.display="none";
	}
	
	
	displayElement.parentNode.style.top  = ((topPos - textHeight) -15) + "px";
	
	if (textHeight == 0) {
		// no text -  no padding		
		displayTextElement.style.paddingBottom = "0px";
		displayTextElement.style.paddingTop = "0px";
		displayTextElement.style.margin = "0px";
		
	} else {
		// no text -> create some whitespace
		displayTextElement.style.paddingBottom = "10px";
		displayTextElement.style.paddingTop = "10px";
		displayTextElement.style.paddingLeft = "7px";
		
	}
	
	
		// handling left position
	if ( maxRight - imgWidth > 0) {
		leftPos = maxRight - imgWidth;
	}
	displayElement.parentNode.style.left = leftPos + "px";
	
	// display the div that contains the image
	displayElement.parentNode.style.display = "block";
	
}




/*
	@description: hiding a bigger version of the images that are used as a thumbnail in articles
	@author:		k.kuplen@netconomy.net
*/

function hideBigImage() {
	document.getElementById("articleBigImageDisplay").style.display="none";
	document.getElementById("popupImage").src = "";
	
}



/*
	@description: validation of an e-mail adress
	@author:      k.kuplen@netconomy.net
*/
function checkEmail(email) {

	if (email == "") {
		return false;
	}

	var usr    = "[a-zA-Z0-9][a-zA-Z0-9_.-]*";
	var domain = "[a-zA-Z0-9][a-zA-Z0-9._-]*\\.*[a-zA-Z]{2,5}";
	var regex  = usr + "@" + domain ;
	var rgx    = new RegExp(regex);
	var res = email.match(rgx) ;

	if (res) {
		return true;
	} else {
		return false;
	}
}





/*
	@description: validation of the subscription form
	@author:      k.kuplen@netconomy.net
*/
function validateSubscribe(theForm) {

	if (! theForm.elements["Subscribe"].checked ) {
		alert("If you want to subscribe to the fiberspectrum magazine, please mark the checkbox.");
		theForm.elements["Subscribe"].focus();
		return false;
	}
	
	if (validateCustomerAddress(theForm) == false) {
		return false;
	}

	if (theForm.elements["Phone"].value == "") {
		alert("Please fil out all required fields!");
		theForm.elements["Phone"].focus()
		return false;
	}	

		
	theForm.submit();
}



/*
	@description: validation of the adress form of a customer
	@author:      k.kuplen@netconomy.net
*/
function validateCustomerAddress(theForm) {

	if (theForm.elements["First_Name"].value == "") {
		alert("Please fil out all required fields!");
		theForm.elements["First_Name"].focus()
		return false;
	}

	if (theForm.elements["Last_Name"].value == "") {
		alert("Please fil out all required fields!");
		theForm.elements["Last_Name"].focus()
		return false;
	}
				
	if (theForm.elements["Title"].value == "") {
		alert("Please fil out all required fields!");
		theForm.elements["Title"].focus()
		return false;
	}

	if (theForm.elements["Company"].value == "") {
		alert("Please fil out all required fields!");
		theForm.elements["Company"].focus()
		return false;
	}
			
	if (theForm.elements["Address_1"].value == "") {
		alert("Please fil out all required fields!");
		theForm.elements["Address_1"].focus()
		return false;
	}
		
	if (theForm.elements["Postal_Code"].value == "") {
		alert("Please fil out all required fields!");
		theForm.elements["Postal_Code"].focus()
		return false;
	}

	if (theForm.elements["City"].value == "") {
		alert("Please fil out all required fields!");
		theForm.elements["City"].focus()
		return false;
	}
			
	if (theForm.elements["State"].value == "") {
		alert("Please fil out all required fields!");
		theForm.elements["State"].focus()
		return false;
	}		

	if (theForm.elements["Country"].value == "") {
		alert("Please fil out all required fields!");
		theForm.elements["Country"].focus()
		return false;
	}	

}
		


/*
	@description: validation of the subscription form
	@author:      k.kuplen@netconomy.net
*/
function validateSubscription(theForm) {

	var removePrint = false;
	
	if (theForm.elements["Remove_From_Printed"].checked) {
		removePrint = true;	
		if (validateCustomerAddress(theForm) == false) {
			return false;
		}
	} 
	
	if (!(removePrint)) {
		alert("Please make your selection.")
		return false;
	}
	
	theForm.submit();
}


function validateUpdate(theForm) { 
/*
	if (! theForm.elements["Remove_From_Printed"].checked ) {
		alert("If you want to unsubscribe, please check the checkbox.");
		return false;
	}
*/

	if (validateCustomerAddress(theForm) == false) {
		return false;
	}

	if (theForm.elements["Phone"].value == "") {
		alert("Please fil out all required fields!");
		theForm.elements["Phone"].focus()
		return false;
	}				
	
	theForm.submit();
}



/*
	@description: toggle visibility of element
	@author:      bernd.pammer@andritz.com
*/
function tv(theElement) {

	var items = document.getElementById("articleItems").childNodes;
	var max =items.length; 
	for(var item=0; item<max; item++) {
		if (items[item].className == "item") {
			
			for(var currDivCnt =0; currDivCnt<items[item].childNodes.length; currDivCnt++) {
		
				if (
					items[item].childNodes[currDivCnt].className == "relo" && 
					items[item].childNodes[currDivCnt].childNodes[0].id != theElement
					) {
					items[item].childNodes[currDivCnt].childNodes[0].style.display = "none";
				}
			}
		}
	}

	if (document.getElementById(theElement).style.display == "none") {
		document.getElementById(theElement).style.display = "block";
	} else {
		document.getElementById(theElement).style.display = "none";
	}
}


/*
	@description: pass a form and get all checkboxes checked
	@author: kkuplen@netconomy.net
*/
function selectAllCheckboxes(form) {
	var maxElements = form.elements.length;
	
	for(var curr=0; curr < maxElements; curr++) {	
		if (form.elements[curr].type.toLowerCase() == "checkbox") {
			form.elements[curr].checked = true;
		}
	}
}


/*
	@description: pass a form and get all checkboxes unchecked
	@author: kkuplen@netconomy.net
*/
function unselectAllCheckboxes(form) {
	var maxElements = form.elements.length;
	
	for(var curr=0; curr < maxElements; curr++) {	
		if (form.elements[curr].type.toLowerCase() == "checkbox") {
			form.elements[curr].checked = false;
		}
	}
}

