


//----------------------------------------------------------------------------------------------
//Helper functions
//----------------------------------------------------------------------------------------------

//Call a php script on server and return output to a target div
function my_remote_function_call(target_script,params,target_div) 
{    
    var tgt = $(target_div);
    tgt.innerHTML = "<div style='height: "+Element.getHeight(target_div)+"px; color: #666666; background: #ffffff; font-size: 11pt;'><table style='width: 786px; margin-left: auto; margin-right: auto;'><tr><td style='text-align: center;'><img src='_pics/loading_big.gif'/></td></tr><tr><td style='text-align: center;'>LOADING PAGE</td></td></div>";
    var url = target_script;
    var params_to_send = params;
    var ajax = new Ajax.Updater(
                {success: target_div},
                url,
                {method: "get", parameters: params_to_send}
                );
}


//Call a php script on server and return output to a target div (modified for the lesson and language menus to show alternate text while loading)
function my_remote_function_call_lessondb(target_script,params,target_div) 
{    
    var tgt = $(target_div);
    tgt.innerHTML = "<div style='height: "+Element.getHeight(target_div)+"px; color: #666666; background: #ffffff; font-size: 11pt;'><table style='width: 786px; margin-left: auto; margin-right: auto;'><tr><td style='text-align: center;'><img src='_pics/loading_big.gif'/></td></tr><tr><td style='text-align: center;'>LOADING LESSON DATABASE</td></td></div>";
    var url = target_script;
    var params_to_send = params;
    var ajax = new Ajax.Updater(
                {success: target_div},
                url,
                {method: "get", parameters: params_to_send}
                );
}



//Validate details input
function validate_details()
    {
     var errors = "";
     
     //Reset backgrounds
     $("title").style.background = "#ffffff";
     $("first_name").style.background = "#ffffff";
     $("last_name").style.background = "#ffffff";
     $("email").style.background = "#ffffff";
     $("telephone").style.background = "#cccccc";
     $("fax").style.background = "#cccccc";
     $("address_1").style.background = "#ffffff";
     $("address_2").style.background = "#ffffff";
     $("city").style.background = "#ffffff";
     $("postcode").style.background = "#ffffff";
     $("country").style.background = "#ffffff";
     
     
     
     if ($("title").value == "")
        {
         $("title").style.background = "#cc0000";
         errors += "<li style='height: 20px;'>Please select you title!</li>";
        }
     
     if ($("first_name").value == "")
        {
         $("first_name").style.background = "#cc0000";
         errors += "<li style='height: 20px;'>Please enter your first name!</li>";
        }
     
     if ($("last_name").value == "")
        {
         $("last_name").style.background = "#cc0000";
         errors += "<li style='height: 20px;'>Please enter your last name!</li>";
        }
     
     if ($("email").value == "")
        {
         $("email").style.background = "#cc0000";
         errors += "<li style='height: 20px;'>Please enter your email address!</li>";
        } else
            {
             var emailFilter=/^.+@.+\..{2,3}$/;
             
             if (!(emailFilter.test($("email").value)))
                {
                 $("email").style.background = "#cc0000";
                 errors += "<li style='height: 20px;'>Please enter a valid email address!</li>";
                }
            }
     
     if ($("telephone").value !== "")
        {
         var allowed_characters = /^[0-9()+ -]*$/
         
         if (!(allowed_characters.test($("telephone").value)))
            {
             $("telephone").style.background = "#cc0000";
             errors += "<li style='height: 20px;'>Please enter only numbers for the telephone number!</li>";
            }
        }
     
     if ($("fax").value !== "")
        {
         var allowed_characters = /^[0-9()+ -]*$/
         
         if (!(allowed_characters.test($("fax").value)))
            {
             $("fax").style.background = "#cc0000";
             errors += "<li style='height: 20px;'>Please enter only numbers for the fax number!</li>";
            }
        }
     
     if ($("address_1").value == "")
        {
         $("address_1").style.background = "#cc0000";
         errors += "<li style='height: 20px;'>Please enter your street address!</li>";
        }
     
     if ($("address_2").value == "")
        {
         $("address_2").style.background = "#cc0000";
         errors += "<li style='height: 20px;'>Please enter your area!</li>";
        }
     
     if ($("city").value == "")
        {
         $("city").style.background = "#cc0000";
         errors += "<li style='height: 20px;'>Please enter your city or town!</li>";
        }
     
     if ($("postcode").value == "")
        {
         $("postcode").style.background = "#cc0000";
         errors += "<li style='height: 20px;'>Please enter your postcode!</li>";
        }
        
     if ($("country").value == "")
        {
         $("country").style.background = "#cc0000";
         errors += "<li style='height: 20px;'>Please select your country!</li>";
        }
     
     
     if (errors == "")
        {
         my_remote_function_call('payment.php','title='+escape($("title").value)+'&first_name='+escape($("first_name").value)+'&last_name='+escape($("last_name").value)+'&email='+escape($("email").value)+'&telephone='+escape($("telephone").value)+'&fax='+escape($("fax").value)+'&promo_code='+escape($("promo_code").value)+'&address_1='+escape($("address_1").value)+'&address_2='+escape($("address_2").value)+'&city='+escape($("city").value)+'&county='+escape($("county").value)+'&postcode='+escape($("postcode").value)+'&country='+escape($("country").value)+'&shipping='+escape($("shipping").value)+'&shipping_type='+escape($("shipping_type").value)+'&shipping_title='+escape($("shipping_title").value)+'&shipping_first_name='+escape($("shipping_first_name").value)+'&shipping_last_name='+escape($("shipping_last_name").value)+'&shipping_address_1='+escape($("shipping_address_1").value)+'&shipping_address_2='+escape($("shipping_address_2").value)+'&shipping_city='+escape($("shipping_city").value)+'&shipping_county='+escape($("shipping_county").value)+'&shipping_postcode='+escape($("shipping_postcode").value)+'&shipping_country='+escape($("shipping_country").value),'main_content');
            
        } else
            {
             error_html = "<span style='font-size: 10pt; color: #333333; padding-left: 20px;'>There are some errors on the form. Please correct the following:</span>";
             error_html += "<ul style='font-size: 9pt; color: #333333; padding-left: 60px;'>";
             error_html += errors;
             error_html += "</ul>";
             
             $("errors").innerHTML = error_html;
             $("errors").style.display = "block";
            }
    }


//Validate payment details
function validate_payment_input()
    {      
      var errors = ""; 
      
      $("card_type").style.background = "#ffffff";
      $("card_name").style.background = "#ffffff";
      $("card_no").style.background = "#ffffff";
      $("card_expiry").style.background = "#ffffff";
      $("card_cv2").style.background = "#ffffff";
      
      
      if ($("card_type").value == "")
        {
         $("card_type").style.background = "#cc0000";
         errors += "<li style='height: 20px;'>Please enter your card type!</li>";
        }
      
      if ($("card_name").value == "")
        {
         $("card_name").style.background = "#cc0000";
         errors += "<li style='height: 20px;'>Please enter your name as it appears on your card!</li>";
        }
      
      if ($("card_no").value == "")
            {
             $("card_no").style.background = "#cc0000";
             errors += "<li style='height: 20px;'>Please enter the card number!</li>";
            } else
                {
                 var allowed_characters = /^[0-9()+ -]*$/
                 
                 if (!(allowed_characters.test($("card_no").value)))
                           {
                            $("card_no").style.background = "#cc0000";
                            errors += "<li style='height: 20px;'>Please enter only numbers for the card number!</li>";
                           }
                }
      
      
      if ($("card_expiry").value == "")
        {
         $("card_expiry").style.background = "#cc0000";
         errors += "<li style='height: 20px;'>Please enter your card's expiry date in MM/YY format!</li>";
        }
      
      
      if ($("card_cv2").value == "")
            {
             $("card_cv2").style.background = "#cc0000";
             errors += "<li style='height: 20px;'>Please enter the security code! (the last 3 digits of the number on your signature strip)</li>";
            } else
                {
                 var allowed_characters = /^[0-9()+ -]*$/
                 
                 if (!(allowed_characters.test($("card_cv2").value)))
                           {
                            $("card_cv2").style.background = "#cc0000";
                            errors += "<li style='height: 20px;'>Please enter only numbers for the security code!</li>";
                           }
                }
                
        
      if (errors == "")
        {
         my_remote_function_call('payment_proc.php','card_type='+escape($("card_type").value)+'&card_name='+escape($("card_name").value)+'&card_no='+escape($("card_no").value)+'&card_expiry='+escape($("card_expiry").value)+'&card_cv2='+escape($("card_cv2").value)+'&card_start='+escape($("card_start").value),'main_content');
         
         //To clear cart
         //my_remote_function_call('-shopping_cart.php','action="hide"','shopping_cart');
        } else
            {
             error_html = "<span style='font-size: 10pt; color: #333333; padding-left: 20px;'>There are some errors on the payment form. Please correct the following:</span>";
             error_html += "<ul style='font-size: 9pt; color: #333333; padding-left: 60px;'>";
             error_html += errors;
             error_html += "</ul>";
             
             $("errors").innerHTML = error_html;
             $("errors").style.display = "block";
            }
      
    }


//Shopping cart functions
function show_cart()
    {
     $("shopping_cart").style.display = "block";
    } 


function hide_cart()
    {
     $("shopping_cart").style.display = "none";
    } 


//??
function goto_download_demo()
    {
     my_remote_function_call('download_demo.php','','main_content');
    }

function goto_about_us()
    {
     my_remote_function_call('about_us.php','','main_content');
    }

function goto_contact()
    {
     my_remote_function_call('contact.php','','main_content');
    }


