﻿function popupWindow( mypage,myname,w,h,features)
{
	if(screen.width)
	{
		var winl = (screen.width-w)/2;
		var wint = (screen.height-h)/2;
	}
	else
	{
		winl = 0;wint =0;
	}
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;
	var settings = 'height=' + h + ',';
	settings += 'width=' + w + ',';
	settings += 'top=' + wint + ',';
	settings += 'left=' + winl + ',';
	settings += features;
	win = window.open(mypage,myname,settings);
	win.window.focus();
}

function isNumeric( input )
{
		if ( parseInt(input, 10) != input ) return false;
		return true;
}

function isEmpty( input )
{
		if ( input == '' ) return true;
		return false;
}

function ConfirmRedirect ( question, positive_url )
{
	if ( confirm(question) )
	{
		this.window.location = positive_url;
	}
}
function scaleImage(image, maxwidth, maxheight ) {
	if (image.width > maxwidth || image.height > maxheight) {
		if (image.width > image.height) {
			image.height = (image.height/image.width)*maxwidth
			image.width = maxwidth
		} else {
			image.width = (image.width/image.height)*maxheight
			image.height = maxheight
		}
		image.style.display = 'block'
	}
}

function hideDebug() {
	var debugDivs = Array()
	debugDivs = document.getElementsByName('solu_debug')
	for (i = 0; i < debugDivs.length; i++) {
		debugDivs[i].style.display = 'none'
	}
}
function toggleDebug() {
	var debugDivs = Array()
	debugDivs = document.getElementsByName('solu_debug')
	for (i = 0; i < debugDivs.length; i++) {
		if (!debugDivs[i].style.display || debugDivs[i].style.display != 'block' || debugDivs[i].style.display == '' || debugDivs[i].style.display == 'none') {
			debugDivs[i].style.display = 'block'
		} else {
			debugDivs[i].style.display = 'none'
		}
	}
}
function MM_preloadImages() { //v3.0
  var d=document; 
  if (d.images) { 
	if(!d.MM_p) d.MM_p=new Array();
	
    var i
	var j = d.MM_p.length
	var a = MM_preloadImages.arguments 
	for(i=0; i<a.length; i++)
			if (a[i].indexOf("#")!=0){ 
				d.MM_p[j]=new Image; 
				d.MM_p[j++].src=a[i];
			}
		}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function e21_pickObject(name) {
	// First let's test for the W3C DOM Level 1
	if (document.getElementById) {
		return document.getElementById(name)
	} // OK, back to DOM Level 0. Is it IE and similar ?
	else if (document.all) {
		return document.all[name]
	} // Still DOM 0, Is it Netscape and similar?
	else if (document.layers) {
		return document.layers[name]
	} // If none of these match then we return null
	else {
		return null
	}
}

function e21_quickLinkSwap(img, replacement, quickTextElementName, quickTextImg) {
	img.src = replacement
	var quickText = e21_pickObject(quickTextElementName)
	quickText.src = quickTextImg
}

function show_yritys() {
  uncheck_checkboxes_under('yksityis_profilointi');
	styleObj=document.getElementById('yritys'); 
	styleObj.style.display='block';
	styleObj=document.getElementById('yksityinen'); 
	styleObj.style.display='none'; 
	styleObj=document.getElementById('yksityis_profilointi'); 
	styleObj.style.display='none'; 
	styleObj=document.getElementById('yritys_profilointi'); 
	styleObj.style.display='block';
}

function show_yksityinen() {
  uncheck_checkboxes_under('yritys_profilointi');
	styleObj=document.getElementById('yksityinen'); 
	styleObj.style.display='block';
	styleObj=document.getElementById('yritys'); 
	styleObj.style.display='none'; 
	styleObj=document.getElementById('yritys_profilointi'); 
	styleObj.style.display='none'; 
	styleObj=document.getElementById('yksityis_profilointi'); 
	styleObj.style.display='block';
}

function uncheck_checkboxes_under(id) {
  root = document.getElementById(id);
  if(!root) {
    alert('Did not find element by id \'' + id + '\'');
    return;
  }
  checks = root.getElementsByTagName('INPUT');
  //alert(checks.length);
  for(i=0; i<checks.length; i++) {
    inputti = checks[i];
    //alert(inputti.id);
    if(inputti.checked)
      inputti.checked = false;
  }
}

//Tarkistaa onko merkkijono sähköpostiosoite
function validate_email(addr) {
	 //Minimi 2 letter local-addr, min 2 letter domain-name.
	 //I. Yksi ja vain yksi @
	 //II. ainakin kaksi merkkiä local-addr.
	 //III. ainakin kahden merkin domain.
	 //IV. ainakin kahden merkin top.
	 //V. viimeisen pisteen jälkeen ainakin 2 merkin top.
	 tmp = addr.split("@");
	 if (tmp.length == 2 
			&& tmp[0].length > 1 
			&& tmp[1].length > 3
			&& tmp[1].lastIndexOf(".") < (tmp[1].length - 2)
		) {
			return true;	 }
	 else { return false; 
	 }
}

//Tarkistaa lomakkeeseen syötettyjen tietojen oikeellisuuden
function verify_subscription_data(form) {
	//Mahd. edelliset errorit piiloon
	document.getElementById("invalid_email").style.display = "none";
	document.getElementById("please_check_email").style.display = "none";
	document.getElementById("invalid_name").style.display = "none";

	//syötetyt arvot lomakkeesta
	var userEmail1 = form.strEmail.value;
  var userEmail2 = form.strEmailVerify.value;
	var strFirstName = form.strFirstName.value;
	var strSurname = form.strSurname.value;

	var nameValid = strSurname.length > 1 && strFirstName.length > 1;
	var emailValid = validate_email(userEmail1);

	if (!emailValid) { //säpö invalidissa muodossa
		document.getElementById("invalid_email").style.display = "block";
		document.getElementById('strEmail').focus();
		return false;
	}

	if (userEmail1 != userEmail2) { //ei identtiset mailiosoitteet
		document.getElementById("please_check_email").style.display = "block";
		document.getElementById('strEmail').focus();	
		return false;
	}

	if (!nameValid) { //nimikentissä ei tietoa
		document.getElementById("invalid_name").style.display = "block";
		return false;
	}

	//jos email ok ja identtiset, palautetaan tosi
	if (nameValid && emailValid && userEmail1 == userEmail2) {
			return true;
	}

}
