function GoToPage(URL)
{
  window.open(URL, '_top');
}

function PopUp(URL)
{
  window.open(URL, '_blank');
}
function doMsg()
{
  alert("message");
}
// DEFINE VARIABLES

// whitespace characters
var whitespace = " \t\n\r";

function PromptErrorMsg(Field,strError)
{
        alert("You have entered an invalid date for " + strError + ".  Please make sure your date format is in M/D/Y format.");
        Field.focus();
}

// Returns true if string s is empty or
// whitespace characters only.
// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function isFixedLength (s,fixedLength,FielldName)
{

    if (s.length == fixedLength)
    {
       return true;
    }
    else
    {
       alert("You must enter a number of " + fixedLength + " digits in " + FielldName);
       return false;
    }
    return false;
}

function ForcePhoneNumber(objField,FieldName)
{
    var strField = new String(objField.value);

        var i = 0;
        for (i = 0; i < strField.length; i++)
                if (strField.charAt(i) < '0' || strField.charAt(i) > '9') {
                        alert(FieldName + " must be a valid numeric entry.  Please do not use commas or dollar signs or any non-numeric symbols.");
                        objField.focus();
                        return false;
                }

        return true;
}

function ForceNumber(objField,FieldName)
{
    var strField = new String(objField.value);

        var i = 0;
        for (i = 0; i < strField.length; i++)
                if (strField.charAt(i) < '0' || strField.charAt(i) > '9' ) {
                        alert(FieldName + " must be a valid numeric entry.  Please do not use commas or dollar signs or any non-numeric symbols.");
                        objField.focus();
                        return false;
                }

        return true;
}

function isDateNumber(strNum,method)
{
        var str = new String(strNum);
        var i = 0;

        if (isNaN(parseInt(str)) || parseInt(str) < 0) return false;

        if (method == 2)
                if (parseInt(str) > 31)
                        return false;
        if (method == 1)
                if (parseInt(str) > 12)
                        return false;

        for (i = 0; i < str.length; i++)
                if (str.charAt(i) < '0' || str.charAt(i) > '9')
                        return false;


        return true;
}

/* PURPOSE: Checks to see if the string is a valid date.  A valid
        date is defined as any of the following:

                MM/DD/YY, MM/DD/YYYY, M/D/YY, M/D/YYYY,
                MM-DD-YY, MM-DD-YYYY, M-D-YY, M-D-YYYY
*/

function ForceDate(strDate,strField)
{
        var str = new String(strDate.value);

        if (isWhitespace(str)) {
                return true;
                // if the field is empty, just return true...
        }

        var i = 0, count = str.length, j = 0;
        while ((str.charAt(i) != "/" && str.charAt(i) != "-") && i < count)
                i++;

        if (i == count || i > 2) {
                PromptErrorMsg(strDate,strField);
                return false;
        }

        var addOne = false;
        if (i == 2) addOne = true;

        if (!isDateNumber(str.substring(0,i),1)) {
                PromptErrorMsg(strDate,strField);
                return false;
        }

        j = i+1;
        i = 0;

        while ((str.charAt(i+j) != "/" && str.charAt(j+i) != "-") && i+j < count)
                i++;

        if (i+j == count || i > 2) {
                PromptErrorMsg(strDate,strField);
                return false;
        }

        if (!isDateNumber(str.substring(j,i+j),2)) {
                PromptErrorMsg(strDate,strField);
                return false;
        }

        j = i+3;
        i = 0;

        if (addOne) j++;

        while (i+j < count)
                i++;


        if (i != 2 && i != 4) {
                PromptErrorMsg(strDate,strField);
                return false;
        }

        if (!isDateNumber(str.substring(j,i+j),3)) {
                PromptErrorMsg(strDate,strField);
                return false;
        }

        return true;
}

function setFocus(field_id)
{
  var field=document.getElementById(field_id);
  field.focus();
}

function printNow()
{
            var pr = (window.print) ? 1 : 0;
            if (pr) {
                    window.print();
            }else {
                    alert("Sorry, your browser doesn't support this feature.");
                    return false;
            }
}

function confirm_deletion(messege)
{
       assure = confirm(messege);
       if (assure !="0")
          return true;
       else
          return false;
}

function addRow()
{
     var tbl = document.getElementById('schoolProgramsTable');
     var lastRow = tbl.rows.length;

     var iteration = lastRow;
     // creates a new row
     var row = tbl.insertRow(lastRow);

      // insert a cell
     var cellLeft = row.insertCell(0);
     var textNode = document.createTextNode(iteration);
     cellLeft.appendChild(textNode);

     appendElement(row,iteration,1,'text','txtProgName','txtProgName',25,0,'');
     appendElement(row,iteration,2,'text','txtProgPrice','txtProgName',10,0,0);
     appendElement(row,iteration,3,'checkbox','chkDelete','chkDelete',0,0,1);
     appendElement(row,iteration,4,'hidden','hiddenProgId','hiddenProgId',10,0,0);
     // get the hiddent field
     var fcount = document.getElementById('hiddenFCount');
     fcount.value = lastRow;
}

function removeRow()
{
     // grab the element again!
     var tbl = document.getElementById('schoolProgramsTable');
     // grab the length!
     var lastRow = tbl.rows.length;
     // delete the last row if there is more than one row!
     if (lastRow > 2) tbl.deleteRow(lastRow - 1);

     // get the hidden field
     var fcount = document.getElementById('hiddenFCount');
     fcount.value = tbl.rows.length - 1;
}

function appendElement(row,iteration,number,type,name,id,size,readonly,value)
{
     var cell = row.insertCell(number);
     var el = document.createElement('input');
     el.type = type;
     el.name = name + iteration;
     if (value != null)
       el.value = value;

     el.id = id + iteration;
     el.size = size;
     if (readonly == 1)
       el.readonly = " readonly";
//     el.onchange = function(){calculateTotal(id);}
     cell.appendChild(el);
}

function check_user_option(form_id,table_to_show,url)
{

   var found_it;
   var x=document.getElementById(form_id);
   for (var i=0; i<x.radioBrowse.length; i++)
   {
     if (x.radioBrowse[i].checked)
     {

       found_it = x.radioBrowse[i].value;
       if  (found_it ==1)
       {
         display_ShowHide(table_to_show);
       }
       else
       {
         GoToPage(url);
       }
     }
   }
}

function display_ShowHide(id)
{
  var target = document.getElementById(id);
  if (target.style.display == '') {
           target.style.display = 'none';
     return true;
  }
  if (target.style.display == "none") {
           target.style.display = '';
  }
}

function check_req_username(form_id)
{
  var x=document.getElementById(form_id);
  var field=x.txtUsername;
  if(field.value.length == 0)
  {
     alert("You must enter a Password");
     return false;
  }
  return true;
}

function check_email(field_id)
{
  var field=document.getElementById(field_id);
//  var field=x.txtEmail;

  if (isWhitespace(field.value))
  {
                return true;
                // if the field is empty, just return true...
  }
  var pass;
  if(field.value)
  {
           pass=1;
           var str = field.value;
           var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
           var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
           if (!reg1.test(str) && reg2.test(str))
           {
            return true;
           }else{
            alert ("Please enter a Correct Email");
            field.focus();
            return false;
           }
  }
  if(pass != 1)
  {
     alert("Please enter an Email");
     field.focus();
     return false;
  }
}
function validate_bookonline_form(form_id)
{
   var option_selected;
   var x=document.getElementById(form_id);
   if (isWhitespace(x.txtFName.value))
   {
      alert("Please fill in the Name Field");
      x.txtFName.focus();
      return false;
   }
   if (isWhitespace(x.txtLName.value))
   {
      alert("Please fill in the Last Name Field");
      x.txtLName.focus();
      return false;
   }
   if (isWhitespace(x.txtEmail.value))
   {
      alert("Please fill in the Email Field");
      x.txtEmail.focus();
      return false;
   }
   if (check_email('txtEmail') == false)
   {
     x.txtEmail.value = "";
     return false;
   }
   if (isWhitespace(x.txtPhone.value) == true)
   {
      alert("Please fill in the Phone Field");
      x.txtPhone.focus();
      return false;
   }
   if (isWhitespace(x.txtAge.value) == true)
   {
      alert("Please fill in the Age Field");
      x.txtAge.focus();
      return false;
   }
   if (isWhitespace(x.txtOccupation.value) == true)
   {
      alert("Please fill in the Occupation Field");
      x.txtOccupation.focus();
      return false;
   }
   if (isWhitespace(x.txtAddress.value) == true)
   {
      alert("Please fill in the Address Field");
      x.txtAddress.focus();
      return false;
   }
   if (isWhitespace(x.txtCity.value) == true)
   {
      alert("Please fill in the City Field");
      x.txtCity.focus();
      return false;
   }
   if (x.sltState.value == 0)
   {
      alert("Please Select a State");
      x.sltState.focus();
      return false;
   }
   if (isWhitespace(x.txtZipCode.value) == true)
   {
      alert("Please fill in the Zip Code Field");
      x.txtZipCode.focus();
      return false;
   }
   if (isWhitespace(x.txtCountry.value) == true)
   {
      alert("Please fill in the Country Field");
      x.txtCountry.focus();
      return false;
   }
   if (x.sltSchools.value == 0)
   {
      alert("Please Select a School");
      x.sltSchools.focus();
      return false;
   }
   if (x.sltProgs.value == 0)
   {
      alert("Please Select a Programs");
      x.sltProgs.focus();
      return false;
   }
   if (x.sltHouses.value != 0)
   {
     if (x.sltPlans.value == 0)
     {
        alert("Please Select a plan");
        x.sltPlans.focus();
        return false;
     }
   }
   if (x.sltTransportations.value != 0)
   {
     if (isWhitespace(x.txtTransArrivalDate.value) == true)
     {
        alert("Please select the arrival date");
        x.txtTransArrivalDate.focus();
        return false;
     }
     if (isWhitespace(x.txtArrivalFN.value) == true)
     {
        alert("Please fill in the arrival flight number field");
        x.txtArrivalFN.focus();
        return false;
     }
     if (isWhitespace(x.txtTransDepartureDate.value) == true)
     {
        alert("Please select the departure date");
        x.txtTransDepartureDate.focus();
        return false;
     }
     if (isWhitespace(x.txtDepartureFN.value) == true)
     {
        alert("Please fill in the departure flight number field");
        x.txtDepartureFN.focus();
        return false;
     }
   }
   return true;
}
function validate_sendform(form_id)
{
   var x=document.getElementById(form_id);
   if (isWhitespace(x.txtRecipientName.value))
   {
      alert("Please fill in the Recipient Name Field");
      x.txtRecipientName.focus();
      return false;
   }
   if (isWhitespace(x.txtRecipientEmail.value))
   {
      alert("Please fill in Recipient Email Field");
      x.txtRecipientEmail.focus();
      return false;
   }
   if (check_email('txtRecipientEmail') == false)
   {
     x.txtRecipientEmail.value = "";
     return false;
   }
   if (isWhitespace(x.txtFromName.value))
   {
      alert("Please fill in the Your Name Field");
      x.txtFromName.focus();
      return false;
   }
   if (isWhitespace(x.txtFromEmail.value))
   {
      alert("Please fill in the Your Name Field");
      x.txtFromEmail.focus();
      return false;
   }
   if (check_email('txtFromEmail') == false)
   {
     x.txtFromEmail.value = "";
     return false;
   }
   return true;
}
function validate_reviewsform(form_id)
{
   var option_selected;
   var x=document.getElementById(form_id);
   if (isWhitespace(x.txtName.value))
   {
     for (var i=0; i<x.radioRating.length; i++)
     {
       if (x.radioRating[i].checked)
       {
          if (x.radioRating[i].value>0)
          {
             option_selected = 1;
             return true;
          }
          else
          {
             option_selected = 0;
          }
       }
     }
     if (option_selected != 1)
     {
       alert("Please choose a rating or write a review");
       return false;
     }
   }
   else
   {
     if (isWhitespace(x.txtReview.value))
     {
        alert("Please fill in the Review Field");
        x.txtReview.focus();
        return false;
     }
   }
   if (option_selected != 1)
   {
     if (isWhitespace(x.txtName.value))
     {
        alert("Please fill in the Name Field");
        x.txtName.focus();
        return false;
     }
     if (isWhitespace(x.txtReview.value))
     {
        alert("Please fill in the Review Field");
        x.txtReview.focus();
        return false;
     }
   }
   return true;
}
function show(id)
{
   var target = document.getElementById(id);
   target.style.display = '';

}
function validate_linksForm(form_id)
{
   var x=document.getElementById(form_id);
   if (isWhitespace(x.txtLinkURL.value) == true)
   {
      alert("Please fill in the URL Field");
      x.txtLinkURL.focus();
      return false;
   }
   if (ForceNumber(x.txtSortOrder,"Sort Order") == false)
   {
      return false;
   }
   return true;
}

function check_validity(form_id)
{
  var x=document.getElementById(form_id);
// verifing the password field
  if(x.txtOldPass.value.length == 0)
  {
     alert("You must enter the Old  Password");
     return false;
  }
  else
  {
    if(x.txtNewPass.value.length == 0)
    {
       alert("You must enter the New  Password");
       return false;
    }
    else if (x.txtConfirmNewPass.value.length == 0)
    {
       alert("You must enter the New Password Confirmation");
       return false;
    }
    else if (x.txtNewPass.value != x.txtConfirmNewPass.value)
    {
       alert("Passwords don't match");
       return false;
    }
    else
    {
       return true;
    }
  }
}

function validateContactUsForm(form_id)
{
   var x=document.getElementById(form_id);

   if (isWhitespace(x.from_name.value))
   {
       alert ("you must enter your name");
       x.from_name.focus();
       return false;
   }
   else
   {
     if (isWhitespace(x.txtEmail.value))
     {
         alert ("you must enter your email address");
         x.txtEmail.focus();
         return false;
     }
     else
     {
       if (check_email('txtEmail') == true)
       {
         if (ForceNumber(x.from_phone,"Phone Number") == true)
         {
            if (isWhitespace(x.comment.value))
            {
                alert ("you must enter a comment/question");
                x.comment.focus();
                return false;
            }
            return true;
         }
         else
         {
           return false;
         }
       }
     }
     return false;
   }
}

function validateUsersForm(form_id)
{
   var x=document.getElementById(form_id);

   if (isWhitespace(x.txtUserName.value))
   {
       alert ("you must enter the username");
       x.txtUserName.focus();
       return false;
   }
   if (isWhitespace(x.txtPassword.value))
   {
       alert ("you must enter the password");
       x.txtPassword.focus();
       return false;
   }

   return true;
}
