//////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////
/// PRIVATE FUNCTIONS

function show(){
	var Digital=new Date();
	var hours=Digital.getHours();
	Digital.setHours(hours-(Digital.getTimezoneOffset()/60));	
	var minutes=Digital.getMinutes();
	var seconds=Digital.getSeconds();
	var tics = Digital.getMilliseconds();
	tics = Math.round(tics/10) - 1;
	if (tics<=9){tics="0"+tics}
	var output2 = Digital.toUTCString();
	var output2 = output2.replace(/ UTC/,"");
	var output2 = output2.replace(/ GMT/,"");
	//var output2 = output2+":"+tics;
	
	if (hours<=9){hours="0"+hours}
	if (minutes<=9){minutes="0"+minutes}
	if (seconds<=9){seconds="0"+seconds}
	document.Tick.Clock.value=output2;
	setTimeout("show()",1000)
}

function hidestatus(){
window.status=''
return true
}

function isValidEmail(str) {
   return (str.indexOf(".") >= 1) && (str.indexOf("@") > 0);
 
}


function CheckSubmit(){
	var checkpwd = true;
	var checkoldpwd = true;
	var checkname = true;
	var checkemail = true;
	var profile = true;
	var first_empty = true;
	
	// Check if profile is filled in
	if (document.profileform.company.value == ""){
		var profile = false;
		document.profileform.company.focus();
		alert('You need to fill in your company name.');
		var first_empty = false;
	}
	if (document.profileform.address.value == ""){
		var profile = false;
		if(first_empty == true){
			document.profileform.address.focus();alert('You need to fill in your address.');}
		var first_empty = false;
	}
	if (document.profileform.zip.value == ""){
		var profile = false;
		if(first_empty == true){document.profileform.zip.focus();alert('You need to fill in your postcode.');}
		var first_empty = false;
	}
	if (document.profileform.city.value == ""){
		var profile = false;
		if(first_empty == true){document.profileform.city.focus();alert('You need to fill in your city name.');}
		var first_empty = false;
	}
	if (document.profileform.country.value == ""){
		var profile = false;
		if(first_empty == true){document.profileform.country.focus();alert('You need to fill in your country name.');}
		var first_empty = false;

	}
	if (document.profileform.c_name_first.value == ""){
		var profile = false;
		if(first_empty == true){document.profileform.c_name_first.focus();alert('You need to fill in your contact persons first name.');}
		var first_empty = false;
	}
	if (document.profileform.c_name_last.value == ""){
		var profile = false;
		if(first_empty == true){document.profileform.c_name_last.focus();alert('You need to fill in your contact persons last name.');}
	}
	if (document.profileform.c_title.value == ""){
		var profile = false;
		if(first_empty == true){document.profileform.c_title.focus();alert('You need to fill in your contact persons title.');}
		var first_empty = false;
	}
	
	if (document.profileform.c_phone.value == ""){
		var profile = false;
		if(first_empty == true){document.profileform.c_phone.focus();alert('You need to fill in your contact persons phone number.');}
		var first_empty = false;
	}
	///////////////////////////////
	
	// Check if passwrods are the same
	if (document.profileform.loginPasA.value != document.profileform.loginPasB.value ){
		var checkpwd = false;
		alert('The passwords you entered do not match.\n Please enter you password again.');
	}
	
	// Check if password is correct for profile update
	if (document.profileform.profile.value == 'true'){
		if(document.profileform.oldhash.value != hex_hmac_md5(document.profileform.oldname.value, document.profileform.loginPasOld.value)){
			var checkoldpwd = false;
			alert('You have entered you Old Password incorrectly.');
		}
	}
	
	// Check if password 6<length<10
	var pwd_length=document.profileform.loginPasA.value
	if((document.profileform.profile.value == 'false')||(document.profileform.profile.value == 'true' && pwd_length !='')){	
		if((pwd_length.length<6) || (pwd_length.length>10)){
			var checkpwd = false;
			alert('The passwords needs to have  a minimum of 6 and a maximum of 10 Characters.\n Please enter you passwords again.');
		}
	}
	// Check if Username is valid
	var name_length=document.profileform.name.value
	if((name_length.length<4) || (name_length.length>10)){
		var checkname = false;
		alert('The username needs to have a minimum of 4 and a maximum of 10 Characters.\n Please enter you username again.');
	}
	
	
	// Check Email
	var emailvalid = isValidEmail(document.profileform.email.value);
	if(emailvalid== false){
		var checkemail = false;
		alert('Please enter a valid email address.');
	}
	
	// Check for Error and Send
	if(checkname == false){
		document.profileform.name.value = '';
		document.profileform.loginPasA.value = '';
		document.profileform.loginPasB.value = '';
		document.profileform.name.focus();
	}else if(checkemail == false){
		document.profileform.email.value = '';
		document.profileform.loginPasA.value = '';
		document.profileform.loginPasB.value = '';
		document.profileform.email.focus();
	}else if(checkpwd == false){
		document.profileform.loginPasA.value = '';
		document.profileform.loginPasB.value = '';
		document.profileform.loginPasA.focus();
	}else if(checkoldpwd == false){
		document.profileform.loginPasOld.value = '';
		document.profileform.loginPasOld.focus();
	}else if(first_empty == false){
	
	}else{
		if(document.profileform.loginPasA.value==''){
			if(document.profileform.profile.value=='true'){
				document.profileform.md5.value = hex_hmac_md5(document.profileform.name.value, document.profileform.loginPasOld.value);					
			}else{
				document.profileform.md5.value = "";
			}
		}else{
				document.profileform.md5.value = hex_hmac_md5(document.profileform.name.value, document.profileform.loginPasA.value);
		}
		//alert(document.profileform.md5.value);
		document.profileform.loginPasA.value = '';
		document.profileform.loginPasB.value = '';	
//prepare final check
            checkregistration="Username "+document.profileform.name.value+", you have entered the following information:\n\n";
            checkregistration=checkregistration+document.profileform.c_title.value+" "+document.profileform.c_name_first.value+" "+document.profileform.c_name_last.value+"\n";
            checkregistration=checkregistration+document.profileform.c_nationality.value+"\n\n";
            checkregistration=checkregistration+document.profileform.company.value+"\n";
            checkregistration=checkregistration+document.profileform.address.value+"\n";        
            checkregistration=checkregistration+document.profileform.zip.value+"\n";        
            checkregistration=checkregistration+document.profileform.city.value+"\n";
            checkregistration=checkregistration+document.profileform.country.value+"\n";
            checkregistration=checkregistration+document.profileform.c_phone.value+"          "+document.profileform.c_mobile.value+"\n";
            checkregistration=checkregistration+document.profileform.email.value+"\n Themes: ";
            if(document.profileform.t_oceans.checked){checkregistration=checkregistration+" Oceans *";}
            if(document.profileform.t_atmosphere.checked){checkregistration=checkregistration+" Atmosphere *";}
            if(document.profileform.t_cryosphere.checked){checkregistration=checkregistration+" Cryosphere *";}
            if(document.profileform.t_landsurface.checked){checkregistration=checkregistration+" Land surface *";}
            if(document.profileform.t_solidearth.checked){checkregistration=checkregistration+" Solid earth *";}
            checkregistration=checkregistration+"\n\n Select OK to continue or Cancel to return to the form \n";

		var answer = confirm(checkregistration);
            	if (answer){document.profileform.submit();}
	}
}

function logincrypt() {
	var hash = hex_hmac_md5(loginform.username.value, loginform.password.value);
	//alert(hash);
	loginform.password.value = hash;
	loginform.action="index.php?fuseaction=login.login";
	loginform.submit();
}

function resendcrypt() {

	var emailcheck = isValidEmail(resendform.email.value)	
	if(emailcheck == false){
		resendform.email.value = '';
		alert('Please enter a valid email address.');
	}else{
		resendform.email.value = hex_hmac_md5(resendform.username.value, resendform.email.value);
		//resendform.email.value = '';
		resendform.action="index.php?fuseaction=login.resendaction";
		resendform.submit();
	}
}

function show(){
	var Digital=new Date();
	var hours=Digital.getHours();
	Digital.setHours(hours-(Digital.getTimezoneOffset()/60));	
	var minutes=Digital.getMinutes();
	var seconds=Digital.getSeconds();
	var tics = Digital.getMilliseconds();
	tics = Math.round(tics/10) - 1;
	if (tics<=9){tics="0"+tics}
	var output2 = Digital.toUTCString();
	var output2 = output2.replace(/ UTC/,"");
	var output2 = output2.replace(/ GMT/,"");
      var output2 = output2.substr(4,18);

	//var output2 = output2+":"+tics;
	
	if (hours<=9){hours="0"+hours}
	if (minutes<=9){minutes="0"+minutes}
	if (seconds<=9){seconds="0"+seconds}
	document.Tick.Clock.value=output2;
	setTimeout("show()",1000)
}
