/* author: www.Star28.com */
/* Creation date: 6/3/2008 */

function validate() {
        if(!input_fields_validation_ar()) {
		return false;
	}
	return true;
}

function has_special_char(str) {
	for(var i = 0; i <str.length; i++) {
		var charat = str.charAt(i);
		 if(charat == '`' || charat == '~'  || charat == '!'  || charat == '@'  || charat == '#'  || charat == '$'  || charat == '%'  || charat == '^'  || charat == '&'  || charat == '*'  || charat == '('  || charat == ')'  || charat == ':'  || charat == '.'  ||
		    charat == '<'  || charat == '>'  || charat == ';'  || charat == ','  || charat == '|'  || charat == '\\'  || charat == '/'  || charat == '?' || charat == '+' || charat == '=' || charat == "'"  || charat == '"' || charat == '-'  || charat == '_'  ||
		    charat == '{'  || charat == '}') {
			return true;
		}
	}
	return false;
}

function valid(str)  {
    for(var i = 0; i < str.length; i++) {
		var charcode = str.charCodeAt(i);
		/* A-Z */
		if(charcode >= 0x41 && charcode <= 0x5A) {
			continue;
		}

        /* a-z */
		if(charcode >= 0x61 && charcode <= 0x7A) {
			continue;
		}

        /* 0-9 */
		if(charcode >= 0x30 && charcode <= 0x39) {
			continue;
		}

		/* . - _ */
		if(charcode == 0x2D || charcode == 0x2E || charcode == 0x5F) {
			continue;
		}
		return false;
    }
    return true;
}

function testemail(obj) {
    var atpos = obj.value.indexOf('@');
    if(atpos == -1) {
		return false;
	}
	if(atpos == 0) {
		return false;
	}
	var dotpos = obj.value.indexOf('.', atpos+2);
	if( dotpos == -1) {
		return false;
	}
	if(dotpos == (obj.value.length - 1) ) {
		return false;
	}
	var fpart = obj.value.substring(0,atpos);
	var host = obj.value.substring(atpos + 1, dotpos);
	var domain = obj.value.substr(dotpos +1);
	if(!( valid(fpart) && valid(host) && valid(domain))) {
		return false;
	}
	var afterat = obj.value.substr(atpos + 1);
	if(afterat.lastIndexOf('.') == (afterat.length - 1)) {
		return false;
	}
	for(var i = 1; i < afterat.length; i++) {
		if(afterat.charAt(i) == '.' && afterat.charAt(i-1) == '.') {
			return false;
		}
	}
	return true;
}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function testuser(obj) {
    for(var i = 0; i < obj.value.length; i++) {
    	var charcode = obj.value.charCodeAt(i);
    	/* A-Z */
 		if(charcode >= 0x41 && charcode <= 0x5A) {
	        continue;
		}

		/* a-z */
		if(charcode >= 0x61 && charcode <= 0x7A) {
			continue;
		}

		/* 0-9 */
		if(charcode >= 0x30 && charcode <= 0x39) {
			continue;
		}

		/* . - _ */
		if(charcode == 0x2D || charcode == 0x5F) {
			continue;
		}
		return false;
	}
	return true;
}

function check_special_chr(obj)
{
   if(obj.value.indexOf('\\(')  >= 0 || obj.value.indexOf('\\)')  >= 0 ||
      obj.value.indexOf('\\[')  >= 0 || obj.value.indexOf('\\]')  >= 0  ||
      obj.value.indexOf('\\<')  >= 0 || obj.value.indexOf('\\>')  >= 0  ||
      obj.value.indexOf('\\\\')  >= 0 || obj.value.indexOf('/')   >= 0   ||
      obj.value.indexOf('\\"')  >= 0 || obj.value.indexOf('~')   >= 0   ||
      obj.value.indexOf('#')  >= 0 || obj.value.indexOf("\$")   >= 0   ||
      obj.value.indexOf('^')  >= 0 || obj.value.indexOf('*')   >= 0   ||
      obj.value.indexOf('%')  >= 0 || obj.value.indexOf('!')   >= 0   ||
      obj.value.indexOf('?')  >= 0 || obj.value.indexOf('&')   >= 0)
      return true;
}

function input_fields_validation_ar ( ) {

    var d = new Date();

	document.mailform.name.value    = trim(document.mailform.name.value);
    document.mailform.email.value   = trim(document.mailform.email.value);
	document.mailform.subject.value = trim(document.mailform.subject.value);
	document.mailform.message.value = trim(document.mailform.message.value);
	document.mailform.code.value    = trim(document.mailform.code.value);
	
	/* name */
	if(document.mailform.name.value == "") {
		window.alert("الرجاء ادخال الاسم");
		document.mailform.name.focus(); 
		return false;
	}
	
    if(has_special_char(document.mailform.name.value)) {
	  window.alert("يرجى استخدام الأحرف فقط في خانة الاسم");
	  document.mailform.name.focus();
      return false;
	}
	
    /* email */
    if(document.mailform.email.value == "") {
         window.alert("يرجى ادخال البريد الالكتروني");
         document.mailform.email.focus();
         return false;
    }
	
    if(document.mailform.email.value != "") {
        if(!testemail(document.mailform.email)) {
            window.alert("يرجى التأكد من صحة البريد الالكتروني");
            document.mailform.email.focus();
            return false;
        }
    }
	
    /* subject */
    if(document.mailform.subject.value == "") {
         window.alert("يرجى ادخال موضوع الرسالة");
         document.mailform.subject.focus();
         return false;
    }
	
	/* message */
    if(document.mailform.message.value == "") {
         window.alert("يرجى ادخال الرسالة");
         document.mailform.message.focus();
         return false;
    }
	
    /* code */
    if(document.mailform.code.value == "") {
        window.alert("الرجاء طباعة الرقم الذي يظهر في الصورة");
        document.mailform.code.focus();
        return false;
    }
	
	if(IsNumeric(document.mailform.code.value) == false ) {
        window.alert("الرجاء استخدام ارقام فقط");
        document.mailform.code.focus();
        return false; 
    }
	
	if(document.mailform.code.value.length != 6) {
        window.alert("رقم التأكيد يجب أن يحتوي على ستة أرقام");
        document.mailform.code.focus();
        return false;
    }
	
    return true;
}

function trim(str) {      
        var from_start = 0;
        var from_end = str.length - 1;
        var return_value;

        while(str.charAt(from_start) == ' ') {
            from_start ++;
        }

        while(str.charAt(from_end) == ' ') {
           from_end --;
        }
   
        if(from_end < from_start){
           return '' ;
        }

        return_value = str.substring(from_start,from_end+1);
        return return_value;

}//end function trim 



