var imaged = new Image();
imaged.src = "images/delete.gif";

var sep = "%#%";
var num_xp = 0;
var num_edu = 0;

function SubString_ifNeeded(str, maxlen)
{
	if (str.length>maxlen) {		
		str_part = str.substring(0,maxlen-2) + "...";
		str = str.replace(/\n/g,'<br>');
		var mystring = '\'';
		var re = new RegExp(mystring, "gi");
		str = str.replace(re, "\\'");		
		str = "<a onmouseover=\"Tip('"+str+"')\" class='c'>"+ str_part+"</a>";
	}	
	return str;
}

function addtable(divIdName)
{
  var company	= document.getElementById("xp_company").value;
  var industry	= (document.getElementById("xp_industry")?document.getElementById("xp_industry").value:"");
  var city		= document.getElementById("xp_city").value;
  var position	= document.getElementById("xp_position").value;
  var smw		= document.getElementById("xp_smw").value;
  var syw		= document.getElementById("xp_syw").value;
  var emw		= document.getElementById("xp_emw").value;
  var eyw		= document.getElementById("xp_eyw").value;
  var lsalary	= document.getElementById("xp_lsalary").value;
  var work_description = document.getElementById("xp_work_description").value;
  var achievement	= document.getElementById("xp_achievement").value;
  var country		= document.getElementById("xp_country").value;
  var jobspec		= document.getElementById("xp_jobspec").value;
  var poslevel		= document.getElementById("xp_poslevel").value;

  var data = company + sep + industry + sep + city + sep + position + sep + smw + sep + syw + sep + emw + sep + eyw + sep + lsalary +
			 sep + work_description + sep + achievement + sep + country + sep + jobspec + sep + poslevel;
  data = data.replace(/"/g,'&quot;');
  
  window['data'+divIdName] = data;

  if ((smw>0) && (smw<13)) smw = months[smw-1];
  if ((syw>1900) && (syw<2100)) syw = syw.substring(2, 4);
  if ((emw>0) && (emw<13)) emw = months[emw-1];
  if ((eyw>1900) && (eyw<2100)) eyw = eyw.substring(2, 4);
  var period = ((smw) && (syw))?smw + ' ' + syw:"";
  period += ((emw) && (eyw))? ' - ' + emw + ' ' + eyw:"";

  if (lsalary.length>1) lsalary = formatCurrency(lsalary);

  company			= SubString_ifNeeded(company,14);
  city				= SubString_ifNeeded(city,10);
  industry			= SubString_ifNeeded(industry,10);
  position			= SubString_ifNeeded(position,9);
  work_description	= SubString_ifNeeded(work_description,11);
  achievement		= SubString_ifNeeded(achievement,11);
  period			= SubString_ifNeeded(period,9);
  lsalary			= SubString_ifNeeded(lsalary,12);

/*
  var tab = "<input type=hidden name=data_experience[] value=\""+data+"\"><table border=0 cellpadding=0 cellspacing=0 width=548 align=center>"+
			"<tr><td colspan=12 bgcolor=#cccccc height=1></td></tr>"+
			"<tr><td height=25 class=small2 width=1 bgcolor=#cccccc></td><td class=small2 width=9></td><td class=small2 width=89>"+ company +
		    "</td><td class=small2 width=59>"+ industry +"</td><td class=small2 width=67>"+ city +"</td><td class=small2 width=53>"+ position +
		    "</td><td class=small2 width=47>"+ period +
			"</td><td class=small2 width=69>"+ lsalary +"</td><td class=small2 width=69>"+ work_description +"</td>"+
			"<td class=small2 width=65>"+ achievement +"</td>"+
			"<td width=19><a href=\"javascript:;\" title='delete this row' onclick=\"removeEvent(\'"+ divIdName+"\')\"><img src='images/delete.gif' border=0></a></td>"+
			"<td width=1 bgcolor=#cccccc></td></tr>" +			
			"</table>";
*/

  var tab = "<input type=hidden name=data_experience[] value=\""+data+"\"><table border=0 cellpadding=0 cellspacing=0 width=548 align=center>"+
			"<tr><td colspan=12 bgcolor=#cccccc height=1></td></tr>"+
			"<tr><td height=25 class=small2 width=1 bgcolor=#cccccc></td><td class=small2 width=9></td><td class=small2 width=89>"+ company +
		    "</td><td class=small2 width=59>"+ industry +"</td><td class=small2 width=67>"+ city +"</td><td class=small2 width=53>"+ position +
		    "</td><td class=small2 width=47>"+ period +
			"</td><td class=small2 width=69>"+ lsalary +"</td><td class=small2 width=69>"+ work_description +"</td>"+
			"<td class=small2 width=45>"+ achievement +"</td>"+
			"<td width=39><a href=\"javascript:;\" onclick=\"EditEvent(\'"+ divIdName+"\')\"><img src='images/edit.gif' border=0 title='edit this row'></a>&nbsp;&nbsp;<a href=\"javascript:;\" title='delete this row' onclick=\"var agree=confirm('Are you sure to delete this row ?'); if (agree) removeEvent(\'"+ divIdName+"\')\"><img src='images/delete.gif' border=0></a></td>"+
			"<td width=1 bgcolor=#cccccc></td></tr>" +			
			"</table>";

  return tab;
}

function EditEvent(divIdName)
{
	var arr = window['data'+divIdName].split(sep);
	if (arr[5]=="")	{ arr[4]=""; }
	if (arr[7]=="")	{ arr[6]=""; }
	document.getElementById("xp_company").value = arr[0];
    document.getElementById("xp_industry").value = arr[1];
    document.getElementById("xp_city").value = arr[2];
    document.getElementById("xp_position").value = arr[3];
    document.getElementById("xp_smw").value = arr[4];
    document.getElementById("xp_syw").value = arr[5];
    document.getElementById("xp_emw").value = arr[6];
	document.getElementById("xp_eyw").value = arr[7];
    document.getElementById("xp_lsalary").value = arr[8];
    document.getElementById("xp_work_description").value = arr[9];
	document.getElementById("xp_achievement").value = arr[10];
	document.getElementById("xp_country").value = arr[11];
	setListBoxValue("xp_jobspec", "resume", arr[12]);
	//setListBoxValue("xp_poslevel", "resume", arr[13])
	document.getElementById("xp_poslevel").value = arr[13];

	document.getElementById('add_exp').style.display="none";
	document.getElementById('edit_exp').style.display="block";

	curr_edit_div_xp = divIdName;	
}

function SaveEvent(is_xp_pro)
{
	if (!is_xp_pro) { var newcontent = addtable(curr_edit_div_xp); } else { var newcontent = addtable_xp_pro(curr_edit_div_xp); }
	document.getElementById(curr_edit_div_xp).innerHTML = newcontent;
	document.getElementById('add_exp').style.display="block";
	document.getElementById('edit_exp').style.display="none";
	ClearInput_Experience();
	curr_edit_div_xp = "";
}


function addtable_xp_pro(divIdName)
{
  var company	= document.getElementById("xp_company").value;
  var industry	= (document.getElementById("xp_industry")?document.getElementById("xp_industry").value:"");
  var city		= document.getElementById("xp_city").value;
  var position	= document.getElementById("xp_position").value;
  var smw		= document.getElementById("xp_smw").value;
  var syw		= document.getElementById("xp_syw").value;
  var emw		= document.getElementById("xp_emw").value;
  var eyw		= document.getElementById("xp_eyw").value;
  var lsalary	= document.getElementById("xp_lsalary").value;
  var work_description = document.getElementById("xp_work_description").value;
  var achievement	= document.getElementById("xp_achievement").value;
  var country		= document.getElementById("xp_country").value;
  var jobspec		= document.getElementById("xp_jobspec").value;
  var poslevel		= document.getElementById("xp_poslevel").value;

  var data = company + sep + industry + sep + city + sep + position + sep + smw + sep + syw + sep + emw + sep + eyw + sep + lsalary +
			 sep + work_description + sep + achievement + sep + country + sep + jobspec + sep + poslevel;
  data = data.replace(/"/g,'&quot;');
  
  if ((smw>0) && (smw<13)) smw = months[smw-1];
  if ((syw>1900) && (syw<2100)) syw = syw.substring(2, 4);
  if ((emw>0) && (emw<13)) emw = months[emw-1];
  if ((eyw>1900) && (eyw<2100)) eyw = eyw.substring(2, 4);
  var period = ((smw) && (syw))?smw + ' ' + syw:"";
  period += ((emw) && (eyw))? ' - ' + emw + ' ' + eyw:"";
  if (lsalary.length>1) lsalary = formatCurrency(lsalary);

  company			= SubString_ifNeeded(company,14);
  city				= SubString_ifNeeded(city,10);
  industry			= SubString_ifNeeded(industry,10);
  position			= SubString_ifNeeded(position,9);
  work_description	= SubString_ifNeeded(work_description,11);
  achievement		= SubString_ifNeeded(achievement,7);
  period			= SubString_ifNeeded(period,9);
  lsalary			= SubString_ifNeeded(lsalary,12);
  
  var tab = "<input type=hidden name=data_experience[] value=\""+data+"\"><table border=0 cellpadding=0 cellspacing=0 width=548 align=center>"+
			"<tr><td colspan=12 bgcolor=#cccccc height=1></td></tr>"+
			"<tr><td height=25 class=small2 width=1 bgcolor=#cccccc></td><td class=small2 width=9></td><td class=small2 width=162>"+ company +
		    "</td><td class=small2 width=108>"+ industry +"</td><td class=small2 width=83>"+ position +
		    "</td><td class=small2 width=82>"+ period +
			"</td><td class=small2 width=83>"+ lsalary +"</td>"+
			"<td width=19><a href=\"javascript:;\" title='delete this row' onclick=\"removeEvent(\'"+ divIdName+"\')\"><img src='images/delete.gif' border=0></a></td>"+
			"<td width=1 bgcolor=#cccccc></td></tr>" +			
			"</table>";

  return tab;
}

function header_table_xp()
{
  var tab = "<table border=0 cellpadding=0 cellspacing=0 width=548 align=center>"+
			"<tr>"+
			"<td width=10 height=24 bgcolor=#cfcdcd></td>"+
			"<td width=89 bgcolor=#cfcdcd class=small2><b>Company Name</b></td>"+
			"<td width=59 bgcolor=#cfcdcd class=small2><b>Industry</b></td>"+
			"<td width=67 bgcolor=#cfcdcd class=small2><b>City/Count.</b></td>"+
			"<td width=53 bgcolor=#cfcdcd class=small2><b>Position</b></td>"+
			"<td width=47 bgcolor=#cfcdcd class=small2><b>Period</b></td>"+
			"<td width=69 bgcolor=#cfcdcd class=small2><b>L.Salary</b></td>"+
			"<td width=69 bgcolor=#cfcdcd class=small2><b>Work Desc.</b></td>"+
			"<td width=85 bgcolor=#cfcdcd class=small2><b>Achievement</b></td>"+
			"</tr>"+
			"</table>";
  return tab;
}

function header_table_xp_pro()
{
  var tab = "<table border=0 cellpadding=0 cellspacing=0 width=548 align=center>"+
			"<tr>"+
			"<td width=10 height=24 bgcolor=#cfcdcd></td>"+
			"<td width=162 bgcolor=#cfcdcd class=small2><b>Company Name</b></td>"+
			"<td width=108 bgcolor=#cfcdcd class=small2><b>Industry</b></td>"+
			"<td width=83 bgcolor=#cfcdcd class=small2><b>Position</b></td>"+
			"<td width=82 bgcolor=#cfcdcd class=small2><b>Period</b></td>"+
			"<td width=103 bgcolor=#cfcdcd class=small2><b>L.Salary</b></td>"+
			"</tr>"+
			"</table>";
  return tab;
}

function addEvent(is_xp_pro)
{
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById("theValue").value -1)+ 2;

// add header
if (num_xp==0)
{
  var newdiv2 = document.createElement('div');
  newdiv2.setAttribute("id","div_xp_header");
  if (!is_xp_pro) { newdiv2.innerHTML = header_table_xp(); } else { newdiv2.innerHTML = header_table_xp_pro(); }
  ni.appendChild(newdiv2);  
}
num_xp++;

numi.value = num;
var divIdName = "my"+num+"Div";
var newdiv = document.createElement('div');
newdiv.setAttribute("id",divIdName);
if (!is_xp_pro) { var newcontent = addtable(divIdName); } else { var newcontent = addtable_xp_pro(divIdName); }
newdiv.innerHTML = newcontent;
ni.appendChild(newdiv);

ClearInput_Experience();
}

function removeEvent(divNum)
{
var d = document.getElementById('myDiv');
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);

num_xp--;
//remove header
if (num_xp==0)
{
  var headerdiv = document.getElementById("div_xp_header");
  d.removeChild(headerdiv);
}

}

function addtable2(divIdName)
{
  var level		= document.getElementById("ed_level").value;
  var institution	= (document.getElementById("ed_institution")?document.getElementById("ed_institution").value:"");
  var city		= document.getElementById("ed_city").value;
  var certificate = document.getElementById("ed_certificate").value;
  var major		= document.getElementById("ed_major").value;
  var gpa		= document.getElementById("ed_gpa").value;
  var sme		= document.getElementById("ed_sme").value;
  var sye		= document.getElementById("ed_sye").value;
  var eme		= document.getElementById("ed_eme").value;
  var eye		= document.getElementById("ed_eye").value;
  var selEduLevel = document.getElementById("selEducationLevel").value;
  var selCertDegree = document.getElementById("selCertificateDegree").value;
  var addInfo = document.getElementById("edu_additionalinfo").value;
  var educountry = document.getElementById("selEduCountry").value;
  eduObj = document.getElementById("selEducationLevel");
  var selEduLevelLabel = eduObj.options[eduObj.selectedIndex].text;
  certObj = document.getElementById("selCertificateDegree");
  var selCertificateDegree = certObj.options[certObj.selectedIndex].text;

  var data = sme + sep + sye + sep + eme + sep + eye + sep + level + sep + institution + sep + city + sep + certificate + sep + 
		     major + sep + gpa + sep + selEduLevel + sep + selCertDegree + sep + addInfo + sep + educountry;
  data = data.replace(/"/g,'&quot;');
  
  window['data'+divIdName] = data;

  institution	= SubString_ifNeeded(institution,10);
  city			= SubString_ifNeeded(city,10);
  level			= SubString_ifNeeded(selEduLevelLabel,7);
  certificate	= SubString_ifNeeded(selCertificateDegree,9);
  major			= SubString_ifNeeded(major,12);
  gpa			= SubString_ifNeeded(gpa,6);
	  
  if ((sme>0) && (sme<13)) sme = months[sme-1];
  if ((sye>1900) && (sye<2100)) sye = sye.substring(2, 4);
  if ((eme>0) && (eme<13)) eme = months[eme-1];
  if ((eye>1900) && (eye<2100)) eye = eye.substring(2, 4);

  var tab = "<input type=hidden name=data_education[] value=\""+data+"\"><table border=0 cellpadding=0 cellspacing=0 width=548 align=center>"+
			"<tr><td colspan=12 bgcolor=#cccccc height=1></td></tr>"+
			"<tr><td height=25 class=small2 width=1 bgcolor=#cccccc></td><td class=small2 width=9></td><td class=small2 width=52>"+ sme + ' ' + sye  +
		    "</td><td class=small2 width=46>"+ eme + ' ' + eye +"</td><td class=small2 width=40>"+ level +"</td><td class=small2 width=92>"+ institution +
		    "</td><td class=small2 width=81>"+ city +
			"</td><td class=small2 width=83>"+ certificate +
			"<td class=small2 width=72>"+ major +"</td>"+
			"<td class=small2 width=32>"+ gpa +"</td>"+
			"<td width=39><a href=\"javascript:;\" onclick=\"EditEvent2(\'"+ divIdName+"\')\"><img src='images/edit.gif' border=0 title='edit this row'></a>&nbsp;&nbsp;<a href=\"javascript:;\" title='delete this row' onclick=\"var agree=confirm('Are you sure to delete this row ?'); if (agree) removeEvent2(\'"+ divIdName+"\')\"><img src='images/delete.gif' border=0></a></td>"+
			"<td width=1 bgcolor=#cccccc></td></tr>" +			
			"</table>";
			
  return tab;
}

function EditEvent2(divIdName)
{
	var arr = window['data'+divIdName].split(sep);
	if (arr[1]=="")	{ arr[0]=""; }
	if (arr[3]=="")	{ arr[2]=""; }
	document.getElementById("ed_level").value = arr[4];
    document.getElementById("ed_institution").value = arr[5];
    document.getElementById("ed_city").value = arr[6];
    document.getElementById("ed_certificate").value = arr[7];
    document.getElementById("ed_major").value = arr[8];
    document.getElementById("ed_gpa").value = arr[9];
    document.getElementById("ed_sme").value = arr[0];
	document.getElementById("ed_sye").value = arr[1];
    document.getElementById("ed_eme").value = arr[2];
    document.getElementById("ed_eye").value = arr[3];
	document.getElementById("selEducationLevel").value = arr[10];
	document.getElementById("selCertificateDegree").value = arr[11];
	document.getElementById("edu_additionalinfo").value = arr[12];
	document.getElementById("selEduCountry").value = arr[13];
	
	document.getElementById('add_edu').style.display="none";
	document.getElementById('edit_edu').style.display="block";

	curr_edit_div = divIdName;	
}

function SaveEvent2(is_xp_pro)
{
	if (!is_xp_pro) { var newcontent = addtable2(curr_edit_div); } else { var newcontent = addtable2_edu_pro(curr_edit_div); }
	document.getElementById(curr_edit_div).innerHTML = newcontent;
	document.getElementById('add_edu').style.display="block";
	document.getElementById('edit_edu').style.display="none";
	ClearInput_Education();
	curr_edit_div = "";
}

function addtable2_edu_pro(divIdName)
{
  var level		= document.getElementById("ed_level").value;
  var institution	= (document.getElementById("ed_institution")?document.getElementById("ed_institution").value:"");
  var city		= document.getElementById("ed_city").value;
  var certificate	= document.getElementById("ed_certificate").value;
  var major		= document.getElementById("ed_major").value;
  var gpa		= document.getElementById("ed_gpa").value;
  var sme		= document.getElementById("ed_sme").value;
  var sye		= document.getElementById("ed_sye").value;
  var eme		= document.getElementById("ed_eme").value;
  var eye		= document.getElementById("ed_eye").value;
  var selEduLevel = document.getElementById("selEducationLevel").value;
  var selCertDegree = document.getElementById("selCertificateDegree").value;
  var addInfo = document.getElementById("edu_additionalinfo").value;
  var educountry = document.getElementById("selEduCountry").value;

  //var data = sme + sep + sye + sep + eme + sep + eye + sep + level + sep + institution + sep + city + sep + certificate + sep + 
//		     major + sep + gpa + sep;
  var data = sme + sep + sye + sep + eme + sep + eye + sep + level + sep + institution + sep + city + sep + certificate + sep + 
		     major + sep + gpa + sep + selEduLevel + sep + selCertDegree + sep + addInfo + sep + educountry;

data = data.replace(/"/g,'&quot;');

  institution			= SubString_ifNeeded(institution,50);
  certificate			= SubString_ifNeeded(certificate,40);

  var tab = "<input type=hidden name=data_education[] value=\""+data+"\"><table border=0 cellpadding=0 cellspacing=0 width=548 align=center>"+
			"<tr><td colspan=12 bgcolor=#cccccc height=1></td></tr>"+
			"<tr><td height=25 class=small2 width=1 bgcolor=#cccccc></td><td class=small2 width=9></td>"+
			"<td class=small2 width=288>"+ institution +
			"</td><td class=small2 width=230>"+ certificate +
			"<td width=19><a href=\"javascript:;\" title='delete this row' onclick=\"removeEvent2(\'"+ divIdName+"\')\"><img src='images/delete.gif' border=0></a></td>"+
			"<td width=1 bgcolor=#cccccc></td></tr>" +			
			"</table>";

  return tab;
}

function header_table_edu()
{
var tab2 = "<table border=0 cellpadding=0 cellspacing=0 width=548 align=center>"+
		"<tr>"+
		  "<td width=10 height=24 bgcolor=#cfcdcd></td>"+
		  "<td width=52 bgcolor=#cfcdcd class=small2><b>Start</b></td>"+
		  "<td width=46 bgcolor=#cfcdcd class=small2><b>End</b></td>"+
		  "<td width=40 bgcolor=#cfcdcd class=small2><b>Level</b></td>"+
		  "<td width=92 bgcolor=#cfcdcd class=small2><b>Institution</b></td>"+
		  "<td width=81 bgcolor=#cfcdcd class=small2><b>City/Country</b></td>"+
		  "<td width=103 bgcolor=#cfcdcd class=small2><b>Certificate</b></td>"+
		  "<td width=72 bgcolor=#cfcdcd class=small2><b>Major</b></td>"+
		  "<td width=52 bgcolor=#cfcdcd class=small2><b>GPA</b></td>"+
		"</tr>"+
	  "</table>";  
return tab2;	
}

function header_table_edu_pro()
{
var tab2 = "<table border=0 cellpadding=0 cellspacing=0 width=548 align=center>"+
		"<tr>"+
		  "<td width=10 height=24 bgcolor=#cfcdcd></td>"+
		  "<td width=288 bgcolor=#cfcdcd class=small2><b>Institution</b></td>"+
		  "<td width=250 bgcolor=#cfcdcd class=small2><b>Certificate</b></td>"+
		"</tr>"+
	  "</table>";  
return tab2;	
}

function addEvent2(is_xp_pro)
{
var ni = document.getElementById('myDiv2');
var numi = document.getElementById('theValue2');
var num = (document.getElementById("theValue2").value -1)+ 2;

// add header
if (num_edu==0)
{
  var newdiv2 = document.createElement('div');
  newdiv2.setAttribute("id","div_edu_header");
  if (!is_xp_pro) { newdiv2.innerHTML = header_table_edu(); } else { newdiv2.innerHTML = header_table_edu_pro(); }
  ni.appendChild(newdiv2);  
}
num_edu++;

numi.value = num;
var divIdName = "my"+num+"Div2";
var newdiv = document.createElement('div');
newdiv.setAttribute("id",divIdName);
if (!is_xp_pro) { var newcontent = addtable2(divIdName); } else { var newcontent = addtable2_edu_pro(divIdName); }
newdiv.innerHTML = newcontent;
ni.appendChild(newdiv);

ClearInput_Education();
}

function removeEvent2(divNum)
{
var d = document.getElementById('myDiv2');
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);

num_edu--;
//remove header
if (num_edu==0)
{
  var headerdiv = document.getElementById("div_edu_header");
  d.removeChild(headerdiv);
}
}

function setListBoxValue(lstbox, form, val)
{
	chkbox = eval("document."+form+".chk"+lstbox);
	container = eval("document."+form+"."+lstbox);
	
	if (chkbox.length!=null)
	{
		container.value = val;
		for(i=0;i<chkbox.length;i++)
		{
			//alert(val + " = " + chkbox[1].value + " = " + listFindNoCase(val, chkbox[i].value, ","));
			if (listFindNoCase(val, chkbox[i].value, ",")>-1)
				chkbox[i].checked = true;
			else
				chkbox[i].checked = false;
		}
	}
}