﻿function changeTypeUser(type)
{
    var chk = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_rdbListType_0");
    
    var lCompanyName = document.getElementById("lCompanyName");
    var lEntityType = document.getElementById("lEntityType");
    var lCompanyNIPC = document.getElementById("lCompanyNIPC");
    var lblEntityAddress = document.getElementById("lblEntityAddress");
    var lPostalCode = document.getElementById("lPostalCode");
    var lCountry = document.getElementById("lCountry");
    
    
    

    var obr = "FormLabel jQuery_FLEntidade FormObrig1";
    var free = "FormLabel";

    if (chk.checked==false)
    {
        lCompanyName.className = free;
        lEntityType.className = free;
        lCompanyNIPC.className = free;
        lPostalCode.className = free;
        lCountry.className = free;
        lblEntityAddress.className = free;
    }
    else
    {
        lCompanyName.className = obr;
        lEntityType.className = obr;
        lCompanyNIPC.className = obr;
        lPostalCode.className = obr;
        lCountry.className = obr;
        lblEntityAddress.className = obr;
    }
    
    return false;

}

var result = true;
function writeError(obj, objError)
{
    obj.className = "RForm_Block1 clearfix FormErro1";
    objError.style.display = 'block';
    result = false;
}

function validateForm()
{

 result = true;
 
 var chk = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_rdbListType_0");
 var divCompanyName = document.getElementById("divCompanyName");
 var divErrorCompanyName = document.getElementById("divErrorCompanyName");
 var txtCompanyName = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_txtCompanyName");
 
 var divCompanyNIPC = document.getElementById("divCompanyNIPC");
 var divErrorCompanyNIPC = document.getElementById("divErrorCompanyNIPC");
 var txtCompanyNIPC = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_txtCompanyNIPC");

 var divEntityType = document.getElementById("divEntityType");
 var divErrorEntityType = document.getElementById("divErrorEntityType");
 var cmbEntityType = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_cmbEntityType");

 var divEntityAddress = document.getElementById("divEntityAddress");
 var divErrorEntityAddress = document.getElementById("divErrorEntityAddress");
 var txtEntityAddress = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_txtEntityAddress");

 var divCountry = document.getElementById("divCountry");
 var divErrorCountry = document.getElementById("divErrorCountry");
 var cmbCountry = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_cmbCountry");

 var divPostalCodeEntity = document.getElementById("divPostalCodeEntity");
 var divErrorPostalCodeEntity = document.getElementById("divErrorPostalCodeEntity");
 var txtPostalCode1Entity = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_txtPostalCode1Entity");
 var txtPostalCode2Entity = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_txtPostalCode2Entity");


 var divName = document.getElementById("divName");
 var divErrorName = document.getElementById("divErrorName");
 var txtName = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_txtName");

 var divPostalCode = document.getElementById("divPostalCode");
 var divErrorPostalCode = document.getElementById("divErrorPostalCode");
 var txtPostalCode1 = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_txtPostalCode1");
 var txtPostalCode2 = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_txtPostalCode2");
 
 var divAddress = document.getElementById("divAddress");
 var divErrorAddress = document.getElementById("divErrorAddress");
 var txtAddress = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_txtAddress");

 var divEmail = document.getElementById("divEmail");
 var divErrorEmail1 = document.getElementById("divErrorEmail1");
 var divErrorEmail2 = document.getElementById("divErrorEmail2");
 var txtEmail = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_txtEmail");
 
 var divPassword = document.getElementById("divPassword");
 var divPasswordConfirm = document.getElementById("divPasswordConfirm");
 var divErrorPassword1 = document.getElementById("divErrorPassword1");
 var divErrorPassword2 = document.getElementById("divErrorPassword2");
 var txtPassword = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_txtPassword");
 var txtPasswordConfirm = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_txtPasswordConfirm");
 
 var divError = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_divError");
 var lblError = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_lblError");

 var divCapcha = document.getElementById("divCaptcha");
 var divErrorCapcha = document.getElementById("divErrorCaptcha");
 var txtCapcha = document.getElementById("ctl00_PlaceHolderMain_uc_UserRegister1_Captcha_txtCodeNumberTextBox");


 if (chk.checked==true) //Utilizador Colectivo
 {
    if (txtCompanyNIPC.value == "")
        writeError(divCompanyNIPC, divErrorCompanyNIPC)
    else
    {
        divCompanyNIPC.className = "RForm_Block1 clearfix";
        divErrorCompanyNIPC.style.display = 'none';
    }
    
    if (txtCompanyName.value == "")
        writeError(divCompanyName, divErrorCompanyName)
    else
    {
        divCompanyName.className = "RForm_Block1 clearfix";
        divErrorCompanyName.style.display = 'none';
    }
       
   if (cmbEntityType.value == "0")
        writeError(divEntityType, divErrorEntityType)
    else
    {
        divEntityType.className = "RForm_Block1 clearfix";
        divErrorEntityType.style.display = 'none';
    }
    
     if (txtEntityAddress.value == "")
        writeError(divEntityAddress, divErrorEntityAddress)
    else
    {
        divEntityAddress.className = "RForm_Block1 clearfix";
        divErrorEntityAddress.style.display = 'none';
    }
    
    if (cmbCountry.value == "0")
        writeError(divCountry, divErrorCountry)
    else
    {
        divCountry.className = "RForm_Block1 clearfix";
        divErrorCountry.style.display = 'none';
    }
    
    if ((txtPostalCode1Entity.value =="")||(txtPostalCode2Entity.value ==""))
        writeError(divPostalCodeEntity, divErrorPostalCodeEntity)
    else
    {
        divPostalCodeEntity.className = "RForm_Block1 clearfix";
        divErrorPostalCodeEntity.style.display = 'none';
    }   
 }
 else
 {

 if ((cmbCountry.value=="PT") &&(txtCompanyNIPC.value == ""))
        writeError(divCompanyNIPC, divErrorCompanyNIPC)
    else
    {
        divCompanyNIPC.className = "RForm_Block1 clearfix";
        divErrorCompanyNIPC.style.display = 'none';
    }
 }


if (txtName.value == "")
        writeError(divName, divErrorName)
else
{
    divName.className = "RForm_Block1 clearfix";
    divErrorName.style.display = 'none';
}


if (txtAddress.value == "")
        writeError(divAddress, divErrorAddress)
else
{
    divAddress.className = "RForm_Block1 clearfix";
    divErrorAddress.style.display = 'none';
}

if ((txtPostalCode1.value =="")||(txtPostalCode2.value ==""))
        writeError(divPostalCode, divErrorPostalCode)
else
{
    divPostalCode.className = "RForm_Block1 clearfix";
    divErrorPostalCode.style.display = 'none';
}   

if (txtCapcha.value == "")
        writeError(divCaptcha, divErrorCaptcha)
else
{
    divCaptcha.className = "RForm_Block1 clearfix";
    divErrorCaptcha.style.display = 'none';
}

divErrorEmail1.style.display = 'none';
divErrorEmail2.style.display = 'none';
if (txtEmail.value == "")
{
      writeError(divEmail, divErrorEmail1)
      divErrorEmail2.style.display = 'none';
}
else
{   
    clearError(divEmail, divErrorEmail1);
    if (validate(txtEmail.value) == false)
        writeError(divEmail, divErrorEmail2);
    
}


divErrorPassword1.style.display = 'none';
divErrorPassword2.style.display = 'none';
if (txtPassword.value == "")
{
      writeError(divPassword, divErrorPassword1)
      divErrorPassword2.style.display = 'none';
}
if (txtPasswordConfirm.value == "")
{
      writeError(divPasswordConfirm, divErrorPassword3)
}

if(txtPassword.value != "" && txtPasswordConfirm.value != "")
{   
    clearError(divPassword, divErrorPassword1);
    clearError(divPasswordConfirm, divErrorPassword3);
    if (validaSamePassword(txtPassword.value, txtPasswordConfirm.value) == false)
        writeError(divPassword, divErrorPassword2);
    
}

if (result==false)
{
    divError.style.display = 'block';
    lblError.innerHTML = 'Corrija, por favor, os erros nos campos assinalados!'
    }
else
{
    divError.style.display = 'none';
    lblError.style.display = 'none';
    lblError.value = '';
}

return result;
}

function validateEmail()
{
 result = true;
 
 var divEmail = document.getElementById("divEmail");
 var divErrorEmail1 = document.getElementById("divErrorEmail1");
 var divErrorEmail2 = document.getElementById("divErrorEmail2");
 var txtEmail = document.getElementById("ctl00_PlaceHolderMain_uc_Password_txtEmail");
 
 var divError = document.getElementById("ctl00_PlaceHolderMain_uc_Password_divError");
 var lblError = document.getElementById("ctl00_PlaceHolderMain_uc_Password_lblError");

divErrorEmail1.style.display = 'none';
divErrorEmail2.style.display = 'none';
if (txtEmail.value == "")
{
      writeError(divEmail, divErrorEmail1)
      divErrorEmail2.style.display = 'none';
}
else
{   
    clearError(divEmail, divErrorEmail1);
    if (validate(txtEmail.value) == false)
        writeError(divEmail, divErrorEmail2);
    
}

if (result==false)
{
    divError.style.display = 'block';
    lblError.innerHTML = 'Corrija, por favor, os erros nos campos assinalados!'
    }
else
{
    divError.style.display = 'none';
    lblError.style.display = 'none';
    lblError.value = '';
}

return result;
}

function validateEmail_SendFriend()
{
    result = true;
 
    var divEmail = document.getElementById("divEmail");
    var divErrorEmail1 = document.getElementById("divErrorEmail1");
    var divErrorEmail2 = document.getElementById("divErrorEmail2");
    var txtEmail = document.getElementById("ctl00_PlaceHolderMain_uc_SendFriend_txtEmail");
 
    divErrorEmail1.style.display = 'none';
    divErrorEmail2.style.display = 'none';
    if (txtEmail.value == "")
    {
        writeError(divEmail, divErrorEmail1)
        divErrorEmail2.style.display = 'none';
    }
    else
    {   
        clearError(divEmail, divErrorEmail1);
        if (validate(txtEmail.value) == false)
            writeError(divEmail, divErrorEmail2);
    }

    return result;
}

function validateEmail_UnsubNewsletter()
{
    result = true;
 
    var divEmail = document.getElementById("divEmail");
    var divErrorEmail1 = document.getElementById("divErrorEmail1");
    var divErrorEmail2 = document.getElementById("divErrorEmail2");
    var txtEmail = document.getElementById("ctl00_PlaceHolderMain_uc_UnsubNewsletter_txtEmail");
 
    divErrorEmail1.style.display = 'none';
    divErrorEmail2.style.display = 'none';
    if (txtEmail.value == "")
    {
        writeError(divEmail, divErrorEmail1)
        divErrorEmail2.style.display = 'none';
    }
    else
    {   
        clearError(divEmail, divErrorEmail1);
        if (validate(txtEmail.value) == false)
            writeError(divEmail, divErrorEmail2);
    }

    return result;
}

function clearError(obj, objError)
{
    obj.className = "RForm_Block1 clearfix";
    objError.style.display = 'none';
}



function validate(address) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/
   if(reg.test(address) == false)
      return false;
   else
     return true;
}

function validaSamePassword(password , confirmePassword)
{
return password == confirmePassword;
}

function numbersOnly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;
// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else if ((keychar == ".") && dec && (dec==true))
   {
   return true;
   }
else
   return false;
}

function validateFormContactUs()
{
 result = true;
 
 var divName = document.getElementById("divName");
 var divErrorName = document.getElementById("divErrorName");
 var txtName = document.getElementById("ctl00_PlaceHolderMain_uc_ContactUs_txtName");
 
 var divEmail = document.getElementById("divEmail");
 var divErrorEmail1 = document.getElementById("divErrorEmail1");
 var divErrorEmail2 = document.getElementById("divErrorEmail2");
 var txtEmail = document.getElementById("ctl00_PlaceHolderMain_uc_ContactUs_txtEmail");
 
 var divCountry = document.getElementById("divCountry");
 var divErrorCountry = document.getElementById("divErrorCountry");
 var cmbCountry = document.getElementById("ctl00_PlaceHolderMain_uc_ContactUs_cmbCountry");
 
 var divSubject = document.getElementById("divSubject");
 var divErrorSubject = document.getElementById("divErrorSubject");
 var txtSubject = document.getElementById("ctl00_PlaceHolderMain_uc_ContactUs_txtSubject");
 
 var divMessage = document.getElementById("divMessage");
 var divErrorMessage = document.getElementById("divErrorMessage");
 var txtMessage = document.getElementById("ctl00_PlaceHolderMain_uc_ContactUs_txtMessage");
 
var divError = document.getElementById("ctl00_PlaceHolderMain_uc_ContactUs_divError");

if (txtName.value == "")
        writeError(divName, divErrorName)
else
{
    divName.className = "RForm_Block1 clearfix";
    divErrorName.style.display = 'none';
}

divErrorEmail1.style.display = 'none';
divErrorEmail2.style.display = 'none';
if (txtEmail.value == "")
{
      writeError(divEmail, divErrorEmail1)
      divErrorEmail2.style.display = 'none';
}
else
{   
    clearError(divEmail, divErrorEmail1);
    if (validate(txtEmail.value) == false)
        writeError(divEmail, divErrorEmail2);    
}

if (cmbCountry.value == "0")
        writeError(divCountry, divErrorCountry)
else
{
    divCountry.className = "RForm_Block1 clearfix";
    divErrorCountry.style.display = 'none';
}

if (txtSubject.value == "")
        writeError(divSubject, divErrorSubject)
else
{
    divSubject.className = "RForm_Block1 clearfix";
    divErrorSubject.style.display = 'none';
}

if (txtMessage.value == "")
    writeError(divMessage, divErrorMessage)
else
{
    divMessage.className = "RForm_Block1 clearfix";
    divErrorMessage.style.display = 'none';
}

if (result==false)
    divError.style.display = 'block';
else
    divError.style.display = 'none';

return result;

}
