addLoadListener(init);

function init(){
  var submit = document.getElementById("submit");
  submit.onclick = validate;
  
  document.getElementById('labelwin').onclick = function(){
    var survey = makePopup(this.href,600,450,'resize');
    return survey.closed;
  };

  return true;
};

function addLoadListener(fn){
  if (typeof window.addEventListener != 'undefined'){
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined'){
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined'){
    window.attachEvent('onload', fn);
  }
  else{
    var oldfn = window.onload;
    if (typeof window.onload != 'function'){
      window.onload = fn;
    }
    else{
      window.onload = function(){
        oldfn();
        fn();
      };
    }
  }
};

function makePopup(url,width,height,overflow){
	// window maker
	if(width>640){width=640;}
	if(height>480){height=480;}
	if(overflow=='' || !/^(scroll|resize|both)$/.test(overflow)){
		overflow='both';
	}
	var win=window.open(url,'','width='+width+',height='+height
				+',scrollbars=no,resizable=yes,status=yes,toolbar=no,menubar=no,location=no');
	return win;
}

function validate(){
	// get elements
	var uemail=document.getElementById("user");
	var uacc=document.getElementById("account");
	var upw=document.getElementById("pass");
	
	var rep = /^[A-Za-z0-9]{6,10}$/;
	var reu = /[^A-Za-z0-9]/;
	
	// validate the data
	if(uemail.value==""){
		alert('Please enter email address.');
		uemail.focus();
		uemail.style.background = "yellow";
		return false;
	}
	if(!verifyEmail(uemail.value)){
		alert('Please enter a valid email address.');
		uemail.focus();
		uemail.style.background = "yellow";
		return false;
	}
	// check password for invalid chars
	if(reu.test(uacc.value)){
		alert('Invalid Username! Only alphanumeric characters!');
		uacc.style.background = "yellow";
		uacc.focus();
		return false;
	}
	// check password for invalid chars
	if(!rep.test(upw.value)){
		alert('Invalid Password! Only alphanumeric characters and at least 6 characters long!');
		upw.style.background = "yellow";
		upw.focus();
		return false;
	}
}	// end validate function

function verifyEmail(checkEmail){
	var emailPattern = /^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/;
	if (!emailPattern.test(checkEmail)){return false;}
	else{return true;}
}
