//var education_count=1;
//var language_count=1;
//var membership_count=1;
//var experience_count=1;

//var education_count=document.getElementById('education_count').value;
//var language_count=document.getElementById('language_count').value;
//var membership_count=document.getElementById('membership_count').value;
//var experience_count=document.getElementById('experience_count').value;


function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function addBlock(type)
{
var education_count=document.getElementById('education_count').value;
var language_count=document.getElementById('language_count').value;
var membership_count=document.getElementById('membership_count').value;
var experience_count=document.getElementById('experience_count').value;


var xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	
	var block_count;
	
	switch (type)
	{
		case 'education':
			block_count=education_count;
			break;
			
		case 'language':
			block_count=language_count;
			break;
			
		case 'membership':
			block_count=membership_count;
			break;
			
		case 'experience':
			block_count=experience_count;
			break;
	}	
	
	
	var url="../ajax/add_block.php";
	url=url+"?type="+type;
	url=url+"&block_count="+block_count;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=
	function StateChanged()
	{
		if (xmlHttp.readyState==4)
		{
			var response=xmlHttp.responseText;			
			var num=block_count+1;		
			var my=document.getElementById(type);     
		    var newdiv=document.createElement('div');
		    var divid=type+num;
		    newdiv.setAttribute('id',divid)
			newdiv.innerHTML=response;
			my.appendChild(newdiv);
						
			switch (type)
			{
				case 'education':
					document.getElementById('education_count').value++;
					break;
					
				case 'language':
					document.getElementById('language_count').value++;
					break;
					
				case 'membership':
					document.getElementById('membership_count').value++;
					break;
					
				case 'experience':
					document.getElementById('experience_count').value++;
					break;
			}		
		}
	};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}


function removeBlock(divid,type)
{
	document.getElementById(divid).innerHTML="";
	switch (type)
	{
		case 'education':
			document.getElementById('education_count').value--;
			break;
			
		case 'language':
			document.getElementById('language_count').value--;
			break;
			
		case 'membership':
			document.getElementById('membership_count').value--;
			break;
			
		case 'experience':
			document.getElementById('experience_count').value--;
			break;
	}
	
}

////////////////////////////////////////////////////Validation///////////////////////////////////////////////

function validateAddExpert()
{
	var message='';
	
	var flag=0;
	
	var user_name=document.getElementById('user_name').value;
	var family_name=document.getElementById('family_name');
	var first_name=document.getElementById('first_name');
	var birth_year=document.getElementById('birth_year');
	var birth_month=document.getElementById('birth_month');
	var birth_day=document.getElementById('birth_day');
	var nationality=document.getElementById('nationality');
	var civil_status=document.getElementById('civil_status');
	var email=document.getElementById('email');
	var mobile=document.getElementById('mobile');
	var cv_file=document.getElementById('cv_file').value;
	var kind=document.getElementById('kind').value;
	
	if(user_name=='')
	{
	message+='Please Enter User Name\n';
	flag++;	
	}
	
	if(document.getElementById('password'))
	{
	var password=document.getElementById('password').value;
	var password_confirmation=document.getElementById('pass_confirmation').value;
	
	
	if(password=='')
	{
	message+='Please Enter Password\n';
	flag++;	
	}
	
	if(password_confirmation=='')
	{
	message+='Please Enter Password Confirmation\n';
	flag++;	
	}
	
	if(password!=password_confirmation)
	{
	message+='The Password and its confirmation are not the same\n';
	flag++;	
	}
	}
	if(family_name.value=='')
	{
	message+='Please Enter Family Name\n';
	flag++;	
	}
	if(first_name.value=='')
	{
	message+='Please Enter First Name\n';
	flag++;	
	}
	
	if(email.value=='')
	{
	message+='Please Enter Email\n';
	flag++;	
	}
	
	if(!email.value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/)) 
	{
       alert('Please Enter a valid Email Address.');
       flag++;
    }
	
	if(birth_year.selectedIndex==0)
	{
	message+='Please Select Birth Year\n';
	flag++;	
	}
	if(birth_month.selectedIndex==0)
	{
	message+='Please Select Birth Month\n';
	flag++;	
	}
	if(birth_day.selectedIndex==0)
	{
	message+='Please Select Birth Day\n';
	flag++;	
	}
	if(nationality.selectedIndex==0)
	{
	message+='Please Select Nationality\n';
	flag++;	
	}
	if(civil_status.selectedIndex==0)
	{
	message+='Please Select Civil Status\n';
	flag++;	
	}
	
	var degree,degree_from,degree_to,lang,reading,speaking,writing,body,memfrom_year,memfrom_month,memto_year,memto_month;
	var position,country,company,description,industry,expfrom_month,expfrom_year,expto_month,expto_year;
	
	for(var i=1;i<=10;i++)
	{
		degree='degree'+i;
		degree_from='degree_from'+i;
		degree_to='degree_to'+i;
		
		if(document.getElementById(degree))
		{
			if(document.getElementById(degree).selectedIndex==0)
			{
				message+='Please Select Degree '+i+'\n';
				flag++;
			}
			if(document.getElementById(degree_from).selectedIndex==0)
			{
				message+='Please Select Degree From '+i+'\n';
				flag++;
			}
			if(document.getElementById(degree_to).selectedIndex==0)
			{
				message+='Please Select Degree To '+i+'\n';
				flag++;
			}
		}
		
		lang='lang'+i;
//		reading='reading'+i;
//		speaking='speaking'+i;
//		writing='writing'+i;
		
		if(document.getElementById(lang))
		{
			if(document.getElementById(lang).selectedIndex==0)
			{
				message+='Please Select Language '+i+'\n';
				flag++;
			}
//			if(document.getElementById(reading).selectedIndex==0)
//			{
//				message+='Please Select Reading Scale '+i+'\n';
//				flag++;
//			}
//			if(document.getElementById(speaking).selectedIndex==0)
//			{
//				message+='Please Select Speaking Scale '+i+'\n';
//				flag++;
//			}
//			if(document.getElementById(writing).selectedIndex==0)
//			{
//				message+='Please Select Writing Scale '+i+'\n';
//				flag++;
//			}
		}
		
		body='body'+i;
		memfrom_year='memfrom_year'+i;
		memfrom_month='memfrom_month'+i;
		memto_year='memto_year'+i;
		memto_month='memto_month'+i;
		
		
		if(document.getElementById(body))
		{
			if(document.getElementById(body).value==0)
			{
				message+='Please Enter Membership Body '+i+'\n';
				flag++;
			}
			if(document.getElementById(memfrom_year).selectedIndex==0)
			{
				message+='Please Select membership from Year '+i+'\n';
				flag++;
			}
			if(document.getElementById(memfrom_month).selectedIndex==0)
			{
				message+='Please Select membership from month '+i+'\n';
				flag++;
			}
			
				if(document.getElementById(memto_year).selectedIndex==0)
			{
				message+='Please Select membership to Year '+i+'\n';
				flag++;
			}
			if(document.getElementById(memto_month).selectedIndex==0)
			{
				message+='Please Select membership to month '+i+'\n';
				flag++;
			}
		}
		
		position='position'+i;
		country='country'+i;
		company='company'+i;
		efunction='function'+i;
		description='job_description'+i;
		industry='industry'+i;
		expfrom_month='expfrom_month'+i;
		expfrom_year='expfrom_year'+i;
		expto_month='expto_month'+i;
		expto_year='expto_year'+i;
		
		
		if(document.getElementById(position))
		{
			if(document.getElementById(position).selectedIndex==0)
			{
				message+='Please Select Position '+i+'\n';
				flag++;
			}
			if(document.getElementById(efunction).selectedIndex==0)
			{
				message+='Please Select Function '+i+'\n';
				flag++;
			}
			if(document.getElementById(country).selectedIndex==0)
			{
				message+='Please Select Location '+i+'\n';
				flag++;
			}
			if(document.getElementById(company).selectedIndex==0)
			{
				message+='Please Select Company '+i+'\n';
				flag++;
			}
			if(document.getElementById(industry).selectedIndex==0)
			{
				message+='Please Select Industry '+i+'\n';
				flag++;
			}
			if(document.getElementById(description).value=='')
			{
				message+='Please Enter Description '+i+'\n';
				flag++;
			}
			if(document.getElementById(expfrom_month).selectedIndex==0)
			{
				message+='Please Select From Month of Experience  '+i+'\n';
				flag++;
			}
			if(document.getElementById(expfrom_year).selectedIndex==0)
			{
				message+='Please Select From Month of Experience '+i+'\n';
				flag++;
			}
			if(document.getElementById(expto_month).selectedIndex==0)
			{
				message+='Please Select To Month of Experience  '+i+'\n';
				flag++;
			}
			if(document.getElementById(expto_year).selectedIndex==0)
			{
				message+='Please Select To Year of Experience  '+i+'\n';
				flag++;
			}		
			
		}
	}

	if(cv_file.length==0)
	{

			if(kind=='add')
			{
			message+="Please browse the cv file ";
			flag++;
			}
	}
	

	else
	{
		var str=cv_file;
        var allowed_extension="txt-doc-pdf";
		allowed_extension_array = allowed_extension.split("-");

		//	var OK = new Array ('jpg', 'gif', 'jpeg', 'png', 'bmp', 'tif');
		var ext = getExt(str);
		var fileOK = 0;

		for (i = 0; i < allowed_extension_array.length; i++) {
			if (allowed_extension_array[i] == ext)
			fileOK = 1; // one of the file extensions found
		}//for

		if (fileOK == 0)
		{
			
			message+='Please upload a valid file type with one of the following extensions ( ' + allowed_extension.replace(/-/g ," , ") + " )";
			flag++;
		}


	}
	
	
	
	if(flag > 0)
	{
		alert(message);
		return false;
	}
	else
	{
		return true;
	}
	
	
}
function getExt(filename) {
	var dot_pos = filename.lastIndexOf(".");
	if(dot_pos == -1)
	return "";
	return filename.substr(dot_pos+1).toLowerCase();
}

function validateLogin()
{
	userz=expertLogin.username.value;
	passz=expertLogin.password.value;
	
	var validRegExp = /^([a-z]|[A-Z]|[0-9]|_|.|@)+$/;
	

	if(userz.length==0)
	{
		alert("Username is empty");
		expertLogin.username.focus();
		return false;
	}
	
	
	if(!(validRegExp.test(userz)))
	{
		alert("Wrong username characters");
		expertLogin.username.focus();
		return false;
	}
	

	if(passz.length==0)
	{
		alert("Password is empty");
		expertLogin.password.focus();
		return false;
	}	
	
	
	if(!(validRegExp.test(passz)))
	{
		alert("Wrong password characters");
		expertLogin.password.focus();
		return false;
	}
	
	
	
	return true;
}

function val_changePassword()
{
	document.getElementById('error_message').innerHTML='';
	
	var old_password=document.getElementById('old_password').value;
	var new_password=document.getElementById('new_password').value;
	var new_password_confirmation=document.getElementById('new_pass_confirm').value;
	var flag=0;
	var message="";
	if(old_password=='')
	{
	message+='Please Enter the old password\n';
	flag++;	
	}
	
	if(new_password=='')
	{
	message+='Please Enter the new password\n';
	flag++;	
	}
	
	if(new_password_confirmation=='')
	{
	message+='Please Enter the password Confirmation\n';
	flag++;	
	}
	
	if(new_password!=new_password_confirmation)
	{
	message+='The New Password and its confirmation are not the same\n';
	flag++;	
	}
	
	if(flag > 0)
	{
	alert(message);
	return false;
	}
	else
	{
		return true;
	}
}



function ajaxRemoveBlock(type,count,id,expert_id)
{
	var divid=type+count;
	
	
	var url="../ajax/delete_block.php";
	url=url+"?type="+type;
	url=url+"&id="+id;
	url=url+"&expert_id="+expert_id;
	var xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	xmlHttp.onreadystatechange=
	function StateChanged()
	{
		if (xmlHttp.readyState==4)
		{
			var response=xmlHttp.responseText;			
			if(response=="1")
			{
			document.getElementById(divid).innerHTML="";
			alert("deleted");
			}
			else
			alert("error in deletion , please try agin");			
				
		}
	};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

	
	
}






