//******************************************************************************
// Ffeil:       /cynnwys/js/GWYgwiro.js
// Disgrifiad:  Llyfrgell o ffwythianinau ar gyfer gwiro ffurflenni.
// Fersiwn:     F1.0 (30/07/2002)
//
// Dyddiad     Fersiwn  Awdur               Disgrifiad
// 30/07/2002  1.0      J.A.Hughes          Creuwyd
//
//******************************************************************************


//------------------------------------------------------------------------------
// Gwiro fod maes llinyn wedi ei lenwi
//------------------------------------------------------------------------------
function gwiro_llinyn(strLlinyn,strMaes,strIaith)
{
  var strNeges;

  if ( strLlinyn.length == 0 )
  {
    if ( strIaith == "cymraeg" )
    {
      strNeges = "Nid ydych wedi llenwi'r maes " + strMaes;
    }
      else
    {
      strNeges = "You have not completed the " + strMaes + " field";
    }
    alert(strNeges);
    return false;
  }

  return true;
}
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
//Gwiro unrhyw e-Bost a fewnbynnwyd gan y defnyddiwr   
//------------------------------------------------------------------------------
function gwiro_ebost(strEBost,strMaes,strIaith,blnGorfodol)
{
  var strCyfeiriad;
  var strNegesEbost;
  var strNegesAngen;
  var regAnghywir;
  var regCywir;

  strCyfeiriad=strEBost;
  if ( strIaith == "cymraeg" )
  {
    strNegesEBost = "Nid yw " + strCyfeiriad + " yn gyfeiriad e-Bost dilys ym maes " + strMaes;
    strNegesAngen = "Rhaid rhoi cyfeiriad e-Bost dilys ym maes " + strMaes;
  }
    else if ( strIaith == "english" )
  {
    strNegesEBost = strCyfeiriad + " is not a valid e-Mail address in field " + strMaes; 
    strNegesAngen = "You must enter a valid e-Mail address in the field " + strMaes;
  }
    else
  {
    alert("Nid yw'r iaith a ddewiswyd yn ddilys. The language chosen is invalid.");
  }

  regAnghywir = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; //patrwm e-bost anghywir
  //regCywir = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; //patrwm e-bost cywir
  regCywir = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z0-9\-]+)(\]?)$/; //patrwm e-bost cywir
  if ( strCyfeiriad == "" && blnGorfodol == true )
  {
    alert(strNegesAngen);
    return false;
  }
    else if ( strCyfeiriad == "" )
  {
    return true;
  }
    else if ( !regAnghywir.test(strCyfeiriad) && regCywir.test(strCyfeiriad) )
  {
    return true;
  }
  alert(strNegesEBost);
  return false;
}
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
//Gwiro unrhyw ddyddiad a fewnbynnwyd gan y defnyddiwr
//------------------------------------------------------------------------------
function gwiro_dyddiad(strDyddiad,strMaes,strIaith,blnGorfodol)
{
  var arrNeges = new Array(6);
  var regDyddiad;
  var arrGwiroDydd;
  var arrDyddiad;
  
  if ( strIaith == "cymraeg" )
  {
    arrNeges[0] = "Rhaid rhoi rhywbeth ym maes " + strMaes;
    arrNeges[1] = "Rhowch y dyddiad i fewn yn y fformat DD/MM/BBBB ym maes " + strMaes;
    arrNeges[2] = "Rhif \"0\" ym maes " + strMaes;  
    arrNeges[3] = "Rhif mis yn fwy na 12 ym maes " + strMaes;
    arrNeges[4] = "Nifer o ddyddiau ddim yn ddilys am y mis ym maes " + strMaes;
    arrNeges[5] = "Nifer o ddyddiau ddim yn ddilys am mis Chwefror ym maes" + strMaes;
  }
    else if ( strIaith == "english" )
  {
    arrNeges[0] = "You must enter a value into the " + strMaes + " field";
    arrNeges[1] = "Please input the date in the format DD/MM/YYYY in the field " + strMaes;
    arrNeges[2] = "The number \"0\" appears in the " + strMaes + "field";
    arrNeges[3] = "The month number is greater than 12 in the field " + strMaes;
    arrNeges[4] = "The number of days is not valid for the month in the field " + strMaes;
    arrNeges[5] = "The number of days is not valid for the month of February in the field " + strMaes;
  }
    else
  {
    alert("Nid yw'r iaith a ddewiswyd yn ddilys. The language chosen is invalid.");
  }


  if ( strDyddiad == "" && blnGorfodol == true )
  {
    alert(arrNeges[0]);
    return false;
  }
    else if ( strDyddiad == "" )
  {
    return true;
  }  
    else if ( strDyddiad != "" )
  {
    //-----------------------
    //gwiro fformat y dyddiad
    //-----------------------
    regDyddiad = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
    arrGwiroDydd = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31, '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31};
    arrDyddiad = strDyddiad.split("/");
    if (!regDyddiad.test(strDyddiad))
    { 
      alert(arrNeges[1]);
      return false;
    }
      else if ( arrDyddiad[0]==0||arrDyddiad[1]==0||arrDyddiad[2]==0 ) //gwiro fod dim "00" yn un o'r maesydd
    {
      alert(arrNeges[2]);
      return false;
    }
      else if ( arrDyddiad[1]>12 ) //gwiro fod rhif y mis ddim yn fwy na 12
    {
      alert(arrNeges[3]);
      return false;
    }
      else if ( arrDyddiad[0] > arrGwiroDydd[arrDyddiad[1]] && arrDyddiad[1]!=2 ) //gwiro fod nifer y dyddiau yn ddilys am y mis
    {
      alert(arrNeges[4]);
      return false;
    }
      else if ( arrDyddiad[1]==2 ) //gwiro fod nifer y dyddiau yn ddilys am mis Chwefror
    {
      if ( !((arrDyddiad[2] % 4 == 0 && arrDyddiad[0] <=29) || (arrDyddiad[2] % 4 !=0 && arrDyddiad[0] <= 28)) )
      {
        alert(arrNeges[5]);
  return false;
      }
        else
      {
        return true;
      }
    }
      else
    {
      return true;
    }
  }
}


//------------------------------------------------------------------------------
//Gwiro unrhyw gyswllt a fewnbynnwyd gan y defnyddiwr
//------------------------------------------------------------------------------
function gwiro_cyswllt(strCyswllt,strMaes,strIaith,blnGorfodol)
{
  var strNegesCyswllt;
  var regPatrwmCyswllt;
  
  if ( strIaith == "cymraeg" )
  {
    strNegesCyswllt = "Nid yw'r cyswllt a fewnbynnwyd yn ddilys ym maes " + strMaes;
    strNegesAngen = "Mae rhaid rhoi cyswllt dilys ym maes " + strMaes;
  }
    else if ( strIaith == "english" )
  {
    strNegesCyswllt = "The inputted link is not valid in the field " + strMaes;
    strNegesAngen = "A valid link must be inputted into the field " + strMaes;
  }  
    else
  {
    alert("Nid yw'r iaith a ddewiswyd yn ddilys. The language chosen is invalid.");
  }
  
  regPatrwmCyswllt = /^https?\:\/\//;   

  if ( strCyswllt == "" && blnGorfodol == true )
  {
    alert(strNegesAngen);
    return false;
  }
    else if ( strCyswllt == "" )
  {
    return true;
  }
    else
  {
    if ( !regPatrwmCyswllt.test(strCyswllt) )
    {
      alert(strNegesCyswllt);
      return false;
    }
      else
    {
      return true;
    }
  }
}
//------------------------------------------------------------------------------


//******************************************************************************
//*** Diwedd ffeil:  /cynnwys/js/GWYgwiro.js                                ****
//******************************************************************************

