var whitespace = " \t\n\r";
var defaultEmptyOK = true;

// replace 
function replaceAll (s, fromStr, toStr)
{
	var new_s = s;
	for (i = 0; i < 100 && new_s.indexOf (fromStr) != -1; i++)
	{
		new_s = new_s.replace (fromStr, toStr);
	}
	return new_s;
}

// replace a single quote by a double quote for sql statement compatibility
function sqlSafe (s)
{
	var new_s = s;
	new_s = replaceAll (new_s, "'", "|");
	new_s = replaceAll (new_s, "|", "''");
	new_s = replaceAll (new_s, "\"", "|");
	new_s = replaceAll (new_s, "|", "''");
	return new_s;
}

// same as sqlsafe but with an object as parameter
function makeSafe (i)
{
	i.value = sqlSafe (i.value);
}

// test if a string is empty or null
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// test if a string is only a set of whitespaces
function isWhitespace (s)

{   var i;

	if (isEmpty(s)) return true;


    for (i = 0; i < s.length; i++)
    {   
	var c = s.charAt(i);

	if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}



// accept only numbers (decimal)
function ForceNumber(objField, FieldName)
{
	var strField = new String(objField.value);
	var strFormatArray;

	if (isWhitespace(strField)) return true;

	var i = 0;

	for (i = 0; i < strField.length; i++)
		if ( strField.charAt(i) != '.' & (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;
		}
	
	if (strField.indexOf(".") != -1)
	{
		strFormatArray = strField.split(".");
		if (strFormatArray.length != 2) 
		{
			alert("" + FieldName + " is not a valid decimal value");
			objField.focus();
			objField.select();
			return false;
		}
	}

	return true;
}

// accept only Integers
function ForceInteger(objField, FieldName)
{
	var strField = new String(objField.value);
	var strFormatArray;

	if (isWhitespace(strField)) return true;

	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 Integer.  Please do not use commas, dots or dollar signs or any non-numeric symbols');
						objField.focus();
			return false;
		}
	
	if (strField.indexOf(".") != -1)
	{
		strFormatArray = strField.split(".");
		if (strFormatArray.length != 2) 
		{
			alert("" + FieldName + " is not a valid decimal value");
			objField.focus();
			objField.select();
			return false;
		}
	}

	return true;
}



// accept only numbers (Integer)
function ForceMoney(objField, FieldName)
{
	var strField = new String(objField.value);
	
	if (isWhitespace(strField)) return true;

	var i = 0;

	for (i = 0; i < strField.length; i++)
		if ((strField.charAt(i) < '0' || strField.charAt(i) > '9') && (strField.charAt(i) != '.')) {
			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;
}

// right trim
function RTrim(strTrim)
{
	var trimvalue = "";
	arglen = strTrim.length;
	if (arglen < 1) return trimvalue;

	var lastpos = -1;
	i = arglen;
	while (i >= 0)
	{
		if (strTrim.charCodeAt(i) != 32 &&
		!isNaN(strTrim.charCodeAt(i)))
		{
			lastpos = i;
			break;
		}
		i--;
	}

	trimvalue = strTrim.substring(0,lastpos+1);
	return trimvalue;
}

// left
function Left(str, length)
{
var objStr = String(str);
return objStr.substring(0, length);
}

// right
function Right(str, length)
{
var objStr = String(str);
var rlength = objStr.length - length;
return objStr.substr(rlength);
}


function ForceDecimal(objField, FieldName, Focus, position, requiredLength)
{
	var current = objField.value; 
	var strField = new String(objField.value);
	var realLength = strField.length;
	var decimal = current.indexOf(".");
	var realpart = requiredLength - position;
	var real = 0;
	
	if (ForceNumber(objField, FieldName))
	{
		if(decimal != -1)
		{
			realLength--;
			real = realLength - decimal;
		}
		else 
			decimal = realLength;

		if (  (decimal > realpart) | (real > position) )
		{ 
			if (position == 0 & decimal !=-1)
				alert("" + FieldName + "Field must have the format : Number(" + realpart + ") ");
			else
				alert("" + FieldName + "Field must have the format : Number(" + realpart + ", " + position + ") " );

			if (Focus){
				objField.focus();
				objField.select();
			}
			return false;
		}
		return true;
	}
	else
		return false;
}

function ForceString(objField, lang)
{
	var strField = new String(objField.value);
	
	if (isWhitespace(strField)) return true;

	var i = 0;

   for (var i = 0; i < strField.length; i++) 
      {
      var ch = strField.substring(i, i + 1);
      if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && ch != ' ') 
         {
         alert("\nThis field can only be formed of letter.\n\nPlease type in correctly.");
         objField.select();
         objField.focus();
         return false;
         }
      }

	return true;
}




function ForceArabicString(objField, strWarning)
{
	var strField = new String(objField.value);
	
	if (isWhitespace(strField)) return true;

	var i = 0;

   for (var i = 0; i < strField.length; i++) 
      {
      var ch = strField.substring(i, i + 1);
      if (((ch < "ء" || "ي" < ch) && ch != ' '))
         {
         alert(strWarning);
         objField.select();
         objField.focus();
         return false;
         }
      }

	return true;
}



//most important**********************************************************************

//validation for requeired fields
function ForceEntry(objField, FieldName, Focus)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField)) {
		alert("يجب إدخال البيانات في الحقل: " + FieldName);
		if (Focus){
			objField.focus();
		    objField.select();
		}
		return false;
	}

	return true;
}





//email validation
function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return EmailAlert();
       else return (isEmail.arguments[1] == true);
   
   // if (isWhitespace(s)) return EmailAlert();
    
    var i = 1;
    var sLength = s.length;

    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return EmailAlert();
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return EmailAlert();
    else return true;
}
// email alert
function EmailAlert(){
   alert('يرجى إدخال بريد الكتروني صحيح.');
   return false;
}



// force field lenght
function ForceMinLength(objField, nLength, strWarning)
{
	var strField = new String(objField.value);

	if (strField.length < nLength) {
		alert(strWarning);
		objField.focus();
		objField.select();

		return false;
	} else
		return true;
}


function ForceMaxLength(objField, nLength, strWarning)
{
	var strField = new String(objField.value);

	if (strField.length > nLength) {
		alert(strWarning);

		objField.focus();
		objField.select();
		return false;
	} else
		return true;
}

function validformVote(formobj){
	// name of mandatory fields
	var fieldRequired = Array("R1");
	// field description to appear in the dialog box
	var fieldDescription = Array("");
	// dialog message
	var alertMsg = "يرجى اختيار أحد الخيارات.";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			if (obj.type == null){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += fieldDescription[i] + "\n";
				}
				continue;
			}

			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}
//-->
