var msg;
var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i 

function showHide (div_from,div_to) {
	msg='';
	if (document.getElementById(div_to)) {
		validate(div_from); 
		if (msg=='') {
			document.getElementById('personal').style.display='none';
			document.getElementById('experience').style.display='none';
			document.getElementById('education').style.display='none';
			document.getElementById('language').style.display='none';
			document.getElementById(div_to).style.display='block';
			document.location='#'+div_to;
			document.getElementById('msg_'+div_from)? document.getElementById('msg_'+div_from).innerHTML=msg: true ;
		} else {
			document.getElementById('msg_'+div_from).innerHTML='PLease Fill In Missing Fields:<br />'+msg+'<br />';
			document.location='#'+div_from;
		}
	}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function addExp() {
	var exp_num=document.getElementById('exp_count').value;
	var next_num= parseInt(exp_num)+1;
	if (next_num > 4) {
		document.getElementById('add_exp').style.display='none';
	}
	var html_div ='<p><b>Experience '+next_num+'</b> <a class="anchor" href="javascript:deleteDiv(\'exp\',\''+exp_num+'\')"> [delete]</a></p>';
			html_div +='<table cellpadding="3" cellspacing="0" border="0" width="450" class="index">';
			html_div +='		<tr>';
			html_div +='			<td>Company Name <span class="star">*</span><br />';
			html_div +='				<input type="text" name="exp_company[]" class="textfield"  id="exp_company'+next_num+'" /></td>';
			html_div +='			<td>Job Role/Department<br />';
			html_div +='				<input type="text" name="exp_role[]" class="textfield" /></td>';
			html_div +='		</tr>';
			html_div +='		<tr>';
			html_div +='			<td>Starting Date<span class="star"><small> yyyy-mm-dd</small></span><br />';
			html_div +='				<input type="text" name="exp_start_date[]" class="textfield" /></td>';
			html_div +='			<td>Position in Company<br />';
			html_div +='				<input type="text" name="exp_position[]" class="textfield" /></td>';
			html_div +='		</tr>';
			html_div +='		<tr>';
			html_div +='			<td>Company Address<br />';
			html_div +='				<textarea cols="24" rows="2" class="textfield" name="exp_address[]"></textarea></td>';
			html_div +='			<td>Work Description<br />';
			html_div +='				<textarea cols="24" rows="2" name="exp_desc[]" class="textfield"></textarea></td>';
			html_div +='		</tr>';
			html_div +='		<tr>';
			html_div +='			<td>Phone Number<br />';
			html_div +='				<input type="text" name="exp_phone[]" class="textfield" /></td>';
			html_div +='			<td>Fax Number<br />';
			html_div +='				<input type="text" name="exp_fax[]" class="textfield" /></td>';
			html_div +='		</tr>';
			html_div +='		<tr>';
			html_div +='			<td>Email Address<br />';
			html_div +='				<input type="text" name="exp_email[]" class="textfield" /></td>';
			html_div +='			<td></td>';
			html_div +='		</tr>';
			html_div +='	</table>';
			html_div +='	<span id="exp'+next_num+'"></span>';

	document.getElementById('exp'+exp_num).innerHTML=html_div
	document.getElementById('exp_count').value=next_num
}
function deleteDiv(div,val) {
	document.getElementById(div+val).innerHTML='';
	document.getElementById(div+'_count').value=val;
}

function addEdu() {
	var edu_num=document.getElementById('edu_count').value;
	var next_num= parseInt(edu_num)+1;
	if (next_num > 4) {
		document.getElementById('add_edu').style.display='none';
	}
	var html_div ='<p><b>Education '+next_num+'</b> <a class="anchor" href="javascript:deleteDiv(\'edu\',\''+edu_num+'\')"> [delete]</a></p>';
			html_div +='<table cellpadding="3" cellspacing="0" border="0" width="450">';
			html_div +='		<tr>';
			html_div +='        <td>Institution<br />';
			html_div +='          <input type="text" name="edu_institution[]" /></td>';
			html_div +='        <td>Location<br />';
			html_div +='          <input type="text" name="edu_location[]" /></td>';
			html_div +='      </tr>';
			html_div +='      <tr>';
			html_div +='        <td>Completion Date<span class="star"><small> yyyy-mm-dd</small></span><br />';
			html_div +='          <input type="text" name="edu_end_date[]" /></td>';
			html_div +='        <td>Degree <span class="star">*</span><br />';
			html_div +='          <select name="edu_degree[]" class="selecteye" ><option> - degree - </option><option>Technical School BT</option><option>Technical School TS</option><option>High-School</option><option>Bachelor</option><option>Masters</option><option>Other</option></select></td>';
			html_div +='      </tr>';
			html_div +='      <tr>';
			html_div +='        <td>Major<br />';
			html_div +='          <input type="text" name="edu_desc[]" /></td>';
			html_div +='        <td></td>';
			html_div +='		</tr>';
			html_div +='	</table>';
			html_div +='	<span id="edu'+next_num+'"></span>';

	document.getElementById('edu'+edu_num).innerHTML=html_div
	document.getElementById('edu_count').value=next_num
}

function isEmpty (inputeye) {
	var val = document.getElementById(inputeye).value;
	if (val=='') {
		return true;
	} else {
		return false;
	}
}

function validate (div) {
	switch (div){
		case 'personal':
			if ( isEmpty('firstname') ) msg = 'First Name. <br />';
			if ( isEmpty('lastname') ) msg += 'Last Name. <br />';
			if ( isEmpty('fathername') ) msg += 'Father Name. <br />';
			if ( isEmpty('dob') ) msg += 'Date of Birth <br />'; 
			if ( isEmpty('nationalityatbirth') ) msg += 'Natinality. <br />';
			if ( isEmpty('sex') ) msg += 'Gender. <br />';
			if ( isEmpty('maritalstatus') ) msg += 'Marital Status. <br />';
			if ( isEmpty('mobile') ) msg += 'Mobile. <br />';
			if ( isEmpty('email') ) msg += 'Email Address. <br />';
			if ( !checkMail('email') ) msg += 'Your Email Address is incorrect.<br />';
			if ( isEmpty('address') ) msg += 'Address. <br />';
			if ( isEmpty('country') ) msg += 'Country. <br />';
		break;
		case 'experience':
			var exp_num=document.getElementById('exp_count').value;
			var test=false;
			for(i=1;i<=exp_num;i++)
				if ( !isEmpty('exp_company'+i+'')) {
					test=true;
					break;
				}
			if ( test==false ) msg = 'One experience is required. <br />';
		break;
		case 'education':
		break;
		case 'language':
		break;
	}
}

function checkMail(e){
	var val = document.getElementById(e).value
	if (val=='') {
		var returnval = true;
	} else {
		var returnval=emailfilter.test(val);
	}
	return returnval;
}

