/**
 * jQuery to call in supersleight (png fix for IE6)
 * 
 * DOES NOT BREAK OTHER BROWSERS!!
 * 
 */
$(document).ready(function()
{
	$('body').supersleight({shim: '/images/blank.gif'});
});

/**
 * Email Checking function
 * Calls ajax.php to check existing email address
 * 
 */
$(document).ready(function()
{
	// When we focus out of the email box
	$('#email').blur(function()
	{
		
		// If it has a value
		if($(this).val() != "")
		{
			$(this).parent().addClass('busy');
			
			// Pop the value of it into a var
			var emailAddress = $(this).val();
			var companyRef	 = $('#companyRef').val();
			
			// Now ajax to the check email page
			$('#emailmessage').load("/ajax/ajax.php", {ajax:"checkExistingEmail", email:emailAddress, company:companyRef}, function()
			{
				$('#email').parent().removeClass('busy');
			});
			
		}
		
	});
	
});

// Function enabler
$(document).ready(function()
{
	// Fire this function when we submit the form
	// We have to do it this way so the form fails
	// if validation is not a success
	$('#form1').submit(function()
	{
		// Firstly assume ready
		var ready = true;
		
		// Clear all invalid classes on the td's
		$('td').removeClass('invalid');
		
		// Now go through each field and validate it
		
		if($('#firstname').val() == "")
		{
			// This adds invalid class to the td wrapper
			$('#firstname').parent().addClass('invalid');
			// Make ready false
			ready = false;
			// Alerts the user
			alert("Please enter your Firstname to continue");
			// And returns false for the form submit
			return false;
		}
		
		// Now continue down the form fields in the same way
		
		if($('#surname').val() == "")
		{
			$('#surname').parent().addClass('invalid');
			ready = false;
			alert("Please enter your Surname to continue");
			return false;
		}
		
		if($('#email').val() == "")
		{
			$('#email').parent().addClass('invalid');
			ready = false;
			alert("Please enter your Email Address to continue");
			return false;
		}
		
		if(checkValidEmail($('#email').val()) != true)
		{
			$('#email').parent().addClass('invalid');
			ready = false;
			return false;
		}
		
		if($('#email').val() != $('#email2').val())
		{
			$('#email').parent().addClass('invalid');
			$('#email2').parent().addClass('invalid');
			ready = false;
			alert("Please enter the same email address in both boxes to continue");
			return false;
		}
		
		if($('#error').val() == "emailinuse")
		{
			$('#email').parent().addClass('invalid');
			ready = false;
			alert("You have entered an email address that is already in use, if you have already registered an account with us then please return to MyRQ using the login page. If you believe this is in error please contact us.");
			return false;
		}
		
		if($('#error').val() == "requirednotmet")
		{
			$('#email').parent().addClass('invalid');
			ready = false;
			alert("You have entered an email address that that is not valid for this login portal. Please check your spelling or use a different email address. If you are having trouble pelase contact us.");
			return false;
		}
		
		if($('#password').val() == "")
		{
			$('#password').parent().addClass('invalid');
			ready = false;
			alert("Please enter a password to continue.");
			return false;
		}
		
		var $pwLength = $('#password').val().length;
		
		if($pwLength < 6)
		{
			$('#password').parent().addClass('invalid');
			ready = false;
			alert("Please enter a password that is at least 6 characters long.");
			return false;
		}
		
		if($('#password').val() != $('#password2').val())
		{
			$('#password').parent().addClass('invalid');
			$('#password2').parent().addClass('invalid');
			ready = false;
			alert("Please enter the same password in both boxes to continue");
			return false;
		}
		
		if(ready == true)
		{
			$('td').removeClass('invalid');
		}
		else
		{
			return false;
		}
	});
	
	// This will enable the custom text field if we have selected other
	$('#sector').change(function()
	{
		var sector = $("#sector option:selected").text();
		
		if(sector == "Other Please Specify Below...")
		{
			$('#sectorName').val("");
			$('#sectorName').slideDown(200);
		}
		else
		{
			$('#sectorName').val(sector);
			$('#sectorName').slideUp(200);
		}
	});
	
	// Quick check to see if we have selected other and have a refresh
	var sector = $("#sector option:selected").text();
	
	if(sector == "Other Please Specify Below...")
	{
		$('#sectorName').val("");
		$('#sectorName').slideDown(200);
	}
	else
	{
		$('#sectorName').val(sector);
		$('#sectorName').slideUp(200);
	}
	
	// We need to validate the form if we have company fields
	$('#form2').submit(function()
	{
		// Grab the var if it's there
		var questions = $('#customFields').val();
		
		// If this is not blank then run our checks
		if(questions != "")
		{
			// Create an array of these items
			var myArray = questions.split(',');
			
			// Now loop through them
			for(var i in myArray)
			{
				// Store the answer in a var
				var answer = $('#companyQuestion' + myArray[i]).val();
				
				// Now check for invalid answers
				if(answer == "0")
				{
					// Invalid answer, alert and return false
					alert("Please answer the compulsory questions before continuing.");
					return false;
				}
				else if(answer == "")
				{
					// Invalid answer, alert and return false
					alert("Please answer the compulsory questions before continuing.");
					return false;
				}
				else
				{
					// Don't do anything because if we return a true it will submit!
				}
			}			
		}
	});
	
	// End of loader functions
});


// This is our email validation script
function checkValidEmail(str) 
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}
		
	if (str.indexOf(" ")!=-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

 		return true;				
}

$(document).ready(function()
{
	$('#change-pass').submit(function()
	{
		// Lets check the password fields
		var valid = true;
		
		if($('#old-pass').val() == "")
		{
			valid = false;
			alert("Please enter your old password");
			return false;
		}
		
		if($('#password').val() == "")
		{
			ready = false;
			alert("Please enter a password to continue.");
			return false;
		}
		
		var $pwLength = $('#password').val().length;
		
		if($pwLength < 6)
		{
			ready = false;
			alert("Please enter a new password that is at least 6 characters long.");
			return false;
		}
		
		if($('#password').val() != $('#password2').val())
		{
			ready = false;
			alert("Please enter the same password in both boxes to continue");
			return false;
		}
		
		if(valid == true)
		{
			return true;
		}
		else
		{
			return false;
		}
	});
});


