﻿function read_search(cname) {
        var cn = cname + "=";
        var allsearch = location.search.substring(1);
        var pos = allsearch.indexOf(cn);
        if (pos != -1) {
                var start = pos + cn.length;
                var end = allsearch.indexOf("&", start);
                if (end == -1) end = allsearch.length;
                var value = allsearch.substring(start, end);
                value = unescape(value);
                return value;
        }
        return "";
}
function getCookie(c_name){
  if (document.cookie.length>0){
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1){ 
      c_start=c_start + c_name.length+1; 
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
  return "";
}
function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getName(c_name) {
    if (c_name.length>0) {
       c_start=c_name.lastIndexOf('/');
       c_end=c_name.indexOf(".aspx");
       if (c_end==-1) c_end=c_name.length;
       return c_name.substring(c_start + 1,c_end);
    }
    return "";         
}
// see if  have a redirect, if so, should redirect?
var cookieName = read_search("redirect");

if (cookieName != "" && cookieName != null && cookieName != undefined) {
    // check for cookie to permit forwarding
    var newCookieName = getName(cookieName);
	var hasCookie = getCookie(newCookieName);
	if (hasCookie != "" && hasCookie != null & hasCookie != undefined) {
		var newLocation = cookieName;
		if (newLocation.indexOf(".aspx") ==-1) newLocation += ".aspx";
  		window.location = newLocation;
  	}
}

// see if in protected directory
var protected = "/Resources/";
var fromForm = "formfilled";
var formLocation = "../../Pages/RegistrationForm.aspx";
var myPath = location.pathname;
if (myPath.indexOf(protected) > 0) {
   // we are protected, check to see if cookie is set
   var cookieName = getName(myPath);
   var myCookie = getCookie(cookieName);
   if (myCookie == "") {
      // no cookie, check to see if comming from form
      var isFormed = read_search(fromForm);
      if (isFormed == "") {
      	 // not from form, redirect to form
      	 formLocation += "?redirect=" + cookieName;
      	 //alert("redirecting to " + formLocation);
      	 window.location = formLocation;
      }
      else {
         setCookie(cookieName, "Formfilled", 3650);
         //alert("setting cookie " + cookieName);
      }	
   }   
}
//alert ("all scripts go");


function display_path() {
        var mypath = location.pathname;
        var newpath2 = escape(mypath);
        newpath2 = newpath2.replace(/\//g, '%2F');
        return newpath2;
}
