/*
 +-------------------------------------------------------------------+
 |                  J S - C H E C K F O R M   (v1.0)                 |
 |                                                                   |
 | Copyright Gerd Tentler               www.gerd-tentler.de/tools    |
 | Created: Oct. 23, 2001               Last modified: Oct. 21, 2005 |
 +-------------------------------------------------------------------+
 | This program may be used and hosted free of charge by anyone for  |
 | personal purpose as long as this copyright notice remains intact. |
 |                                                                   |
 | Obtain permission before selling the code for this program or     |
 | hosting this software on a commercial website or redistributing   |
 | this software over the Internet or in any other medium. In all    |
 | cases copyright must remain intact.                               |
 +-------------------------------------------------------------------+

======================================================================================================

 ARGUMENTS:

  - form-name or -number
  - 'field:title:type:minimum length'[, ...]  (type = number / mail / url / [none])

 Example:

 checkForm('frm1', 'name:::2', 'age::number:1', 'eMail:e-mail:mail:1', 'homepage::url:0');

------------------------------------------------------------------------------------------------------
 This script was tested with the following systems and browsers:

 - Windows XP: IE 6, NN 7, Opera 7, Firefox 1
 - Mac OS X:   IE 5, Safari 1

 If you use another browser or system, this script may not work for you - sorry.
======================================================================================================
*/
//--------------------------------------------------------------------------------------------------------
// Language settings
//--------------------------------------------------------------------------------------------------------

var msgNumber  = "يجب أن يكون عدد صحيح";
var msgEMail   = "يجب أن يكون بريد إلكتروني";
var msgURL     = "يجب أن يكون عنوان لموقع";
var msgFillOut = "قم بكتابة :";
var msgNoForm  = "النموذج غير متوفر";
var msgNoField = "الخانة غير متوفرة";

//--------------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------------
function codename(add,file_up,file) {

if(document.add.file_up.checked)
{
document.add.file.disabled=false;
}

else
{
document.add.file.disabled=true;
}

}
function codename2(add,file_up2,file2) {

if(document.add.file_up2.checked)
{
document.add.file2.disabled=false;
}

else
{
document.add.file2.disabled=true;
}

}
function _trim(str) {
  if(str) {
    str = str.replace(/^\s+/, "");
    str = str.replace(/\s+$/, "");
  }
  return str;
}

function checkForm() {
  var args = checkForm.arguments;
  var f = args[0];
  var msg = "";

  var arr, field, title, type, minLength, elem, val, cnt, i, j;

  var valid_url = /^(https?|ftp):\/\/([a-z0-9._-]+:[a-z0-9._-]+@)?[a-z0-9نِüؤضـ#._\/~% -]+(\?([a-z0-9_-]+(=[a-zA-Z0-99نِüؤضـك+%?_-]+&?)?)*)?$/;
  var valid_mail = /^[a-z0-9._-]+@[a-z0-9نِüؤضـ.-]+\.[a-z]{2,4}$/;

  if(document.forms[f]) {
    for(i = 1; i < args.length; i++) {
      arr = args[i].split(":");
      field = _trim(arr[0]);
      title = _trim(arr[1]);
      if(!title) title = field;
      type = _trim(arr[2]);
      minLength = _trim(arr[3]);
      elem = document.forms[f].elements[field];

      if(elem) {
        val = _trim(elem.value);

        if(val != "") {
          if(type == "number") {
            val = val.replace(",", ".");
            if(isNaN(val)) msg += '"' + title + '" ' + msgNumber + "\n";
          }
          else if(type == "mail" && val.search(valid_mail) == -1) msg += '"' + title + '" ' + msgEMail + "\n";
          else if(type == "url" && val.search(valid_url) == -1) msg += '"' + title + '" ' + msgURL + "\n";
        }

        if(minLength) {
          if(elem.length) {
            if(elem.options) {
              for(j = cnt = 0; j < elem.options.length; j++) {
                if(elem.options[j].selected && elem.options[j].value != "") cnt++;
              }
            }
            else for(j = cnt = 0; j < elem.length; j++) {
              if(elem[j].checked) cnt++;
            }
          }
          else if(elem.type == "checkbox") cnt = elem.checked ? 1 : 0;
          else cnt = val.length;
          if(cnt < minLength) msg += msgFillOut + ' "' + title + '"\n';
        }
      }
      else msg += msgNoField + ': "' + field + '"\n';
    }

    if(msg) alert(msg);
    else document.forms[f].submit();
  }
  else alert(msgNoForm + ': "' + f + '"');
}

//--------------------------------------------------------------------------------------------------------
