﻿function sendContactForm()
{
    var txName = $("ContactCtrl_txName").value;
    var txEmail = $("ContactCtrl_txEmail").value;
    var txPhone = $("ContactCtrl_txPhone").value;
    var txMessage = $("ContactCtrl_txMessage").value;
    
    var error = "";
    
    if (!isNotEmpty(txName))
    {
        //error += "Va rugam completati Numele dumneavastra\n";
        error += getJSPH("Contact_CompletatiNume");
    }
    
    if (isNotEmpty(txEmail) || isNotEmpty(txPhone))
    {
        if (isNotEmpty(txEmail) && !isEmail(txEmail))
        {
            //error += "Va rugam specificati o adresa de Email valida\n";
            error += getJSPH("Contact_EmailValid");
        }
        if (isNotEmpty(txPhone) && !isPhoneValid(txPhone))
        {
            //error += "Va rugam specificati un nr de telefon valid\n";
            error += getJSPH("Contact_TelefonValid");
        }
    }
    else
    { 
        //error += "Trebuie sa specificati o adresa de Email sau un numar de Telefon\n";
        error += getJSPH("Contact_CompletatiTelefonEmail");
    }
    try{
        if($("ContactCtrl_ddAddressee").selectedIndex==0)
        {
            error += getJSPH("Contact_SelectatiDestinatar");
        }
    }
    catch(Ex){}
    
    if (!isNotEmpty(txMessage))
    {
        //error += "Va rugam sa completati Mesajul dumneavoastra\n";
        error += getJSPH("Contact_CompletatiMesaj");
        
    }
    
    if (error.length > 0) 
    {
        //error = "Va rugam sa remediati urmatoarele probleme: \n" + error;
        error = getJSPH("Contact_RemediatiProblemele") + error;
        alert(error);
    }
    else
    {
        $("ContactCtrl_hAction").value = "SaveContact";
        document.getElementById('aspnetForm').submit();
    }
}
function isNotEmpty(val)
{
  return ((trim(val)).length>0);
}

function isEmail(val)
{
    if (!isNotEmpty(val)) {
        return true;
    }
	else {
	    var m = val.match(/\w+[.]?\w*@\w+[.]\w+/g);
	    return ((m!=null)&&(m.length>0));
	}
}

function isValidPhoneChar(c)
{
	if((c == "0")||(c == "1")||(c == "2")||(c == "3")||(c == "4")) return true;
	if((c == "5")||(c == "6")||(c == "7")||(c == "8")||(c == "9")) return true;
	if((c == '/')||(c == '-')||(c == '+')) return true;
	if((c == '(')||(c == ')')||(c == ' ')) return true;
	return false;
}

function isPhoneValid(value)
{
	var i=0;
	var s = new String();
	var c='';
	s = value.toString()
	for(i=0;i<s.length;i++)
	{
		c = s.substring(i,i+1);
		if(!isValidPhoneChar(c))
		{
			return false;
		}
	}
	return true;
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function goToHome()
{
    document.location.href = "Default.aspx";
}
