/* author: www.Star28.com */
/* Creation date: 6/3/2008 */

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 == '}') {
			return true;
		}
	}
	return false;
}

function validate_ar() {
        if(!input_fields_validation_ar()) {
		return false;
	}
	return true;
}

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();

	/* name */
	document.myfrm.name.value = leftTrim(rightTrim(document.myfrm.name.value));
	document.myfrm.name.value = trim(document.myfrm.name.value);
    document.myfrm.password.value        = trim(document.myfrm.password.value );
	document.myfrm.password2.value = trim(document.myfrm.password2.value );
	document.myfrm.email.value          = trim(document.myfrm.email.value );
	document.myfrm.U_num.value               = trim(document.myfrm.U_num.value );
		
	if(document.myfrm.name.value == "") {
		window.alert("الرجاء ادخال الاسم");
		document.myfrm.name.focus(); 
		return false;
	}
	
	if(has_special_char(document.myfrm.name.value)) {
		window.alert("الأحرف والأرقام و العلامات - _ . مسموحة في الاسم فقط");
		document.myfrm.name.focus();
        return false;
	} 
	
	if((document.myfrm.name.value.length < 3) || (document.myfrm.name.value.length > 20)){
		window.alert("يرجى استخدام من 3 الى 20 خانة كحد أقصى");
		document.myfrm.name.focus();
        return false; 
	}

    /* email */
    if(document.myfrm.email.value == "") {
         window.alert("يرجى ادخال البريد الالكتروني");
         document.myfrm.email.focus();
         return false;
    }
	
    if(document.myfrm.email.value != "") {
        if(!testemail(document.myfrm.email)) {
            window.alert("يرجى التأكد من صحة البريد الالكتروني");
            document.myfrm.email.focus();
            return false;
        }
    }
	
	if(document.myfrm.email.value.length > 50) {
            window.alert("لا يمكن استخدام بريد الكتروني بأكثر من 50  خانة");
            document.myfrm.email.focus();
            return false;
    }

    /* password & password2 */
    if(document.myfrm.password.value.length > 20) {
        window.alert("الرجاء اختيار كلمة سر أقل من 20 خانة");
        document.myfrm.password.focus();
        return false;
    }
	
    if(document.myfrm.password.value == "") {
        window.alert("الرجاء اختيار كلمة سر");
        document.myfrm.password.focus();
        return false;
    }
    
    if(document.myfrm.password.value != document.myfrm.password2.value) {
        window.alert("كلمات السر لا تتوافق");
        document.myfrm.password2.focus();
        return false;
    }
	
	/* country */
		if(document.myfrm.country.value == "") {
		window.alert("الرجاء اختيار البلد");
		document.myfrm.country.focus(); 
		return false;
	    }
		
    /* U_num */
    if(document.myfrm.U_num.value == "") {
        window.alert("الرجاء طباعة الرقم الذي يظهر في الصورة");
        document.myfrm.U_num.focus();
        return false; 
    }
	
	if(IsNumeric(document.myfrm.U_num.value) == false ) {
        window.alert("الرجاء استخدام ارقام فقط");
        document.myfrm.U_num.focus();
        return false; 
    }
	
	if(document.myfrm.U_num.value.length != 6) {
        window.alert("رقم التأكيد يجب أن يحتوي على ستة أرقام");
        document.myfrm.U_num.focus();
        return false;
    }
    
    /* agree */
    if(document.myfrm.agree.checked == false) {
        window.alert("الرجاء التأكيد على سياسية الخصوصية وشروط الخدمة");
        document.myfrm.agree.focus();
        return false;
    }
    return true;
}



function leftTrim(sString)
{
while (sString.substring(0,1) == ' '){
sString = sString.substring(1, sString.length);
}
return sString;
}

function rightTrim(sString){
while (sString.substring(sString.length-1,sString.length) == ' '){
sString = sString.substring(0,sString.length-1);
}
return sString;
}

	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 



