 /**
 * @projectname     Izvorul cristalin
 * @version         1.0 2009.10.04
 * @copyright       Copyright (C) 2005 - 2009 Open Source Matters. All rights reserved.
 * @license         GNU/GPL
 * @author          Székely Csaba
 * 
 * @desc            Javascript (AJAX) form message sending
 *
 *
 * <script type="text/javascript" src="inc/contact.js"></script>
 *
 */
 
 // getting borwser type and generating request 
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    } // end if else borwser type
    return ro;
} // end function create Request Object

// creating new object
var http = createRequestObject();
                                        
// handeling response from php file
function handleResponse() {
    // if message sent ok
    if(http.readyState == 4){
        // read the echo on php into varibale
        var response = http.responseText;
        // declaring arra vhere response will be loaded
        var update = new Array();
        // if there are | in respons
        if(response.indexOf('|' != -1)) {
            // spliting the response into the array by |
            update = response.split('|');
            // updating document id=update[0] content=update[1]
			//alert(update[1]);
			//alert(document.getElementById(update[0]).innerHTML);
            document.getElementById(update[0]).innerHTML = update[1];
         
        } // end if | in message
    } // end if response if ok
} // end fucntion handle response

 
// form checker for phone number sender
function check_phone_form (form) {
    // getting input values
    var input_name  = form.name.value;
    var input_phone = form.phone.value;
    // cheking input phone for non numeric values
    var input_phone_number = /^\d+$/.test(input_phone);

    // cheking for input values
    if(input_name==null || input_name=="" || input_name=="numele tau"){
        // showing alert
        alert("Introdu numele!");
        // setting input bacground color to yellow
        form.name.focus();
        form.name.select();
        form.name.style.backgroundColor = 'yellow';
        return false;
    } else if (input_phone==null || input_phone=="" || input_phone=="telefonul tau"){
        // showing alert
        alert("Introdu numarul de telefon!");
        // setting input bacground color to yellow
        form.phone.style.backgroundColor = 'yellow';
        form.phone.focus();
        form.phone.select();
        return false;
    } else {
        // cheking for phone number numeric format
        if(!input_phone_number){
            alert("Introdu numarul de telefon valid !\nIntroduceti numai numere!");
            // setting input bacground color to yellow
            form.phone.style.backgroundColor = 'red';
            form.phone.focus();
            form.phone.select();
        } else {
            // if alles ok sending.
            send_phone(form, input_name, input_phone);
        } // end if phone number numeric format
        
        
    } // end if else input ok

} // end function cheking email and phone number

// sending email with phone 
function send_phone(form, c_name, c_phone ) {
    // disabling send button
   // form.send.disabled=true; 
    document.getElementById('send').disabled=true; 
    // setting button text to sending value
    //form.send.value='Trimite ...';
	document.getElementById('send').value='Trimite ...';
    // sending values to php file
    http.open('get', 'inc/contact.php?name='+c_name+'&phone='+c_phone+'&paction=send_phone');
    // handling response from php
    http.onreadystatechange = handleResponse;
    // stopping page form changing
    http.send(null);
} // end function send phone number

 
 function check_test_form(){
    var f_button    = document.getElementById('t_Trimite');
    var f_name      = document.getElementById('numele_tau').value;
    var f_company   = document.getElementById('compania').value;
    var f_address   = document.getElementById('adresa').value;
    var f_phone     = document.getElementById('telefon').value;
    // cheking input phone for non numeric values
    var f_phone_number = /^\d+$/.test(f_phone);
    var f_email     = document.getElementById('email').value;
    // cheking for valid email 
    var apos=f_email.indexOf("@");
    var dotpos=f_email.lastIndexOf(".");

    
     if(f_name==null || f_name==""){
        // showing alert
        alert("Introduceti numele!");
        // setting input bacground color to yellow
        document.getElementById('numele_tau').focus();
        document.getElementById('numele_tau').style.backgroundColor = 'yellow';
        return false;
        
     } else if (f_company==null || f_company==""){
        // showing alert
        alert("Introduceti numele companiei!");
        // setting input bacground color to yellow
        document.getElementById('compania').focus();
        document.getElementById('compania').style.backgroundColor = 'yellow';
        return false;
        
     } else if (f_address==null || f_address==""){
        // showing alert
        alert("Introduceti adresa!");
        // setting input bacground color to yellow
        document.getElementById('adresa').focus();
        document.getElementById('adresa').style.backgroundColor = 'yellow';
        return false;
        
     } else if (f_phone==null || f_phone==""){
        // showing alert
        alert("Introduceti numarul de telefon!");
        // setting input bacground color to yellow
        document.getElementById('telefon').focus();
        document.getElementById('telefon').style.backgroundColor = 'yellow';
        return false;
        
     } else if (f_email==null || f_email==""){
        // showing alert
        alert("Introduceti adresa de email!");
        // setting input bacground color to yellow
        document.getElementById('email').focus();
        document.getElementById('email').style.backgroundColor = 'yellow';
        return false;
        
     } else {
        // cheking if phone > number and valdi email address
        if(!f_phone_number){
            alert("Introdu numarul de telefon valid !\nIntroduceti numai numere!"); 
            document.getElementById('telefon').focus(); 
            document.getElementById('telefon').select(); 
            document.getElementById('telefon').style.backgroundColor = 'red';
            return false; 
        }  else if(apos<1||dotpos-apos<2){
            alert("Introduceti un email valid!");
            document.getElementById('email').focus();
            document.getElementById('email').style.backgroundColor = 'red';
            return false;
        } else {
        
            send_test(f_button, f_name, f_company, f_address, f_phone, f_email);
          
        }// end if else email addres and phoun > number
     
     } // end if else chek input values
    
 } // end func. chek test from 
 
 
 // sending email for test
function send_test(f_button, f_name, f_company, f_address, f_phone, f_email) {
    // disabling send button
    f_button.disabled=true; 
    // setting button text to sending value
    f_button.value='Trimite ...';
    // sending values to php file
    http.open('get', 'inc/contact.php?name='+f_name+'&company='+f_company+'&address='+f_address+'&phone='+f_phone+'&email='+f_email+'&paction=send_test');
    // handling response from php
    http.onreadystatechange = handleResponse;
    // stopping page form changing
    http.send(null);
} // end function send phone number

// sending recomandation to frend >> test
function check_recomand_form(){

    // send (submit) button
    var f_button    = document.getElementById('pTrimite');
    // retriving sent values
    var f_name      = document.getElementById('nume').value;
    var f_company   = document.getElementById('companie').value;
    var f_email     = document.getElementById('email').value;
    // cheking email validity
    var apos        =f_email.indexOf("@");
    var dotpos      =f_email.lastIndexOf(".");

    var f_phone     = document.getElementById('telefon').value;
    // cheking input phone for non numeric values
    var f_phone_number = /^\d+$/.test(f_phone);
    
    // frend data
    var f_name_p    = document.getElementById('nume_p').value;
    var f_company_p = document.getElementById('companie_p').value;
    var f_address_p = document.getElementById('adresa_p').value;
    var f_email_p   = document.getElementById('email_p').value;
    // cheking email validity
    var apos_p      =f_email_p.indexOf("@");
    var dotpos_p    =f_email_p.lastIndexOf(".");

    var f_phone_p     = document.getElementById('telefon_p').value;
    // cheking input phone for non numeric values
    var f_phone_number_p = /^\d+$/.test(f_phone_p);
    
     if(f_name==null || f_name==""){
        // showing alert
        alert("Introduceti numele!");
        // setting input bacground color to yellow
        document.getElementById('nume').focus();
        document.getElementById('nume').style.backgroundColor = 'yellow';
        return false;
        
     } else if (f_company==null || f_company==""){
        // showing alert
        alert("Introduceti numele companiei!");
        // setting input bacground color to yellow
        document.getElementById('companie').focus();
        document.getElementById('companie').style.backgroundColor = 'yellow';
        return false;

     } else if (f_email==null || f_email==""){
        // showing alert
        alert("Introduceti adresa de email!");
        // setting input bacground color to yellow
        document.getElementById('email').focus();
        document.getElementById('email').style.backgroundColor = 'yellow';
        return false;
        
     } else if (f_phone==null || f_phone==""){
        // showing alert
        alert("Introduceti numarul de telefon!");
        // setting input bacground color to yellow
        document.getElementById('telefon').focus();
        document.getElementById('telefon').style.backgroundColor = 'yellow';
        return false;
        
     } else if(f_name_p==null || f_name_p==""){
        // showing alert
        alert("Introduceti numele!");
        // setting input bacground color to yellow
        document.getElementById('nume_p').focus();
        document.getElementById('nume_p').style.backgroundColor = 'yellow';
        return false;
        
     } else if (f_company_p==null || f_company_p==""){
        // showing alert
        alert("Introduceti numele companiei!");
        // setting input bacground color to yellow
        document.getElementById('companie_p').focus();
        document.getElementById('companie_p').style.backgroundColor = 'yellow';
        return false;

     } else if (f_address_p==null || f_address_p==""){
        // showing alert
        alert("Introduceti adresa companiei!");
        // setting input bacground color to yellow
        document.getElementById('adresa_p').focus();
        document.getElementById('adresa_p').style.backgroundColor = 'yellow';
        return false;

     } else if (f_email_p==null || f_email_p==""){
        // showing alert
        alert("Introduceti adresa de email!");
        // setting input bacground color to yellow
        document.getElementById('email_p').focus();
        document.getElementById('email_p').style.backgroundColor = 'yellow';
        return false;
        
     } else if (f_phone_p==null || f_phone_p==""){
        // showing alert
        alert("Introduceti numarul de telefon!");
        // setting input bacground color to yellow
        document.getElementById('telefon_p').focus();
        document.getElementById('telefon_p').style.backgroundColor = 'yellow';
        return false;
        
     } else {
     
     // cheking if phone > number and valdi email address
        if(!f_phone_number){
            alert("Introdu numarul de telefon valid !\nIntroduceti numai numere!"); 
            document.getElementById('telefon').focus(); 
            document.getElementById('telefon').select(); 
            document.getElementById('telefon').style.backgroundColor = 'red';
            return false; 
        }  else if(apos<1||dotpos-apos<2){
            alert("Introduceti un email valid!");
            document.getElementById('email').focus();
            document.getElementById('email').style.backgroundColor = 'red';
            return false;
        } else if(!f_phone_number_p){
            alert("Introdu numarul de telefon valid !\nIntroduceti numai numere!"); 
            document.getElementById('telefon_p').focus(); 
            document.getElementById('telefon_p').select(); 
            document.getElementById('telefon_p').style.backgroundColor = 'red';
            return false; 
        }  else if(apos_p<1||dotpos_p-apos_p<2){
            alert("Introduceti un email valid!");
            document.getElementById('email_p').focus();
            document.getElementById('email_p').style.backgroundColor = 'red';
            return false;
        } else {
            // sending data to php               
            send_recomand(f_button, f_name, f_company, f_phone, f_email, f_name_p, f_company_p, f_address_p, f_phone_p, f_email_p);
          
        }// end if else email addres and phoun > number
     
     } // end if else erro fund in check
        
 // sending email for test
function send_recomand(f_button, f_name, f_company, f_phone, f_email, f_name_p, f_company_p, f_address_p, f_phone_p, f_email_p) {
    // disabling send button
    f_button.disabled=true; 
    // setting button text to sending value
    f_button.value='Trimite ...';
    // sending values to php file
    http.open('get', 'inc/contact.php?name='+f_name+'&company='+f_company+'&phone='+f_phone+'&email='+f_email+'&name_p='+f_name_p+'&company_p='+f_company_p+'&address_p='+f_address_p+'&phone_p='+f_phone_p+'&email_p='+f_email_p+'&paction=send_recomand');
    // handling response from php
    http.onreadystatechange = handleResponse;
    // stopping page form changing
    http.send(null);
} // end function send phone number        
     
    
    
    
    

    

} // end function rec. test
