/* author: www.Star28.com */
/* Creation date: 6/3/2008 */

function vali() {
        if(!input_fields_login()) {
		return false;
	}
	return true;
}

function ISvalid(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 ISemail(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(!( ISvalid(fpart) && ISvalid(host) && ISvalid(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 input_fields_login ( ) {

    var d = new Date();

	// Trim 

    document.log.password.value = trim_s(document.log.password.value );
	document.log.email.value    = trim_s(document.log.email.value );


    // email
    if(document.log.email.value == "") {
         window.alert("يرجى ادخال البريد الالكتروني");
         document.log.email.focus();
         return false;
    }
	
    if(document.log.email.value != "") {
        if(!ISemail(document.log.email)) {
            window.alert("يرجى التأكد من صحة البريد الالكتروني");
            document.log.email.focus();
            return false;
        }
    }
	
	if(document.log.email.value.length > 50) {
            window.alert("البريد الالكتروني تجاوز 50 خانة");
            document.log.email.focus();
            return false;
    }

    // password
    if(document.log.password.value.length > 20) {
        window.alert("كلمة السر تجاوزت 20 خانة");
        document.log.password.focus();
        return false;
    }
	
    if(document.log.password.value == "") {
        window.alert("الرجاء ادخال كلمة المرور");
        document.log.password.focus();
        return false;
    }

    return true;
}


function trim_s(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_s 


