var xmlHttp

function sendMessage()
{
	
	for  (var i = 0; i < document.getElementsByName('rcpt').length; i++) {
	if ( document.getElementsByName('rcpt')[i].checked ) {
		var rcpt = document.getElementsByName('rcpt')[i].value;
		// alert(rcpt);
		var subject = document.getElementById('textSubject').value
	var msg = document.getElementById('textMessage').value
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="/user/inc/msgAjax.inc.php"
	url=url+"?action=send"
	url=url+"&rcpt="+rcpt
	url=url+"&subject="+escape(subject)
	url=url+"&msg="+escape(msg)
	
	xmlHttp.open("GET",url,false)
	xmlHttp.send(null)
	}
	}
	
	window.location = "http://www.cvdemon.com/user/?mail"

}

function discardMessage()
{
	document.getElementById('textMessage').value = ""
	document.getElementById('textSubject').value = ""
}

function saveMessage()
{
	xmlHttp=GetXmlHttpObject()
	
	// write to DB 
	var msg = escape(document.getElementById('textMessage').value);
	var subject = escape(document.getElementById('textSubject').value);
	
	var url="/user/inc/msgAjax.inc.php"
	url=url+"?action=savedraft"
	url=url+"&subject="+subject
	url=url+"&msg="+msg
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			// document.getElementById("messageStatus").innerHTML = xmlHttp.responseText
			window.location = "/user/?mail&new";
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function loadMessageDraft(msgID, dest)
{
	// dropdown onChange="loadMessageDraft(this.value)"
	// php db function code to get messagetext from db WHERE msgID=msgID and userID=$_SESSION['userID']
	// document.getElementById(dest).value = 
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="/user/inc/msgAjax.inc.php";
	url=url+"?action=load";
	url=url+"&msgID="+msgID;
	xmlHttp.open("GET",url,false);	
//	xmlHttp.onreadystatechange=function()
//	{
//		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
//		{	
//			//alert(document.getElementById(dest).value);
//			document.getElementById(dest).value = xmlHttp.responseText	
//			
//		}
//	}	
//	
	xmlHttp.send(null)
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{	
		//alert(document.getElementById(dest).value);
		document.getElementById(dest).value = xmlHttp.responseText	
		
	}	
}

function loadMessageDraftSubject(msgID, dest)
{
	// dropdown onChange="loadMessageDraft(this.value)"
	// php db function code to get messagetext from db WHERE msgID=msgID and userID=$_SESSION['userID']
	// document.getElementById(dest).value = 
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="/user/inc/msgAjax.inc.php"
	url=url+"?action=getsubject"
	url=url+"&msgID="+msgID
	xmlHttp.open("GET",url,false);
//	xmlHttp.onreadystatechange=function()
//	{
//		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
//		{	document.getElementById(dest).value = xmlHttp.responseText	}
//	}
	
	xmlHttp.send(null);
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{	document.getElementById(dest).value = xmlHttp.responseText	}	
}

// Returns a random integer between 0 and $num
function randomPin(num)
{
	var rand_no = Math.random();
	rand_no = rand_no * num;
	rand_no = Math.ceil(rand_no);
	return rand_no;
}

function remCustSec(sec)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="/user/inc/addSection.inc.php"
	url=url+"?remove"
	url=url+"&id="+sec
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			document.getElementById("custSecDiv").innerHTML = xmlHttp.responseText
			updateSection("custsec", "custSecDiv") 
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	
}

function remEmployerSec(sec)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="/user/inc/addEmployerSection.inc.php"
	url=url+"?remove"
	url=url+"&id="+sec
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			// document.getElementById("custSecDiv").innerHTML = xmlHttp.responseText 
			updateSection("employer", "custSecDiv")
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	
}

function updateSection(type, dest)
{
	// returns ajax for section
	// Where type = "employer", "eduhist", "emphist", "custsec"
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="/user/inc/getAjaxSection.inc.php"
	url=url+"?s="+type
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			document.getElementById(dest).innerHTML = xmlHttp.responseText 
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function addCVSection()
{ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="/user/inc/addSection.inc.php"
	url=url+"?name="+escape(document.getElementById("sectionname").value)
	url=url+"&text="+escape(getWidgTextareaSource("sectiontext"))
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			// document.getElementById("custSecDiv").innerHTML = xmlHttp.responseText
			updateSection("custsec", "custSecDiv")
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	
	document.getElementById("sectionname").value = ""
	resetWidgSource("sectiontext");
	addStatusMessage("custom-section-status", "Section added!");
}

function addEmployerSec()
{ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="/user/inc/addEmployerSection.inc.php"
	url=url+"?name="+escape(document.getElementById("sectionname").value)
	url=url+"&text="+escape(getWidgTextareaSource("sectiontext"))
		
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			// document.getElementById("custSecDiv").innerHTML = xmlHttp.responseText
			// alert(xmlHttp.responseText);
			document.getElementById("sectionname").value = ""
			resetWidgSource("sectiontext");
			updateSection("employer", "custSecDiv")
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	addStatusMessage("custom-section-status", "Section added!");
}

function remEmpHist(id)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="/user/inc/addEmployment.inc.php?remove"
	url=url+"&id="+id
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			// document.getElementById("empHistDiv").innerHTML = xmlHttp.responseText 
			updateSection("emphist", "empHistDiv")
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function remEduHist(id)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="/user/inc/addEducation.inc.php?remove"
	url=url+"&id="+id
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			// document.getElementById("eduHistDiv").innerHTML = xmlHttp.responseText 
			updateSection("eduhist", "eduHistDiv")
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function addEmpHist()
{ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	 
	var fromDate = getDDValue('jobFromYear') + "-" + getDDValue('jobFromMonth') + "-" + getDDValue('jobFromDay');
	
	if($('jobToPresent').checked) {
		var toDate = "2500-1-1" // Dummy year to represent present
	} else {
		var toDate = getDDValue('jobToYear') + "-" + getDDValue('jobToMonth') + "-" + getDDValue('jobToDay');
	}
	
	var url="/user/inc/addEmployment.inc.php"
	url=url+"?from="+fromDate
	url=url+"&to="+toDate
	url=url+"&title="+escape(document.getElementById("textJobTitle").value)
	url=url+"&comp="+escape(document.getElementById("textJobCompany").value)
	url=url+"&further="+escape(getWidgTextareaSource("textJobFurther"))
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			updateSection("emphist", "empHistDiv")
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	
	clearEmploymentAddForm()
	resetWidgSource("textJobFurther");
	addStatusMessage("emp-hist-status", "Section added!");
}

function editEduHist(eduHistID)
{ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="/user/inc/editEduHist.inc.php"
	url=url+"?id="+eduHistID
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			document.getElementById("eduHistInput").innerHTML = xmlHttp.responseText 
			updateSection("eduhist", "eduHistDiv")
			widgInit()
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function editSaveEduHist(eduHistID)
{ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	 
	var fromDate = getDDValue('eduFromYear') + "-" + getDDValue('eduFromMonth') + "-" + getDDValue('eduFromDay');
	if($('eduToPresent').checked) {
		var toDate = "2500-1-1" // Dummy year to represent present
	} else {
		var toDate = getDDValue('eduToYear') + "-" + getDDValue('eduToMonth') + "-" + getDDValue('eduToDay');
	}
	
	var url="/user/inc/editEduHist.inc.php"
	url=url+"?save"
	url=url+"&id="+eduHistID
	url=url+"&from="+fromDate
	url=url+"&to="+toDate
	url=url+"&loc="+escape(document.getElementById("textEduInstitution").value)
	url=url+"&q="+escape(document.getElementById("textEduQualification").value)
	url=url+"&further="+escape(getWidgTextareaSource("textEduFurther"))
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			document.getElementById("eduHistInput").innerHTML = xmlHttp.responseText
			updateSection("eduhist", "eduHistDiv")
			widgInit()
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	addStatusMessage("edu-hist-status", "Section saved!");
}

function editEmpHist(empHistID)
{ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="/user/inc/editEmpHist.inc.php"
	url=url+"?id="+empHistID
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			document.getElementById("empHistInput").innerHTML = xmlHttp.responseText 
			updateSection("emphist", "empHistDiv") 
			widgInit()
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function editSaveEmpHist(empHistID)
{ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	 
	var fromDate = getDDValue('jobFromYear') + "-" + getDDValue('jobFromMonth') + "-" + getDDValue('jobFromDay');
	if($('jobToPresent').checked) {
		var toDate = "2500-1-1" // Dummy year to represent present
	} else {
		var toDate = getDDValue('jobToYear') + "-" + getDDValue('jobToMonth') + "-" + getDDValue('jobToDay');
	}
	
	var url="/user/inc/editEmpHist.inc.php"
	url=url+"?save"
	url=url+"&id="+empHistID
	url=url+"&from="+fromDate
	url=url+"&to="+toDate
	url=url+"&title="+escape(document.getElementById("textJobTitle").value)
	url=url+"&comp="+escape(document.getElementById("textJobCompany").value)
	url=url+"&further="+escape(getWidgTextareaSource("textJobFurther"))
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			document.getElementById("empHistInput").innerHTML = xmlHttp.responseText
			updateSection("emphist", "empHistDiv")
			widgInit()
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	addStatusMessage("emp-hist-status", "Section saved!");
}

function editSaveCustSec(secID)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
		
	var url="/user/inc/editCustomCVSection.inc.php"
	url=url+"?save"
	url=url+"&id="+secID
	url=url+"&name="+escape(document.getElementById("sectionname").value)
	url=url+"&text="+escape(getWidgTextareaSource("sectiontext"))
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			document.getElementById("customSecInput").innerHTML = xmlHttp.responseText 
			updateSection("custsec", "custSecDiv")
			widgInit()
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	addStatusMessage("custom-section-status", "Section saved!")
}

function editCustSec(secID)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="/user/inc/editCustomCVSection.inc.php"
	url=url+"?id="+secID
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			document.getElementById("customSecInput").innerHTML = xmlHttp.responseText 
			// alert(xmlHttp.responseText)
			updateSection("custsec", "custSecDiv")
			widgInit()
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function editEmpSec(secID)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="/user/inc/editEmployerSection.inc.php"
	url=url+"?id="+secID
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			document.getElementById("section-box").innerHTML = xmlHttp.responseText 
			updateSection("employer", "custSecDiv")
			widgInit()
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function editSaveEmpSec(secID)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="/user/inc/editEmployerSection.inc.php"
	url=url+"?save"
	url=url+"&id="+secID
	url=url+"&name="+escape(document.getElementById("sectionname").value)
	url=url+"&text="+escape(getWidgTextareaSource("sectiontext"))
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			document.getElementById("section-box").innerHTML = xmlHttp.responseText 
			updateSection("employer", "custSecDiv")
			widgInit()
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	addStatusMessage("custom-section-status", "Section saved!");
}

function addEduHist()
{ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	 
	var fromDate = getDDValue('eduFromYear') + "-" + getDDValue('eduFromMonth') + "-" + getDDValue('eduFromDay');
	if($('eduToPresent').checked) {
		var toDate = "2500-1-1" // Dummy year to represent present
	} else {
		var toDate = getDDValue('eduToYear') + "-" + getDDValue('eduToMonth') + "-" + getDDValue('eduToDay');
	}
	
	var url="/user/inc/addEducation.inc.php"
	url=url+"?from="+fromDate
	url=url+"&to="+toDate
	url=url+"&loc="+escape(document.getElementById("textEduInstitution").value)
	url=url+"&q="+escape(document.getElementById("textEduQualification").value)
	url=url+"&further="+escape(getWidgTextareaSource("textEduFurther"))
	
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			//document.getElementById("eduHistDiv").innerHTML = xmlHttp.responseText 
			updateSection("eduhist", "eduHistDiv")
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	
	clearEducationAddForm();
	resetWidgSource("textEduFurther");
	addStatusMessage("edu-hist-status", "Section added!");
}

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 clearEmploymentAddForm() {
	$('jobFromDay').selectedIndex = 0
	$('jobFromMonth').selectedIndex = 0
	$('jobFromYear').selectedIndex = 40
	$('jobToDay').selectedIndex = 0
	$('jobToMonth').selectedIndex = 0
	$('jobToYear').selectedIndex = 40
	$('textJobTitle').value = ""
	$('textJobCompany').value = ""
	$('textJobFurther').value = ""
	$('jobToDay').disabled = false
	$('jobToMonth').disabled = false
	$('jobToYear').disabled = false
	$('jobToPresent').checked = false
}

function clearEducationAddForm() {
	$('eduFromDay').selectedIndex = 0
	$('eduFromMonth').selectedIndex = 0
	$('eduFromYear').selectedIndex = 40
	$('eduToDay').selectedIndex = 0
	$('eduToMonth').selectedIndex = 0
	$('eduToYear').selectedIndex = 40
	$('textEduInstitution').value = ""
	$('textEduQualification').value = ""
	$('textEduFurther').value = ""
	$('eduToDay').disabled = false
	$('eduToMonth').disabled = false
	$('eduToYear').disabled = false
	$('eduToPresent').checked = false
}


function toggleEduToMenus() {
	if($('eduToPresent').checked) {
		$('eduToDay').disabled = "disabled"
		$('eduToMonth').disabled = "disabled"
		$('eduToYear').disabled = "disabled"
	} else {
		$('eduToDay').disabled = false
		$('eduToMonth').disabled = false
		$('eduToYear').disabled = false
	}
}

function toggleJobToMenus() {
	if($('jobToPresent').checked) {
		$('jobToDay').disabled = "disabled"
		$('jobToMonth').disabled = "disabled"
		$('jobToYear').disabled = "disabled"
	} else {
		$('jobToDay').disabled = false
		$('jobToMonth').disabled = false
		$('jobToYear').disabled = false
	}
}

function verifyDelete() {
    if (confirm("Are you sure you would like to continue?")) {
       return true;
    } else {
       return false;
    }
}

function getDDValue(elemID) {
	var elem = document.getElementById(elemID);
	return elem[elem.selectedIndex].value;
}

function getWidgTextareaSource(elemID) {
	return getIFrameDocument(elemID+"WidgIframe").getElementsByTagName("body")[0].innerHTML
}

function resetWidgSource(elemID) {
	getIFrameDocument(elemID+"WidgIframe").getElementsByTagName("body")[0].innerHTML = ""
}

function getIFrameDocument(aID){ 
 var rv = null; 

 // if contentDocument exists, W3C compliant (Mozilla) 
 if (document.getElementById(aID).contentDocument){ 
   rv = document.getElementById(aID).contentDocument; 
 } else { 
   // IE 
   rv = document.frames[aID].document; 
 } 

 return rv; 
}

function addStatusMessage(elemID, msg) {
	document.getElementById(elemID).innerHTML = msg;	
}
