// All target paths will be relative to this top level root

var rfw_application="ColgateSmile";

// =========================== PUBLIC API ======================= //

// ------------------------------------------------------------------------------------------------------- //
// Call this from a page to tag the page to be remembered.
// Syntax: <script> rfw_remember_this_page(['/registration.srv~/home.srv','/passwordthankyouview.srv'], "Colgate Smile Studio");</script>
// arg1: array of strings to match for target (once the target page is displayed, it will redirect to the "return page"
//       if you require a sequence of pages to be invoked before returning, delimit with ~
// arg2: textual description of the page to return to (returned from rfw_get_returnlabel() see below.
// ------------------------------------------------------------------------------------------------------- //

function rfw_remember_this_page(targets, returnlabel) {
    if (targets.length > 0) {
        _rfw_savecookie(rfw_application+"remembertargets", targets.join("|"),.01);
        _rfw_savecookie(rfw_application+"rememberreturn", location.href,.01);
        _rfw_savecookie(rfw_application+"rememberreturnlabel", returnlabel,.01);
    }
}

// ------------------------------------------------------------------------------------------------------- //
// to zap any remember in place call this function
// ------------------------------------------------------------------------------------------------------- //

function rfw_clear_remember() {
    _rfw_eraseCookie(rfw_application+"remembertargets");
    _rfw_eraseCookie(rfw_application+"rememberreturn");
    _rfw_eraseCookie(rfw_application+"rememberreturnlabel");
}

// ------------------------------------------------------------------------------------------------------- //
// override these functions in your page to provide form variable saving/restoring
// ------------------------------------------------------------------------------------------------------- //

function rfw_restorestate_this_page() {
   // alert('rfw_restorestate_this_page()')
}

function rfw_savestate_this_page() {
   // alert('rfw_savestate_this_page()')
}


// ------------------------------------------------------------------------------------------------------- //
// call these methods to create a "Return to ______" link on a page elsewhere in the application
// e.g. <script type="text/javascript">document.write("<a href='"+rfw_get_return()+"'>Return to "+rfw_get_returnlabel()+"</a>");</script>
// ------------------------------------------------------------------------------------------------------- //

function rfw_get_returnlabel() {
    return _rfw_readcookie(rfw_application+"rememberreturnlabel");
}

function rfw_get_return() {
    return _rfw_readcookie(rfw_application+"rememberreturn");
}


// ================================== END PUBLIC API ================================= //

function rfw_body_onload() {
  // alert('rfw_body_onload()')
    if (_rfw_find_return(location.href)) {
      rfw_restorestate_this_page();
    }
}

function rfw_body_onunload() {
///    alert('rfw_body_onunload()')
    if (_rfw_find_return(location.href)) {
      rfw_savestate_this_page();
    }
}

function _rfw_clean_current_url() {
    var r = location.href.substring(location.href.indexOf(rfw_application)+rfw_application.length, (location.href.indexOf("?")>0?location.href.indexOf("?"):location.href.length));
//   alert ( 'location.href='+location.href+' r='+r+' location.href.indexOf(rfw_application)'+location.href.indexOf(rfw_application));
    return r;
}

// is this url the return url
function _rfw_find_return(url) {
    var r = _rfw_readcookie(rfw_application+"rememberreturn");
    return (r==url);
}

function _rfw_find_target(url) {
    var t = _rfw_readcookie(rfw_application+"remembertargets");
//   alert('findtarget url='+url);
    if (t!=null) {
        var targets = t.split("|");
        for (i=0;i<targets.length;i++) {
            // check for chaining
            chainindex = targets[i].indexOf("~")
            if (chainindex > 0) {
                turl = targets[i].substring(0,targets[i].indexOf("~"))
//                alert('targets before:'+targets[i]+" turl="+turl);
                if (turl == url) {  // remove this URL from the chain so that next time it will look for the next item in the chain.
                    targets[i] = targets[i].substring(chainindex+1);
//                    alert('targets after:'+targets[i])
                    _rfw_savecookie(rfw_application+"remembertargets", targets.join("|"),1);
                }
                return false;
            } else {
//                alert('targets[i]='+targets[i]+" url="+url);
                if (targets[i] == url) return true;
            }
        }
    }
    return false;
}


function _rfw_process_redirect() {
 //   alert('rfw_process_redirect()')
    if (_rfw_find_target(_rfw_clean_current_url())) {
 //       alert("this page is a target for remember:  redirecting to:"+rfw_get_return());
        location.href = rfw_get_return();
    }
}

function _rfw_savecookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function _rfw_readcookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function _rfw_eraseCookie(name) {
	_rfw_savecookie(name,"",-1);
}

// see if this page should redirect and do it
_rfw_process_redirect();





