// s1jobs mys1 JavaScipt functions
// jeremy.jones@s1now.com

//==================================
// cookie required functions first:
//==================================
function setCookie(name, value, path, expires, domain, secure)
{
    var tpath = "/";

  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((tpath) ? "; path=" + tpath : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name)
{
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1)
  {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain)
{
    var thispath = "/";

    if (getCookie(name))
    document.cookie = name + "=" +
    ((thispath) ? "; path=" + thispath : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";

    thispath = "/mys1";
    if (getCookie(name))
    document.cookie = name + "=" +
    ((thispath) ? "; path=" + thispath : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";

    thispath = "/mys1/";
    if (getCookie(name))
    document.cookie = name + "=" +
    ((thispath) ? "; path=" + thispath : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
//==================================

function getFriendlyMys1Invitation () {
    // just a central place for the content of the mys1 invitation
    var msg = "";
    msg += "You must register and sign in to mys1 to use this feature.";
    msg += "\n\n";
    msg += "Registering with mys1 allows you to save relevant information";
    msg += "\n";
    msg += "from s1 websites to your personal mys1 file, including";
    msg += "\n";
    msg += "jobs you are interested in, automatic email searches,";
    msg += "\n";
    msg += "and your CV.";
    msg += "\n\n";
    msg += "Registration also gives you automatic access to advanced";
    msg += "\n";
    msg += "features on the other websites in the s1 family, including";
    msg += "\n";
    msg += "s1homes.com and s1play.com.";
    msg += "\n\n";
    msg += "Click OK to go to the mys1 register/sign-in page.";
    //    msg += "\n\n\n";
    return msg;
}

function s1search () {;}

function s1jobs_mys1_to_cv_database (d)
{
    var d = new Date();
    parent.mainContent.document.location.href = "http://" + location.hostname + "/mys1/cvdb_pcs.cgi?mys1_eid=" + escape(getCookie('mys1_eid')) + "&timestamp=" + d.valueOf();
    return true;
}

function s1jobs_mys1_jump_to_cv (d)
{
    var d = new Date();
    parent.mainContent.document.location.href = "http://" + location.hostname + "/mys1/cvdb_cv.cgi?cvstraight=1&mys1_eid=" + escape(getCookie('mys1_eid')) + "&timestamp=" + d.valueOf();
    return true;
}

function s1jobs_mys1_save_search (mySearchParams) {
    // saves a search to a user's mys1 file. 

    if (! mySearchParams) {
	alert("This search cannot be saved to mys1 due to an application error. Please contact webmaster@s1now.com for assistance.");
	return false;
    }

    var goURL = "http://" + top.document.location.hostname + "/mys1/mys1.cgi?";
    var searchParams = new Array ();
    for (meth in mySearchParams) 
	if (meth != "base" && meth != "page") searchParams[searchParams.length] = escape(meth) + "=" + escape(mySearchParams[meth]);
    goURL += "action=save_search&mys1_eid=" + escape(getCookie('mys1_eid'));

    goURL += "&searchQuery=" + escape(searchParams.join('&'));

    var whichWindow = 'mainContent';

    var mainContentURL = new String();
    if (! parent.mainContent.location.href.match(/mys1\/mys1\.cgi/)) {
	whichWindow = 'smgBusiness';
	goURL += "&hidden=1";
    }    
    var cook = getCookie('mys1_eid');
    if (!cook) { // should be !cook
	if (confirm(getFriendlyMys1Invitation())) {
	    setCookie('mys1_reqURL',goURL);
	    parent.popups.location.href = "/mys1/signin.shtml";
	}
    }
    else parent[whichWindow].location.replace(goURL);
    return false;
}

function mys1_logout () {
    if (confirm('Logout of mys1?')) {
	navigator.mys1eid="";
	navigator.mys1id="";
	deleteCookie("mys1_eid");
	deleteCookie("sessionID");
	alert('You have logged out of mys1. You should also close your browser window to fully clear your mys1 settings.');
	top.document.location.href = "http://" + top.document.location.hostname + "/";
	//	if (confirm("You have logged out of mys1. Click OK to close your browser window."))
	//   window.close();
	//else top.document.location.href = "http://" + top.document.location.hostname + "/";
    }	
    return false;
}

function mys1_logout_after_unsubscribe () {
    navigator.mys1eid="";
    navigator.mys1id="";
    deleteCookie("mys1_eid");
    deleteCookie("sessionID");
    if (confirm("Click OK to return to the home page.")) 
	top.document.location.href = "http://" + top.document.location.hostname + "/";
    return false;
}

function mys1_check_for_only_registered (frameName) {
    // designed for the /mys1/ default page

    var whichWindow = frameName ? frameName : 'mainContent';

    if (navigator.mys1id && ! navigator.mys1eid) // s1play uses mys1id instead of mys1eid
	navigator.mys1eid = navigator.mys1id;

    if ( (!getCookie('mys1_eid')) && navigator.mys1eid && navigator.mys1eid != 0 )
	setCookie('mys1_eid',navigator.mys1eid);

    var cook = getCookie('mys1_eid');
    if (cook) {
	parent.mainContent.document.location.href = "http://" + location.hostname + "/mys1/mys1.cgi?mys1_eid=" + escape(cook);
	parent.popups.document.location.href = "http://" + location.hostname + "/mys1/mys1_sign_in.cgi?mys1_eid=" + escape(cook);
    }
    else
	parent.popups.document.location.href = "http://" + location.hostname + "/mys1/signin.shtml";

    return false;
}

function isSignedIn (n) {
    if (n.mys1id && ! n.mys1eid) // s1play uses mys1id instead of mys1eid
	n.mys1eid = n.mys1id;

    if ( (!getCookie('mys1_eid')) && n.mys1eid && n.mys1eid != 0 )
	setCookie('mys1_eid',n.mys1eid);

    if (getCookie('mys1_eid') != 0 && getCookie('mys1_eid') != false) 
	return true;
    else 
	return false;
}

function s1jobs_mys1_unsubscribe (d) {
    var msg = "";
    msg += "Unsubscribing from mys1 means that you will no longer receive email updates";
    msg += "\n";
    msg += "about new vacancies or have access to mys1's personalisation features.";
    msg += "\n\n";
    msg += "              IN ADDITION PLEASE NOTE THE FOLLOWING:";
    msg += "\n\n";
    msg += "Unsubscribing from mys1 will remove your account on ALL of the s1 websites!";
    msg += "\n";
    msg += "Please be sure you really want to do this before clicking OK.";

    var cook = getCookie('mys1_eid');
    if (!cook) { // should be !cook // this should never be true -- have to be logged in to unsubscribe...
	alert("This action cannot be completed to mys1 due to an application error.\nPlease contact webmaster@s1now.com for assistance.");
	return false;
    }

    if (confirm(msg)) {
	var goURL = "http://" + d.location.hostname + "/mys1/unsubscribe.cgi?mys1_eid=" + escape(cook);
	d.location.href = goURL;
	return true;
    }
    return false;
}

function resetSuspend (newVal) {
    var cook = getCookie('mys1_eid');
    if (!cook) {
	alert("This action cannot be completed to mys1 due to an application error.\nPlease contact webmaster@s1now.com for assistance.");
	return false;
    }
    document.location.href = "http://" + location.hostname + "/mys1/resetSuspended.cgi?mys1_eid=" + escape(cook) + "&newVal=" + escape(newVal);
    return true;
}

function s1jobs_mys1_delete_search (d,idNo)
{
    var cook = getCookie('mys1_eid');
    if (!cook) { // should be !cook // this should never be true -- have to be logged in to unsubscribe...
	alert("This action cannot be completed to mys1 due to an application error.\nPlease contact webmaster@s1now.com for assistance.");
	return false;
    }
    d.location.href = "http://" + d.location.hostname + "/mys1/retrieve.cgi?mys1_eid=" + escape(cook) + "&a=deletesearch&delete_search=" + escape(idNo);
    return true;
}

function s1jobs_mys1_delete_job (d,idNo)
{
    var cook = getCookie('mys1_eid');
    if (!cook) { // should be !cook // this should never be true -- have to be logged in to unsubscribe...
	alert("This action cannot be completed to mys1 due to an application error.\nPlease contact webmaster@s1now.com for assistance.");
	return false;
    }
    d.location.href = "http://" + d.location.hostname + "/mys1/retrieve.cgi?mys1_eid=" + escape(cook) + "&a=deletejob&delete_job=" + escape(idNo);
    return true;
}

function s1jobs_mys1_delete_jaf_entry (d,idNo)
{
    var cook = getCookie('mys1_eid');
    if (!cook) { // should be !cook // this should never be true -- have to be logged in to unsubscribe...
	alert("This action cannot be completed to mys1 due to an application error.\nPlease contact webmaster@s1now.com for assistance.");
	return false;
    }
    d.location.href = "http://" + d.location.hostname + "/mys1/retrieve.cgi?mys1_eid=" + escape(cook) + "&a=deletejobapply&delete_jobapply=" + escape(idNo);
    return true;
}

function s1jobs_mys1_delete_article (d,idNo)
{
    var cook = getCookie('mys1_eid');
    if (!cook) { // should be !cook // this should never be true -- have to be logged in to unsubscribe...
	alert("This action cannot be completed to mys1 due to an application error.\nPlease contact webmaster@s1now.com for assistance.");
	return false;
    }
    d.location.href = "http://" + d.location.hostname + "/mys1/retrieve.cgi?mys1_eid=" + escape(cook) + "&a=deletearticle&delete_article=" + escape(idNo);
    return true;
}


