// (c) 2005, 2006 and beyond - postGorilla, Inc.
// emurse.com

function hostResume(id) {
	var newSubdomain = cleanSubdomain(document.getElementById('newSubdomain').value);
	var filter  = /^([a-zA-Z0-9])+/;
	if (filter.test(newSubdomain))  { 
		getURL("/?ajax=1&a=website&type=overview&a2=ajax-hosting-create-subdomain&id="+id+"&subdomain="+newSubdomain,"Checking...");
	} else {
		hostResumeERROR("Letters A-Z and numbers 0-9 only!");
	}
}

function hostResumeOK() {
	location.reload();
}

function hostResumeERROR(data) {
	document.getElementById('error').innerHTML=data;
}

function changeHosting(id, name) { 
	getURL("/?ajax=1&a=website&type=overview&a2=ajax-hosting-change-resume&id="+escapeString(id)+"&name="+escapeString(name),"Changing...");
}

function changeHostingOK() {
	window.location=".";
}
function hostApplySettings(id) {
	var isHosted;

        if(document.getElementById('hosted').checked == true) {
		isHosted = 1;
        } else {
		isHosted = 0;
	}

	getURL("/?ajax=1&a=website&type=overview&a2=ajax-hosting-apply-settings&id="+id+"&hosted="+isHosted+"&searchable=1&keywords="+document.getElementById("keywords").value,"Updating...");	
}

function hostApplySettingsOK(data) {
	document.getElementById("settingsApplied").innerHTML = data;
}

function getGroups() {
	getURL("/?ajax=1&a=share&type=groups&a2=ajax-groups-get-groups","Loading...");
}

function getGroupsOK(data) {
	document.getElementById("groupsContainer").innerHTML = data;	
}

function showNewGroup() {
	getURL("/?ajax=1&a=share&type=groups&a2=ajax-groups-show-new-group","Loading...");
}

function showNewGroupOK(data) {
	document.getElementById("groupsContainer").innerHTML = data;
}

function createGroup() {
	var name = document.getElementById('groupName').value;
	var publicGroup = (document.getElementById('publicGroup').checked)?"1":"0";
	var password = (publicGroup=='0')?document.getElementById('groupPassword').value:"";
	var description = document.getElementById('groupDescription').innerHTML;
	var openinvites = (document.getElementById('openInvites').checked)?"1":"0";
	var resumeToShare = document.getElementById('resumeToShare').value;

	getURL("/?ajax=1&a=share&type=groups&a2=ajax-groups-create-group&name="+escapeString(name)+"&public="+publicGroup+"&password="+escapeString(password)+"&description="+escapeString(description)+"&openinvites="+openinvites+"&resume="+escapeString(resumeToShare),"Creating...");
}

function createGroupOK() {
	window.location="/share/groups/";
}

function editGroup(id) {
	getURL("/?ajax=1&a=share&type=groups&a2=ajax-groups-edit-group&id="+id,"Loading...");
}

function editGroupOK(data) {
	document.getElementById("groupsContainer").innerHTML = data;
}

function updateGroup(id) {
	var selectedResume = document.getElementById("selectSharedResume").value;
	if(document.getElementById("groupDescription")) {
		var groupDescription = document.getElementById("groupDescription").innerHTML;
		var publicGroup = (document.getElementById("publicGroup").checked)?"1":"0";
		var openInvites = (document.getElementById("openInvites").checked)?"1":"0";
		var groupPassword = document.getElementById("groupPassword").value;

		getURL("/?ajax=1&a=share&type=groups&a2=ajax-groups-update-group&id="+id+"&resume="+selectedResume+"&description="+groupDescription+"&public="+publicGroup+"&openinvites="+openInvites+"&password="+groupPassword,"Updating...");
	} else {
		getURL("/?ajax=1&a=share&type=groups&a2=ajax-groups-update-group&id="+id+"&resume="+selectedResume,"Updating...");
	}
}

function updateGroupOK() {
	alert("yep");
	window.location = "/share/groups/";
}

function showJoinGroup() {
	getURL("/?ajax=1&a=share&type=groups&a2=ajax-groups-show-join-group","Updating...");
}

function showJoinGroupOK(data) {
	document.getElementById("groupsContainer").innerHTML = data;
}

function joinGroup() {
	var resumeId = document.getElementById("resumeToShare").value;
	var groupId = document.getElementById("groupId").value;
	if(!document.getElementById("groupPassword")) {
		if(groupId != "") {	
			getURL("/?ajax=1&a=share&type=groups&a2=ajax-groups-join-group&id="+groupId+"&resume="+resumeId+"&password=","Joining...");
		} else {
			joinGroupOK("You must enter a group id! If you would like to join a public group, use the search link below!",0);
		}
	} else {	
		var groupPassword = document.getElementById("groupPassword").value;
		if(groupPassword != "") {
			getURL("/?ajax=1&a=share&type=groups&a2=ajax-groups-join-group&id="+groupId+"&resume="+resumeId+"&password="+groupPassword,"Joining...");
		} else {
			joinGroupOK("You must enter a password to join this group!",0);
		}
	}
}

function joinGroupOK(data,passNeeded) {
	if(passNeeded != '1') {
		if(data) {
			document.getElementById("joinError").innerHTML = data;		
		} else {
			window.location = "/share/groups/";
		}
	} else {
		document.getElementById("groupsContainer").innerHTML = data;
	}
}

function setUsePassword() {
	var toggle = (document.getElementById('hosting_reqpassword').checked)?"1":"0";

	getURL('/?a=website&type=overview&a2=set_use_password&usepassword='+toggle, 'Setting...');
}

function setUsePasswordOK() {
	if(document.getElementById('hosting_reqpassword').checked) {
		showSharePasswordDialog();
	}
}

function saveSubDomainPassword() {
	var password = document.getElementById('newsitepassword').value;
	var password2 = document.getElementById('verifysitepassword').value;
	if(password == password2) {
		getURL('/?a=website&type=overview&a2=save_subdomain_password&subdomain_password='+escapeString(password)+'&subdomain_password2='+escapeString(password2),'Saving...');
	} else {
		document.getElementById('statusmsg').innerHTML = '<font color=\'#880000\'>Passwords do not match!</font>';
	}
}

function saveSubDomainPasswordOK(status) {
	//window.location = '.';
	if(status && status != '0') {
		document.getElementById('statusmsg').innerHTML = '<font color=\'#880000\'>'+status+'</font>';
	} else if(status != '0') {
		document.getElementById('hosting_reqpassword').checked = 0;
		cancelDialog();
	} else {
		document.getElementById('hosting_reqpassword').checked = 1;
		cancelDialog();
	}
}

function cleanSubdomain(subdomain) {
	subdomain = subdomain.replace(/\'/gi, '');
	subdomain = subdomain.replace(/&/gi, 'and');
	subdomain = subdomain.replace(/[^\w\-]/gi,'-');
	subdomain = subdomain.replace(/\-+/gi, '-');
	subdomain = subdomain.replace(/\-$/gi, '');
	subdomain = subdomain.replace(/^\-/gi, '');
	subdomain = subdomain.toLowerCase();
	
	return subdomain;
}

function clearResumeStats(resumeid) {
	if(confirm("Are you sure you want to clear these stats?")) {
		// clear call
		getURL('/?a=share&type=webpag&a2=ajax-clear-resume-stats&resumetoclear='+escapeString(resumeid),'Clearing...');
	}
}

function clearResumeStatsOK() {
	location.reload();
}

function clearResumeHosts(resumeid) {
	if(confirm("Are you sure you want to clear these hosts?")) {
		// clear call
		getURL('/?a=share&type=webpag&a2=ajax-clear-resume-hosts&resumetoclear='+escapeString(resumeid),'Clearing...');
	}
}

function clearResumeHostsOK() {
	location.reload();	
}

function clearResumeRefers(resumeid) {
	if(confirm("Are you sure you want to clear these referring pages?")) {
		// clear call
		getURL('/?a=share&type=webpag&a2=ajax-clear-resume-refers&resumetoclear='+escapeString(resumeid),'Clearing...');
	}
}

function clearResumeRefersOK() {
	location.reload();
}

function setEmailAlert() {
	var toggle = (document.getElementById('hosting_emailme').checked)?"1":"0";

	getURL('/?a=website&type=overview&a2=ajax-set-email-alert&alert='+toggle,"Setting...");
}

function setEmailAlertOK() {
}

function setIndexable() {
	var toggle = (document.getElementById('hosting_indexable').checked)?"1":"0";

	getURL('/?a=website&type=overview&a2=ajax-set-indexable&indexable='+toggle,"Setting...");
}

function setIndexableOK() {

}

function setUsePrivacy() {
	var toggle = (document.getElementById('hosting_hidepersonalinfo').checked)?"1":"0";

	getURL('/?a=website&type=overview&a2=ajax-set-use-privacy&toggle='+toggle,"Setting...");
}

function setUsePrivacyOK() {
	var toggle = (document.getElementById('hosting_hidepersonalinfo').checked)?"1":"0";
	if(toggle==1) {
		showSharePersonalDialog();
	}
}

function setShareStatus() {
	var status = (document.getElementById('active_resume').checked)?"1":"0";

	getURL('/?a=website&type=overview&a2=ajax-set-share-status&status='+status,"Setting...");
}

function setShareStatusOK() {
	if(document.getElementById('inactiveResume')) {
		document.getElementById("inactiveResume").parentNode.removeChild(document.getElementById("inactiveResume"));
	}
	return false;
}

function setShareStatusUPGRADE() {
	showAccountUpgradeDialog();
	document.getElementById('active_resume').checked = false;
	return false;
}

function getShareStats() {
	var statType = document.getElementById("statType").value;

	getURL('/?a=website&type=overview&a2=ajax-get-share-stats&statType='+escapeString(statType),"Building...");
}

function getShareStatsOK(data) {
	document.getElementById("hostedStatsAddl").innerHTML = data;
}

