// JavaScript Document
// String Trim Function
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function clearSelect(a){
	a.options.length = 0;
	
	switch(a.id){
	case 'line': a.options[a.length] = new Option('Select Product Line...', ''); break;
	case 'category': a.options[a.length] = new Option('Select Product Category...', ''); break;
	}
}

function getUTI(){
	var a = new Date();
	var uti = '' + a.getHours() + a.getMinutes() + a.getMilliseconds();
	return uti;
}

function ajaxObject(){
	var xhr;
	try{xhr=new XMLHttpRequest();}	// Firefox, Opera 8.0+, Safari  
	catch (e){	// Internet Explorer
		try{xhr=new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e){	// IE 7 on Vista
			try{xhr=new ActiveXObject("Microsoft.XMLHTTP");}
			catch (e){        
				alert("Asynchronis Data Transfer unavailable, please use FireFox Browser!");        
				return false;
			}
		}
	}
	return xhr;
}

function populateCategory(a){
	var line = document.getElementById('line');
	var category = document.getElementById('category');
	
	clearSelect(line);
	clearSelect(category);
	
	var loader = document.getElementById('loader');
	var location = document.product_finder.location.value;
	if (location =='products'){
	loader.innerHTML = '<img src="http://www.tecniplast.it/_images/loading_red.gif" border="none" style="vertical-align:middle;"> Searching...';
	}
	else{
	loader.innerHTML = '<img src="http://www.tecniplast.it/_images/loading.gif" border="none" style="vertical-align:middle;"> Searching...';
	}
	
	url = '/_async/product_finder.php?familyid='+a[a.selectedIndex].value+'&uti='+getUTI();
	var xhr = ajaxObject();
	if (xhr) {
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4 || xhr.readyState == 'undefined') {
				if (xhr.status == 200){
					// process results
					var result = parseProductResult(xhr.responseXML,category);
					loader.innerHTML = '';
					xhr.abort();
					if(a.selectedIndex == 0){	
						populateLine(category);
					}
				}
				else{
					//prevWin.innerHTML = 'Error: ' + xhr.status;
					xhr.abort();
				}
			}
		};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Asynchronous Data Error!");
	}
}

function populateLine(a){
	var line = document.getElementById('line');
	clearSelect(line);
	
	var loader = document.getElementById('loader');
	var location = document.product_finder.location.value;
	if (location =='products'){
	loader.innerHTML = '<img src="http://www.tecniplast.it/_images/loading_red.gif" border="none" style="vertical-align:middle;"> Searching...';
	}
	else{
	loader.innerHTML = '<img src="http://www.tecniplast.it/_images/loading.gif" border="none" style="vertical-align:middle;"> Searching...';
	}
			
	url = '/_async/product_finder.php?categoryid='+a[a.selectedIndex].value+'&uti='+getUTI();
	var xhr = ajaxObject();
	if (xhr) {
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4 || xhr.readyState == 'undefined') {
				if (xhr.status == 200){
					// process results
					var result = parseProductResult(xhr.responseXML,line);
					loader.innerHTML = '';
					xhr.abort();
				}
				else{
					//prevWin.innerHTML = 'Error: ' + xhr.status;
					xhr.abort();
				}
			}
		};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Asynchronous Data Error!");
	}
}

function parseProductResult(a,b){
	var xmlDoc= a.documentElement;
	var records = xmlDoc.getElementsByTagName("records")[0].childNodes[0].nodeValue;
	//alert(records);
	
	// POPULATE THE APPROPRIATE DROPDOWN
	if(records > 0){
		for (e=0;e<records;e++){
			var r = e + 1;
			var value = xmlDoc.getElementsByTagName("value"+r)[0].childNodes[0].nodeValue;
			var title = xmlDoc.getElementsByTagName("title"+r)[0].childNodes[0].nodeValue;
			b.options[b.length] = new Option(title,value);
		}
		b.focus();
	}
}

function searchProducts(){
	document.product_finder.submit();
}

function showEvents(a) {
	var content = document.getElementById('event_content_'+a);
	var header = document.getElementById('header_'+a);
	var img = document.getElementById('event_img_'+a);
	var location = document.getElementById('event_location_'+a);
	var contact = document.getElementById('event_contact_info_'+a);
	
	if(content.style.display == 'none'){//SHOW CONTENT
		content.style.display = 'inline';
		header.className = 'event_toggle_active';
		img.src='http://www.tecniplast.it/_images/icons/close_active.gif';
		location.innerHTML = 'Location';
		contact.innerHTML = 'Contact Information';
	}
	else {// HIDE CONTENT
		content.style.display = 'none';
		header.className = 'event_toggle';
		img.src='http://www.tecniplast.it/_images/icons/open_inactive.gif';
		location.innerHTML = '';
		contact.innerHTML = '';
	}
}

function showTopStories(a) {
	var content = document.getElementById('event_content_'+a);
	var header = document.getElementById('header_'+a);
	var img = document.getElementById('event_img_'+a);
	var location = document.getElementById('event_location_'+a);
	var contact = document.getElementById('event_contact_info_'+a);
	
	if(content.style.display == 'none'){//SHOW CONTENT
		content.style.display = 'inline';
		header.className = 'event_toggle_active';
		img.src='http://www.tecniplast.it/_images/icons/close_active.gif';
		//location.innerHTML = '';
		//contact.innerHTML = '';
	}
	else {// HIDE CONTENT
		content.style.display = 'none';
		header.className = 'event_toggle';
		img.src='http://www.tecniplast.it/_images/icons/open_inactive.gif';
		//location.innerHTML = '';
		//contact.innerHTML = '';
	}
}

function showNewsletters(a) {
	var content = document.getElementById('newsletter_content_'+a);
	var header = document.getElementById('header_'+a);
	var img = document.getElementById('newsletter_img_'+a);
	var location = document.getElementById('newsletter_location_'+a);
	var issue = document.getElementById('newsletter_issue_number_'+a);
	var contact = document.getElementById('newsletter_contact_info_'+a);
	
	if(content.style.display == 'none'){//SHOW CONTENT
		content.style.display = 'inline';
		header.className = 'newsletter_toggle_active';
		img.src='http://www.tecniplast.it/_images/icons/close_active.gif';
		location.innerHTML = 'Published';
		contact.innerHTML = 'Title';
		issue.innerHTML = 'Issue';
	}
	else {// HIDE CONTENT
		content.style.display = 'none';
		header.className = 'newsletter_toggle';
		img.src='http://www.tecniplast.it/_images/icons/open_inactive.gif';
		location.innerHTML = '';
		contact.innerHTML = '';
		issue.innerHTML = '';
	}
}

function showLinks(a) {
	var content = document.getElementById('link_content_'+a);
	var header = document.getElementById('header_'+a);
	var img = document.getElementById('link_img_'+a);
	
	if(content.style.display == 'none'){//SHOW CONTENT
		content.style.display = 'inline';
		header.className = 'link_toggle_active';
		img.src='http://www.tecniplast.it/_images/icons/close_active.gif';
	}
	else {// HIDE CONTENT
		content.style.display = 'none';
		header.className = 'link_toggle';
		img.src='http://www.tecniplast.it/_images/icons/open_inactive.gif';
	}
}

function findReps(){
	document.reps.submit();
}

function changeProducts(a){
	var prod = document.getElementById('products');
	var abstract = document.getElementById('reports');
	if((a[a.selectedIndex].value)=='Products'){
		prod.disabled = "";
		abstract.disabled = "disabled";
	}
	else if((a[a.selectedIndex].value)=='Reports'){
		prod.disabled = "disabled";
		abstract.disabled = "";
	}
	else {
		prod.disabled = "disabled";
		abstract.disabled = "disabled";
	}
}

function showAbstract(a){
	var div = document.getElementById(a);
	var toggle = document.getElementById('toggle_'+a);
	
	if(div.style.display !== 'inline'){
		div.style.display = 'inline';
		toggle.innerHTML = 'Hide Content';
	}
	else {
		div.style.display = 'none';
		toggle.innerHTML = 'Show Content';
	}
	
}

function showFAQ(a){
	var div = document.getElementById(a);
	var toggle = document.getElementById('toggle_'+a);
	
	if(div.style.display !== 'inline'){
		div.style.display = 'inline';
		toggle.innerHTML = 'Hide Answer';
	}
	else {
		div.style.display = 'none';
		toggle.innerHTML = 'Show Answer';
	}
	
}

function positionElement(a){
	var oNode=a;
	var x=0,y=oNode.offsetHeight;
	
	while(oNode.offsetParent){
		x+=oNode.offsetLeft;
		y+=oNode.offsetTop;
		oNode=oNode.offsetParent;
	}

	return{
		x: x + 'px',
		y: y + 'px'
	}	
}

function showCal(a,b){
	var minical = document.getElementById('minical');
	var tDate = document.getElementById(b);
	if (isDate(tDate.value,false)){
		tdate = tDate.value;
	}
	else{
		var tdate = new Date();
		var dy = tdate.getFullYear();
		var dm = tdate.getMonth() + 1;
		var dd = tdate.getDate();
		var tdate = dm + '/' + dd + '/' + dy; 
	}
	
	// Postion the Calendar
	var pos = positionElement(a);
	minical.style.display = '';
	minical.style.top = pos.y;
	minical.style.left = pos.x;

	// Call the Calendar from Server
	getCalendar(tdate,'','minical',b);
}

function hideCal(){
	var minical = document.getElementById('minical');
	minical.style.display = 'none';
}


function showQuickMenu(){
	var menu = document.getElementById('quicklinksmenu');
	var a = document.getElementById('quicklinks');
	var img = document.getElementById('ql_image');
	var pos = positionElement(a);
	img.src='http://www.tecniplast.it/_images/menu_arrow_active.gif';
	menu.style.display = '';
	menu.style.top = pos.y ;
	menu.style.left = pos.x ;
}

function hideQuickMenu(){
	var men = document.getElementById('quicklinksmenu');
	var img = document.getElementById('ql_image');
	men.style.display = 'none';
	img.src='http://www.tecniplast.it/_images/menu_arrow.gif';
}

function populateSites(){
	var location = document.getElementById('location');
	var loader = document.getElementById('loader');
	
	loader.innerHTML = "Loading Sites...";
	
	url = '/_async/get_sites.php?uti='+getUTI();
	var xhr = ajaxObject();
	if (xhr) {
		
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4 || xhr.readyState == 'undefined') {
				if (xhr.status == 200){					
					// process results
					var result = parseProductResult(xhr.responseXML,location);
					loader.value = 'index.php';
					loader.innerHTML = 'Select Location...';
					xhr.abort();
				}
				else{
					//prevWin.innerHTML = 'Error: ' + xhr.status;
					xhr.abort();
				}
			}
		};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Asynchronous Data Error!");
	}

}

function gotoSite(){
	var location = document.getElementById('location');
	var target = location[location.selectedIndex].value;
	if(target !== ''){
		window.location = target;
	}
}

function check_gotoSite(){
	var location = document.getElementById('location');
	var target = location[location.selectedIndex].value;
	if(target == 'index.php')
	{
		alert("Please select a location");
		location.focus();
	}
	else
	{
		if(target !== '')
		{
			document.forms.change_location.submit();
		}
	}
}

