
function validateForm(theForm)
{
	// Customize these calls for your form

	// Start ------->
	if (!validRequired(theForm.cont_user,"اسم صاحب العقار "))
		return false;
	if (!validEmail(theForm.cont_email,"البريد الالكتروني لصاحب العقار",true))
		return false;
	if (!validRequired(theForm.cont_contact,"ارقام الاتصال "))
		return false;


	// <--------- End
	
	return true;
}

function validateForm2(theForm)
{
	// Customize these calls for your form

	// Start ------->
	if (!validRequired(theForm.cont_phone,"ارقام الاتصال "))
		return false;
	if (!validRequired(theForm.cont_fullname,"الاسم الكريم "))
		return false;
	if (!validEmail(theForm.cont_email,"البريد الالكتروني",true))
		return false;		
// <--------- End
	return true;
}

function validate_calc_form(theForm)
{
	// Start ------->
	if (!validInt(theForm.pay_years,"عدد السنوات",1))
		return false;
//	if (!validInt(theForm.last_paid_input," في حقل الدفعة الاخيرة",0))
//		return false;
	if (!validNum(theForm.first_paid_input,"الدفعة الاولى",1))
		return false;
	if (!validRequired(theForm.profit_percent," النسبة المئوية "))
		return false;


// <--------- End
	return true;
}//end function

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function is_comma_in(formField)
{
  var result = false;
  var theStr = new String(num);
  var index = theStr.indexOf(",");
  var index2 = theStr.indexOf(".");

  if (index > 0)
  {
  	result = true;
  }//end if index
  if (index2 > 0)
  {
	result = true;
  }//end if index2

  return result;
}

//window.onerror = handleError;

function handleError()
{
	alert("لم يتم تحميل الصفحة بشكل صحيح ");
	
	return false;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("الرجاء ادخال بريد الكتروني صحيح كما بالشكل التالي: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
   
  return result;

}

function validNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('الرجاء ادخال رقم صحيح في حقل "' + fieldLabel );
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validInt(formField,fieldLabel,required)
{
	var result = true;
	var theStr = new String(formField.value);
  	var index = theStr.indexOf(",");
  	var index2 = theStr.indexOf(".");

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var num = parseInt(formField.value,10);
 		if (isNaN(num) || index > 0 || index2 > 0)
 		{
 			alert('الرجاء ادخال رقم صحيح من غير فواصل او حروف' + fieldLabel );
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validDate(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var elems = formField.value.split("/");
 		
 		result = (elems.length == 3); // should be three components
 		
 		if (result)
 		{
 			var month = parseInt(elems[0],10);
  			var day = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();		
		}
	} 
	
	return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;
	if (formField.value == "")
	{
		alert('الرجاء تعبئه حقل "' + fieldLabel +'" وعدم تركها فارغه.');
		formField.focus();
		result = false;
	}
	
	return result;
}

//<!--
function MyValidation()
{
    var list = document.FileUpload;

    // First make sure the applet has initialized
    if (!list.hasInitialized())
        return false; // silently fail 

    // Next make sure that there are two files
    if (list.getFileCount() != 2)
    {
        alert('Please select 2 files, not ' + list.getFileCount());
        return false;
    }
    
    // Finally make sure they are the right types
    var file1 = new String(list.getFile(1));
    var file2 = new String(list.getFile(2));
    var ext1 = file1.substring(file1.lastIndexOf('.')+1,file1.length);
    var ext2 = file2.substring(file2.lastIndexOf('.')+1,file2.length);
    if (ext1 != '.xls' && ext2 != '.xls')
    {
        alert('You forgot to select a spreadsheet file');
        return false;
    }
    if (ext1 != '.doc' && ext2 != '.doc')
    {
        alert('You forgot to select a word processing file');
        return false;
    }

    // File list is valid
    return true;
}
//-->

function validateForm(theForm)
{
	// Customize these calls for your form
	// Start ------->
	if (!validRequired(theForm.cont_title,"اسم العقار"))
		return false;
	if (!validRequired(theForm.cont_city,"المدينة"))
		return false;
	if (!validRequired(theForm.cont_ground,"الحي"))
		return false;
	if (!validNum(theForm.cont_price,"السعر",1))
		return false;
//	if (!validNum(theForm.cont_earthnum,"رقم الارض"))
	//	return false;
	if (!validRequired(theForm.cont_wprice,"السعر كتابة"))
		return false;
	if (!validRequired(theForm.cont_client,"اسم العميل "))
		return false;
	if (!validRequired(theForm.cont_contact,"ارقام الاتصال "))
		return false;
	// <--------- End
	return true;
}

function validateSearch(theForm)
{
	// Customize these calls for your form
	// Start ------->
//	if (!validRequired(theForm.search_text,"كلمة البحث"))
//		return false;
//	if (!validRequired(theForm.cont_city,"المدينة"))
//		return false;
//	if (!validRequired(theForm.cont_ground,"الحي"))
//		return false;
	if ((theForm.price_one.value == "") && (theForm.price_two.value != "")){
		alert("لم تقم بتعبئة الحقل اكبر من");
		return false;
	}
	if ((theForm.price_two.value == "") && (theForm.price_one.value != "")){
		alert("لم تقم بتعبئة الحقل اقل من ");
		return false;
	}
	// <--------- End
	return true;
}//end function

function test(theForm)
{
	//مسح البيانات القديمة 
	//مسح الدفعة الاولى
	theForm.first_paid.value = "";
	//مسح الزيادة
	theForm.profit.value = "" ;
	//مسح اجمالى المبلغ الذي سيتم تقسيطه
	theForm.full_payment.value = "";
	//مسح القسط الشهري
	theForm.montly_pay.value = "";
	//طباعة الدفعة الاخيرة
	theForm.last_paid.value = "";
	
	if (!validInt(theForm.aqar_orginal_price,"السعر",1))
	return false;
	
	if (!validInt(theForm.pay_years,"عدد سنوات التقسيط",1))
	return false;

	if (!validInt(theForm.first_paid_input," الدفعة الاولى ",1))
	return false;

	if (theForm.last_paid_input==null){
		theForm.last_paid_input.value = 0;
	}
	

	if (!validRequired(theForm.profit_percent," النسبة المئوية "))
	return false;

	//سعر العقار الاصلي
	calc_orginal_price = Math.floor(theForm.aqar_orginal_price.value) ;
	//عدد سنوات التقسيط
	var calc_pay_years = theForm.pay_years.value ;
	//النسبة المئوية للزيادة
	var calc_profit_percent = theForm.profit_percent.value ;
	//الدفعة الاولي
	var calc_first_paid_input = theForm.first_paid_input.value ;
	var calc_last_paid_input = theForm.last_paid_input.value ;

	//عدد اشهر التقسيط
	var calc_pay_months = calc_pay_years * 12 ; 
	//الدفعة الاولى 
	var calc_first_paid = Math.floor(calc_first_paid_input) ;
	var calc_last_paid = Math.floor(calc_last_paid_input) ;
	//المتبقي
	var calc_motabaqy = calc_orginal_price - calc_first_paid - calc_last_paid;
	//صافي الزيادة
	var calc_the_profit = calc_motabaqy * calc_pay_years * (calc_profit_percent/100);
	//اجمالى القيمة تقسيط
	var calc_full_taqseet =(calc_motabaqy + calc_the_profit);
	//var calc_full_taqseet2 =(calc_motabaqy + calc_the_profit );
	var calc_full_taqseet2 =(calc_orginal_price + calc_the_profit );
	//القسط الشهري
	var calc_monthly_paid = calc_full_taqseet / calc_pay_months ;
	//اظهار البيانات 
	//طباعة الدفعة الاولى
	theForm.first_paid.value = Math.floor(calc_first_paid);
	//طباعة الدفعة الاخيرة
	theForm.last_paid.value = Math.floor(calc_last_paid);

	//طباعة الزيادة
	theForm.profit.value = Math.floor(calc_the_profit) ;
	//طباعة اجمالي المبلغ الذي سيتم تقسيطه
	theForm.full_payment.value = Math.floor(calc_full_taqseet2);
	//طباعة القسط الشهري
	theForm.montly_pay.value = Math.floor(calc_monthly_paid);
	//alert("تمت عملية حساب النتائج بنجاح");
	
	return false;
}//end 

function validateMsg(theForm)
{
	// Customize these calls for your form
	// Start ------->
	if (!validRequired(theForm.msg_title,"عنوان الرسالة"))
		return false;
	// <--------- End
	return true;
}

function Validate(val)
{
    var regEx = /^[0-9]$/;
    if(regEx.test(val))
    {
    alert('Valid Zip Code');
    }
    else
    {
    alert('Invalid Zip Code');
    }
}

function validateInt(iString) {
    // no leading 0s allowed
    return (("" + parseInt(iString)) == iString);
}

