function showElement(elementId){
	document.getElementById(elementId).style.display = "block";
}

function hideElement(elementId){
	document.getElementById(elementId).style.display = "none";
}

function toggleVisibility(elementId){
	if (document.getElementById(elementId).style.display == "none")
		showElement(elementId);
	else
		hideElement(elementId);
}

function validate_email(field,alerttxt) {
	with (field) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
  		if (apos<1||dotpos-apos<2) {
  			document.getElementById('AlertArea').innerHTML = alerttxt;
  	  		return false;
  	  	}
  		else {
  	  		return true;
  	  	}
  	}
}

function validate_required(field,alerttxt, alertBoxID) {
	with (field) {
		if (value==null||value=="") {
			document.getElementById(alertBoxID).innerHTML = alerttxt;
    		return false;
    	}
  		else {
    		return true;
    	}
  	}
}

function validate_form(thisform, alertID) {
	with (thisform) {
		if (thisform == document.infoForm1) {
			if (validate_required(firstname,"Full name must be filled out!", alertID)==false) {
				firstname.focus();
				return false;
			}
			if (validate_required(lastname,"Full name must be filled out!", alertID)==false) {
				lastname.focus();
				return false;
			}
			if (validate_required(jobtitle,"Job Title must be filled out!", alertID)==false) {
				jobtitle.focus();
				return false;
			}
			if (validate_required(company,"Company name must be filled out!<br>n/a if not applicable.", alertID)==false) {
				company.focus();
				return false;
			}
			if (validate_required(website,"Web Site must be filled out!", alertID)==false) {
				website.focus();
				return false;
			}
		}

		if (thisform == document.infoForm2) {
			if (validate_required(city,"City must be filled out!", alertID)==false) {
				city.focus();
				return false;
			}
			if (validate_required(country,"Country must be selected!", alertID)==false) {
				country.focus();
				return false;
			}
			if (validate_required(phone,"Phone must be filled out!", alertID)==false) {
				phone.focus();
				return false;
			}
			if (validate_required(email,"Email must be filled out!", alertID)==false) {
				email.focus();
				return false;
			}
			if (validate_email(email,"Invalid e-mail address!", alertID)==false) {
				email.focus();
				return false;
			}
		}

		if (thisform == document.emailForm) {
			if (validate_email(email,"Invalid e-mail address!", alertID)==false) {
				email.focus();
				return false;
			}
		}
		
		if(thisform == document.contactForm){
			if (validate_required(cname,"Name must be filled out!", alertID)==false) {
				cname.focus();
				return false;
			}
			if (validate_required(coWeb,"Company Website must be filled out!", alertID)==false) {
				coWeb.focus();
				return false;
			}
			/*if (validate_required(phone,"Phone must be filled out!", alertID)==false) {
				phone.focus();
				return false;
			}*/
			if (validate_required(email,"Email must be filled out!", alertID)==false) {
				email.focus();
				return false;
			}
			if (validate_email(email,"Invalid e-mail address!", alertID)==false) {
				email.focus();
				return false;
			}
		}
				if(thisform == document.contactR){
			if (validate_required(cname,"Name must be filled out!", alertID)==false) {
				cname.focus();
				return false;
			}
			if (validate_required(coWeb,"Company Website must be filled out!", alertID)==false) {
				coWeb.focus();
				return false;
			}
			if(document.contactR.method[0].checked){
			
				if (validate_required(phone,"Phone must be filled out!", alertID)==false) {
					phone.focus();
					return false;
				}
			}
			else{
			if (validate_required(email,"Email must be filled out!", alertID)==false) {
				email.focus();
				return false;
			}
			if (validate_email(email,"Invalid e-mail address!", alertID)==false) {
				email.focus();
				return false;
			}
			}
		}
  	}
}

function showRequestEmail(url, filename, mode) {
	scroll(0,0);
	new Ajax.Updater('RequestEmail', url, {
      method: 'get',
	  parameters: { filename: filename, mode: mode }
	});
	showElement('downloadbox');

}

function showRequestEmail2(url){
	new Ajax.Updater('RequestEmail', url, {
      method: 'get',
	  parameters: { }
	});
	showElement('downloadbox');
}

function hideRequestEmail(){
	hideElement('downloadbox');
}

function openDItem(idPic, idE){
	var ele = document.getElementById(idE);
	var pic = document.getElementById(idPic);
	if(ele.style.display == "block") {			
    		ele.style.display = "none";
			if(idPic=="dab"){
				pic.src="images/daclosed.png";
			}
			else if(idPic=="javab"){
				pic.src="images/javasdkclosed.png";
			}
			else if(idPic=="cb"){
				pic.src="images/csdkclosed.png";
			}
			else if(idPic=="gateb"){
				pic.src="images/gatewayclosed.png";
			}
  	}
	else {
		ele.style.display = "block";
		if(idPic=="dab"){
		pic.src="images/daopen.png";
		}
		else if(idPic=="javab"){
			pic.src="images/javasdkopen.png";
		}
		else if(idPic=="cb"){
			pic.src="images/csdkopen.png";
		}
		else if(idPic=="gateb"){
			pic.src="images/gatewayopen.png";
		}
	}
	
}
/*
function launchToggleElement(idname) {
	var ele = document.getElementById(idname);
	
	hideOtherEsales(idname);
	
	if(ele.style.display == "block") {
    		ele.style.display = "none";
  	}
	else {
		ele.style.display = "block";
	}
} 
*/
function launchToggleElement(idOfNew){
	var ele = document.getElementById(idOfNew);

	var chat = document.getElementById('toggleChat');
	var me = document.getElementById('toggleContact');
	var ord = document.getElementById('toggleOrder');
	var down = document.getElementById('toggleDownload');

	if(ele.style.display == "block") {
			ele.style.display = "none";
	}
	else {
		ele.style.display = "block";
	}

	if(idOfNew == 'toggleChat'){
		me.style.display = "none";
		ord.style.display = "none";
		down.style.display = "none";
	}
	else if(idOfNew == 'toggleContact'){
		chat.style.display = "none";
		ord.style.display = "none";
		down.style.display = "none";
	}
	else if(idOfNew == 'toggleOrder'){
		chat.style.display = "none";
		me.style.display = "none";
		down.style.display = "none";
	}
	else if(idOfNew == 'toggleDownload'){
		chat.style.display = "none";
		me.style.display = "none";
		ord.style.display = "none";
	}
	/*Jos joskus tulee lisää vaihtoehtoja, ne laitetaan tähän */
}



function eSalesRequestHandler(url,idWhere){
	launchToggleElement(idWhere);

	new Ajax.Updater(idWhere, url, {
      method: 'get',
	  parameters: { }
	});
	

}

function showEmailSent(url, filename, email, mode){
	if (validate_form(document.emailForm, 'AlertArea')==false)
		return false;
	new Ajax.Updater('RequestEmail', url, {
      method: 'get',
	  parameters: { filename: filename, email: email, mode: mode  }
	});
}

function showEmailSent2(url, firstname, lastname, jobtitle, company, website, city, country, phone, email){
	var filename = "";
	for(var i=0; i < document.selectFilesForm.files.length; i++){
		if(document.selectFilesForm.files[i].checked)
			filename += document.selectFilesForm.files[i].value + "\n";
	}
	
	if(filename == "") {
		document.getElementById('AlertArea').innerHTML = "Please select at least one item to request.";
		return false;
	}
	
	if (validate_form(document.infoForm1, 'AlertArea')==false)
		return false;
	if (validate_form(document.infoForm2, 'AlertArea')==false)
		return false;
	
	new Ajax.Updater('RequestEmail', url, {
      method: 'get',
	  parameters: { firstname: firstname, lastname: lastname, jobtitle: jobtitle, company: company, website: website, city: city, country: country, phone: phone, filename: filename, email: email}
	});
	
	scroll(0,0);
}

/*onClick="showEmailSentEsales('d-request-e-sales.php?',
			$('firstname').value, $('lastname').value,$('jobtitle').value, $('company').value,
			$('website').value, $('city').value, $('country').value, $('phone').value, $('email').value)" />*/

function showEmailSentEsales(url, firstname, lastname, jobtitle, company, website, city, country, phone, email){
	var filename = "";
	for(var i=0; i < document.downloadRequestForm.files.length; i++){
		if(document.downloadRequestForm.files[i].checked)
			filename += document.downloadRequestForm.files[i].value + "\n";
	}
	
	if(filename == "") {
		document.getElementById('AlertArea2').innerHTML = "Please select at least one item to request.";
		return false;
	}
	
	if (validate_form(document.infoForm1, 'AlertArea2')==false)
		return false;
	if (validate_form(document.infoForm2, 'AlertArea2')==false)
		return false;
	
	new Ajax.Updater('toggleDownload', url, {
      method: 'get',
	  parameters: { firstname: firstname, lastname: lastname, jobtitle: jobtitle, company: company, website: website, city: city, country: country, phone: phone, filename: filename, email: email}
	});
	
	scroll(0,0);
}

function showEmailSentEsales2(url, message, cname, company, country, coWeb, phone, email){
	
	if(document.contactForm.message.value == "" || document.contactForm.message.value == " Your message here! ") {
		document.getElementById('AlertArea3').innerHTML = "Please fill in your message";
		return false;
	}
	if(document.contactForm.method[0].checked){
		var method = document.contactForm.method[0].value;
	}
	else if(document.contactForm.method[1].checked){
		var method = document.contactForm.method[1].value;
	}
	else{
		document.getElementById('AlertArea3').innerHTML = "Choose preferred contact method.";
		return false;
	}
	
	if (validate_form(document.contactForm, 'AlertArea3')==false){	
		return false;
	}
	new Ajax.Updater('toggleContact', url, {
      method: 'get',
	  parameters: { message: message, cname: cname, company: company, country:country, coWeb: coWeb, phone: phone, email: email, method: method}
	});

	scroll(0,0);
}


function showEmailSentEsales3(url, cname, company, coWeb, country, phone, email){
		if(document.contactR.method[0].checked){
			var method = document.contactR.method[0].value;
		}
		else if(document.contactR.method[1].checked){
			var method = document.contactR.method[1].value;
		}
		else{
			document.getElementById('AlertArea3').innerHTML = "Choose preferred contact method.";
			return false;
		}
	if (validate_form(document.contactR, 'AlertArea3')==false){
		return false;
	}
	new Ajax.Updater('toggleOrder', url, {
      method: 'get',
	  parameters: {cname: cname, company: company, coWeb: coWeb, country: country, phone: phone, email: email, method: method}
	});
	
	scroll(0,0);
}

function showOpcUaVideoPopup() {
	window.open('opc-ua-video-popup.php', 'opc-ua-video-popup', config='toolbar=0,statusbar=0,menubar=0,resizable=1,height=500,width=500')
}

function showRequestQuote(url, product, edition) {
	scroll(0,0);
	new Ajax.Updater('RequestQuote', url, {
      method: 'get',
	  parameters: { product: product, edition: edition }
	});
	showElement('requestbox');

}
function showRequestQuoteSent(url, firstname, lastname, jobtitle, company, website, city, country, phone, email){
	var quote = "";
	
	$$('select').each( function(product){
	  if (product.value > 0){
	    quote = quote + product.name +": "+product.value+'\n';
	  }
	});
	
	
	if(quote == "") {
		document.getElementById('AlertArea').innerHTML = "Please select at least one product.";
		return false;
	}
	
	new Ajax.Updater('RequestQuote', url, {
      method: 'get',
	  parameters: { firstname: firstname, lastname: lastname, jobtitle: jobtitle, company: company, website: website, city: city, country: country, phone: phone, email: email, quote: quote}
	});
	
	scroll(0,0);
}
function hideRequestQuote(){
	hideElement('requestbox');
}

function showSideBubbleWith(imgID){
	var ele = document.getElementById('sideBubble');
	if(ele.style.display == "block") {
			ele.style.display = "none";			
	}
	else{
		var docH = document.getElementById('wrapper').clientHeight;
		var windH = window.innerHeight;
		var footer = document.getElementById('footer');
		var setter = 0;
		var place = 0;
		if(footer != null){
			var y = footer.offsetTop;
			if( windH > y){
				var place = place + document.getElementById('content').clientHeight;
				var topH = place + "px";
				ele.style.top = topH;
				setter = 1;
			}
		}
		
		if(setter == 0){ /*ei asetettu yläreunaa, joten voidaan asettaa alareuna*/
			ele.style.bottom = "20px";
		}
		
		ele.style.display = "block";

	}
	var img = document.getElementById(imgID);
		if(img.style.display == "block") {
			img.style.display = "none";
	}
	else {
		img.style.display = "block";
	}
}

function androidAdd(){
	var times = 0;
	   jQuery('#robot').animate({
    top: '-=45'
  }, 1000, function() {
    flashLights(times);
  });
}
function flashLights(i){
	$times = i +1;
	if($times<10){
		flashFirstRedOn($times);
	}
	else{
	flashRestOn();
	}
}

function flashFirstRedOn(i){
$times = i;
	 jQuery('#AALY11').animate({
		opacity: 1.0
		}, 800, function() {
				 jQuery('#AALY12').animate({
				opacity: 1.0
			}, 800, function() {
						 jQuery('#AALY13').animate({
					opacity: 1.0
				}, 800, function() {
									flashSecondRedOff($times);
							});
						});
					});
}
function flashSecondRedOff(i){
$times = i;
		 jQuery('#AALY21').animate({
		opacity: 0
		}, 1200, function() {
				 jQuery('#AALY22').animate({
				opacity: 0
			}, 1000, function() {
						 jQuery('#AALY23').animate({
					opacity: 0
				}, 800, function() {
									flashSecondYellowOn($times);
							});
						});
					});
	
}
function flashSecondYellowOn(i){
$times = i;
	 jQuery('#AALY43').animate({
		opacity: 1.0
	}, 800, function() {
	 jQuery('#AALY42').animate({
		opacity: 1.0
	}, 800, function() {
		 jQuery('#AALY41').animate({
	opacity: 1.0
	}, 800, function() {
						flashFirstYellowOff($times);
	});
	});
	});

}
function flashFirstYellowOff(i){
$times = i;
	
	 jQuery('#AALY33').animate({
		opacity: 0
	}, 1200, function() {
		 jQuery('#AALY32').animate({
			opacity: 0
		}, 1000, function() {
			 jQuery('#AALY31').animate({
				opacity: 0
			}, 800, function() {
				flashSecondRedOn($times);
			});
		});
	});	
}
function flashSecondRedOn(i){
$times = i;
	 jQuery('#AALY23').animate({
		opacity: 1.0
	}, 800, function() {
	 jQuery('#AALY22').animate({
		opacity: 1.0
	}, 800, function() {
		 jQuery('#AALY21').animate({
	opacity: 1.0
	}, 800, function() {
						flashFirstRedOff($times);
	});
	});
	});

}
function flashFirstRedOff(i){
$times = i;
	
	 jQuery('#AALY13').animate({
		opacity: 0
	}, 1200, function() {
		 jQuery('#AALY12').animate({
			opacity: 0
		}, 1000, function() {
			 jQuery('#AALY11').animate({
				opacity: 0
			}, 800, function() {
				flashFirstYellowOn($times);
			});
		});
	});	
}
function flashFirstYellowOn(i){
$times = i;
	 jQuery('#AALY31').animate({
		opacity: 1.0
		}, 800, function() {
				 jQuery('#AALY32').animate({
				opacity: 1.0
			}, 800, function() {
						 jQuery('#AALY33').animate({
					opacity: 1.0
				}, 800, function() {
									flashSecondYellowOff($times);
							});
						});
					});
}
function flashSecondYellowOff(i){
$times = i;
		 jQuery('#AALY41').animate({
		opacity: 0
		}, 1200, function() {
				 jQuery('#AALY42').animate({
				opacity: 0
			}, 1000, function() {
						 jQuery('#AALY43').animate({
					opacity: 0
				}, 800, function() {
									 flashLights($times);
							});
						});
					});
	
}
function flashRestOn(){
	 jQuery('#AALY11').animate({
		opacity: 1.0
		}, 800, function() {
				 jQuery('#AALY12').animate({
				opacity: 1.0
			}, 800, function() {
						 jQuery('#AALY13').animate({
					opacity: 1.0
				}, 800, function() {
										 jQuery('#AALY43').animate({
		opacity: 1.0
	}, 800, function() {
	 jQuery('#AALY42').animate({
		opacity: 1.0
	}, 800, function() {
		 jQuery('#AALY41').animate({
	opacity: 1.0
	}, 800, function() {
						//ready
	});
	});
	});
							});
						});
					});
}
