//Declare variables
//var SendURL:String = "http://www.miweb.es/contacto.asp";
var SendURL:String = "http://www.miweb.es/contacto.php";
var Result:String;
 
//Input variables
var T_Firstname:TextField = Form_mc.Firstname_txt;
var T_Lastname:TextField = Form_mc.Lastname_txt;
var T_Email:TextField = Form_mc.Email_txt;
var T_Company:TextField = Form_mc.Company_txt;
var T_Subject:TextField = Form_mc.Subject_txt;
var T_Message:TextField = Form_mc.Message_txt;
var T_Telefono:TextField = Form_mc.Telefono_txt;
var B_Send:Button = Form_mc.Send_btn;
 
//Validator variables
var V_FirstnameValidator:TextField = Form_mc.FirstnameValidator;
var V_LastnameValidator:TextField = Form_mc.LastnameValidator;
var V_EmailValidator:TextField = Form_mc.EmailValidator;
var V_CompanyValidator:TextField = Form_mc.CompanyValidator;
var V_SubjectValidator:TextField = Form_mc.SubjectValidator;
var V_MessageValidator:TextField = Form_mc.MessageValidator;
var V_TelefonoValidator:TextField = Form_mc.TelefonoValidator;
var FormHasErrors:Boolean = false;
 
//Hide validators first
V_FirstnameValidator._visible = false;
V_LastnameValidator._visible = false;
V_LastnameValidator._visible = false;
V_EmailValidator._visible = false;
V_CompanyValidator._visible = false;
V_SubjectValidator._visible = false;
V_MessageValidator._visible = false;
V_TelefonoValidator._visible = false;
 
 
//Declare Required Fields
var IsFirstnameRequired:Boolean = true;
var IsLastnameRequired:Boolean = true;
var IsEmailRequired:Boolean = true;
var IsCompanyRequired:Boolean = true;
var IsSubjectRequired:Boolean = true;
var IsMessageRequired:Boolean = false;
var IsTelefonoRequired:Boolean = true;
 
//Some settings for our form fields...
//Set max lengths
T_Firstname.maxChars = 25;
T_Lastname.maxChars = 25;
T_Email.maxChars = 30;
T_Company.maxChars = 25;
T_Subject.maxChars = 50;
T_Message.maxChars = 500;
T_Telefono.maxChars = 500;
 
//Set tabindexes (Tab order)
T_Firstname.tabIndex = 0;
T_Lastname.tabIndex = 1;
T_Email.tabIndex = 2;
T_Company.tabIndex = 3;
T_Telefono.tabIndex = 4;
T_Message.tabIndex = 5;
 
 
B_Send.onRelease = function() {
    ControlForm();
};
 
 
//We are ready to control form fields...
function ControlForm() {
    
//Reset validators to check them again
FormHasErrors = false;
V_FirstnameValidator._visible = false;
V_LastnameValidator._visible = false;
V_EmailValidator._visible = false;
V_CompanyValidator._visible = false;
V_SubjectValidator._visible = false;
V_MessageValidator._visible = false;
V_TelefonoValidator._visible = false;
    
    //Check if the fields are required (from options)
    if (IsFirstnameRequired) {
        if (T_Firstname.text.length<3) {
            //If firstname length is smaller than 3 then we have some problems 
            FormHasErrors = true;
            V_FirstnameValidator._visible = true;
        }
    }
    if (IsLastnameRequired) {
        if (T_Lastname.text.length<3) {
            FormHasErrors = true;
            V_LastnameValidator._visible = true;
        }
    }
    if (IsEmailRequired) {
        //run email control function
        if (!checkEmail(T_Email.text)) {
            FormHasErrors = true;
            V_EmailValidator._visible = true;
        }
    }
    if (IsCompanyRequired) {
        if (T_Company.text.length<3) {
            FormHasErrors = true;
            V_CompanyValidator._visible = true;
        }
    }
    if (IsSubjectRequired) {
        if (T_Subject.text.length<3) {
            FormHasErrors = true;
            V_SubjectValidator._visible = true;
        }
    }
    if (IsMessageRequired) {
        if (T_Subject.text.length<3) {
            FormHasErrors = true;
            V_MessageValidator._visible = true;
        }
    }
    
    if (IsTelefonoRequired) {
        if (T_Telefono.text.length<3) {
            FormHasErrors = true;
            V_TelefonoValidator._visible = true;
        }
    }
    //If our form has no errors we will send variables to our server-side page and go to next frame to show the result. 
    //We will use SendAndLoad to send the variables and get a result.
 
    if (FormHasErrors == false) {
        //We will define 2 SendAndLoad objects: One for sending and the other one is for getting result
        var Send_lv:LoadVars = new LoadVars();
        var Load_lv:LoadVars = new LoadVars();
 
        //Define sending variables
        Send_lv.Firstname = T_Firstname.text;
        Send_lv.Lastname = T_Lastname.text;
        Send_lv.Email = T_Email.text;
        Send_lv.Company = T_Company.text;
        Send_lv.Subject = T_Subject.text;
        Send_lv.Message = T_Message.text;
        Send_lv.Telefono = T_Telefono.text;
        
        
        //Submit variables
        Send_lv.sendAndLoad(SendURL,Load_lv,"POST");
 
        //Get result
        Load_lv.onLoad = function(success:Boolean) {
            if (success) {
                //Successfully sent and loaded  but this not means that we sent the mail...
                Result = Load_lv.Result;
                trace(Result);
                if (Result == "Success") {
                    Form_mc.gotoAndStop(2);
                } else {
                    Form_mc.gotoAndStop(3);
                }
            } else {
                Form_mc.gotoAndStop(3);
            }
        };
 
    }
}
 
 
 
 
function checkEmail(inputEmail:String):Boolean {
    //check for spaces
    if (inputEmail.indexOf(" ")>0) {
        return false;
    }
    //bust the email apart into what comes before the @ and what comes after               
    var emailArray:Array = inputEmail.split("@");
    //make sure there's exactly one @ symbol also make sure there's at least one character before and after the @
    if (emailArray.length != 2 || emailArray[0].length == 0 || emailArray[1].length == 0) {
        return false;
    }
    //bust apart the stuff after the @ apart on any . characters               
    var postArray:Array = emailArray[1].split(".");
    //make sure there's at least one . after the @
    if (postArray.length<2) {
        return false;
    }
    //make sure there's at least 1 character in in each segment before, between and after each .               
    for (var i:Number = 0; i<postArray.length; i++) {
        if (postArray[i].length<1) {
            return false;
        }
    }
    //get what is left after the last .
    var suffix = postArray[postArray.length-1];
    //make sure that the segment at the end is either 2 or 3 characters
    if (suffix.length<2 || suffix.length>3) {
        return false;
    }
    //it passed all tests, it's a valid email               
    return true;
}