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

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

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 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 disable_dates(form_id)
{
   var x=document.getElementById(form_id);
   if (x.chkAlways.checked == true)
   {
     x.sltFromDay.disabled = true;
//     x.sltToDay.disabled = true;
     x.sltFromMonth.disabled = true;
//     x.sltToMonth.disabled = true;
     x.sltFromYear.disabled = true;
//     x.sltToYear.disabled = true;
   }
   else
   {
     x.sltFromDay.disabled = false;
//     x.sltToDay.disabled = false;
     x.sltFromMonth.disabled = false;
//     x.sltToMonth.disabled = false;
     x.sltFromYear.disabled = false;
//     x.sltToYear.disabled = false;
   }
   return false;
}

function enable_genders(form_id)
{
   var x=document.getElementById(form_id);
   GenderOption =x.radioBothGenders;

   if (GenderOption[0].checked == true)
   {
     x.radioGenders[0].disabled = true;
     x.radioGenders[1].disabled = true;
   }
   else
   {
     x.radioGenders[0].disabled = false;
     x.radioGenders[1].disabled = false;
   }
   return true;
}

function enable_other(form_id)
{
   var x = document.getElementById(form_id);
   Cities = x.sltCities;
   OtherCity = x.txtCityOther;
   if (Cities.value == -1)
   {
      OtherCity.disabled = false;
   }
   else
   {
      OtherCity.disabled = true;
      OtherCity.value = '';
   }

   Districts = x.sltDistricts;
   OtherDistrict = x.txtDistrictOther;
   if (Districts.value == -1)
   {
      OtherDistrict.disabled = false;
   }
   else
   {
      OtherDistrict.disabled = true;
      OtherDistrict.value = '';
   }

   return true;
}

// 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 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(form_id)
{
  var x=document.getElementById(form_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");
            x.txtEmail.focus();
            return false;
           }
  }
  if(pass != 1)
  {
     alert("Please enter an Email");
     x.txtEmail.focus();
     return false;
  }
}

function validate_SearchForm(form_id)
{
   var x=document.getElementById(form_id);
   if (x.sltAllCountries.value == 0)
   {
     alert("Please Choose the Country");
     x.sltAllCountries.focus();
     return false;
   }
/*
   if (x.sltAllCities.value == 0)
   {
     alert("Please Choose the City");
     x.sltAllCities.focus();
     return false;
   }
   if (x.sltAllDistricts.value == 0)
   {
     alert("Please Choose the District");
     x.sltAllDistricts.focus();
     return false;
   }
   if (x.sltAllAccommTypes.value == 0)
   {
     alert("Please Choose the Type");
     x.sltAllAccommTypes.focus();
     return false;
   }     */
   return true;
}

function validate_tutors_form(form_id)
{
   var x=document.getElementById(form_id);
   if (isWhitespace(x.txtName.value))
   {
      alert("Please fill in the Name Field");
      x.txtName.focus();
      return false;
   }
   if (isWhitespace(x.txtEmail.value))
   {
      alert("Please fill in the Email Field");
      x.txtEmail.focus();
      return false;
   }
   if (check_email(form_id) == 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 (ForceNumber(x.txtPhone,"Phone") == false)
   {
      x.txtPhone.value="";
      x.txtPhone.focus();
      return false;
   }
   return true;
}

function validate_guestbook(form_id)
{
   var x=document.getElementById(form_id);
   if (isWhitespace(x.txtName.value))
   {
      alert("Please fill in the Name Field");
      x.txtName.focus();
      return false;
   }
   if (isWhitespace(x.txtEmail.value))
   {
      alert("Please fill in the Email Field");
      x.txtEmail.focus();
      return false;
   }
   if (check_email(form_id) == false)
   {
     x.txtEmail.value = "";
     return false;
   }
   return true;
}

function validate_addHouseForm(form_id)
{
   var x=document.getElementById(form_id);
   if (isWhitespace(x.txtOwner.value))
   {
      alert("Please fill in the Owner Name Field");
      x.txtOwner.focus();
      return false;
   }
   if (isWhitespace(x.txtOwnerAdd.value) == true)
   {
      alert("Please fill in the Owner Address Field");
      x.txtOwnerAdd.focus();
      return false;
   }
   if (isWhitespace(x.txtOwnderPhone.value) == true)
   {
      alert("Please fill in the Owner Phone Field");
      x.txtOwnderPhone.focus();
      return false;
   }
   if (ForceNumber(x.txtOwnderPhone,"Ownder Phone") == false)
   {
      x.txtOwnderPhone.value="";
      x.txtOwnderPhone.focus();
      return false;
   }
   if (check_email(form_id) == false)
   {
     x.txtEmail.value = "";
     return false;
   }
   if (x.sltCities.value == 0)
   {
     alert("Please Choose the City");
     x.sltCities.focus();
     return false;
   }
   if (x.sltDistricts.value == 0)
   {
     alert("Please Choose the District");
     x.sltDistricts.focus();
     return false;
   }
   if (isWhitespace(x.txtRoomsN.value))
   {
       alert ("Please fill in Number of Rooms Field");
       x.txtRoomsN.focus();
       return false;
   }
   if (ForceNumber(x.txtRoomsN,"Number of Rooms") == false)
   {
      x.txtRoomsN.value="";
      return false;
   }
   if (isWhitespace(x.txtArea.value) == true)
   {
      alert("Please fill in the Area Field");
      x.txtArea.focus();
      return false;
   }
   if (ForceNumber(x.txtArea,"Area") == false)
   {
      x.txtArea.value="";
      return false;
   }
   if (isWhitespace(x.txtPrice.value) == true)
   {
      alert("Please fill in the Price Field");
      x.txtPrice.focus();
      return false;
   }
   if (ForceNumber(x.txtPrice,"Price") == false)
   {
      x.txtPrice.value="";
      return false;
   }
   return true;
}

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(form_id) == 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 validateRequestForm(form_id)
{
   var x=document.getElementById(form_id);

   if (isWhitespace(x.txtName.value))
   {
       alert ("you must enter your name");
       x.txtName.focus();
       return false;
   }
   if (check_email(form_id) == false)
   {
       x.txtEmail.value="";
       x.txtEmail.focus();
       return false;
   }
   if (isWhitespace(x.txtPhone.value) == true)
   {
      alert("Please fill in the Phone Field");
      x.txtPhone.focus();
      return false;
   }
   if (ForceNumber(x.txtPhone,"Phone Number") == false)
   {
       x.txtPhone.value="";
       x.txtPhone.focus();
      return false;
   }
   if (isWhitespace(x.txtAddress.value))
   {
       alert ("you must enter your address");
       x.txtAddress.focus();
       return false;
   }

   return true;
}

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;
}

