////////////////////////////////////////////////////// publich functions ///////////////////////////
var serr;
//date validation
var dtCh= "/";
var d = new Date();
var minYear= d.getFullYear() -90;
var maxYear= d.getFullYear() -10;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function daysInFebruary (year){
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay =dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
	return true
}
// number or not
function is_number(num){
  	var i;
    for (i = 0; i < num.length; i++)
    { 
		var c = num.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}
// email or not
function is_email(email){
	var reemail = new RegExp(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
	if(email.match(reemail)) return true;
	return false;
}
// empty or not
function validate_empty(element){
	if(jQuery.trim($(element).val()) == ""){
		serr = serr + 1;// error
		$(element).next().text('فارغ');
		showerror($(element));
		return;
	}
	hidehint($(element));
}
// show hint
function showhint(element, hinttext){
	element.parent().removeClass();
	element.next().removeClass();
	element.next().addClass('hint');
	element.next().text(hinttext);
}
// show error
function showerror(element){
	element.next().removeClass();
	element.next().html('<img src="darory/templates/images/x.gif" height="15">');
	serr=serr+1;
}
// hide error and hint
function hidehint(element){
	element.next().removeClass();
	element.next().html('<img src="darory/templates/images/check.gif" height="15">');
}
// replace form
function rform(element, msgtext){
	element.removeClass();
	element.html(msgtext);
}
////////////////////////////////////////////////////// SPESIAL FUNCTIONS ///////////////////////////
// validate full name for any full name
function validate_full_name(element){
	if(jQuery.trim($(element).val()).indexOf(" ") == "-1"){
		showerror($(element));
		return;
	}
	$(element).next().html('<img src="darory/templates/images/check.gif">');
}
// validate gender m or f
function validate_gender(element){
	if(!( $(element).val() == "m" || $(element).val() == "f")){
		showerror($(element));
		return;
	}
	hidehint($(element));
}
// validate phone
function validate_phone(element){
	if((!is_number($(element).val()))||($(element).val()=="")){
		showerror($(element));	
		return;
	}
	hidehint($(element));
}
// validate speed
function validate_select(element){
	if((!is_number($(element).val()))||($(element).val()<1)){
		showerror($(element));	
		return;
	}
	hidehint($(element));
}
// validate email
function validate_email(element){
	if((!is_email($(element).val()))){
		showerror($(element));	
		return;
	}
	hidehint($(element));
}
// validate date
function validate_date(element){
	if(!isDate($(element).val())){
		showerror($(element));	
		return;
	}
	hidehint($(element));
}

//////////////////////////////////////////////////////////// FINAL CHECK ///////////////////////////
// Validate CONTACTUS
function validate_contactus_form(){
	serr = 0;// reset error
	validate_empty($('#contactus_name'));
	validate_phone($('#contactus_mobile'));
	validate_email($('#contactus_email'));
	validate_empty($('#contactus_to'));
	validate_full_name($('#contactus_text'));
	
	if (serr==0)
	{
		$.post("validate/contactus/save_contactus_form.php",
		{
			contactus_name:$("#contactus_name").val(),
			contactus_mobile:$("#contactus_mobile").val(),
			contactus_email:$("#contactus_email").val(),
			contactus_to:$("#contactus_to").val(),
			contactus_text:$("#contactus_text").val()
		} ,function(data)
		{
			if(data=='yes')
			{
				rform($('#contactus_form'), '<br>شكراً علي إتصالك بنا وسوف يتم الرد عليك في اقرب وقت<br><span class=n_link><a href="index.php?page=contactus">إريد إرسال رسالة أخري</a></span>');
			}else{
				rform($('#contactus_form'), 'لا');
			}
		});
	}else{
	}
}
// Validate UDATA
function validate_udata_form(){
	serr = 0;// reset error
	validate_phone($('#udata_mob'));
	validate_email($('#udata_email'));
	
	if (serr==0)
	{
		$.post("validate/save_udata_form.php",
		{
			udata_mob:$("#udata_mob").val(),
			udata_email:$("#udata_email").val()
		} ,function(data)
		{
			alert("تم حفظ البيانات");
		});
	}else{
			alert("الرجاء كتابة البيانات بشكل صحيح");
	}
}
// Validate REG
function validate_reg_form(){
	var serr=0;// reset error
	validate_empty($('#reg_un'));
	validate_email($('#reg_email'));
	validate_phone($('#reg_mob'));
	
$.post("validate/reg/v_reg_un.php",{reg_un:$("#reg_un").val()},
function(regun)
{
	if(regun=='no')
	{
		alert('الرجاء اخيار اسم مستخدم آخر');
		$("#reg_un").focus();
	}else{
		if (serr==0)
		{
			$.post("validate/reg/save_reg_form.php",
			{
				reg_un:$("#reg_un").val(),
				reg_email:$("#reg_email").val(),
				reg_mob:$("#reg_mob").val(),
				reg_cn:$("#reg_cn").val()
			} ,function(data)
			{
				if(data == 'no')
				{
					rform($('#reg_form'), "لقد تم ارسال كلمة السر الي بريدك الإلكتورني<br> الرجاء فحص بريدك الإلكتروني");
				}else{
					rform($('#reg_form'), "لقد تم ارسال كلمة السر الي بريدك الإلكتورني<br> الرجاء فحص بريدك الإلكتروني");
				}
			});
		}
	}
});
}
// Validate LOGIN
function validate_login_form(){
$.post("validate/login.php",{ login_un:$('#login_un').val(),login_passwd:$('#login_passwd').val() } ,function(data)
{
	if(data=='no'){
		alert("لقد قمت بإدخال كلمة السر او اسم المستخدم خطاً, من فضلك اعد المحاولة");
	}else if (data=='yes'){
		window.location = "index.php";
	}else{
		alert('الحساب معلق نرجو مراسلة الإدارة');
	}
});
}
// Validate FPASS
function validate_fpass_form(){
serr=0;
validate_empty($('#fpass_un'));
validate_email($('#fpass_email'));
	if (serr==0){
		$.post("validate/fpass.php",{ fpass_un:$('#fpass_un').val(),fpass_email:$('#fpass_email').val() } ,function(data)
		{
			if(data=='no'){
				alert("الرجاء ادخال البيانات صحيحة");
			}else{
				alert("تم إرسال كلمة السر الي بريدك الإلكتروني");
				window.location = "index.php";
			}
		});
	}else{
		alert("الرجاء ادخال البيانات صحيحة");
	}
}
// Validate CHPASSWD
function validate_passwd_form(){
serr=0;
$.post("validate/passwd_user/passwd_old_user.php",{ passwd_old:$('#passwd_old').val() } ,function(data)
{
	if(data=='no'){
		showerror($('#passwd_old'));
	}else{
		hidehint($('#passwd_old'));
	}
});

validate_empty($('#passwd_old'));
validate_empty($('#passwd_new'));
validate_empty($('#passwd_newt'));

	if (($('#passwd_newt').val())==($('#passwd_new').val())){
		hidehint($('#passwd_newt'));
		//alert("equal");
	}else{
		showerror($('#passwd_newt'));
		//alert("not equal");
	}
	
	if (serr==0){
		alert("تم تغيير كلمة السر بنجاح");
		$.post("validate/passwd_user/passwd_change_user.php",{ passwd_old:$('#passwd_old').val(),passwd_new:$('#passwd_new').val(),passwd_newt:$('#passwd_newt').val() } ,function(passdata){if(passdata=='no'){alert("cant");}else{rform($('#passwd_form'), 'تم تغيير كلمة السر بنجاح');}
});	
	}
}

// Validate CHNEWS
function validate_news_form(){
serr=0;
$.post("validate/news_data.php",{ news_text:$('#news_text').val() } ,function(data)
{
	alert("تم نشر الإعلان");
	window.location = "index.php?do=chnews";
});
}

// Validate BILL FORM
function validate_bill_form(){
serr=0;
$.post("validate/save_bill.php",{ ubill_name_id:$('#ubill_name_id').val(),ubill_name:$('#ubill_name').val(),ubill_no:$('#ubill_no').val(),ubill_kind:$('#ubill_kind').val() } ,function(data)
{
	alert("تم حفظ البوليصه");
	window.location = "index.php?do=user&ustatus=active";
});
}

// Validate event FORM
function validate_event_form(){
serr=0;
$.post("validate/save_event.php",{ bevent_date:$('#bevent_date').val(),bevent_to:$('#bevent_to').val(),bevent_event:$('#bevent_event').val(),bill_id:$('#bill_id').val() } ,function(data)
{
	alert("تم حفظ الحدث");
	window.location = "index.php?do=user&ustatus=active";
});
}
// Validate sbill FORM
function validate_sbill_form(){
serr=0;
$.post("validate/sbill.php",{ sbill:$('#sbill').val() } ,function(data)
{
	
	if (data=='no'){
		$("#result_all").html("<span align='center'>الرجاء إدخال رقم البوليصة بشكل صحيح</span>");
		//alert("الرجاء إدخال رقم البوليصة بشكل صحيح");
	}else{
		$("#result_all").load("validate/sbill_result.php");
	}
});
}
// full check
$(document).ready(function(){
////////////////////////////////////////////////////// account data form ///////////////////////////
//CONTACTUS Form
	$('#contactus_name').blur(function(){ validate_empty($('#contactus_name')); });
	$('#contactus_mobile').blur(function(){ validate_phone($('#contactus_mobile')); });
	$('#contactus_email').blur(function(){ validate_email($('#contactus_email')); });
	$('#contactus_to').blur(function(){ validate_empty($('#contactus_to')); });
	$('#contactus_text').blur(function(){ validate_full_name($('#contactus_text')); });
	$('#contactus_save').click(function(){ validate_contactus_form(); });
//REG Form
	$('#reg_un').blur(function(){ validate_empty($('#reg_un')); });
	$('#reg_email').blur(function(){ validate_email($('#reg_email')); });
	$('#reg_mob').blur(function(){ validate_phone($('#reg_mob')); });
	$('#reg_save').click(function(){ validate_reg_form(); });
//LOGIN Form
	$('#login_un').blur(function(){ validate_empty($('#login_un')); });
	$('#login_passwd').blur(function(){ validate_empty($('#login_passwd')); });
	$('#login_save').click(function(){ validate_login_form(); });
//CHPASS Form
	$('#passwd_old').blur(function(){ validate_empty($('#passwd_old')); });
	$('#passwd_new').blur(function(){ validate_empty($('#passwd_new')); });
	$('#passwd_newt').blur(function(){ validate_empty($('#passwd_newt')); });
	$('#passwd_save').click(function(){ validate_passwd_form(); });
//NEWS Form
	$('#news_save').click(function(){ validate_news_form(); });
//CHBILL USER
	$('#ubill_no').blur(function(){ validate_empty($('#ubill_no')); });
	$('#bill_save').click(function(){ validate_bill_form(); });
//CHBILL EVENT
	$('#bevent_date').blur(function(){ validate_empty($('#bevent_date')); });
	$('#bevent_event').blur(function(){ validate_empty($('#bevent_event')); });
	$('#event_save').click(function(){ validate_event_form(); });
//SEARCH BILL EVENT
	$('#sbill').blur(function(){ validate_empty($('#sbill')); });
	$('#save_sbill').click(function(){ validate_sbill_form(); });
//CHUSER DATA
	$('#udata_mob').blur(function(){ validate_phone($('#udata_mob')); });
	$('#udata_email').blur(function(){ validate_email($('#udata_email')); });
	$('#udata_save').click(function(){ validate_udata_form(); });
//CHFPASS
	$('#fpass_un').blur(function(){ validate_empty($('#fpass_un')); });
	$('#fpass_email').blur(function(){ validate_email($('#fpass_email')); });
	$('#fpass_save').click(function(){ validate_fpass_form(); });
});
