function isValidEmail (email,strict){
   if ( !strict ) email = email.replace(/^\s+|\s+$/g, '');
   return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}
function numbersOnly (field, event) {
	  var key, keychar;
	  if (window.event) key = window.event.keyCode;
	  else{
		 if (event) key = event.which;
		 else return true;
	  }
	  keychar = String.fromCharCode (key);
	  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27)) return true;
	  else {
		 if (("0123456789").indexOf(keychar) > -1) return true;
		 else return false;
	  }
}

function closeJob(id){$("#jobtr_"+id).fadeOut("slow");}
function closeEdu(id){$("#educationtr_"+id).fadeOut("slow");}
function closeCurs(id){$("#curstr_"+id).fadeOut("slow");}

$(document).ready(function(){
	/*if ( $('#auto_0').attr("checked") ) $("#autodet").hide();
	else $("#autodet").show();
    $("input:radio").change(function(){
		if ( $('#auto_0').attr("checked") ) $("#autodet").hide();
		else $("#autodet").show();
		$('#auto_0').attr("checked");
    });*/
	var tempString = new String();
	var tempArray = new Array();
	var cjob = "";
	var tempHtml = "";
	var countErr = 0;
    tempString = $(".jobtr:last").attr("id");
	tempArray = tempString.split("_",2);
    var cjob = tempArray[1];
	tempString = $(".educationtr:last").attr("id");
	tempArray = tempString.split("_",2);
    var ceducation = tempArray[1];
	tempString = $(".curstr:last").attr("id");
	tempArray = tempString.split("_",2);
    var ccurs = tempArray[1];
	$("#addJob").click(function(){	   
		cjob++;
	    tempHtml = '<tr id="jobtr_' + cjob + '" class="jobtr"><td style="border-top:1px solid #78c73a;" colspan="3" width="100%"><table cellspacing="0" cellpadding="2" border="0">';
        tempHtml += '<tr style="text-align:center;"><td nowrap><b>Начало (мм/гггг)</b></td><td nowrap><b>Организация (название, профиль деятельности)</b></td><td><b>Должность</b></td></tr>';
        tempHtml += '<tr style="text-align:center;"><td><input type="text" name="dstrt_' + cjob + '" id="dstrt_' + cjob + '" value="" style="width:100px;" maxlength="200"></td>';
        tempHtml += '<td><input type="text" name="firmtitle_' + cjob + '" id="firmtitle_' + cjob + '" value="" style="width:200px;" maxlength="200"></td>';
        tempHtml += '<td><input type="text" name="position_' + cjob + '" id="position_' + cjob + '" value="" style="width:100px;" maxlength="200"></td></tr>';
        tempHtml += '<tr style="text-align:center;"><td nowrap><b>Окончание (мм/гггг)</b></td><td><b>Обязанности, функции, достижения</b></td><td></td></tr>';
        tempHtml += '<tr style="text-align:center;"><td valign="top"><input type="text" name="dfinish_' + cjob + '" id="dfinish_' + cjob + '" value="" style="width:100px;" maxlength="200"></td>';
        tempHtml += '<td><textarea name="function_' + cjob + '" id="function_' + cjob + '"  style="width:200px;"></textarea></td><td valign="bottom" align="right"><img title="закрыть" onClick="closeJob('+ cjob +')" style="cursor:pointer;" src="/images/close.gif"></td></tr>';
        tempHtml += '</table></td></tr>';
		cjob--;
		tempString = "#jobtr_" + cjob;
		cjob++;
		$(tempString).after(tempHtml);
		
        return false;
    });
	$("#addEducation").click(function(){	   
		ceducation++;
	    tempHtml = '<tr id="educationtr_' + ceducation + '" class="educationtr"><td style="border-top:1px solid #78c73a;" colspan="3" width="100%"><table cellspacing="0" cellpadding="2" border="0" width="100%">';
        tempHtml += '<tr style="text-align:center;"><td nowrap><b>Название</b></td><td nowrap><b>Факультет/спец.</b></td><td></td></tr>';
        tempHtml += '<tr style="text-align:center;"><td><input type="text" name="education_' + ceducation + '" id="education_' + ceducation + '" value="" style="width:150px;" maxlength="200"></td><td><input type="text" name="facultet_' + ceducation + '" id="facultet_' + ceducation + '" value="" style="width:250px;" maxlength="200"></td><td valign="middle" align="right"><img title="закрыть" onClick="closeEdu('+ ceducation +')" style="cursor:pointer;" src="/images/close.gif"></td></tr>';
        tempHtml += '<tr style="text-align:center;"><td nowrap><b>Диплом/степень</b></td><td><b>Год нач. обучения</td><td><b>Год оконч. обучения</td></tr>';
        tempHtml += '<tr style="text-align:center;"><td><input type="text" name="diplom_' + ceducation + '" id="diplom_' + ceducation + '" value="" style="width:150px;" maxlength="200"></td><td><input type="text" onkeypress="return numbersOnly(this,event)" name="educationstart_' + ceducation + '" id="educationstart_' + ceducation + '" value="" style="width:150px;" maxlength="200"></td><td><input type="text" onkeypress="return numbersOnly(this,event)" name="educationfinish_' + ceducation + '" id="educationfinish_' + ceducation + '" value="" style="width:150px;" maxlength="200"></td></tr>';
        tempHtml += '</table></td></tr>';
		ceducation--;
		tempString = "#educationtr_" + ceducation;
		ceducation++;
		$(tempString).after(tempHtml);
        return false;
    });
	$("#addCurs").click(function(){	   
		ccurs++;
	    tempHtml = '<tr id="curstr_' + ccurs + '" class="curstr"><td style="border-top:1px solid #78c73a;" colspan="3" width="100%"><table cellspacing="0" cellpadding="2" border="0" width="100%">';
        tempHtml += '<tr style="text-align:center;"><td nowrap><b>Уч.заведение</b></td><td nowrap><b>Срок обучения</b></td><td><b>Приобретенные навыки</b></td></tr>';
        tempHtml += '<tr style="text-align:center;"><td><input type="text" name="curstitle_' + ccurs + '"  id="curstitle_' + ccurs + '"  value="" style="width:150px;" maxlength="200"></td><td><input type="text" name="cursduration_' + ccurs + '" id="cursduration_' + ccurs + '"  value="" style="width:150px;" maxlength="200"></td><td><input type="text" name="cursknoledge_' + ccurs + '"  id="cursknoledge_' + ccurs + '"  value="" style="width:150px;" maxlength="200"></td></tr>';
        tempHtml += '<tr style="text-align:center;"><td nowrap><b>Диплом/степень</b></td><td><b>Специальность</td><td>&nbsp;</td></tr>';
        tempHtml += '<tr style="text-align:center;"><td><input type="text" name="cursdiplom_' + ccurs + '"  id="cursdiplom_' + ccurs + '"  value="" style="width:150px;" maxlength="200"></td><td><input type="text" name="cursspec_' + ccurs + '" id="cursspec_' + ccurs + '"  value="" style="width:150px;" maxlength="200"></td><td valign="bottom" align="right"><img title="закрыть" onClick="closeCurs('+ ccurs +')" style="cursor:pointer;" src="/images/close.gif"></td></tr>';
        tempHtml += '</table></td></tr>';
		ccurs--;
		tempString = "#curstr_" + ccurs;
		ccurs++;
		$(tempString).after(tempHtml);
			
        return false;
    });
	$("#send").click(function(){	   
	    countErr = 0;
		$("input.requered:text").each(function(){
	       if (!$(this).val()){
		      $(this).addClass("alert"); countErr++;
			  if (countErr==1) $(this).focus();
		   }
        }); 
		if ( $("#email").val() && !isValidEmail($("#email").val()) ){
		    $("#email").addClass("alert");
			countErr++;
			if (countErr==1) $("#email").focus();
        }		
        if (countErr) return false;
    });
	$("#clear").click(function(){	   
	    $("input:text").val("");
		$("textarea").val("");
		$("input.alert").removeClass("alert");
        return false;
    });
	$("#commoninfo").click(function(){	   
	    $("#commoninfotr").toggle();
		if ($("#commoninfotr").is(":hidden")) $("#commoninfo").attr("src","/images/plus.gif");
		else $("#commoninfo").attr("src","/images/minus.gif");
    });
	$("#contactinfo").click(function(){	   
	    $("#contactinfotr").toggle();
		if ($("#contactinfotr").is(":hidden")) $("#contactinfo").attr("src","/images/plus.gif");
		else $("#contactinfo").attr("src","/images/minus.gif");
    });
	$("#dopaboutmyself").click(function(){	   
	    $("#dopaboutmyselftr").toggle();
		if ($("#dopaboutmyselftr").is(":hidden")) $("#dopaboutmyself").attr("src","/images/plus.gif");
		else $("#dopaboutmyself").attr("src","/images/minus.gif");
    });
	$("#dopinfo").click(function(){	   
	    $("#dopinfotr").toggle();
		if ($("#dopinfotr").is(":hidden")) $("#dopinfo").attr("src","/images/plus.gif");
		else $("#dopinfo").attr("src","/images/minus.gif");
    });
	$("#familyinfo").click(function(){	   
	    $("#familyinfotr").toggle();
		if ($("#familyinfotr").is(":hidden")) $("#familyinfo").attr("src","/images/plus.gif");
		else $("#familyinfo").attr("src","/images/minus.gif");
    });
	$("input:text").focus(function(){if ($(this).hasClass("alert")) $(this).removeClass("alert");});
	$("input:text").css({"border":"1px solid #0E1655"},{"color":"#0E1655"});
});	  