function convertGtoHDate(D,M,Y) 
{
	d=parseInt(D)
	m=parseInt(M)
	y=parseInt(Y)	
	
	if((y>1582)||((y==1582)&&(m>10))||((y==1582)&&(m==10)&&(d>14))) 
	{
		jd=GetIntPart((1461*(y+4800+GetIntPart((m-14)/12)))/4)+
			GetIntPart((367*(m-2-12*(GetIntPart((m-14)/12))))/12)-
			GetIntPart((3*(GetIntPart((y+4900+GetIntPart((m-14)/12))/100)))/4)+d-32075
	}
	else
	{
		jd =367*y-GetIntPart((7*(y+5001+GetIntPart((m-9)/7)))/4)+GetIntPart((275*m)/9)+d+1729777
	}
		l=jd-1948440+10632
		n=GetIntPart((l-1)/10631)
		l=l-10631*n+354
		
	j=(GetIntPart((10985-l)/5316))*(GetIntPart((50*l)/17719))+(GetIntPart(l/5670))*(GetIntPart((43*l)/15238))
	l=l-(GetIntPart((30-j)/15))*(GetIntPart((17719*j)/50))-(GetIntPart(j/16))*(GetIntPart((15238*j)/43))+29

	m=GetIntPart((24*l)/709)
	d=l-GetIntPart((709*m)/24)
	y=30*n+j-30
	
	/* ------------------------Conversion  --------------------------*/
	
	form1.HigryD.value=d
	form1.HigryM.value=m
	form1.HigryY.value=y
	
	
}

function convertHtoGDate(D,M,Y)
{
	/*  ------------- Coversion  ----------------*/
	dh=parseInt(D)
	mh=parseInt(M)
	yh=parseInt(Y)
	
	jd=GetIntPart((11*yh+3)/30)+354*yh+30*mh-GetIntPart((mh-1)/2)+dh+1948440-385
	
	if (jd> 2299160 )
	{
		l=jd+68569
		n=GetIntPart((4*l)/146097)
		l=l-GetIntPart((146097*n+3)/4)
		i=GetIntPart((4000*(l+1))/1461001)
		l=l-GetIntPart((1461*i)/4)+31
		j=GetIntPart((80*l)/2447)
		dh=l-GetIntPart((2447*j)/80)
		l=GetIntPart(j/11)
		mh=j+2-12*l
		yh=100*(n-49)+i+l
	}	
	else	
	{
		j=jd+1402
		k=GetIntPart((j-1)/1461)
		l=j-1461*k
		n=GetIntPart((l-1)/365)-GetIntPart(l/1461)
		i=l-365*n+30
		j=GetIntPart((80*i)/2447)
		dh=i-GetIntPart((2447*j)/80)
		i=GetIntPart(j/11)
		mh=j+2-12*i
		yh=4*k+n+i-4716
	}
	
	/* ------------------------Conversion --------------------------*/

	form1.MeladyD.value=dh
	form1.MeldayM.value=mh
	form1.MeldayY.value=yh
}

// to get the Int Part of any Number
function GetIntPart(Fnum)
{
	if (Fnum< -0.0000001)
		{
		 return Math.ceil(Fnum-0.0000001)
		}
		
return Math.floor(Fnum+0.0000001)	
}
// chech the date for leap year

function checkDate(DE,MO,YE)
{
	mnth = MO;	
	dy=DE;
	yr1=YE;
			
if (mnth != 2)
{

	if (mnth == 4 || mnth == 6|| mnth ==9 || mnth == 11)
	{
	
		if (dy > 30)
		{  
		alert("اليوم لا يمكن أن يكون أكبر من30")		
		return false;
		}
	}				
}
else
	{
		
		
		leapYrTest = yr1 % 4
		if (leapYrTest == 0)
		{
			
			maxdy = 29
		}
		else
		{
			maxdy = 28
		}
		
		if (dy > maxdy)
		{
			


							alert("اليوم لا يمكن أن يكون أكبر من   "+maxdy+"  يوم");					
							return false;
		}
	}
	return true;
}

// =========================================
/*check for heigry Month*/
function ishijrileap(yearvalue)
{
	switch (yearvalue%30)
	{
		case 2  :
		case 5  : 
		case 7  :
		case 10 :
		case 13 :
		case 15 :
		case 18 :
		case 21 :
		case 24 :
		case 26 :
		case 29 :
			return true;
		default :
			return false;
	}
}


function hijri_days_month(M,Y)  
{
	yearval=Y;
	monthval=M;
	if ( (monthval&1) || (ishijrileap(yearval) && monthval==12) )	
		return(30);
	else
		return(29);
}

function hijri_days_monthalerts(D,M,Y)
{
	if(D==30)
	{
		var nodays=hijri_days_month(M,Y);
		if (nodays==29)
		{  
			alert( "    اليوم لا يمكن أن يكون أكبر من 29 ")		
			
		 return false;	
		}
	return true;	
	}
return true;
 }
function Convert()
{
MDay=form1.MeladyD.value;
MMonth=form1.MeldayM.value;
MYear=form1.MeldayY.value;
HDay=form1.HigryD.value;
HMonth=form1.HigryM.value;
HYear=form1.HigryY.value;
// check if convert from Hig to Melady or not
if (form1.Hig.checked==true)
{
	if (HYear==""||HYear==null ||HYear=='null'||HYear==0 ||isNaN(HYear))
	{
	alert("من فضلك ادخل السنة الهجرية بطريقة صحيحة")
	form1.HigryY.focus()
	return false
	
	}
	if(hijri_days_monthalerts(HDay,HMonth,HYear)==true)
	convertHtoGDate(HDay,HMonth,HYear)
	else
	return false;
	nmdt=form1.month.value+"/"+form1.MeladyD.value+"/"+form1.MeldayY.value
nd=new Date(nmdt);
fccc=nd.getDay();
if (fccc=="0")
{
form1.tdnmm.value="الأحد"
}
else if (fccc=="1")
{
form1.tdnmm.value="الإثنين"
}
else if (fccc=="2")
{
form1.tdnmm.value="الثلاثاء"
}
else if (fccc=="3")
{
form1.tdnmm.value="الأربعاء"
}
else if (fccc=="4")
{
form1.tdnmm.value="الخميس"
}
else if (fccc=="5")
{
form1.tdnmm.value="الجمعة"
}
else if (fccc=="6")
{
form1.tdnmm.value="السبت"
}

}
// check if convert from Melady to Higry or not
if(form1.Mel.checked==true)
{
	if (MYear==""||MYear==null ||MYear=='null'||MYear==0 ||isNaN(MYear))
	{
	alert("من فضلك ادخل العام الميلادى بطريقة صحيحة")
	form1.MeldayY.focus()
	return false	
	}
	if (MYear<622)
		{
		alert("يجب ان يبدأ العام الميلادى من 622");
			return false
		}
		else			
	if(MYear==622)
	 {
	if((MMonth<7)|(MMonth<17 & MMonth==7))
				{               
	       alert("لكى تحصل على تاريخ هجرى يجب ان  يكون التاريخ الميلادى بعد 622/7/16 ميلادية");		       
		       return false ;	       		       		       		       
		       }
		}       
	if(checkDate(MDay,MMonth,MYear)==true)
		convertGtoHDate(MDay,MMonth,MYear);
		else
		return false;	       
nmdt=form1.month.value+"/"+form1.MeladyD.value+"/"+form1.MeldayY.value
nd=new Date(nmdt);
fccc=nd.getDay();
if (fccc=="0")
{
form1.tdnmm.value="الأحد"
}
else if (fccc=="1")
{
form1.tdnmm.value="الإثنين"
}
else if (fccc=="2")
{
form1.tdnmm.value="الثلاثاء"
}
else if (fccc=="3")
{
form1.tdnmm.value="الأربعاء"
}
else if (fccc=="4")
{
form1.tdnmm.value="الخميس"
}
else if (fccc=="5")
{
form1.tdnmm.value="الجمعة"
}
else if (fccc=="6")
{
form1.tdnmm.value="السبت"
}

}









}
