/* author: www.Star28.com */
/* Creation date: 6/3/2008 */

function restrictinput(maxlength,e,placeholder){
 if (window.event&&event.srcElement.value.length>=maxlength) {
  return false;
 }
 else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
  var pressedkey=/[a-zA-Z0-9\.\,\/]/ ; //detect alphanumeric keys
  if (pressedkey.test(String.fromCharCode(e.which)))e.stopPropagation();
 }
}

function countlimit(maxlength,e,placeholder){

 var theform=eval(placeholder);
 var lengthleft=maxlength-theform.value.length;
 var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder);
 
 if (window.event||e.target&&e.target==eval(placeholder)){
  if (lengthleft<0) {
   theform.value=theform.value.substring(0,maxlength);
  }
  placeholderobj.innerHTML=lengthleft;
 }
}

function displaylimit(theform,thelimit){
 var ns6=document.getElementById&&!document.all;
 var limit_text='<font face=TAHOMA size=1>عدد الأحرف المتبقية: <b><span id="'+theform.toString()+'">'+thelimit+'</span></b></font>'

 if (document.all||ns6) document.write(limit_text);

 if (document.all){
  eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
  eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
 } 
 else if (ns6){
  document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true);
  document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, 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 == '=') {
			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 Isphone(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "+0123456789-() ";
   var strChar;
   var blnResult = true;
   
   if ((strString.length > 3) && (strString.length < 35)) {
   
   //  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;
    }
   else return false;
}

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 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 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();

	/* Trim */
	document.addform.title.value = leftTrim(rightTrim(document.addform.title.value));
	document.addform.title.value = trim(document.addform.title.value);
    document.addform.des.value   = trim(document.addform.des.value );
	document.addform.city.value  = trim(document.addform.city.value );
	document.addform.area.value  = trim(document.addform.area.value );
	document.addform.phone.value  = trim(document.addform.phone.value );
	document.addform.mobile.value  = trim(document.addform.mobile.value );
	document.addform.pusd.value  = trim(document.addform.pusd.value );
	document.addform.parea.value  = trim(document.addform.parea.value );
	
	// title
	if(document.addform.title.value == "") {
		window.alert("حقل عنوان الطلب فارغ");
		document.addform.title.focus(); 
		return false;
	}
	
	if(has_special_char(document.addform.title.value)) {
		window.alert("يرجى استخدام الأحرف فقط في عنوان الطلب");
		document.addform.title.focus();
        return false;
	} 
	
	if(document.addform.title.value.length < 7) {
		window.alert("يرجى كتابة 7 احرف على الاقل في عنوان الطلب");
		document.addform.title.focus();
        return false;
	}
	// buyorsell
    	if(document.addform.buyorsell.value == "0") {
		window.alert("يرجى اختيار نوع الطلب");
		document.addform.buyorsell.focus(); 
		return false;
	}
	
	// adtype
    	if(document.addform.adtype.value == "0") {
		window.alert("يرجى اختيار نوع العقار");
		document.addform.adtype.focus(); 
		return false;
	}
	
	// des
    	if(document.addform.des.value == "") {
		window.alert("حقل الوصف فارغ");
		document.addform.des.focus(); 
		return false;
	}
	
	    if(document.addform.des.value.length < 60) {
		window.alert("يرجى استخدام 60 حرف كحد أدنى في خانة وصف العقار");
		document.addform.des.focus(); 
		return false;
	}
	
	    if(document.addform.des.value.length > 1000) {
		window.alert("يرجى استخدام أقل من 1000 حرف في خانة وصف العقار");
		document.addform.des.focus(); 
		return false;
	}
	// space
    	if(document.addform.space.value == "0") {
		window.alert("يرجى ادخال المساحة");
		document.addform.space.focus(); 
		return false;
	}
	// city
    	if(document.addform.city.value == "") {
		window.alert("يرحى ادخال المدينة");
		document.addform.city.focus(); 
		return false;
	}
		if(has_special_char(document.addform.city.value)) {
		window.alert("يرجى استعمال الأحرف فقط في خانة المدينة");
		document.addform.city.focus(); 
		return false;
	}
	// area
    	if(document.addform.area.value == "") {
		window.alert("يرجى ادخال المنطقة");
		document.addform.area.focus(); 
		return false;
	}
		if(has_special_char(document.addform.area.value)) {
		window.alert("يرجى استعمال الأحرف فقط في خانة المنطقة");
		document.addform.area.focus(); 
		return false;
	}
	// phone
	if(document.addform.phone.value == "") {
         window.alert("يرجى ادخال رقم الهاتف");
         document.addform.phone.focus();
         return false;
    }
	if(Isphone(document.addform.phone.value) == false ) {
         window.alert("يرجى ادخال رقم صحيح");
         document.addform.phone.focus();
         return false;
    }
	// mobile
	if(document.addform.mobile.value != "") {
         if(Isphone(document.addform.mobile.value) == false ) {
         window.alert("يرجى ادخال رقم صحيح");
         document.addform.mobile.focus();
         return false;
         }
    } 
   	// parea 
	if(document.addform.parea.value != "") {
	 if(IsNumeric(document.addform.parea.value) == false ) {
		window.alert("يرجى استخدام الأرقام فقط في السعر");
		document.addform.parea.focus();
        return false;
	 } 
	}
	// pusd
	if(document.addform.pusd.value != "") {
	 if(IsNumeric(document.addform.pusd.value) == false ) {
		window.alert("يرجى استخدام الأرقام فقط في السعر");
		document.addform.pusd.focus();
        return false;
	 } 
	}
   // deletedate
    	if(document.addform.deletedate.value == "0") {
		window.alert("تحديد مدة صلاحية الطلب مطلوبة");
		document.addform.deletedate.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

