// Copyright Urban Fine Art Galleries Ltd 2007

// FORM VALIDATION

// First establish form name and create & initialise array

function validate_form (form_name) {

	switch (form_name) {

		case "reg-trade-address": // Registration Trading Address
		case "contact-details": // Contact Details
		case "update-contact": // Contact Details (update)
		case "update-trade": // Trading Address (update)
			var check_fields = new Array();
				check_fields[0] = "address-line-1";
				check_fields[1] = "city"; 
				check_fields[2] = "postcode"; 
				check_fields[3] = "country";
				check_fields[4] = "province-state";
				check_fields[5] = "telephone"; 
		
			var other_fields = new Array();
			// No other fields for this form
			
			break;

        case "delivery-details": // Delivery Details
			var check_fields = new Array();
				check_fields[0] = "delivery-name";
				check_fields[1] = "address-line-1";
				check_fields[2] = "city"; 
				check_fields[3] = "postcode"; 
				check_fields[4] = "country";
				check_fields[5] = "province-state";
				check_fields[6] = "telephone"; 
		
			var other_fields = new Array();
			// No other fields for this form

			break;

        case "publication-details": // Delivery Details
			var check_fields = new Array();
				check_fields[0] = "requester-name";
				check_fields[1] = "address-line-1";
				check_fields[2] = "city"; 
				check_fields[3] = "postcode"; 
				check_fields[4] = "country";
				check_fields[5] = "email-address";
				check_fields[6] = "province-state";
				check_fields[7] = "heard-about";
		
			var other_fields = new Array();
			// No other fields for this form
			
			break;

		case "poa": // Payment On Account
			var check_fields = new Array();
				check_fields[0] = "account-number";
				check_fields[1] = "purchase-order"; 
				check_fields[2] = "security-phrase"; 
		
			var other_fields = new Array();
			// No other fields for this form
			
			break;
			
		case "wishlist-email": // Wish list email
		case "email-a-friend": // Email a friend Popup
			var check_fields = new Array();
				check_fields[0] = "from-name";
				check_fields[1] = "from-email"; 
				check_fields[2] = "recipient-name"; 
				check_fields[3] = "recipient-email"; 
		
			var other_fields = new Array();
			// No other fields for this form

			break;
		case "contact_us": // Contact us Popup

			var check_fields = new Array();  
				check_fields[0] = "enquiry-type";
				check_fields[1] = "from-name"; 
				check_fields[2] = "from-email"; 
				check_fields[3] = "telephone"; 
				check_fields[4] = "message"; 
		
			var other_fields = new Array();
			// No other fields for this form

			break;	

		case "help": // Help Popup
			var check_fields = new Array();
				check_fields[0] = "from-name";
				check_fields[1] = "from-email"; 
				check_fields[2] = "telephone"; 
				check_fields[3] = "message"; 
		
			var other_fields = new Array();
			// No other fields for this form

			break;
		
		case "wishlist-rename": // Rename Wish list
			var check_fields = new Array();
				check_fields[0] = "wish-list-name";
		
			var other_fields = new Array();
			// No other fields for this form

			break;	

		case "login": // Login 
			var check_fields = new Array();
				check_fields[0] = "login-id";
				check_fields[1] = "login-password"; 
			
			var other_fields = new Array();
				other_fields[0] = "title";
				other_fields[1] = "first-name";
				other_fields[2] = "last-name"; 
				other_fields[3] = "email-address"; 
				other_fields[4] = "password"; 
				other_fields[5] = "confirm-password";
	
			break;
			
		case "subscriptions-login": // Subscriptions Login
			var check_fields = new Array();
				check_fields[0] = "login-id";
				check_fields[1] = "login-password"; 
			
			var other_fields = new Array();
			
			break;

		case "password-reminder": // Forgotten Password
			var check_fields = new Array();
				check_fields[0] = "login-id";
			
			var other_fields = new Array();
			
			break;
			
		case "register": // Registration
			var check_fields = new Array();
				check_fields[0] = "first-name";
				check_fields[1] = "last-name"; 
				check_fields[2] = "email-address"; 
				check_fields[3] = "password"; 
				check_fields[4] = "confirm-password";
	
			var other_fields = new Array();
				other_fields[0] = "login-id";
				other_fields[1] = "login-password"; 
				
			var passwordConfirmCheck = true;
				
			break;
	
		case "reg-trade-details": // Registration Trade Details
			var check_fields = new Array();
				check_fields[0] = "company-name";
				check_fields[1] = "company-nature"; 
	
			var other_fields = new Array();
				
			break;
			
		case "update_login": // Login Account (update)
			var check_fields = new Array();
				check_fields[0] = "first-name";
				check_fields[1] = "last-name"; 
				check_fields[2] = "email-address"; 
				check_fields[3] = "password"; 
				check_fields[4] = "confirm-password";
	
			var other_fields = new Array();
			var passwordConfirmCheck = true;
			
			break;
			
        case "info-req-consumer": // Information Required: Consumer
        case "info-req-trade": // Information Required: Trade
    
            var check_fields = new Array();
                check_fields[0] = "first-name";
                check_fields[1] = "last-name"; 
            
            var other_fields = new Array();

            break;

        case "returns": // Returns Form
    
        var check_fields = new Array();
            check_fields[0] = "order-number";
            check_fields[1] = "product-code"; 
            check_fields[2] = "reason"; 
            check_fields[3] = "customer-name";
            check_fields[4] = "address-line-1";
            check_fields[5] = "city"; 
            check_fields[6] = "postcode"; 
            check_fields[7] = "country";
            check_fields[8] = "province-state";
            check_fields[9] = "telephone"; 
            check_fields[10] = "login-id";
        
        var other_fields = new Array();

        break;
    }
	
	// Next reset user-message and set errors to 0
	var errors = 0;
	var message = document.getElementById("user-message");
	var msgString = "";
	message.innerHTML = "";
	
	// Reset other_fields which are not being checked
	
	for (i=0; i < other_fields.length; i++)
		{	var x = document.getElementById(other_fields[i]);
			x.style.borderColor = "#999999";
			x.style.backgroundColor = "#ffffff";
			x.value = "";
			x.onblur();
		}
	
	// Now validate fields
	for (i=0; i < check_fields.length; i++)
		{	var x = document.getElementById(check_fields[i]);

			switch (check_fields[i]) {
			
				case "login-id":
				case "email-address":
				case "from-email":
				case "recipient-email":
					{	if (x.value=="Enter your email address here" || x.value=="")
							{
								x.style.borderColor = "#ff5404";
								x.style.backgroundColor = "#ffdbca";
								errors++;
								msgString = "Required field, please retry.";
							}
						else
							{
								// Check to see if the id is a valid email address
                                // Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/
                                var str = x.value;
                        		var at="@"
                        		var dot="."
                        		var lat=str.indexOf(at)
                        		var lstr=str.length
                        		var ldot=str.indexOf(dot)
                                var invalid_email = false;
                                
                        		if (str.indexOf(at)==-1)
                                    {invalid_email = true;}
                        		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
                                    {invalid_email = true;}
                        		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
                                    {invalid_email = true;}
                        		if (str.indexOf(at,(lat+1))!=-1)
                                    {invalid_email = true;}
                        		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
                                    {invalid_email = true;}
                        		if (str.indexOf(dot,(lat+2))==-1)
                                    {invalid_email = true;}
                        		if (str.indexOf(" ")!=-1)
                                    {invalid_email = true;}
                                
								//var atPos = x.value.indexOf("@")
								//var stopPos = x.value.lastIndexOf(".")

								if (invalid_email) 
									{	errors++;
										if (msgString=="") msgString = "Email address is invalid, please retry.";
										x.style.borderColor = "#ff5404";
										x.style.backgroundColor = "#ffdbca";
									}
								else
									{	x.style.borderColor = "#999999";
										x.style.backgroundColor = "#ffffff";
									}

                                
                                var pageCheck = true;
                                // Ignore help and contact pages
                                if (document.getElementById("help") || document.getElementById("contact-form")) {
                                    pageCheck = false;
                                }
                                
                                if (str.indexOf("hotmail")!=-1 && pageCheck) {
                                    errors++;
                                    msgString = "Regrettably Hotmail addresses are not supported."
                                    x.style.borderColor = "#ff5404";
                                    x.style.backgroundColor = "#ffdbca";
                                    x.value = "";
                                }
							}
					}
					break;
				
				case "login-password":
				case "password":
				case "confirm-password":
					{
						if (x.value=="")
							{	errors++;
								if (msgString=="") msgString = "Required field, please retry.";
								x.style.borderColor = "#ff5404";
								x.style.backgroundColor = "#ffdbca";
							}
						else
							{	if (x.value.length < 6) 
									{	errors++
										if (msgString=="" || msgString=="Required field, please retry.") 
											{	msgString = "Password less than 6 characters, please retry.";
											}
										x.style.borderColor = "#ff5404";
										x.style.backgroundColor = "#ffdbca";
									}
								else
									{	x.style.borderColor = "#999999";
										x.style.backgroundColor = "#ffffff";
									}
							}
					}
					break;

				case "province-state":
					{	var countryValue = document.getElementById("country").value;
						if (countryValue=="CA" || countryValue=="US") 
							{	if (x.value=="")
									{	x.style.borderColor = "#ff5404";
										x.style.backgroundColor = "#ffdbca";
										errors++;
										msgString = "Required field, please retry.";
									}
								else
									{	x.style.borderColor = "#999999";
										x.style.backgroundColor = "#ffffff";
									}
							}
					}
					
					break;
				
				default:
					{	if (x.value=="")
							{	x.style.borderColor = "#ff5404";
								x.style.backgroundColor = "#ffdbca";
								errors++;
								msgString = "Required field, please retry.";
							}
						else
							{	x.style.borderColor = "#999999";
								x.style.backgroundColor = "#ffffff";
							}
					}
					
					break;
			}
		}
	
	// Check to see if password & confirm password match
	if (errors==0 && passwordConfirmCheck) {
		
		var password = document.getElementById("password");
		var confirmPassword = document.getElementById("confirm-password");
		
		if (password.value != confirmPassword.value)
			{	errors++
				msgString = "Passwords do not match, please retry.";
				password.style.borderColor = "#ff5404";
				password.style.backgroundColor = "#ffdbca";
				confirmPassword.style.borderColor = "#ff5404";
				confirmPassword.style.backgroundColor = "#ffdbca";
			}
		else
			{
				password.style.borderColor = "#999999";
				password.style.backgroundColor = "#ffffff";
				confirmPassword.style.borderColor = "#999999";
				confirmPassword.style.backgroundColor = "#ffffff";
			}
	}
	
	// If multiple errors, set standard message
	if (errors > 1 && msgString=="Required field, please retry.") msgString = "Required fields, please retry.";
	
	// Output msgString
	if (errors)
		{	message.style.display = "block";
			message.innerHTML = msgString;
		}
		
	else
		{// No errors so proceed to the next page
		
			switch (form_name)
			{
				case "delivery-details": // Delivery Details
					document.getElementById('delivery-details').submit();
					break;

                case "publication-details": // Address Details
					document.getElementById('address-details').submit();
					break;

				case "poa": // Payment On Account
					document.getElementById('on-account').submit();
					break;

				case "wishlist-email": // Wish list email (anonymous)
					document.getElementById('email_wish_list').submit();
					break;
				
				case "email-a-friend": // Email a friend Popup
					document.getElementById('email-friend').submit();
					break;
				
				case "contact_us": // Email a friend Popup
					document.getElementById('contact-form').submit();
					break;
				
				case "help": // Help Popup
					//window.location='help.html?messageid=email_sent';
					document.getElementById('help').submit();
					break;
				
				case "wishlist-rename": // Rename Wish list
					document.getElementById('rename_wish_list').submit();
					break;
				
				case "login": // Login 
					document.getElementById('login').submit();
					break;
					
				case "subscriptions-login": // Subscriptions Login
					document.getElementById('login').submit();
					break;
					
				case "password-reminder": // Forgotten Password
					document.getElementById('forgotten_password').submit();
					break;
				
				case "register": // Registeration
					document.getElementById('registration').submit();
					break;
					
				case "reg-trade-details": // Registration Trade Details
					document.getElementById('trade_details').submit();
					break;

				case "reg-trade-address": // Registration Trade Address
					document.getElementById('trading_address').submit();
					break;

				case "contact-details": // Contact Details
				case "update-contact": // Contact Details (update)
				case "update-trade": // Trade Address (update)
					document.getElementById('contact_details').submit();
					break;
			
				case "update_login": // Login Account (update)
					document.getElementById('login-account').submit();
					break;
								
				case "info-req-consumer": // Information Required: Consumer
					document.getElementById('b').value=2;
					document.getElementById('additional-information').submit();
					break;

				case "info-req-trade": // Information Required: Trade
					document.getElementById('b').value=1;
					document.getElementById('additional-information').submit();
					break;

                case "returns": // Returns Form
					document.getElementById('returns').submit();
					break;
				}
		}
}