/*
  This object is utilized by both front-end and back-end forms to process different
  aspects of webuser profiles.  Therefore, it is necessary for the caller to differentiate
  between which interface was used to call this object.  This is handled via the in_manager
  argument, which should be true if this object was instantiated from within the manager UI.
*/
SunergeoWebUser = new function()
{
  this.in_manager = false;
  this.processor_url = "";
  this.site_root_url = "";
  this.msg = "";
  this.profile_forms = new Array("form_general", "form_contact", "membership_form", "form_common", "edit_event_details");
  this.form_validators = new Array();
  this.cal_target_controls = new Array("dob", "event_start", "event_end");
  this.calendar_controls = new Array();

  this.create = function(in_manager, processor, msg, site_root)
  {
//alert("In manager? " + in_manager + "\r\nProcessor: " + processor + "\r\nMsg: " + msg + "\r\nSite root: " + site_root);

    this.in_manager = in_manager;
    this.processor_url = processor;
    this.msg = msg;
    this.site_root_url = site_root;

    this.createFormValidators();
    this.createCalendarControls();
  }

  this.createFormValidators = function()
  {
    this.form_validators = new Array();
    for(i = 0; i < this.profile_forms.length; i++)
    {
      if($(this.profile_forms[i]))
      {
//alert("creating form validator for " + this.profile_forms[i]);
        this.form_validators[i] = new Validation(this.profile_forms[i], {immediate:true});
      }
    }
  };

  this.createCalendarControls = function()
  {
    this.calendar_controls = new Array();
    for(i = 0; i < this.cal_target_controls.length; i++)
    {
      if($(this.cal_target_controls[i]))
      {
//alert("creating calendar control for " + this.cal_target_controls[i]);
        this.calendar_controls[i] = new calendar1($(this.cal_target_controls[i]), new Object);
        this.calendar_controls[i].path=this.site_root_url + "/manager/media/";
        this.calendar_controls[i].year_scroll = true;
        this.calendar_controls[i].time_comp = false;

//alert("Calendar target control: " + this.cal_target_controls[i]);
        if(this.cal_target_controls[i] == "event_start" || this.cal_target_controls[i] == "event_end")
        {
          this.calendar_controls[i].time_comp = true;
        }
      }
    }
  };

  this.validateForms = function()
  {
    if(this.form_validators.length == 0) this.createFormValidators();

    for(i = 0; i < this.form_validators.length; i++)
    {
      if(this.form_validators[i])
        if(!this.form_validators[i].validate()) return false;
    }

    return true;
  }

  this.calPopup = function(ctl)
  {
    if(this.calendar_controls.length == 0) this.createCalendarControls();

    for(i = 0; i < this.cal_target_controls.length; i++)
    {
      if(this.cal_target_controls[i] == ctl)
      {
//alert($(ctl));
        $(ctl).addClassName('input-field-dirty');;
        this.calendar_controls[i].popup();
      }
    }

    return true;
  }

  this.submitEmploymentAffidavit = function(profileId, letterId, months)
  {
//ui_utils.showActivityFeedback('emp_affidavit_form', this.msg);
    var pars = "command=submit_emp_affidavit" +
               "&profileId=" + profileId +
               "&letterid=" + letterId +
               "&months=" + months;

    var ajax = new Ajax.Updater(
      {success: 'wlpeUser', failure: 'msg'},
      this.processor_url,
			{
				method: "get",
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback('', 'msg'); },
        onComplete: function(){ ui_utils.hideActivityFeedback('', 'msg'); document.location.href = "index.php" }
			});
  }

  this.addCeuDoc = function(ceuId, url, filename, desc)
	{
    var pars = "command=addCeuDoc&ceuid=" + ceuId + "&filename=" + filename + "&url=" + url + "&desc=" + desc;

    var ajax = new Ajax.Updater(
      {success: 'wlpeCeuDocTable', failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback("form_ceu", "msg"); },
        onComplete: function(){ ui_utils.hideActivityFeedback("form_ceu", "msg"); }
			});
	},

  this.deleteceudoc = function(id)
	{
		if(!confirm("Are you sure you want to delete this document?")) return;

    var pars = "command=deleteCeuDoc" + "&id=" + id;

    var ajax = new Ajax.Updater(
      {success: 'wlpeCeuDocTable', failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback("form_ceu", "msg"); },
        onComplete: function(){ ui_utils.hideActivityFeedback("form_ceu", "msg"); }
			});
  }

  this.addceu = function(letterId, ceu_date, ceu_location, ceu_event, ceu_points, ceu_points_noncore, ceu_desc)
	{
    var pars = "command=addceu&letterid=" + letterId + "&ceu_date=" + ceu_date + "&ceu_location=" + ceu_location +
               "&ceu_event=" + ceu_event + "&ceu_points=" + ceu_points + "&ceu_points_noncore=" + ceu_points_noncore +
               "&ceu_desc=" + ceu_desc + "&in_manager=" + this.in_manager;

    var ajax = new Ajax.Updater(
      {success: 'ceu_controls', failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback("form_ceu", "msg"); },
        onComplete: function()
        {
          ui_utils.hideActivityFeedback("form_ceu", "msg");
          $(success_div).style.display="";
          var objValidateCeuForm = new Validation('form_ceu', {immediate:true});

          //Create calendar object...
          calCEU = new calendar1($('ceu_date'), new Object);
          if(this.in_manager) calCEU.path="media/";
          else calCEU.path = "manager/media/";
          calCEU.year_scroll = true;
          calCEU.time_comp = false;
        }
			});
  }

  this.updateceu = function(ceuid, ceu_date, ceu_location, ceu_event, ceu_points, ceu_points_noncore, ceu_desc)
	{
    var pars = "command=updateceu&ceuid=" + ceuid + "&ceu_date=" + ceu_date + "&ceu_location=" + ceu_location +
               "&ceu_event=" + ceu_event + "&ceu_points=" + ceu_points + "&ceu_points_noncore=" + ceu_points_noncore +
               "&ceu_desc=" + ceu_desc + "&in_manager=" + this.in_manager;

    var ajax = new Ajax.Updater(
      {success: 'ceu_controls', failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback("form_ceu", "msg"); },
        onComplete: function(transport)
        {
          ui_utils.hideActivityFeedback("form_ceu", "msg");
          SunergeoWebUser.updateCeuTable($('profileId').value, $('ceu_point_letters').value, "ceu_date");
          hideCeuCtrls();
          showCeuTable();
        }
			});
  }

  this.deleteceu = function(ceuid)
	{
		if(!confirm("Are you sure you want to delete this CEU?")) return;

    var pars = "command=deleteceu&ceuid=" + ceuid + "&in_manager=" + this.in_manager;

    var ajax = new Ajax.Updater(
      {success: 'ceu_edit_section', failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback("form_ceu", "msg"); },
        onComplete: function(transport){ ui_utils.hideActivityFeedback("form_ceu", "msg"); }
			});
  }

  this.submitceu = function(ceuid)
	{
    var pars = "command=submitceu&ceuid=" + ceuid + "&in_manager=" + this.in_manager;

    var ajax = new Ajax.Updater(
      {success: 'ceu_edit_section', failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback("", "msg"); },
        onComplete: function(transport)
        {
          SunergeoWebUser.updateCeuTable($('profileId').value, $('ceu_point_letters').value, "ceu_date");
          ui_utils.hideActivityFeedback("", "msg");
        }
			});
  }

  this.updateCeuTable = function(profileId, letterId, which)
	{
    var pars = "command=getceutable&profileid=" + profileId + "&letterid=" + letterId + "&in_manager=" + this.in_manager + "&which=" + which;

    var ajax = new Ajax.Updater(
      {success: 'ceu_edit_section', failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        onLoading: function(){ ui_utils.showActivityFeedback('', 'msg'); },
        onComplete: function(){ ui_utils.hideActivityFeedback('', 'msg'); }
			});
	},

  this.getCeuEditForm = function(ceuid)
	{
    var pars = "command=getCeuEditForm&ceuid=" + ceuid + "&in_manager=" + this.in_manager;

    var ajax = new Ajax.Updater(
      {success: 'ceu_controls', failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        onLoading: function(){ ui_utils.showActivityFeedback('', 'msg'); },
        onComplete: function()
        {
          ui_utils.hideActivityFeedback('', 'msg');
          var objValidateCeuForm = new Validation('form_ceu', {immediate:true});

          //Create calendar object...
          calCEU = new calendar1($('ceu_date'), new Object);
          if(this.in_manager) calCEU.path="media/";
          else calCEU.path = "manager/media/";
          calCEU.year_scroll = true;
          calCEU.time_comp = false;

          $('ceu_controls').style.display = "";
        }
			});
  }

  this.addHistoricalPtLetter = function(userid)
	{
    if(!this.in_manager) return alert("This can only be done from manager");

    var pars = "command=addHistoricalPtLetter&profileid=" + userid;

    var ajax = new Ajax.Updater(
      {success: 'ceu_pt_letter_list', failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback('', 'msg'); },
        onComplete: function(){ ui_utils.hideActivityFeedback('', 'msg'); }
			});
  }

  this.closePtLetter = function(letterId)
	{
    if(!confirm("You cannot add points to this letter once it is closed.  Are you sure you want to close this letter?")) return;

    var pars = "command=closePtLetter&id=" + letterId;

    var ajax = new Ajax.Updater(
      {success: 'ceu_pt_letter_list', failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        onLoading: function(){ ui_utils.showActivityFeedback('', 'msg'); },
        onComplete: function(){ ui_utils.hideActivityFeedback('', 'msg'); }
			});
  }

  this.openPtLetter = function(letterId)
	{
    var pars = "command=openPtLetter&id=" + letterId;

    var ajax = new Ajax.Updater(
      {success: 'ceu_pt_letter_list', failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        onLoading: function(){ ui_utils.showActivityFeedback('', 'msg'); },
        onComplete: function(){ ui_utils.hideActivityFeedback('', 'msg'); }
			});
  }

  this.viewdocs = function(ceuid)
	{
    var pars = "command=getCeuDocLinks&ceuid=" + ceuid;

    var ajax = new Ajax.Updater(
      {success: 'view_docs_list_' + ceuid, failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        onLoading: function(){ ui_utils.showActivityFeedback('', 'msg'); },
        onComplete: function(){ ui_utils.hideActivityFeedback('', 'msg'); }
			});
  }

  this.updateMemberPhoto = function(id, file)
	{
		var pars = "command=updateMemberPhoto" +
               "&profileid=" + id +
               "&file1=" + file
							 ;

    var ajax = new Ajax.Updater(
      {success: 'memberPhoto', failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        onLoading: function(){ ui_utils.showActivityFeedback('', 'msg'); },
        onComplete: function(){ ui_utils.hideActivityFeedback('', 'msg'); }
			});
  }

  this.ResetDefaultPhoto = function(id)
  {
    var pars = "command=resetDefaultPhoto" +
               "&profileid=" + id
							 ;

    var ajax = new Ajax.Updater(
      {success: 'memberPhoto', failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        onLoading: function(){ ui_utils.showActivityFeedback('', 'msg'); },
        onComplete: function(){ ui_utils.hideActivityFeedback('', 'msg'); }
			});
  }

  this.updateCompanyList = function(userId, companyId)
	{
    var pars = "command=get_company_list&profileId=" + userId + "&companyId=" + companyId + "&in_manager=" + this.in_manager;
    var ajax = new Ajax.Updater(
      {success: 'EmployerSiteDiv', failure: 'msgArea'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback('', 'msg'); $('cbCompany').disabled = true; },
        onComplete: function(){ui_utils.hideActivityFeedback('', 'msg');$('cbCompany').disabled = false; }
			});
  }

  this.addCompany = function(userId)
  {
    var pars = "command=add_company&profileId=" + userId + "&in_manager=" + this.in_manager;

    frm = "form_add_company";
    Form.getElements($(frm)).each(function(elm){ if(elm.id != "wlpeAddCompany" && elm.id != "wlpeCancel") pars = pars + "&" + elm.id + "=" + encodeURIComponent(escape(elm.value)); });

    var ajax = new Ajax.Request(
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback(frm, 'msg'); },
        onComplete: function(){ui_utils.hideActivityFeedback(frm, 'msg'); cancelEmployerEdit(userId); }
			});
  }

  this.updateCompany = function(userId, companyId)
  {
    var pars = "command=update_company&profileId=" +  userId + "&companyId=" + companyId + "&in_manager=" + this.in_manager;

    frm = "form_edit_company";
    Form.getElements($(frm)).each(function(elm){ if(elm.id != "wlpeUpdateCompany" && elm.id != "wlpeCancel") pars = pars + "&" + elm.id + "=" + encodeURIComponent(escape(elm.value)); });

    var ajax = new Ajax.Request(
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback(frm, 'msg'); },
        onComplete: function(){ui_utils.hideActivityFeedback(frm, 'msg'); cancelEmployerEdit(userId); }
			});
  }

  this.addSite = function(userId, companyId, siteId)
  {
    var pars = "command=add_company_site&profileId=" + userId + "&companyId=" + companyId + "&in_manager=" + this.in_manager;

    frm = "form_edit_site_0";
    Form.getElements($(frm)).each(function(elm){ if(elm.id != "wlpeAddCompanySite" && elm.id != "wlpeCancel") pars = pars + "&" + elm.id + "=" + encodeURIComponent(escape(elm.value)); });

    var ajax = new Ajax.Request(
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback(frm, 'msg'); },
        onComplete: function(){ui_utils.hideActivityFeedback(frm, 'msg'); cancelEmployerEdit(userId); }
			});
  }

  this.updateSite = function(userId, companyId, siteId)
  {
    var pars = "command=update_company_site&profileId=" + userId + "&companyId=" + companyId + "&siteId=" + siteId + "&in_manager=" + this.in_manager;

    frm = "form_edit_site_" + siteId;
    Form.getElements($(frm)).each(function(elm){ if(elm.id != "wlpeUpdateCompanySite" && elm.id != "wlpeCancel") pars = pars + "&" + elm.id + "=" + encodeURIComponent(escape(elm.value)); });

    var ajax = new Ajax.Request(
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback(frm, 'msg'); },
        onComplete: function(){ui_utils.hideActivityFeedback(frm, 'msg'); cancelEmployerEdit(userId); }
			});
  }

/*
  this.updateCompanySiteList = function(companyId, userId)
	{
    var pars = "command=get_company_sites&companyId=" + companyId + "&userId=" + userId;

    var ajax = new Ajax.Updater(
      {success: 'cbCompanySiteDiv', failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback('', 'msg'); $('cbSite').disabled = true; },
        onComplete: function(){ui_utils.hideActivityFeedback('', 'msg'); $('cbSite').disabled = false; }
			});
  }

  this.updateSiteContactInfo = function()
	{
    var site = $('cbSite');
  	if(site.selectedIndex >= 0) siteId = site.options[site.selectedIndex].value;

    var pars = "command=get_site_info&siteid=" + siteId;

    var ajax = new Ajax.Updater(
      {success: 'SiteContactDiv', failure: 'msg'},
      this.processor_url,
			{
        method: 'get',
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback('', 'msg'); },
        onComplete: function(){ ui_utils.hideActivityFeedback('', 'msg'); }
			});
  }
*/

  this.ValidateUserName = function(profileId, txtUsername)
	{
		var pars = "command=validate_username&" +
							 "profileid=" + profileId +
               "&fld=" + txtUsername.id +
							 "&username=" + txtUsername.value
							 ;

    var ajax = new Ajax.Updater(
      {success: 'ValidateUsernameResponse', failure: 'msg'},
      this.processor_url,
			{
				method: "get",
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback('', 'msg'); },
        onComplete: function(){ ui_utils.hideActivityFeedback('', 'msg'); }
			});
  }

  this.getProfileForm = function(userId)
  {
    var pars = "command=get_profile_form&profileId=" + userId + "&in_manager=" + this.in_manager;
    var ajax = new Ajax.Updater(
      {success: 'wlpeUser', failure: 'msg'},
      this.processor_url,
			{
				method: "get",
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback('', 'msg'); },
        onComplete: function(){ ui_utils.hideActivityFeedback('', 'msg'); }
			});
  }

  this.getDirtyFields = function()
  {
    var arFields = new Array();
    for(i = 0; i < this.form_validators.length; i++)
    {
      var obj = this.form_validators[i];
      if(obj)
      {
        if(!obj.validate()) return "";
        ar = obj.getDirtyControls();

        for(j = 0; j < ar.length; j++)
        {
          arFields.push(ar[j]);
        }
      }
    }

    var str = "";
    for(i = 0; i < arFields.length; i++)
    {
      var fldname = "" + arFields[i];
      if(fldname != "")
      {
        if($F(fldname) == "")
          str += "&" + fldname + "=null";
        else
          str += "&" + fldname + "=" + encodeURIComponent(escape($F(fldname)));
      }
    }

    //These fields requires separate handling...
    if($('cbSite') && $('cbSite').hasClassName('input-field-dirty')) str = str + "&employerSiteId=" + $('cbSite').value;

    return str;
  }

  this.createProfile = function()
	{
    str = this.getDirtyFields();
    alert("Dirty controls: \r\n" + str);
    return;

    var pars = "command=createprofile&in_manager=" + this.in_manager +
               "&password=" + $('wlpeUserProfilePassword').value + str;

    var ajax = new Ajax.Updater(
      {success: 'wlpeUser', failure: 'msg'},
      this.processor_url,
			{
				method: "get",
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback('controls', 'msg'); },
        onComplete: function(){ ui_utils.hideActivityFeedback('controls', 'msg'); }
			});
/*
		feedback = fbDiv;
		formname = f;

    ui_utils.showActivityFeedback(formname, feedback);

		var ajax = new Ajax.Request(
      this.processor_url,
			{
				method: "get",
				parameters: pars,
				onFailure: web_user.reportCreationError,
				onComplete: web_user.showCreationCheckResult
			});

	showCreationCheckResult: function(originalRequest)
	{
    window.setTimeout("ui_utils.hideActivityFeedback(formname, feedback)", 400);

		//alert(originalRequest.responseXML);
		//var xml = (new DOMParser()).parseFromString(originalRequest.responseText, "text/xml");
		xml = originalRequest.responseXML;

		//alert(xml);
		var nodes = xml.getElementsByTagName("status");
		var stat = nodes[0].firstChild;
		var profileId = stat.nextSibling;
		if(stat.nodeName == "failure")
		{
			$("msgArea").innerHTML = stat.firstChild.nodeValue;
			return;
		}

		$('newprofileid').value = profileId.firstChild.nodeValue;
		$('firstname').value = profileId.firstChild.nodeValue;
		documentDirty=false;
	},

*/
  }

  this.updateProfile = function(profileId, frm)
	{
		if($('wlpeUserProfilePassword') && $('wlpeUserProfilePassword').value != "")
		{
			if($('wlpeUserProfilePassword').value != $('wlpeUserProfilePasswordConfirm').value)
			{
				alert("Password and password confirmation do not match - please retype your password and password confirmation to ensure they are correct.");
				$('wlpeUserProfilePassword').focus();
				return;
			}
		}
    //alert("Dirty controls: \r\n" + this.getDirtyFields());

    var flds = this.getDirtyFields();
    if(flds == "") return;
    var pars = "command=updateprofile&profileid=" + profileId + "&in_manager=" + this.in_manager + flds;

    var ajax = new Ajax.Updater(
      {success: 'wlpeUser', failure: 'msg'},
      this.processor_url,
			{
				method: "get",
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback('controls', 'msg'); },
        onComplete: function(){ ui_utils.hideActivityFeedback('controls', 'msg'); }
			});
/*

		web_user.formname = frm;
		$("msgArea").style.display="none";
    ui_utils.showActivityFeedback($(web_user.formname), $(web_user.msg));

		var pars = "command=updateprofile&" +
							 "profileid=" + profileId +
							 web_user.getFormParams();

		var ajax = new Ajax.Request(
      this.processor_url,
			{
				method: "get",
				parameters: pars,
				onFailure: web_user.reportError,
				onComplete: web_user.showUpdateProfileResult
			});

  },
	showUpdateProfileResult: function(originalRequest)
	{
    window.setTimeout("ui_utils.hideActivityFeedback($(web_user.formname), $(web_user.msg))", 1500);

		if($('firstname').value == '') $('firstname').focus();
		else if($('lastname').value == '') $('lastname').focus();

		//alert(originalRequest.responseXML);
		//var xml = (new DOMParser()).parseFromString(originalRequest.responseText, "text/xml");
		xml = originalRequest.responseXML;
		var nodes = xml.getElementsByTagName("status");

		//alert(xml);

		var nodes = xml.getElementsByTagName("status");
		var stat = nodes[0].firstChild;
		if(stat.nodeName == "failure")
		{
			$("msgArea").innerHTML = stat.firstChild.nodeValue;
		}
		else documentDirty=false;
	},
*/
  }

  this.deleteProfile = function(profileId)
	{
    var pars = "command=deleteprofile&profileid=" + profileId + "&in_manager=" + this.in_manager;

    var ajax = new Ajax.Updater(
      {success: 'wlpeUser', failure: 'msg'},
      this.processor_url,
			{
				method: "get",
				parameters: pars,
        evalScripts: true,
        onLoading: function(){ ui_utils.showActivityFeedback('controls', 'msg'); },
        onComplete: function(){ ui_utils.hideActivityFeedback('controls', 'msg'); }
			});
  }

  // Cookie handling
  this.setCookie = function(sName, sValue, nDays) {
    var expires = "";
    if ( nDays ) {
      var d = new Date();
      d.setTime( d.getTime() + nDays * 24 * 60 * 60 * 1000 );
      expires = "; expires=" + d.toGMTString();
    }

    document.cookie = sName + "=" + sValue + expires + "; path=/";
  };

  this.getCookie = function(sName) {
    var re = new RegExp( "(\;|^)[^;]*(" + sName + ")\=([^;]*)(;|$)" );
    var res = re.exec( document.cookie );
    return res != null ? res[3] : null;
  };

  return this;
}
