// registration.js - routines to enable class registration from
// alphacollegeofrealestate.com, using IMS.
// 03/18/2008, 05/16/2008, 06/08/2009

// setCookie - sets the name/value pair of a cookie
function setCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

// GetCookie loads the cookie and finds the name/data pair requested 
function getCookie(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;
}

// deleteCookie sets the expire time for the cookie in the past, deleting the cookie
function deleteCookie(name){
	var twoHours =  1800*1000;
	var expDate = new Date();
	expDate.setTime(expDate.getTime() - twoHours);
	setCookie(name,null,expDate,"/",null,false);
}

// SaveCurrentLocation sets the URL in the cookie
function SaveCurrentLocation(){
	var twoHours =  1800*1000;
	var expDate = new Date();
	expDate.setTime(expDate.getTime() + twoHours);
	setCookie("Calling_Page",location.href,expDate,"/",null,false);
}

// LoadCart - 09/18/11 - Creates and executes the URL to view the policy,
// 	    then add the class to the cart.
function LoadCart($ClassCode, $policy) {
   $classreg ='http://www.alphacollegeofrealestate.com/';
   if (typeof $policy == 'undefined' || $policy=="")
	$classreg=$classreg + 'shoppingcart/shopping_cart.php' + '?ClassID=' + $ClassCode;
	else
	$classreg=$classreg + 'policies/' + $policy + '_policy.htm' + '?sl=' + $policy + '&ClassID=' + $ClassCode;
   SaveCurrentLocation();
   location.href = $classreg;
}

// ClassLink - Creates and executes the URL to view the policy, and then
//	       register in IMS for the class.
function ClassLink($ClassCode, $policy) {
   if (typeof $policy == 'undefined' )
      $policy = 'no';
   $classreg ='http://www.alphacollegeofrealestate.com/policies/policy_router.htm'
	+ '?sl='
	+ Extract($policy)
	+ '&ClassID='
	+ $ClassCode;
   SaveCurrentLocation();
   location.href = $classreg;
}

// LinkTo - 06/08/09 - Creates and executes the URL to view the policy,
// 	    then register for the class.
function LinkTo($ClassCode, $policy) {
   if (typeof $policy == 'undefined')
      LoadCourse($ClassCode);
   $classreg ='http://www.alphacollegeofrealestate.com/policies/'
	+ $policy
	+ '.html'
	+ '?sl='
	+ $policy
	+ '&ClassID='
	+ $ClassCode;
   location.href = $classreg;
}

// LoadCourse creates and executes the href link to register for the class.
function LoadCourse($ClassCode) {
   $classreg ='https://www.internetmemberservices.com/scripts/mgrqispi.dll'
	+ '?APPNAME=IMS'
	+ '&PRGNAME=IMSMenuDirectCall'
	+ '&ARGUMENTS=-AALPH'
	+ '&SessionType=N'
	+ '&ServiceName=REGE'
	+ '&ClassID='
	+ $ClassCode;
   location.href = $classreg;
}

// RegClass - 06/08/09 - Loads the url to register for the class
function RegClass($provider) {
	var IDCode = gup("ClassID");
	LoadCourse(IDCode);
}

// Extract - Extracts the source directory/page from the calling web page URL
//	     Used to identify the course policy to display
function Extract($policy) {
	if ($policy == "none")
	   return "no";
	if ($policy != "no")
	   return $policy;
	var the_url = window.location.href;
	var first_split = the_url.split("//");
	var without_resource = first_split[1];
	var second_split = without_resource.split("/");
	var domain = second_split[0];
	var directory = second_split[1];
	if (directory == "" || directory.lastIndexOf(".php") > 0 ||directory == "index.php")
	     return "no";
	var page = second_split[2];
	var pagename = page.split(".");
	if (directory == "designations")
	     return pagename[0];
	else
	     return directory;
}

// gup - "Get URL Parameter" - returns the value for the specified URL string parameter
function gup( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return results;
  else
    return results[1];
}

// TrafficCop - process which decides how to route the user when returning from IMS
function TrafficCop() {
	var route_id = gup("backto");
	var calling_page = getCookie("Calling_Page");
	deleteCookie("Calling_Page");
	$cp = unescape(calling_page);
	if( route_id == null || calling_page == null)
		$cp="http://www.alphacollegeofrealestate.com/";
	location.href = $cp;
}

