
var keyagain = false;
var countflag = 0;

function validate(current_form) {

		var missing_message = "";
		var form_ng = false;

  // 03Aug 2009 Check if shopping cart is empty
  for (counter = 0; counter < current_form.length; counter++){
	  if (current_form[counter].name == "Cart1Total") {
	   //alert ("DEBUG MEASSAGE: Cart1Total is = " + current_form[counter].value)
		 if (current_form[counter].value =="0") {
		 		//alert ("Shopping cart is empty.")
				form_ng = true;
				missing_message = "Shopping cart is empty. Please return to catalog and choose an item."
		 }
		} //end if
	} //end for - to check if shopping cart is empty.
	
  //if shopping cart is not empty, prceed to check the contact details form.
  if (form_ng == false) { 
	//ensure mandatory fields are entered
    var missing_fields = new Array();
    var total_missing = 0;
    
		//alert ("DEBUG MESSAGE: in validate(current_form)") //for debug
    // Loop through all the text elements
    for (counter = 0; counter < current_form.length; counter++) {
    
        // Is this a visible text field that's mandatory?
        if ((current_form[counter].type == "text" ||
            current_form[counter].type == "textarea" ||
            current_form[counter].type == "password" ||
						current_form[counter].type == "checkbox") &&
            current_form[counter].mandatory) {
            
						//alert ("DEBUG MESSAGE: Current field is " + current_form[counter].name)
						
            // If input input field, is it empty?
            if (its_empty(current_form[counter].value)) {              
                // If so, add the field to the array of missing fields
                missing_fields[total_missing] = current_form[counter]
                total_missing++
            } // end if (its_empty(current_form[counter].value))

						//If it's a checkbox, is it checked? 3June08
						if (current_form[counter].type == "checkbox") {
						  if (!current_form[counter].checked) {
						    // If so, add the field to the array of missing fields
                missing_fields[total_missing] = current_form[counter]
                total_missing++
								//alert ("DEBUG MESSAGE: Checkbox " + CBname + " not checked")
							}// end if (current_form[counter].CBname[0].checked)
						} //end if (current_form[counter].type == "checkbox")
						
						
        } // end if ((current_form[counter].type == "text" ||...
    }// end for (counter = 0; counter < current_form.length; counter++) 

						
		
				//alert ("DEBUG MESSAGE: form_ng is " + form_ng) //for debug		
				//alert ("DEBUG MESSAGE: total_missing is " + total_missing) //for debug		
		
    // Were there any fields missing?
    if (total_missing > 0) {
    
			  form_ng = true;
				
        // Start the message
				missing_message += "\n Please provide the following information : " +
                              "\n______________________________\n\n"
        // Loop through the missing fields
				var MissingFields;
        for (counter = 0; counter < missing_fields.length; counter++) {
						switch(missing_fields[counter].name) {
									 case "username" :
									 			MissingFields = "Name "
												break
									 case "useremail" :
									 			MissingFields = "Email address "
												break
									 case "usertel" :
									 			MissingFields = "Phone Number "
												break
									 case "unit1" :
									 			MissingFields = "Mailing list Unit number"
												break												
									 case "addr1" :
									 			MissingFields = "Mailing list Address1 "
												break											
									 case "country" :
									 			MissingFields = "Mailing list Country "
												break
									 case "postcode" :
									 			MissingFields = "Mailing list Postcode "
												break
									 case "UserID" :
									 			MissingFields = "User ID "
												break
									 /*case "password1" :
									 			MissingFields = "Password1 "
												break
									 case "password2" :
									 			MissingFields = "Password2 "
												break
									 */
									 case "Delivername" :
									 			MissingFields = "Delivery contact name "
												break												
									 case "Delivertel" :
									 			MissingFields = "Delivery contact telephone "
												break	
									 case "Deliverdate" :
									 			MissingFields = "Delivery date "
												break	
									 case "Delivertime" :
									 			MissingFields = "Delivery time "
												break	
									 case "Deliverunit1" :
									 			MissingFields = "Delivery unit number "
												break																									
									 case "Deliveraddr1" :
									 			MissingFields = "Delivery address line 1 "
												break	
									 case "Deliveraddr2" :
									 			MissingFields = "Delivery address line 2 "
												break	
									 case "Delivercountry" :
									 			MissingFields = "Delivery address country "
												break	
									 case "Deliverpostcode" :
									 			MissingFields = "Delivery address postcode "
												break																																						
									 case "TnCChecked" :
									 			MissingFields = "Please read Terms and Conditions"
												break												
									 default :
									 			MissingFields = ""
						} // end Switch
						
            missing_message += MissingFields + "\n"
        } // end for

				missing_message += "\n"
				
				// For emphasis, put the focus on the first missing field
        missing_fields[0].focus()				
    } // end if (total_missing > 0)

		//check validity of field entries
		//missing_message = Postcode_check(current_form, missing_message);
		//missing_message = Number_check(current_form, missing_message);
		//missing_message = Salutation_check(current_form, missing_message);
		
		//check validity of email address
		var missing_message1;
		missing_message1 = Email_Check(current_form, missing_message);
		
		if (missing_message1 != missing_message){
			 form_ng = true;
			 missing_message = missing_message1;
			 current_form.useremail.focus()
		} // end if (missing.....
		// finished email address checking
		
	} //end if - check if shopping cart is empty	(03Aug 09)
		
		
	//alert ("DEBUG MESSAGE: line 80 form_ng is " + form_ng) //for debug
		if(form_ng == true){
				 alert(missing_message)
		} 
		return form_ng
		
} // end function validate(current_form)

function its_empty(string_value) {

    // Check for the empty string and null
    if (string_value == "" || string_value == null) {
    
        // If either, it's empty so return true
        return true
    }
    
    // Otherwise, it's not empty so return false
    return false
}

function quantity_check(current_form, message){

		var noneselected = true;

		for (count = 0; count < current_form.length; count++){
		 
				 //is this a title checkbox field?
				 if (current_form[count].type == "checkbox"){

				 		//if so, is it checked?
						if (current_form[count].checked){
							 noneselected = false;
							 //if so, check if the Quantity is entered
							 if (current_form[count+1].selectedIndex == 0){

							 		if (countflag == 0){
									  message += "\n Please select the quantity needed for the following items :" +
										"\n_________________________________\n\n"
										form_ng = true; //indicate there is error in the form
									}
									countflag++;
									message += countflag + ".) " + current_form[count].name.substr(5,current_form[count].name.length) + "\n"
							
							 }
						}
				 }
		} //end for
		
		if (noneselected){
			 message += "\n Please select at least one audiobook to submit form. \n"
			 form_ng = true; //indicate there is error in the form
		}
  	 noneselected = true; //reset noneselected
		 countflag = 0 //reset countflag
		return(message)		 			 	
}

function its_a_digit(character) {

    var digit_characters = "0123456789"

    // If it's not in the digit_characters string,
    // then it's not a digit so return false
    
    if (digit_characters.indexOf(character) == -1) {
        return false
    }
    
    // Otherwise, it's a digit, so return true
    return true
}

function its_integer(string_value) {

    // Run through each character in the string
    for (var counter = 0; counter < string_value.length; counter++) {
        
        // Get the current character
        current_char = string_value.charAt(counter)
        
        // If it's not a digit, return false
        if (!its_a_digit(current_char)) {
            return false
        }
    }
    
    // Otherwise, the string has nothing but
    // digits, so return true
    return true
}

function Email_Check(_form1, message1){

				//alert ("DEBUG MESSAGE: line 174 in Email_Check") //for debug
				//alert ("DEBUG MESSAGE: line 175 message is : " + message1) //for debug
				
				if (_form1.useremail.value != ""){ 
				 if (!valid_email(_form1.useremail.value)) {
				 message1 += "\n Please provide a valid email address. \n"
				 }
				}
				//alert ("DEBUG MESSAGE: line 182 message is : " + message1) //for debug				 
				 return(message1)
}

function Postcode_check(_form1, message1){

				if (_form1.form_POSTCODE.value != ""){ 
				 if (!its_integer(_form1.form_POSTCODE.value)) {
				 message1 += "\nPlease enter a valid 6-digit Postcode \n"
				 form_ng = true; //indicate there is error in the form
				 }
				} 
				 return(message1)
}

function Number_check(_form2, message2){

				 for (var count2 = 0; count2 < _form2.length; count2++){
				 				 
				 if (_form2[count2].name == "form_TELEPHONE_1"){
				 		//check TELEPHONE_1
				 		if(_form2[count2].value != ""){
				 		 if(!its_integer(_form2[count2].value)){
				 		 message2 += "Please enter a valid number for TELEPHONE_1 \n"
						 form_ng = true; //indicate there is error in the form 
				 		 }
				 		 else if (!its_integer(_form2[count2].value)|| (_form2[count2].value.length < 7)){
				 		 message2 += "Please enter a valid number for TELEPHONE_1 \n"
						 form_ng = true; //indicate there is error in the form 
				 		 }
						} 
						//check TELEPHONE_2
						if(_form2[count2 + 1].value != ""){
						 if(!its_integer(_form2[(count2) +1].value)){
				 		 message2 += "Please enter a valid number for TELEPHONE_2 \n"
						 form_ng = true; //indicate there is error in the form
				 		 }
				 		 else if (!its_integer(_form2[(count2) + 1].value)|| (_form2[count2 + 1].value.length < 7)){
				 		 message2 += "Please enter a valid number for TELEPHONE_2 \n"
						 form_ng = true; //indicate there is error in the form
				 		 }
						}
					} //end if
					} //end for	 
				 
				 return(message2) 
}

function Salutation_check(_form3,message3){

				 if (_form3.form_SALUTATION.selectedIndex!="0"){
				 		return(message3)
				 }
				 else {
				 			message3 += "Please choose a Salutation \n"
				 			form_ng = true; //indicate there is error in the form
				 }

				 return(message3)
}
				 
function display_data(current_form) {
//Displays order form data that user has entered and prompt user to confirm submission    
    var confirm_message = "THANK YOU!\nHere's your order and contact details:\n" +
                          "______________________________\n\n"
    var text_field_counter = 0;
		var checked_counter = 0;
    // Loop through all the form elements
   		
		confirm_message += "\n You have ordered the following audiobooks : \n\n";
				
		 for (var counter = 0; counter < (current_form.length - 11); counter++) {		
        // Is it a checkbox?
       if (current_form[counter].type == "checkbox") {
        
				   if (current_form[counter].checked){
            // If so and if it is checked, add the name to the message
            checked_counter++;
						confirm_message += (checked_counter) + ") " + current_form[counter].name.substr(5,current_form[counter].name.length)+ ",  "; 
					 }										 
        }
        
        // Is it a radio button?
        else if (current_form[counter].type == "radio") {
        
            // If so, is it activated?
            if (current_form[counter].checked) {
            
                // If so, add the name and checked value to the message
                confirm_message += current_form[counter].name.substr(5,current_form[counter].name.length) + " = " +
                                   current_form[counter].value + "\n"
            }
        }
        
        // Is it a single-select list?
        else if (current_form[counter].type == "select-one") {
            if (current_form[counter-1].checked){ 
            // If so and if it is item is previously checked, add the name and selected option text to the message
            confirm_message += current_form[counter].name.substr(5,3) + " = " +
            current_form[counter].options[current_form[counter].selectedIndex].text + "\n"
        		}
				}
        
        // Is it a multiple-select list?
        else if (current_form[counter].type == "select-multiple") {
        
            // If so, get the selected options
            var chosen_ones = new Array()
            chosen_ones = get_selections(current_form[counter])
            
            // Add the name and the text of the selected options to the message
            if (chosen_ones.length > 0) {
                confirm_message += current_form[counter].name.substr(5,current_form[counter].name.length) + " = "
                for (var counter2 = 0; counter2 < chosen_ones.length; counter2++) {
                    confirm_message += current_form[counter].options[chosen_ones[counter2]].text
                    if (counter2 < chosen_ones.length - 1) {
                        confirm_message += " + "
                    }
                }
            }
        }
    } // end of search for items order information
		
				 for (var counter = (current_form.length - 11); counter < current_form.length; counter++) {
    		 
        // Is this a visible text field?
        if (current_form[counter].type == "text" ||
            current_form[counter].type == "textarea" ||
            current_form[counter].type == "password") {
            
                
						++text_field_counter;		
						if(text_field_counter == 1)
						{	confirm_message += "\n Your contact details : \n\n";
						  confirm_message += current_form[counter].name.substr(5,current_form[counter].name.length) + " = " +
                                   current_form[counter].value + "\n"
						}
						else
						{
						// If so, add the name and value to the message	
            confirm_message += current_form[counter].name.substr(5,current_form[counter].name.length) + " = " +
                                   current_form[counter].value + "\n"
						}
        }
				
			// Is it a multiple-select list?
        if (current_form[counter].type == "select-multiple") {
        
            // If so, get the selected options
            var chosen_ones = new Array()
            chosen_ones = get_selections(current_form[counter])
            
            // Add the name and the text of the selected options to the message
            if (chosen_ones.length > 0) {
                confirm_message += current_form[counter].name.substr(5,current_form[counter].name.length) + " = "
                for (var counter2 = 0; counter2 < chosen_ones.length; counter2++) {
                    confirm_message += current_form[counter].options[chosen_ones[counter2]].text
                    if (counter2 < chosen_ones.length - 1) {
                        confirm_message += " + "
                    }
                }
            }
        }	// end of Is it a multiple-select list?
				
			// Is it a single-select list?
       if (current_form[counter].type == "select-one") {
        
            // If so, add the name and selected option text to the message
            confirm_message += current_form[counter].name.substr(5,current_form[counter].name.length) + " = " +
            current_form[counter].options[current_form[counter].selectedIndex].text + "\n"
        }//end of  Is it a single-select list	
				
			} // end of search for contact information fields





    
    confirm_message += "\n______________________________\n\n" +
                       "Do you want to submit this order?"
    
    // Ask the user to confirm the submit
    var submit_ok = confirm(confirm_message)
    if (submit_ok) {
        current_form.submit()
    }
}

function get_selections(current_list) {

    var selected_array = new Array()
    var current_index = 0
    for (var counter = 0; counter < current_list.options.length; counter++) {
        if (current_list.options[counter].selected) {
            selected_array[current_index] = current_list.options[counter].index
            current_index++
        }
    }
    return selected_array
}

function calculate_price(cur_field,price){

    var cur_form = cur_field.form
    var total_fields = cur_form.elements.length		
		var quantity = 0
		var amount = 0

			// If so, get the index number of the current field	 
       for (counter = 0; counter < total_fields; counter++) {
            if (cur_field.name == cur_form.elements[counter].name) {
                var cur_index = counter
                break
            }
			}
						
		  quantity = (cur_field.selectedIndex)
			amount = quantity*price
			
			cur_field.form[cur_index + 1].value = amount
}


function deflect_focus(current_field) {
    
    var current_form = current_field.form
    var total_fields = current_form.elements.length
    
    // Is there another field?
    if (total_fields > 1) {

        // If so, get the index number of the current field
        for (counter = 0; counter < total_fields; counter++) {
            if (current_field.name == current_form.elements[counter].name) {
                var current_index = counter
                break
            }
        }
        
        // Is this the first field?
        if (current_index == 0) {
        
            // If so, set the focus on the zsecond field
            current_form.elements[1].focus()
        }
        else {

            // Otherwise, set the focus on the previous field
            current_form.elements[current_index - 1].focus()
        }
    }
}

function valid_email(email_address) {

    // Check the length
    if (email_address.length < 5) {
        return false
    }
    
    // Check @ and .
    at_location = email_address.indexOf("@")
    dot_location = email_address.lastIndexOf(".")
    
    if (at_location == -1 || dot_location == -1 || at_location > dot_location ) {
        return false
    }

    // Is there at least one character before @?
    if (at_location == 0) {
        return false
    }
    
    // Is there at least one character between @ and .?
    if (dot_location - at_location < 2 ) {
        return false
    }

    // Is there at least one character after .?
    if (email_address.length - dot_location < 2) {
        return false
    }

    // Otherwise, it's a valid address, so return true
    return true
}

