/**************
Class Module
***************/
function Modulo()
{
    //if error in captcha
    this.captchaNonOK = 0;
    this.valori = new Array();
    
    this.draw = draw;
    this.control = control;
    
    /***********
    Draw module
    ***********/
    function draw ()
    {   
        //start form
        var stringa="<form action=\"./thanks.php\" method=\"POST\" id =\"form_submit\">"+
        "<fieldset><legend>Contact me directly</legend>"+
        "<table>"+
                "<tr>"+
                    "<td>"+
                        "Name*"+
                    "</td>"+
                    "<td>"+
                       "<input type= \"text\" id=\"name\" name=\"name\"";
                                                if(this.captchaNonOK == 1) stringa+= " value =\"" + this.valori[0] + "\" ";
                                                stringa += "onchange=\"document.getElementById('surname').focus();\" />"+
                    "</td>"+
                "</tr>"+
                 "<tr>"+
                    "<td>"+
                        "Surname*"+
                    "</td>"+
                    "<td>"+
                       "<input type= \"text\" id=\"surname\" name=\"surname\"";
                                                if(this.captchaNonOK == 1) stringa+= " value =\"" + this.valori[1] + "\" ";
                                                stringa += " onchange=\"document.getElementById('org').focus();\" />"+
                    "</td>"+
                "</tr>"+
                "<tr>"+
                    "<td>"+
                        "Company"+
                    "</td>"+
                    "<td>"+
                        "<input type=\"text\" id=\"org\" name=\"org\"";
                                                if(this.captchaNonOK == 1) stringa+= " value =\"" + this.valori[2] + "\" ";
                                                stringa += " onchange=\"document.getElementById('email').focus();\" />"+
                    "</td>"+
                "</tr>"+
                "<tr>"+
                    "<td>"+
                        "Email*"+
                    "</td>"+
                    "<td>"+
                        "<input type=\"text\" id=\"email\" name=\"email\"";
                                                if(this.captchaNonOK == 1) stringa+= " value =\"" + this.valori[3] + "\" ";
                                                stringa += " onchange=\"document.getElementById('num1').focus();\" />"+
                    "</td>"+
                "</tr>"+
                "<tr>"+
                    "<td>"+
                        "International code"+
                    "</td>"+
                    "<td>"+
                        "<input type=\"text\" maxlength=\"3\" name=\"num1\"";
                                                if(this.captchaNonOK == 1) stringa+= " value =\"" + this.valori[4] + "\" ";
                                                else stringa+= "value=\"+39\"";
                                                stringa += " id=\"num1\" onchange=\"document.getElementById('num2').focus();\" />"+
                    "</td>"+
                "</tr>"+
                "<tr>"+
                    "<td>"+
                        "Phone number"+
                    "</td>"+
                    "<td>"+
                        "<input type=\"text\" name=\"num2\"";
                                                if(this.captchaNonOK == 1) stringa+= " value =\"" + this.valori[5] + "\" ";
                                                stringa += " id=\"num2\" onchange=\"document.getElementById('mess').focus();\" />"+
                    "</td>"+
                "</tr>"+
                "<tr>"+
                    "<td>"+
                        "Message*"+
                    "</td>"+
                    "<td>"+
                        "<textarea name=\"mess\" id=\"mess\" onchange=\"document.getElementById('captcha').focus();\">";
                                                if(this.captchaNonOK == 1) stringa+= this.valori[6];
                                                stringa += "</textarea>"+
                    "</td>"+
                "</tr>"+
                "<tr>"+
                    "<td style=\"text-align:right;\">"+
                        "<img src='../captcha.php' />"+
                    "</td>"+
                    "<td>"+
                        "<input type=\"text\" maxlength=\"6\" name=\"captcha\" id=\"captcha\" onchange=\"document.getElementById('but').focus();\" />"+
                        "<input type=\"hidden\" value=\"ita\" name=\"lan\" />"+
                    "</td>"+
                "</tr>"+
                "<tr>"+
                   "<td colspan=2 id=\"invia\">"+
                        "<input type=\"button\" id =\"but\" value=\"SEND\" onclick=\"if(modulo.control("+
                                                                                                    "document.getElementById('name').value,"+
                                                                                                    "document.getElementById('surname').value,"+
                                                                                                    "document.getElementById('org').value,"+
                                                                                                    "document.getElementById('email').value,"+
                                                                                                    "document.getElementById('num1').value,"+
                                                                                                    "document.getElementById('num2').value,"+
                                                                                                    "document.getElementById('mess').value,"+
                                                                                                    "document.getElementById('captcha').value)"+
                                                                                                    ") submit();\"/>   "+
                        "<input type=\"button\" value=\"RESET\" onclick=\"modulo.draw();\" />"+
                    "</td>"+
                "</tr>"+
                "<tr>"+
                   "<td colspan=2>"+
                        "All fields marked with an asterisk (*) are required"+
                    "</td>"+
                "</tr>"+
        "</fieldset>"+
        "</form>";
        //end form

		//insert in div
        document.getElementById("modulo").innerHTML = stringa;

		//set visibility div
        document.getElementById("modulo").style.visibility="visible";
        
        //if error in captcha highlight input
        if(this.captchaNonOK == 1) 
        {
            document.getElementById("captcha").style.background = "none";
            document.getElementById("captcha").style.backgroundColor = "#FF99CC";
                                            
            document.getElementById("captcha").focus();
        }
    }
    
    /************
    Control value
    ************/
    function control(name, surname, org, email, num1, num2, mess, captcha)
    {
        var correct = true;
        
        //name control
        if (name.length < 2)
        {
            document.getElementById("name").style.background = "none";
            document.getElementById("name").style.backgroundColor = "#FF99CC";
            correct = false;
        }
        else
            document.getElementById("name").style.background = "#FFFFFF url(images/img04.gif) repeat-x";
            
        //surname control
        if (surname.length < 2)
        {
            document.getElementById("surname").style.background = "none";
            document.getElementById("surname").style.backgroundColor = "#FF99CC";
            correct = false;
        }
        else
            document.getElementById("surname").style.background = "#FFFFFF url(images/img04.gif) repeat-x";
        
        //email control
        var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
	    if (!espressione.test(email))
        {
            document.getElementById("email").style.background = "none";
            document.getElementById("email").style.backgroundColor = "#FF99CC";
            correct = false;
        }
        else
            document.getElementById("email").style.background = "#FFFFFF url(images/img04.gif) repeat-x";
            
        //international prefix control
        if (num1.length < 2 || num1.charAt(0)!="+")
        {
            document.getElementById("num1").style.background = "none";
            document.getElementById("num1").style.backgroundColor = "#FF99CC";
            correct = false;
        }
        else
        {
            for(i=1;i<num1.length;i++)
            {
                codeA = num1.charCodeAt(i);
                if (num1.charAt(i)!=" " && codeA<48 || codeA>57)
                {
                    document.getElementById("num1").style.background = "none";
                    document.getElementById("num1").style.backgroundColor = "#FF99CC";
                    correct = false;
                }
                else
                    document.getElementById("num1").style.background = "#FFFFFF url(images/img04.gif) repeat-x";
            }
        }
        
        //telephon number control
        if (num2.length > 1)
        {
            if (num2.length > 4)
            {
                for(i=0;i<num2.length;i++)
                {
                    codeA = num2.charCodeAt(i);
                    if (num2.charAt(i)!=" " && codeA<48 || codeA>57)
                    {
                        document.getElementById("num2").style.background = "none";
                        document.getElementById("num2").style.backgroundColor = "#FF99CC";
                        correct = false;
                    }
                    else
                        document.getElementById("num2").style.background = "#FFFFFF url(images/img04.gif) repeat-x";
                }
            }
            else
            {
                document.getElementById("num2").style.background = "none";
                document.getElementById("num2").style.backgroundColor = "#FF99CC";
                correct = false;
            }
        }
        
        //message control
        if (mess == "")
        {
            document.getElementById("mess").style.background = "none";
            document.getElementById("mess").style.backgroundColor = "#FF99CC";
            correct = false;
        }
        else
            document.getElementById("mess").style.background = "#FFFFFF url(images/img04.gif) repeat-x";
            
        //first captcha control
        if (captcha.length != 6)
        {
            document.getElementById("captcha").style.background = "none";
            document.getElementById("captcha").style.backgroundColor = "#FF99CC";
            correct = false;
        }
        else
            document.getElementById("captcha").style.background = "#FFFFFF url(images/img04.gif) repeat-x";
        
        return correct;
    }
}
