// Clear Form Fields
function clearForms()
{
  var i;
  for (i = 0; (i < document.forms.length); i++) {
    document.forms[i].reset();
  }
}

// Toggle Form Field Backgroung Color
function toggleColor(objElement)
{
  if (objElement.className=='normal')
    objElement.className='focus';
  else
    objElement.className='normal';
}

// Set Focus of Form Field
function setFocus(aField) 
{
   document.forms[0][aField].focus();
}

// Validate Phone Number
function ValidateNo(NumStr, String) 
{ 
    for(var Idx=0; Idx<NumStr.length; Idx++) 
    { 
        var Char = NumStr.charAt(Idx); 
        var Match = false; 

        for(var Idx1=0; Idx1<String.length; Idx1++) 
        { 
            if(Char == String.charAt (Idx1)) 
                Match = true; 
        } 

        if (!Match) 
            return false; 
    } 
    return true; 
}

// Remove Unwanted Characters From a String
function trim(str)
{
   return str.replace(/^\s+|\s+$/g,'');
}

// Check for Valid Email Address
function isEmail(str)
{
   var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;

return regex.test(str);
}

// Check for Party Date in the past
function validReservation(strMonth, strDay)
{
   var d=new Date();
   var md=(d.getMonth() + 1);
   var wd=(d.getDate());

   if (((strMonth <= 8) && (strDay < 8)) || ((strMonth >= 9) && (strDay > 8)))
   {
      // Reservations not started or ended
      return true;
   }
   
   else if (((strMonth <= md) && (strDay < wd + 1)))
   {
      // No 24 hour notice
      return true;
   }

   else
   {
      return false;
   }
}

// Validate Form Fields
function validate_form ( )
{
    valid = true;

    if (document.contactinfo.State.value == "" )
    {
        alert ( "Please select your 'Hooters State Location'." );
        setFocus("State");
		valid = false;
        return valid;
    }

    else if (document.contactinfo.Location.value == "" )
    {
        alert ( "Please select your 'Favorite Hooters Restaurant'." );
        setFocus("Location");
		valid = false;
        return valid;
    }

    else if ( document.contactinfo.Name.value == "" )
    {
        alert ( "Please provide your 'Full Name'." );
        setFocus("Name");
        valid = false;
        return valid;
    }

    else if (!isEmail(document.contactinfo.Email.value))
    {
        alert ( "Please provide a valid 'Email' address." );
        setFocus("Email");
        valid = false;
        return valid;        
    }

    else if (document.getElementById("Month").selectedIndex < 1 || document.getElementById("Day").selectedIndex < 1 || document.getElementById("Year").selectedIndex < 1)
  	{
	    alert ( "Please provide your 'Birthday'." );
        setFocus("Month");
        valid = false;
        return valid;    
  	}

    else if (document.getElementById("CellAreaCode").value.length < 3 || !ValidateNo(document.getElementById("CellAreaCode").value,"1234567890"))
    {
        alert ( "Please provide a valid 'Cell' number." );
		document.getElementById("CellAreaCode").value = "";
        setFocus("CellAreaCode");
        valid = false;
        return valid;
    }

    else if (document.getElementById("CellPrefix").value.length < 3 || !ValidateNo(document.getElementById("CellPrefix").value,"1234567890"))
    {
        alert ( "Please provide a valid 'Cell' number." );
		document.getElementById("CellPrefix").value = ""; 
        setFocus("CellPrefix");
        valid = false;
        return valid;
    }

    else if (document.getElementById("CellSuffix").value.length < 4 || !ValidateNo(document.getElementById("CellSuffix").value,"1234567890"))
    {
        alert ( "Please provide a valid 'Cell' number." );
		document.getElementById("CellSuffix").value = "";
        setFocus("CellSuffix");
        valid = false;
        return valid;
    }

    else if (document.getElementById("PartyMonth").selectedIndex < 1)
  	{
	    alert ( "Please select your desired party 'Month'." );
        setFocus("PartyMonth");
        valid = false;
        return valid;    
  	}

    else if (document.getElementById("PartyDay").selectedIndex < 1)
  	{
	    alert ( "Please select your desired party 'Day'." );
        setFocus("PartyDay");
        valid = false;
        return valid;    
  	}

    else if (validReservation(document.getElementById("PartyMonth").value, document.getElementById("PartyDay").value))
     {
        alert("Selected 'Party Date' is either a date in the past, a date before August 8, a date after September 8, or doesn't provide a minimum of 24 hour advance notice.");
        document.getElementById("PartyMonth").selectedIndex = 0;
        document.getElementById("PartyDay").selectedIndex = 0;
        document.getElementById("PartyMonth").value = "";
        document.getElementById("PartyDay").value = "";
        setFocus("PartyMonth");
        valid = false;
        return valid;
    }

    else if (document.getElementById("Hour").selectedIndex < 1)
  	{
	    alert ( "Please select the 'Hour' for your desired party time." );
        setFocus("Hour");
        valid = false;
        return valid;    
  	}

    else if (document.getElementById("Minute").selectedIndex < 1)
  	{
	    alert ( "Please select the 'Minute' for your desired party time." );
        setFocus("Minute");
        valid = false;
        return valid;    
  	}

    else if (document.getElementById("Meridian").selectedIndex < 1)
  	{
	    alert ( "Please select 'AM or PM' for your desired party time.");
        setFocus("Meridian");
        valid = false;
        return valid;    
  	}

    else if (document.getElementById("Number").value.length < 1 || !ValidateNo(document.getElementById("Number").value,"1234567890"))
    {
        alert ( "Please provide your 'Party Total' number." );
		document.getElementById("Number").value = ""; 
        setFocus("Number");
        valid = false;
        return valid;
    }

    else

    {
       return valid;
    }       
}

