﻿

function sendContactForm()
{
    var txName = document.getElementById("ContactCtrl_txNume").value;
    var txPrenumeName = document.getElementById("ContactCtrl_txPrenume").value;
    var txEmail = document.getElementById("ContactCtrl_txEmail").value;
    var txPhone = document.getElementById("ContactCtrl_txPhone").value;
    var txMessage = document.getElementById("ContactCtrl_txCourseType").value;
    var txLocation = document.getElementById("ContactCtrl_txLocation").value;
    var error = "";
    if (!isNotEmpty(txMessage))
    {
        error += "- Trebuie sa completati campul \"Tipul cursului\";\n";
        
    }
    if (!isNotEmpty(txLocation))
    {
        error += "- Trebuie sa completati campul \"Localitate sustinere cursuri\";\n";
    }
    if (!isNotEmpty(txName))
    {
        error += getJSPH("Contact_CompletatiNume");
    }
    if (!isNotEmpty(txPrenumeName))
    {
        error += getJSPH("Contact_CompletatiPrenume");
    }
    if (!isNotEmpty(txEmail))
    {
        error += getJSPH("Contact_CompletatiEmail");
    }
    if (isNotEmpty(txEmail) && !isEmail(txEmail))
    {
         error += getJSPH("Contact_EmailValid");
    }
     if (!isNotEmpty(txPhone))
    {
        error += getJSPH("Contact_CompletatiTelefon");
    }
    if (isNotEmpty(txPhone) && !isPhoneValid(txPhone))
    {
        error += getJSPH("Contact_TelefonValid");
    }
    
   	

    if (error.length > 0) 
    {
        error = getJSPH("Contact_RemediatiProblemele") + error;
        alert(error);
    }
    else
    {
        document.getElementById("ContactCtrl_hAction").value = "SaveContact";
        document.getElementById("frmMain").submit();
    }
}
function isNotEmpty(val)
{
  return ((trim(val)).length>0);
}

function isEmail(val)
{
    if (!isNotEmpty(val)) {
        return true;
    }
	else {
	    var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	  
	  //  var m = val.match(/\w+[.]?\w*@\w+[.]\w+/g); //orig.
	    var m = val.match(emailRegEx);
	  
	    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";
}
