/*======================================================================*\
|| #################################################################### ||
|| # ÜberNet Javascript Libary                                        # ||
|| # ---------------------------------------------------------------- # ||
|| # All Javascript in this file is ©2000-2007 ÜberNet.               # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # --------------- THIS FILE IS NOT FREE SOFTWARE ----------------- # ||
|| # http://www.ubernet.biz  |  http://www.ubernet.biz/license.unc    # ||
|| #################################################################### ||
\*======================================================================*/

//Script Requirements
require_once('tooltips');

//Displays a tooltip under the given element with the given message
function displayError( field, errorMessage )
{
	$('tooltipMessage').innerHTML = errorMessage;
	showTooltip( field );
	$( field ).focus();
}

function displayHint( field, hint, width )
{
	$('hintToolTipMessage').innerHTML = hint;
	showTooltip( field, 'hintToolTip', 3, width );
	$( field ).focus();	
}

//Calculates input for password strength
function calcPasswordStrength( password )
{
	var strength = 0;
	var combo = 1;
	
	//Give a strength score for the length of the password
	if (password.length > 15)
	{
		strength += 18;
	} 
	else if (password.length > 8)
	{
		strength += 12;
	} 
	else if (password.length > 4)
	{
		strength += 6;
	}
	
	//Check for lower case and upper case numbers
	if (password.match(/[a-z]/)) //Has a lower case character
	{
		strength += 1;
	}
	
	if (password.match(/[A-Z]/)) //Has a captial character
	{
		strength += 5;
	}
	
	//Number checks
	if (password.match(/\d+/)) //At least one number
	{
		strength += 5;
	}
	
	if (password.match(/(.*[0-9].*[0-9].*[0-9])/)) //Has three numbers
	{
		strength += 5;
	}
	
	// Special Characters
	if (password.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)) //Has a special character
	{
		strength += 5;
	}
	
	if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) //Has two special characters
	{
		strength += 5;
	}
	
	//Combinations
	if (password.match(/[a-z]/) && password.match(/[A-Z]/)) //Has a mixture of captial and lower case letters
	{
		combo += 10;
	}
	
	if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) // Contains both letters and numbers
	{
		combo += 10;
	}
	
	if (password.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
	{
		combo += 10;
	}
	
	return (strength + combo);
}

//Update the calculation of the password strength and display the tooltip
function updatePasswordStrength( passwordField, passwordTooltip, strengthLayer, strengthBar, event)
{
	//These three if statements all check for random key presses in which we dont want this tooltip coming up on.
	
	//Everything before numbers (Expect Delete Key & Backspace)
	if ((event.keyCode < 48) && (event.keyCode != 8) && (event.keyCode != 46))
		return false;
		
	//Random Control Keys
	if ((event.keyCode > 90) && (event.keyCode < 96))
		return false;
		
	//Function Keys -> Semi Colon
	if ((event.keyCode > 111) && (event.keyCode < 186))
		return false;
	
	//Calcuate the password strength
	var strength = calcPasswordStrength( $(passwordField).value );
	
	var verdict = "Please Enter Password";
	var barLength = 0;
	var colour = '';
	
	if (strength > 40)
	{
		verdict = "Very Strong";
		colour = "#3bce08";
		barLength = 100;
	}
	else if (strength > 30)
	{
		verdict = "Strong";
		colour = "#8ace08";
		barLength = 75;
	}
	else if (strength > 20)
	{
		verdict = "Average";
		colour = "#b4ce08";
		barLength = 50;
	}
	else if (strength > 10)
	{
		verdict = "Weak";
		colour = "#ce8a08";
		barLength = 25;
	} else {
		verdict = "Very Weak";
		colour = "#f32e1a";
		barLength = 10;
	}
	
	$(strengthLayer).innerHTML = verdict;
	$(strengthLayer).style.color = colour;
	$(strengthBar).style.backgroundColor = colour;
	$(strengthBar).style.width = barLength + "%";
	
	//Display the password tooltip
	showTooltip( passwordField, passwordTooltip, 0.75 );
	
}

function checkMatchBetweenFields( field1, field2, whatIsBeingMatched )
{
	//If the two fields dont match we have an error
	if ( $(field1).value != $(field2).value )
	{
		displayError( field2, 'The ' + whatIsBeingMatched + ' you entered do not match.' )
	}
}

function checkEmailInput( emailField )
{
	//Create a filter for an email address and test it against the field given
	if (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test( $(emailField).value ) == false)
	{
		displayError( emailField, 'Invalid email address.');
	}
}

function checkLoginField( username )
{
	//Create a filter for an email address and test it against the field given
	if (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test( $(username).value ) == false)
	{
		displayError( username, 'Your username is the email address you registered with.');
	}
}

function doRegForm()
{
    if(document.newuser.email.value == "" || document.newuser.email2.value == "" || document.newuser.password.value == "" || document.newuser.password2.value == "" ||
       document.newuser.firstname.value == "" || document.newuser.lastname.value == "" || document.newuser.email.value == "" || document.newuser.phonecc.value == "" ||
       document.newuser.phone.value == "" || document.newuser.address1.value == "" || document.newuser.city.value == "" || document.newuser.state.value == "" ||
       document.newuser.country.value == "" || document.newuser.zip.value == "")
    {
        alert('Please ensure that all required fields are completed.');
        return false;
    }
    
    if(document.newuser.email.value != document.newuser.email2.value)
    {
        alert('Your email addresses do not match. Please check them.');
        return false;
    }
    
    if(document.newuser.password.value.length < 8)
    {
        alert('Your password is too short. Please correct this.');
        return false;     
    }
    
    if(document.newuser.password.value != document.newuser.password2.value)
    {
        alert('Your passwords addresses do not match. Please check them.');
        return false;    
    }
    
    if(document.newuser.country.value == "GB")
    {
        if(/^[A-Z]{1,2}[\d]{1,2}([A-Z])?\s?[\d][A-Z]{2}$/.test( document.newuser.zip.value.toUpperCase() ) == false) 
        {
            alert('Please enter a valid UK post code.');
            return false;    
        }
    }
        
    var dictionary = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var salt = dictionary.substr(Math.random()*52,1) + dictionary.substr(Math.random()*52,1) + dictionary.substr(Math.random()*52,1) + dictionary.substr(Math.random()*52,1) + dictionary.substr(Math.random()*52,1); 
    var pw = hex_md5( hex_md5(salt) + hex_md5(document.newuser.password.value) );
   
    var input1 = document.createElement("input");
    input1.type = "hidden";
    input1.name = "md5";
    input1.value = pw;
    document.newuser.appendChild(input1);
    
    var input2 = document.createElement("input");
    input2.type = "hidden";
    input2.name = "salt";
    input2.value = salt;
    document.newuser.appendChild(input2);
    
    document.newuser.password.value = "";
    document.newuser.password2.value = "";
    
    return true;
}

function domainNamesHint()
{
	displayHint('domainName', 'To enter more than one,<br/>place each name on seperate lines.');
}

var tldsToggle = true;
function domainTldsToggle()
{
	if(tldsToggle == false)
	{
		for(i=0; $('tld'+i); i++)
		{
			$('tld'+i).checked = "";
		}
		tldsToggle = true;
	}
	else
	{
		for(i=0; $('tld'+i); i++)
		{
			$('tld'+i).checked = "checked";
		}	
		tldsToggle = false;
	}
}

function domainNamesCheck()
{
	
}

function domainTldsCheck()
{
	
}