var dtCh= "/";
var minYear=1900;
var maxYear=2009;
var errorMsg = "";

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
///////////////////////////////////////////////////////////////////////////////
function isYear(s)
{
  var today=new Date();
  var this_year=today.getFullYear();

  if (s.length!=4)
     return false;

  if (s<1950)
     return false;

  if (s>this_year)
    return false;

   else
    return true;

}
///////////////////////////////////////////////////////////////////////////////
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    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
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////
function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
function modify_validation()
{
  var status=true;
  var objid=document.getElementById('pgid');
  document.modify_news.title.style.backgroundColor="#FFFFFF";
  document.modify_news.begin.style.backgroundColor="#FFFFFF";
  objid.style.backgroundColor="#FFFFFF";




    if (document.modify_news.title.value==''){
            document.modify_news.title.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.modify_news.begin.value==''){
            document.modify_news.begin.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (isImage(objid.value)==false && document.modify_news.modify_photo.value==1){
           objid.style.backgroundColor="#ffb9b9";
           status=false;

    }
    return status;

}




function app_validation()
{

  var status;

    var objid=document.getElementById('pgid');
    var objcv=document.getElementById('cv');
    var objdiploma=document.getElementById('diploma');
    var objbachelor=document.getElementById('bachelor');
    var objmaster=document.getElementById('master');
    

    
  document.app.name_ar.style.backgroundColor="#e6e6e6";
  document.app.name_en.style.backgroundColor="#e6e6e6";
  
  document.app.sex[0].style.backgroundColor="#e6e6e6";
  document.app.sex[1].style.backgroundColor="#e6e6e6";
  
  document.app.birth_date.style.backgroundColor="#e6e6e6";
  document.app.nationality.style.backgroundColor="#e6e6e6";
  document.app.id_card.style.backgroundColor="#e6e6e6";
  document.app.address.style.backgroundColor="#e6e6e6";
  document.app.phone_number.style.backgroundColor="#e6e6e6";
  document.app.fax_number.style.backgroundColor="#e6e6e6";
  document.app.email.style.backgroundColor="#e6e6e6";
  
  document.app.degree[0].style.backgroundColor="#e6e6e6";
  document.app.degree[1].style.backgroundColor="#e6e6e6";
  document.app.degree[2].style.backgroundColor="#e6e6e6";
  document.app.degree[3].style.backgroundColor="#e6e6e6";
  document.app.degree[4].style.backgroundColor="#e6e6e6";
  
  document.app.image_path.style.backgroundColor="#e6e6e6";
  document.app.spec1.style.backgroundColor="#e6e6e6";
  document.app.spec2.style.backgroundColor="#e6e6e6";
  document.app.cv_file.style.backgroundColor="#e6e6e6";
  
  document.app.uni_bachelor.style.backgroundColor="#e6e6e6";
  document.app.degree_bachelor.style.backgroundColor="#e6e6e6";
  document.app.date_bachelor.style.backgroundColor="#e6e6e6";
  document.app.date_diploma.style.backgroundColor="#e6e6e6";
  document.app.date_master.style.backgroundColor="#e6e6e6";
  document.app.image_master.style.backgroundColor="#e6e6e6";
  document.app.image_diploma.style.backgroundColor="#e6e6e6";
  document.app.image_bachelor.style.backgroundColor="#e6e6e6";
  
  document.app.job.style.backgroundColor="#e6e6e6";
  document.app.experience.style.backgroundColor="#e6e6e6";
  document.app.employer_address.style.backgroundColor="#e6e6e6";
  
  document.app.time[0].style.backgroundColor="#e6e6e6";
  document.app.time[1].style.backgroundColor="#e6e6e6";


    if (document.app.name_ar.value==''){
            document.app.name_ar.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.name_en.value==''){
            document.app.name_en.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.sex[0].checked==false && document.app.sex[1].checked==false){
            document.app.sex[0].style.backgroundColor="#ffb9b9";
            document.app.sex[1].style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (!isDate(document.app.birth_date.value)){
            document.app.birth_date.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.nationality.value==''){
            document.app.nationality.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.id_card.value==''){
            document.app.id_card.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.address.value==''){
            document.app.address.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.mobile_number.value=='' || !isInteger(document.app.mobile_number.value)){
            document.app.mobile_number.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.phone_number.value!='' && !isInteger(document.app.phone_number.value)){
            document.app.phone_number.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.fax_number.value!='' && !isInteger(document.app.fax_number.value)){
            document.app.fax_number.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (!isValidEmail(document.app.email.value)){
            document.app.email.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.degree[0].checked==false && document.app.degree[1].checked==false && document.app.degree[2].checked==false && document.app.degree[3].checked==false && document.app.degree[4].checked==false){
            document.app.degree[0].style.backgroundColor="#ffb9b9";
            document.app.degree[1].style.backgroundColor="#ffb9b9";
            document.app.degree[2].style.backgroundColor="#ffb9b9";
            document.app.degree[3].style.backgroundColor="#ffb9b9";
            document.app.degree[4].style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (isImage(objid.value)==false or isFile(objcv.value)==false){
            document.app.image_path.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.spec1.value==''){
            document.app.spec1.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.spec2.value==''){
            document.app.spec2.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (isFile(objcv.value)==false){
            document.app.cv_file.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.uni_bachelor.value==''){
            document.app.uni_bachelor.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.degree_bachelor.value==''){
            document.app.degree_bachelor.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (!isDate(document.app.date_bachelor.value)){
            document.app.date_bachelor.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (!isDate(document.app.date_diploma.value) && document.app.date_diploma.value!=''){
            document.app.date_diploma.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (!isDate(document.app.date_master.value)  && document.app.date_master.value!=''){
            document.app.date_master.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (isImage(objbachelor.value)==false){
            document.app.image_bachelor.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (isImage(objdiploma.value)==false && objdiploma.value != ''){
            document.app.image_diploma.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (isImage(objmaster.value)==false && objmaster.value != ''){
            document.app.image_master.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.job.value==''){
            document.app.job.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.experience.value==''){
            document.app.experience.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.employer_address.value==''){
            document.app.employer_address.style.backgroundColor="#ffb9b9";
            status=false;
    }
    if (document.app.time[0].checked==false && document.app.time[1].checked==false){
            document.app.time[0].style.backgroundColor="#ffb9b9";
            document.app.time[1].style.backgroundColor="#ffb9b9";
            status=false;
    }

    return status;

}





function delete_confirm()
{

var answer = confirm("تنبيه .. هل تريد الحذف فعلا ؟؟")
if (answer)
	{
return true;
	}
else
	{
return false;
	}
}



function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}




///////////VAlidates that a filename is an image with the extensions jpg or gif////////////////////////////////
function isImage(filname)
{
   var ex=filname.toLowerCase();
   var extension=ex.slice(-3);
   if (extension !='jpg' && extension!='gif')
     return false;
   else
     return true;

}

function isFile(filname)
{
   var ex=filname.toLowerCase();
   var extension=ex.slice(-3);
   if (extension !='doc' && extension!='txt' && extension!='pdf')
     return false;
   else
     return true;

}


