//<![CDATA[
function trim(s)
{
    var l = 0; 
    var r = s.length -1;
    
    while(l < s.length && s[l] == ' ')
    {    l++; }
    while(r > l && s[r] == ' ')
    {    r-=1;    }
    
    return s.substring(l, r+1);
}

function IsFieldEmpty(field)
{
    field = trim(field);
    if(field == "")
    {
        return true;
    }
    else
    {
        return false;
    }
}

function IsDateExceeded(year, month)
{
    var yearInt = parseInt(year);
    var monthInt = parseInt(month);
    
    var curDate = new Date();
    
    var expirationDate = new Date(yearInt,(monthInt-1), 30);

    return (curDate > expirationDate);
}

function IsNumeric(sText)
{
    var validChars = "0123456789";
    var isNumber = true;
    var character;

    for (i = 0; i < sText.length && isNumber == true; i++) 
    {  
        character = sText.charAt(i); 
        if (validChars.indexOf(character) == -1) 
        {
            isNumber = false;
        }
    }
    
   return isNumber;
}

function IsEmail(sText)
{
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	
	return filter.test(sText);
}

function ValidatePwd(pw1,pw2) 
{
	var invalid = " "; // Invalid character is a space

	// check for a value in both fields.
	if ((pw1 != '' && pw2 == '') || (pw1 == '' && pw2 != '')) 
	{
		alert('Please enter your password twice.');
		return false;
	}

	// check for spaces
	if (pw1.indexOf(invalid) > -1)
	{
		alert("Sorry, spaces are not allowed. Please re-enter your password.");
		return false;
	}
	else 
	{
		if (pw1 != pw2)
		{
			alert ("You did not enter the same password twice. Please re-enter your password.");
			return false;
		}
		else
		{
			return true;
		}
	}
}

function ValidateEmail(email1, email2)
{
    email1 = trim(email1);
    email2 = trim(email2);
    
    // check for a value in both fields.
    if ((email1 == '') || (email2 == '')) 
    {
        alert('Please enter your email twice.');
        return false;
    }

    if(!IsEmail(email1))
	{
    	alert('Please provide a valid email address.');
		return false;
	}
    else 
    {
    	if (email1 != email2) 
        {
            alert ("You did not enter the same email twice. Please re-enter your email.");
            return false;
        }
    	else
    	{
    		return true;
    	}
    }
}

function LostPasswordForm_Submit(thisform)
{
	var formInvalidClassName = " form-invalid";
	
	with(thisform)
	{
		user_login.className = user_login.className.replace(formInvalidClassName, "");
		
		if(!IsFieldEmpty(user_login.value))
		{
			return true;
		}
		else
		{
			user_login.className += formInvalidClassName;
			alert("The field Username cannot be empty.");
		}
		
		return false;
	}
}

function UserConnectionForm_Submit(thisform)
{
	var formInvalidClassName = " form-invalid";
	
	with(thisform)
	{
		connectionEmail.className = connectionEmail.className.replace(formInvalidClassName, "");
		connectionPassword.className = connectionPassword.className.replace(formInvalidClassName, "");
		
		if(!IsFieldEmpty(connectionEmail.value))
		{
			if(!IsFieldEmpty(connectionPassword.value))
			{
				return true;
			}
			else
			{
				connectionPassword.className += formInvalidClassName;
				alert("The field Password cannot be empty.");
			}
		}
		else
		{
			connectionEmail.className += formInvalidClassName;
			alert("The field Email cannot be empty.");
		}
		
		return false;
	}
}

function UserRegistrationForm_Submit(thisform)
{
	var formInvalidClassName = " form-invalid";
	
	with(thisform)
	{
		registerEmail.className = registerEmail.className.replace(formInvalidClassName, "");
		registerPassword.className = registerPassword.className.replace(formInvalidClassName, "");
		registerConfirmPassword.className = registerConfirmPassword.className.replace(formInvalidClassName, "");
		registerFirstName.className = registerFirstName.className.replace(formInvalidClassName, "");
		registerLastName.className = registerLastName.className.replace(formInvalidClassName, "");
		registerOrganisation.className = registerOrganisation.className.replace(formInvalidClassName, "");
		
		if(!IsFieldEmpty(registerEmail.value))
		{
			if(IsEmail(registerEmail.value))
			{
				if(!IsFieldEmpty(registerPassword.value))
				{
					if(!IsFieldEmpty(registerConfirmPassword.value))
					{	
						if(ValidatePwd(registerPassword.value, registerConfirmPassword.value))
						{
							if(!IsFieldEmpty(registerFirstName.value))
							{
								if(!IsFieldEmpty(registerLastName.value))
								{
									if(!IsFieldEmpty(registerOrganisation.value))
									{
										return true;
									}
									else
									{
										registerOrganisation.className += formInvalidClassName;
										alert("The field Organization cannot be empty.");
									}
								}
								else
								{
									registerLastName.className += formInvalidClassName;
									alert("The field Last Name cannot be empty.");
								}
							}
							else
							{
								registerFirstName.className += formInvalidClassName;
								alert("The field First Name cannot be empty.");
							}
						}
						else
						{
							registerPassword.className += formInvalidClassName;
							registerConfirmPassword.className += formInvalidClassName;
						}
					}
					else
					{
						registerConfirmPassword.className += formInvalidClassName;
						alert("The field Confirm Password cannot be empty.");
					}
				}
				else
				{
					registerPassword.className += formInvalidClassName;
					alert("The field Password cannot be empty.");
				}
			}
			else
			{
				registerEmail.className += formInvalidClassName;
				alert('Please provide a valid email address.');
			}
		}
		else
		{
			registerEmail.className += formInvalidClassName;
			alert("The field Email cannot be empty.");
		}
		
		return false;
	}
}


function MyProfileForm_Submit(thisform)
{
	var formInvalidClassName = " form-invalid";
	
	with(thisform)
    {
		firstName.className = firstName.className.replace(formInvalidClassName, "");
		lastName.className = lastName.className.replace(formInvalidClassName, "");
		user_email.className = user_email.className.replace(formInvalidClassName, "");
		organizationName.className = organizationName.className.replace(formInvalidClassName, "");
		password.className = password.className.replace(formInvalidClassName, "");
    	confirmpassword.className = confirmpassword.className.replace(formInvalidClassName, "");
    	    	
    	if(!(IsFieldEmpty(firstName.value)))
    	{
    		if(!(IsFieldEmpty(lastName.value)))
    		{
    			if(!(IsFieldEmpty(user_email.value)))
    			{
    				if(IsEmail(user_email.value) )
    				{
    					if(!(IsFieldEmpty(organizationName.value)))
    					{
    						if(IsFieldEmpty(password.value))
    						{
    							return true;
    						}
    						else
    						{
    							if(ValidatePwd(password.value,confirmpassword.value))
    							{
    								return true;
    							}
    							else
    							{
    								password.className += formInvalidClassName; 
    								confirmpassword.className += formInvalidClassName;
    								return false;
    							}
    						}
    					}
    					else
    					{
    						organizationName.className += formInvalidClassName;
    						alert("The field Organization cannot be empty.");
    					}
    				}
    				else
    				{
    					user_email.className += formInvalidClassName;
    					alert('Please provide a valid email address.');
    				}
    			}
    			else
    			{
    				user_email.className += formInvalidClassName;
    				alert("The field Email Address cannot be empty.");
    			}
    		}
    		else
    		{
    			lastName.className += formInvalidClassName;
    			alert("The field Last Name cannot be empty.");
    		}
    	}
    	else
    	{
    		firstName.className += formInvalidClassName;
    		alert("The field First Name cannot be empty.");
    	}
    	
    	return false;
    }    
}

function JoinBetaForm_Submit(thisform)
{
	var formInvalidClassName = " form-invalid";

	with(thisform)
	{
		firstName.className = firstName.className.replace(formInvalidClassName, "");
		lastName.className = lastName.className.replace(formInvalidClassName, "");
		organization.className = organization.className.replace(formInvalidClassName, "");
		jobTitle.className = jobTitle.className.replace(formInvalidClassName, "");
		userEmail.className = userEmail.className.replace(formInvalidClassName, "");
		userEmailConfirmation.className = userEmailConfirmation.className.replace(formInvalidClassName, "");

		if(!IsFieldEmpty(firstName.value))
		{
			if(!IsFieldEmpty(lastName.value))
			{
				if(!IsFieldEmpty(userEmail.value))
				{
					if(!IsFieldEmpty(userEmailConfirmation.value))
					{
						if(ValidateEmail(userEmail.value, userEmailConfirmation.value))
						{
							if(!IsFieldEmpty(organization.value))
							{
								if(!IsFieldEmpty(jobTitle.value))
								{
									return true;  
								}
								else
								{
									jobTitle.className += formInvalidClassName;
									alert("The field Job Title cannot be empty.");
								}
							}
							else
							{
								organization.className += formInvalidClassName;
								alert("The field Organization cannot be empty.");
							}
						}
						else
						{
							userEmail.className += formInvalidClassName;
							userEmailConfirmation.className += formInvalidClassName;
						}
					}
					else
					{
						userEmailConfirmation.className += formInvalidClassName;
						alert("The field Confirm Email Address cannot be empty.");
					}
				}
				else
				{
					userEmail.className += formInvalidClassName;
					alert("The field Email Address cannot be empty.");
				}
			}
			else
			{
				lastName.className += formInvalidClassName;
				alert("The field Last Name cannot be empty.");
			}
		}
		else
		{
			firstName.className += formInvalidClassName;
			alert("The field First Name cannot be empty.");
		}

		return false;
	}
    
    return false;
}

function CheckoutForm_Submit(thisform){

	var formInvalidClassName = " form-invalid";

	with(thisform)
	{
		firstName.className = firstName.className.replace(formInvalidClassName, "");
		lastName.className = lastName.className.replace(formInvalidClassName, "");
		creditCardNumber.className = creditCardNumber.className.replace(formInvalidClassName, "");
		expirationDateYear.className = expirationDateYear.className.replace(formInvalidClassName, "");
		expirationDateMonth.className = expirationDateMonth.className.replace(formInvalidClassName, "");
		cvv2Number.className = cvv2Number.className.replace(formInvalidClassName, "");
		address1.className = address1.className.replace(formInvalidClassName, "");
		city.className = city.className.replace(formInvalidClassName, "");
		zip.className = zip.className.replace(formInvalidClassName, "");
		country.className = country.className.replace(formInvalidClassName, "");

		if(!IsFieldEmpty(firstName.value))
		{
			if(!IsFieldEmpty(lastName.value))
			{
				if(!IsFieldEmpty(creditCardNumber.value))
				{
					if(IsNumeric(creditCardNumber.value))
					{
						if(!IsDateExceeded(expirationDateYear.value, expirationDateMonth.value))
						{
							if(!IsFieldEmpty(cvv2Number.value))
							{
								if(IsNumeric(cvv2Number.value))
								{
									if(!IsFieldEmpty(address1.value))
									{
										if(!IsFieldEmpty(city.value))
										{
											if(!IsFieldEmpty(zip.value))
											{
												if(!IsFieldEmpty(country.value))
												{
													return true;
												}
												else
												{
													country.className +=  formInvalidClassName;
													alert("The field Country cannot be empty.");
												}
											}
											else
											{
												zip.className +=  formInvalidClassName;
												alert("The field Zip Code cannot be empty.");
											}
										}
										else
										{
											city.className +=  formInvalidClassName;
											alert("The field City cannot be empty.");
										}
									}
									else
									{
										address1.className +=  formInvalidClassName;
										alert("The field Address 1 cannot be empty.");
									}
								}
								else
								{
									cvv2Number.className +=  formInvalidClassName;
									alert("The field Verification Number is invalid.");
								}
							}
							else
							{
								cvv2Number.className +=  formInvalidClassName;
								alert("The field Verification Number cannot be empty.");
							}
						}
						else
						{
							expirationDateYear.className +=  formInvalidClassName;
							expirationDateMonth.className +=  formInvalidClassName;
							alert("The Credit Card Expiration Date is exceeded.");
						}
					}
					else
					{
						creditCardNumber.className +=  formInvalidClassName;
						alert("The field Credit Card Number is invalid.");
					}
				}
				else
				{
					creditCardNumber.className +=  formInvalidClassName;
					alert("The field Credit Card Number cannot be empty.");
				}
			}
			else
			{
				lastName.className +=  formInvalidClassName;
				alert("The field Last Name cannot be empty.");
			}
		}
		else
		{
			firstName.className +=  formInvalidClassName;
			alert("The field First Name cannot be empty.");
		}

		return false;
	}
}
//]]>
