﻿var inputIdEmail = "txtEmail";
var inputUserName = "txtUserName";

//---------------------------------------
var pInfo = "pInfo";


//--------------------------------------
var trInfo = "trInfo";

//----------------------------




//----------------------------
var errorEmpty = "User Name or Email Address is required"; 
var errorEmailFormat = "Please enter your e-mail address in the format someone@example.com."






function changeRowVisibleState(isVisible, rowId)
{
   if(isVisible!='true'&&isVisible!='false')
   {
      return;
   }
   var oRow = document.getElementById(rowId);
   if(oRow.style.display=='none'&&isVisible=='true')
   {
      oRow.style.display='';
      return;
   }
   
   if(oRow.style.display!='none'&&isVisible=='false')
   {   
      oRow.style.display="None";
      return;
   }
}

//type info: info, warning, error
function makeInfoText(txt, infoType, pId)
{
   
   var oP = document.getElementById(pId);
   if(infoType=="error")
   {
      oP.className="errMsg";
   }
   if(oP.hasChildNodes())
      oP.removeChild(oP.childNodes[0]);
   oP.appendChild(document.createTextNode(txt));
}





function validateEmail()
{
   var regEmail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address  = document.getElementById(inputIdEmail).value;
   if(regEmail.test(address) == false) 
   {
      if(address=="")
      {
         return true;
      }
      return false;
   }
   else
   {
      return true;
   }
}





function submitCheck()
{
   //check user name
   var userName = RTrim(document.getElementById(inputUserName).value);
   var email = RTrim(document.getElementById(inputIdEmail).value);
   if(email == "" && userName == "")
   {
      makeInfoText(errorEmpty,"error",pInfo);
      changeRowVisibleState("true",trInfo);
      return false;
      
   }
   if(!validateEmail())
   {
      makeInfoText(errorEmailFormat,"error",pInfo);
      changeRowVisibleState("true",trInfo);
      return false;
   }
   changeRowVisibleState("false",trInfo);
   return true;
}

// --------------------------------------------------------------------------
// Remove trailing blanks from our string.

// I               str - the string we want to RTrim
// Return          the input string without any trailing whitespace

// --------------------------------------------------------------------------
function RTrim(str)
{
  // We don't want to trip JUST spaces, but also tabs,
  // line feeds, etc.  Add anything else you want to
  // "trim" here in Whitespace
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
    // We have a string with trailing blank(s)...

    var i = s.length - 1;       // Get length of string

    // Iterate from the far right of string until we
    // don't have any more whitespace...
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
      i--;


    // Get the substring from the front of the string to
    // where the last non-whitespace character is...
    s = s.substring(0, i+1);
  }

  return s;
}


// --------------------------------------------------------------------------
// Remove railing and leading blanks from our string.

// I               str - the string we want to Trim
// Return          the trimmed input string

// --------------------------------------------------------------------------
function Trim(str)
{
  return RTrim(LTrim(str));
}

// EOF