/* Joined.com EditAccount API 
 * Date: 02 July 2008
 */
/* --------------------------------------------- */
/* -----------  Networks Management */
/* --------------------------------------------- */
function networkMakePrimary(updateElement, iNetworkID) {
	var url = getBaseURL("networks");
	url += "setprimary/" + iNetworkID + "/";
	getAjaxContents(url, updateElement);
}
function adminNetworkMakePrimary(updateElement, iNetworkID, iUserID) {
	var url = getBaseURL("admin");
	url += "setprimarynetwork/?id=" + iNetworkID + "&userid=" + iUserID;
	getAjaxContents(url, updateElement);
}
function delNetwork(updateElement, iNetworkID) {
	var url = getBaseURL("networks");
	url += "del/" + iNetworkID + "/";
	getAjaxContents(url, updateElement);												
}
function adminDelNetwork(updateElement, iNetworkID, iUserID) {
	var url = getBaseURL("admin") + "delnetwork/?id=" + iNetworkID + "&userid=" + iUserID;
	getAjaxContents(url, updateElement);	
}
function addNetwork(updateElement, dropdown) {
	var dropdownelement = document.getElementById(dropdown);
	if(dropdownelement) {
		var url = getBaseURL("networks") + "add/";
		var index 			= dropdownelement.selectedIndex;
		if(index != -1) {
			var sNetwork 		= dropdownelement.options[index].value;
			if(sNetwork == "") return;
			var poststring = dropdown + "=" + sNetwork;
			/* Take the ending ampersand off */
			// poststring = poststring.substring(0, poststring.length-1);
			/* Send off the POST request to the URL */
			sendPostAjaxContent(url, poststring, updateElement);
		}
	}
}
function adminAddNetwork(updateElement, dropdown, iUserID) {
	var dropdownelement = document.getElementById(dropdown);
	if(dropdownelement) {
		var url = getBaseURL("admin") + "addnetwork/";
		var index 			= dropdownelement.selectedIndex;
		if(index != -1) {
			var sNetwork 		= dropdownelement.options[index].value;
			if(sNetwork == "") return;
			var poststring = dropdown + "=" + sNetwork + "&net_userid=" + iUserID;
		
			/* Send off the POST request to the URL */
			sendPostAjaxContent(url, poststring, updateElement);
		}
	}
}


/* --------------------------------------------- */
/* ----------- Work Histroy Management -------------- */
/* --------------------------------------------- */
function addWorkHistory(updateElement, sHistoryName) {
	var eHistoryElement = document.getElementById(sHistoryName);
	if(eHistoryElement) {
		var sHistory = eHistoryElement.value;
		if(sHistory == "") return;
		var poststring = sHistoryName + "=" + sHistory;
		/* Take the ending ampersand off */
		// poststring = poststring.substring(0, poststring.length-1);
		var url = getBaseURL("workhistory") + "add/";
		/* Send off the POST request to the URL */
		sendPostAjaxContent(url, poststring, updateElement);
	}
}
function adminAddWorkHistory(updateElement, sHistoryName, iUserID) {
	var eHistoryElement = document.getElementById(sHistoryName);
	if(eHistoryElement) {
		var sHistory = eHistoryElement.value;
		if(sHistory == "") return;
		var poststring = sHistoryName + "=" + sHistory + "&history_userid=" + iUserID;
		/* Take the ending ampersand off */
		poststring = poststring.substring(0, poststring.length-1);	
		var url = getBaseURL("admin") + "addworkhistory/";	
		/* Send off the POST request to the URL */
		sendPostAjaxContent(url, poststring, updateElement);
	}
}
function delWorkHistory(delElement, updateElement, iWorkHistoryID) {
	var url = getBaseURL("workhistory") + "del/" + iWorkHistoryID + "/";
	getAjaxContents(url, updateElement);
	bindThickBoxEvents();
}
function adminDelWorkHistory(delElement, updateElement, iWorkHistoryID, iUserID) {
	var url = getBaseURL("admin") + "delworkhistory/?id=" + iWorkHistoryID + "&userid=" + iUserID;
	getAjaxContents(url, updateElement);		
}
function editWorkHistories(updateElement, iHisID) {	
	var aInputs = new Array(
					'history_edit_title_' + iHisID,
					'history_edit_jobtitle_' + iHisID,
					'history_edit_jobdesc_' + iHisID,					
					'history_edit_id_' + iHisID,
					'history_edit_from_month_' + iHisID,
					'history_edit_to_month_' + iHisID,
					'history_edit_from_year_' + iHisID,
					'history_edit_to_year_' + iHisID			
	);
	
	var some = new Array();
	/* Get all the titles from hidden field */		
	if(aInputs.length > 0) {
		var poststring = '';
		/* lets go through all the edit names */
		for(var i = 0; i < aInputs.length; i++) {
			var sInput = aInputs[i];
			/* do nothing if it's blank, use remove button!! */
			if(sInput == "") continue;
			var eEditElement = document.getElementById(sInput);
			sInput = sInput.replace(/_\d+$/, "");
			if(eEditElement) {
				if(eEditElement.tagName == "SELECT") {
					sEditValue = getDropdownValue(eEditElement);
				} else {
					sEditValue = eEditElement.value;		
				}
				poststring += sInput + "=" + sEditValue + "&";
				
				some[i] = sEditValue;
				
			}
		}
		/* Take the ending ampersand off */
		poststring = poststring.substring(0, poststring.length-1);
		var url = getBaseURL("workhistory") + "edit/";
/* andriy start */

	
	$.post(url, { iHisID: iHisID,  history_edit_title: some[0], history_edit_jobtitle: some[1], history_edit_jobdesc: some[2], history_edit_id: some[3], history_edit_from_month: some[4], history_edit_to_month: some[5], history_edit_from_year: some[6], history_edit_to_year: some[7]},
  updateElement);
	$('#history_edit_'+iHisID).toggle(); switchToggleText(this);
/* andriy end */
		/* Send off the POST request to the URL */
/* sendPostAjaxContent(url, poststring, updateElement); */ 
	}
}
function adminEditWorkHistories(updateElement, iHisID, iUserID) {	
	var aInputs = new Array(
					'history_edit_title_' + iHisID,
					'history_edit_jobtitle_' + iHisID,
					'history_edit_jobdesc_' + iHisID,					
					'history_edit_id_' + iHisID,
					'history_edit_from_month_' + iHisID,
					'history_edit_to_month_' + iHisID,
					'history_edit_from_year_' + iHisID,
					'history_edit_to_year_' + iHisID			

	);
	/* Get all the titles from hidden field */		
	if(aInputs.length > 0) {
		var poststring = '';
		/* lets go through all the edit names */
		for(var i = 0; i < aInputs.length; i++) {
			var sInput = aInputs[i];
			/* do nothing if it's blank, use remove button!! */
			if(sInput == "") continue;
			var eEditElement = document.getElementById(sInput);
			sInput = sInput.replace(/_\d+$/, "");
			if(eEditElement) {
				if(eEditElement.tagName == "SELECT") {
					sEditValue = getDropdownValue(eEditElement);
				} else {
					sEditValue = eEditElement.value;		
				}
				poststring += sInput + "=" + sEditValue + "&";
			}
		}
		/* Take the ending ampersand off */
		poststring = poststring.substring(0, poststring.length-1);
		var url = getBaseURL("admin") + "editworkhistory/" + iUserID;
	
		/* Send off the POST request to the URL */
		sendPostAjaxContent(url, poststring, updateElement);
	}
}
function updateWorkHistoryStatus(sElemName, updateElement) {
	if(eElem = getID(sElemName)) {
		sStatus = getDropdownValue(eElem);
		if(sStatus == "") {
			return;	
		}
		disableSelects(new Array(sElemName));
		var url = getBaseURL("workhistory") + "status/" + sStatus + "/";
		getAjaxContents(url, updateElement);		
	}
}


/* --------------------------------------- */
/* ---------- Resume Management --------------- */
/* --------------------------------------- */
function editResume(updateElement, iResID) {
	var aInputs = new Array(
		'resume_edit_id_' + iResID,
		'resume_edit_title_' + iResID,
		'resume_edit_body_' + iResID
	);
	var some = new Array();
	/* Get all the titles from hidden field */		
	if(aInputs.length > 0) {
		var poststring = '';
		/* lets go through all the edit names */
		for(var i = 0; i < aInputs.length; i++) {
			var sInput = aInputs[i];
			/* do nothing if it's blank, use remove button!! */
			if(sInput == "") continue;
			var eEditElement = document.getElementById(sInput);
			if(eEditElement) {
				sEditValue = eEditElement.value;
				sInput = sInput.replace(/_\d+$/, '');	
				poststring += sInput + "=" + sEditValue + "&";
				some[i] = sEditValue;
			}
		}
		/* Take the ending ampersand off */
		poststring = poststring.substring(0, poststring.length-1);
		var url = getBaseURL("resumes") + "edit/";	
	
		/* Send off the POST request to the URL */
		/* sendPostAjaxContent(url, poststring, updateElement); */
		$.post(url, { iResID: iResID,  resume_edit_id: some[0], resume_edit_title: some[1], resume_edit_body: some[2]},
				  updateElement);
		$('#resume_edit_'+iResID).toggle(); switchToggleText(this);
	}
}
function adminEditResume(updateElement, iResID, iUserID) {
	var aInputs = new Array(
		'resume_edit_id_' + iResID,
		'resume_edit_title_' + iResID,
		'resume_edit_body_' + iResID
	);
	
	/* Get all the titles from hidden field */		
	if(aInputs.length > 0) {
		var poststring = '';
		/* lets go through all the edit names */
		for(var i = 0; i < aInputs.length; i++) {
			var sInput = aInputs[i];
			/* do nothing if it's blank, use remove button!! */
			if(sInput == "") continue;
			var eEditElement = document.getElementById(sInput);
			if(eEditElement) {
				sEditValue = eEditElement.value;
				sInput = sInput.replace(/_\d+$/, '');			
				poststring += sInput + "=" + sEditValue + "&";
			}
		}
		/* Take the ending ampersand off */
		poststring = poststring.substring(0, poststring.length-1);
		var url = getBaseURL("admin") + "editresume/" + iUserID;	
	
		/* Send off the POST request to the URL */
		sendPostAjaxContent(url, poststring, updateElement);
	}
}
function delResume(delElement, updateElement, iResumeID) {
	var url = getBaseURL("resumes") + "del/" + iResumeID + "/";
	getAjaxContents(url, updateElement);
}
function adminDelResume(delElement, updateElement, iResumeID, iUserID) {
	var url = getBaseURL("admin") + "delresume/?id=" + iResumeID + "&userid=" + iUserID;
	getAjaxContents(url, updateElement);		
}
function addResume(updateElement, sResumeName) {
	var eElem = document.getElementById(sResumeName);
	if(eElem) {
		var sResume = eElem.value;
		if(sResume == "") return;
		var poststring = sResumeName + "=" + sResume;
		/* Take the ending ampersand off */
		// poststring = poststring.substring(0, poststring.length-1);
		var url = getBaseURL("resumes") + "add/";	
		/* Send off the POST request to the URL */
		sendPostAjaxContent(url, poststring, updateElement);
	}
}
function adminAddResume(updateElement, sResumeName, iUserID) {
	var eElem = document.getElementById(sResumeName);
	if(eElem) {
		var sResume = eElem.value;
		if(sResume == "") return;
		var poststring = sResumeName + "=" + sResume + "&resume_userid=" + iUserID;
		/* Take the ending ampersand off */
		// poststring = poststring.substring(0, poststring.length-1);
		var url = getBaseURL("admin") + "addresume/";	
		/* Send off the POST request to the URL */
		sendPostAjaxContent(url, poststring, updateElement);
	}
}
/* --------------------------------------------- */
/* -------------- Skills Management ---------- */
/* --------------------------------------------- */
function addSkills(updateElement, sSkillsName) {
	var eElem = document.getElementById(sSkillsName);
	if(eElem) {
		var sSkills = eElem.value;
		if(sSkills == "") return;
		var poststring = sSkillsName + "=" + sSkills;
		/* Take the ending ampersand off */
		// poststring = poststring.substring(0, poststring.length-1);
		var url = getBaseURL("skills") + "add/";	
		/* Send off the POST request to the URL */
		sendPostAjaxContent(url, poststring, updateElement);
	}
}
function editSkills(updateElement, iResID) {
	var aInputs = new Array(
		'skills_edit_id_' + iResID,
		'skills_edit_title_' + iResID,
		'skills_edit_body_' + iResID,
		'skills_edit_level_' + iResID
	); 

	var some = new Array();
	/* Get all the titles from hidden field */		
	if(aInputs.length > 0) {
		var poststring = '';
		/* lets go through all the edit names */
		for(var i = 0; i < aInputs.length; i++) {
			var sInput = aInputs[i];
			/* do nothing if it's blank, use remove button!! */
			if(sInput == "") continue;
			var eEditElement = document.getElementById(sInput);
            if(eEditElement.length > 100) {alert('100 character max.');return;}//maxlength check
            if(eEditElement) {
				sEditValue = eEditElement.value;
				sInput = sInput.replace(/_\d+$/, '');	
				poststring += sInput + "=" + sEditValue + "&";
				some[i] = sEditValue;
			}
		}
		/* Take the ending ampersand off */
		poststring = poststring.substring(0, poststring.length-1);
		var url = getBaseURL("skills") + "edit/";	
		/* Send off the POST request to the URL */
		/* sendPostAjaxContent(url, poststring, updateElement); */
		$.post(url, { iResID: iResID,  skills_edit_id: some[0], skills_edit_title: some[1], skills_edit_body: some[2], skills_edit_level: some[3]},
				  updateElement);
		$('#skills_edit_'+iResID).toggle(); switchToggleText(this);
	}
}
function delCerts(delElement, updateElement, iCertsID) {
	var url = getBaseURL("certifications") + "del/" + iCertsID + "/";
	getAjaxContents(url, updateElement);
}
function delSkills(delElement, updateElement, iSkillsID) {
	var url = getBaseURL("skills") + "del/" + iSkillsID + "/";
	getAjaxContents(url, updateElement);
}
/* --------------------------------------------- */
/* -------------- Education Management ---------- */
/* --------------------------------------------- */
function addEducation(updateElement, sName, sListName) {
	var dropdownelement = document.getElementById(sName);
	if(dropdownelement) {
		var url = getBaseURL("education") + "add/";
		var sEducation = dropdownelement.value;
		if(sEducation == "") return;
		var poststring = sName + "=" + sEducation;
		/* Send off the POST request to the URL */
		sendPostAjaxContent(url, poststring, updateElement);
	}
}
function adminAddEducation(updateElement, sName, sListName, iUserID) {
	var dropdownelement = document.getElementById(sName);
	if(dropdownelement) {
		var url = getBaseURL("admin") + "addeducation/";
		var sEducation = dropdownelement.value;
		if(sEducation == "") return;
		var poststring = sName + "=" + sEducation + "&edu_userid=" + iUserID;
		/* Send off the POST request to the URL */
		sendPostAjaxContent(url, poststring, updateElement);
	}
}
function delEducation(delElement, updateElement, iEducationID) {
	/* Get all the titles from hidden field */
	var eEditIDs = document.getElementById("edit_titles");
	if(eEditIDs) {
		sEditIDs = eEditIDs.value;
		disableEditEducations(sEditIDs);
	}
	var url = getBaseURL("education");
	url += "del/" + iEducationID + "/";
	getAjaxContents(url, updateElement);
	bindThickBoxEvents();
}
function adminDelEducation(delElement, updateElement, iEducationID, iUserID) {
	/* Get all the titles from hidden field */
	var eEditIDs = document.getElementById("edit_titles");
	if(eEditIDs) {
		sEditIDs = eEditIDs.value;
		disableEditEducations(sEditIDs);
	}
	var url = getBaseURL("admin") + "deleducation/?id=" + iEducationID + "&userid=" + iUserID;
	getAjaxContents(url, updateElement);		
}
function editEducation(updateElement, iEduID) {	
	var aInputs = new Array(
					'edu_edit_title_' + iEduID,
					'edu_edit_educationdegreeid_' + iEduID,
					'edu_edit_program_' + iEduID,
					'edu_edit_id_' + iEduID,
					'edu_edit_from_month_' + iEduID,
					'edu_edit_to_month_' + iEduID,
					'edu_edit_from_year_' + iEduID,
					'edu_edit_to_year_' + iEduID			

	);
	var some = new Array();									
	var educationdegreeElem = document.getElementById('edu_edit_educationdegreeid_' + iEduID);
	if (educationdegreeElem.options[educationdegreeElem.selectedIndex].value == "")
	{
		var elem = document.getElementById('edu_edit_errors_' + iEduID);
		elem.style.display="";
		return;
	}
		
	/* Get all the titles from hidden field */		
	if(aInputs.length > 0) {
		var poststring = '';
		/* lets go through all the edit names */
		for(var i = 0; i < aInputs.length; i++) {
			var sInput = aInputs[i];
			/* do nothing if it's blank, use remove button!! */
			if(sInput == "") continue;
			var eEditElement = document.getElementById(sInput);
			sInput = sInput.replace(/_\d+$/, "");
			if(eEditElement) {
				if(eEditElement.tagName == "SELECT") {
					sEditValue = getDropdownValue(eEditElement);
				} else {
					sEditValue = eEditElement.value;		
				}
				poststring += sInput + "=" + sEditValue + "&";
				some[i] = sEditValue;
			}
		}
		/* Take the ending ampersand off */
		poststring = poststring.substring(0, poststring.length-1);		
		var url = getBaseURL("education") + "edit/";	
		/* Send off the POST request to the URL */
		/* andriy start */
		$.post(url, { iEduID: iEduID,  edu_edit_title: some[0], edu_edit_educationdegreeid: some[1], edu_edit_program: some[2], edu_edit_id: some[3], edu_edit_from_month: some[4], edu_edit_to_month: some[5], edu_edit_from_year: some[6], edu_edit_to_year: some[7]},
	  updateElement);
		$("#edu_edit_"+iEduID).toggle(); switchToggleText(this);
	/* andriy end */
	/* sendPostAjaxContent(url, poststring, updateElement); */
	}
}
function adminEditEducation(updateElement, iEduID, iUserID) {	
	var aInputs = new Array(
					'edu_edit_title_' + iEduID,
					'edu_edit_educationdegreeid_' + iEduID,
					'edu_edit_program_' + iEduID,
					'edu_edit_id_' + iEduID,
					'edu_edit_from_month_' + iEduID,
					'edu_edit_to_month_' + iEduID,
					'edu_edit_from_year_' + iEduID,
					'edu_edit_to_year_' + iEduID			

	);
	
	/* Get all the titles from hidden field */		
	if(aInputs.length > 0) {
		var poststring = '';
		/* lets go through all the edit names */
		for(var i = 0; i < aInputs.length; i++) {
			var sInput = aInputs[i];
			/* do nothing if it's blank, use remove button!! */
			if(sInput == "") continue;
			var eEditElement = document.getElementById(sInput);
			sInput = sInput.replace(/_\d+$/, "");
			if(eEditElement) {
				if(eEditElement.tagName == "SELECT") {
					sEditValue = getDropdownValue(eEditElement);
				} else {
					sEditValue = eEditElement.value;		
				}
				poststring += sInput + "=" + sEditValue + "&";
			}
		}
		/* Take the ending ampersand off */
		poststring = poststring.substring(0, poststring.length-1);		
		var url = getBaseURL("admin") + "editeducation/" + iUserID;	
		/* Send off the POST request to the URL */
		sendPostAjaxContent(url, poststring, updateElement);
	}
}

/* ------------------------------------ */
/* ---- Avtar Management ------ */
/* ------------------------------------ */
function checkTerms(form, checkelem) {
	var eCheck = document.getElementById(checkelem);
	if(eCheck) {
		if(eCheck.checked == true) {
			return true;
		} else {
			alert("You must certify that you have the right to distribute the image.");	
			return false;
		}
	}
}

/* -- */
/*
 * ----------------------------- Country / State Dropdown Management
 * ---------------
 */
/* -- */
function updateStateProvinceDropdown(p_eCountry, p_sDestDropdown) {
	var aStates = new Array('Alabama','Alaska','Arizona','Arkansas','California','Colorado','Connecticut','Delaware',
			'District of Columbia','Florida','Georgia','Hawaii','Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana',
			'Maine','Maryland','Massachusetts','Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada',
			'New Hampshire','New Jersey','New Mexico','New York','North Carolina','North Dakota','Ohio','Oklahoma','Oregon',
			'Pennsylvania','Rhode Island','South Carolina','South Dakota','Tennessee','Texas','Utah','Vermont','Virginia','Washington',
			'West Virginia','Wisconsin','Wyoming');
	
	var aProvinces = new Array('Alberta','British Columbia','Manitoba','Newfoundland and Labrador','New Brunswick','Nova Scotia','Ontario','Prince Edward Island','Quebec','Saskatchewan','Northwest Territories','Nunavut','Yukon');

	var index 			= p_eCountry.selectedIndex;
	var sCountry 		= p_eCountry.options[index].value;	
	statebox = document.getElementById(p_sDestDropdown);	
	clearList(statebox);
	statebox[0] = new Option('Select a State/Province','');
	var j = 1;
	if(sCountry == "") {		
				
	} else if(sCountry == "United States") {
		/* Populate States Dropdown */
		for(var i = 0; i < aStates.length; i++) {
			statebox[j] = new Option(aStates[i], aStates[i]);
			j++;
		}
	} else if(sCountry == "Canada") {
		/* Populate States Dropdown */
		for(var i = 0; i < aProvinces.length; i++) {
			statebox[j] = new Option(aProvinces[i], aProvinces[i]);
			j++;
		}		
	} else {
		/* Populate States Box */
		statebox[j] = new Option("Other", "Other");
	}
}

/* -- */	
/* --------------------- Delete Account Management ------------- */
/* -- */
// should be depricated
function checkDelAccount() {
	if(confirm("Are you sure you want to cancel your Joined account?\nAll your personal information stored on the site will be lost.\nYou cannot undo this action.")) {
		var url = getBaseURL("members") + "cancelaccount/";
		window.document.location = url;
	}
	return;
}
// this too should be depricated
function adminCheckDelAccount(iUserID) {
	if(confirm("Are you sure you want to cancel the user's Joined account?\nAll of their personal information stored on the site will be lost.\nYou cannot undo this action.")) {
		var url = getBaseURL("admin") + "delaccount/?userid=" + iUserID;
		window.document.location = url;
	}
	return;
}

function delAccount() {
	var url = getBaseURL("members") + "cancelaccount/";
	window.document.location = url;
	return;
}

function adminDelAccount(iUserID) {
	var url = getBaseURL("admin") + "delaccount/?userid=" + iUserID;
	window.document.location = url;
	return;
}

/* -- */
/* ----------------- Privacy Management --------------------- */
/* -- */
function editPrivacy(sName, messageDivID) {
	var dropdownelement = document.getElementById(sName);
	if(dropdownelement) {
		var url = getBaseURL("members") + "edit_privacy/";
		var sPrivacy = dropdownelement.value;
		if(sPrivacy == "") return;
		var poststring = sName + "=" + sPrivacy;
		/* Send off the POST request to the URL */
		sendPostAjaxContent(url, poststring, '');
		document.getElementById(messageDivID).innerHTML = 'Saved';
		$('#'+messageDivID).fadeOut(2000, function resetMessage() {
				document.getElementById(messageDivID).innerHTML = '';
				$('#'+messageDivID).show();
			}
		);
		
	}
}

function postOnWall(sToField, sContentField, wallDivID) {
	var to_field = document.getElementById(sToField);
	var content_field = document.getElementById(sContentField);
	if(to_field && content_field)
	{
		var url = getBaseURL("wall") + "post/";
		var to_id = to_field.value;
		var content = content_field.value;
		var poststring = sToField + "=" + to_id + "&" + sContentField + "=" + content;
		
		/* Send off the POST request to the URL */
		sendPostAjaxContent(url, poststring, wallDivID);
	}
}

function selectState(state) {
	var state_field = document.getElementById('state');
	var listLength = state_field.length;
	
	for(var i = 0; i < listLength; i++) {
		if ( state_field.options[i].text == state) {
			state_field.options[i].selected = 1;
		}
	}
}
