var share_window
var code = ""
var pass = ""
var cust_share = ""
var cust_aff = ""
var cust_start = ""
var cust_type = ""
var promo_code = ""
var cntr = 0
var hold_chr = ""
var string_test = 0
var cookie_domain = ""
var cookie_domain_string = ""

cookie_domain_string = String(document.location.host)
if(cookie_domain_string.indexOf(".") < cookie_domain_string.lastIndexOf("."))
	{
	hold_chr = cookie_domain_string.substr(0,cookie_domain_string.lastIndexOf("."))
	cntr = hold_chr.lastIndexOf(".")
	cookie_domain = cookie_domain_string.slice(cntr+1)
	hold_chr = ""
	cntr = 0
	}
else
	{
	cookie_domain = ""
	}
// alert('host is "' + cookie_domain_string + '"\ndomain for cookie is "' + cookie_domain + '"')

//this is for popup window loading. 
function loadpage(what_page)
{
window.open(what_page,"HomeMovie") 
}

function getCookieVal (offset) 
{
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) 
{
 var arg = name + "=";
 var alen = arg.length;
 var clen = document.cookie.length;
 var i = 0;
 while (i < clen) 
   {
    var j = i + alen;
     if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
     i = document.cookie.indexOf(" ", i) + 1;
     if (i == 0) break; 
   }
 return null;
}

function logout(goto_login)
{
try
	{
	var folder=""
	if(String(document.location.pathname).indexOf("partners") > -1)
		{
		folder = "/partners"
		}
	else
		{
		folder = partner_folder
		}
	if(GetCookie("sb_exe") == "loaded")
		{
		set_special_cookie ("sb_exe",'',-1)
		document.location = "/sb_web/stashexelogin.htm"
		}
	else
		{
		if(goto_login==1)
			{
			document.location.href = "index.stm?login"
			}
		else
			{
			//document.location.href = "index.stm"
			document.location.href = (document.location.protocol + "//" + document.location.host + folder + "/index.stm")
			}
		}
	}
catch(err)
	{
	return false
	}
/*
set_special_cookie("homemovie","",-1)
set_special_cookie("vs_guest","",-1)
set_special_cookie("tracker","",-1)
set_special_cookie("af_open","",-1)
set_special_cookie("sb_ver","",-1)
set_special_cookie("partner","",-1)
*/
delete_all_cookies()
}

function parse_cookie()
{
 var cntr = 0
 var cntr1 = 0
 hold_chr = ""
 if(GetCookie("homemovie"))
   {
    hold_chr = new String(GetCookie("homemovie"))
   }
 
 if(hold_chr.length > 0)
   {
    code = ""
    pass = ""
    cust_type = "" 
    
    var string_sort = hold_chr.split('~')
	
 	code = string_sort[1]
 	pass = string_sort[2]
 	cust_type = string_sort[3]
 	
 	if (code.length != 10)
 		{
	    code = ""
	    pass = ""
	    cust_type = "" 
		set_special_cookie("homemovie","",-1)
		set_special_cookie("vs_guest","",-1)
		set_special_cookie("tracker","",-1)
		set_special_cookie("af_open","",-1)
 		return
 		}
	}
}

function FixCookieDate (date) 
{
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}

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" : "");
}


function delete_all_cookies(path)
{
/******************************************************************************
delete_all_cookies deletes all cookies matching the specified path.  If no path
is supplied, the document's default cookie path is used.
******************************************************************************/
// Get cookie string and separate into individual cookie phrases:
var get_cookie_string = "" + document.cookie;
var get_cookie_array = get_cookie_string.split("; ");

// Try to delete each cookie:
for (var cntr=0;cntr<get_cookie_array.length;++cntr)
	{
	var single_cookie = get_cookie_array[cntr].split("=");
	if (single_cookie.length != 2)continue;
	var cookie_name = unescape(single_cookie[0]);
	set_special_cookie(cookie_name,"",-1)
	}
}

var win_check_timer = 0
function popup_blocked()
{
if(share_window == null)
	{
	if(document.getElementById("load_share_player_window"))
		{
		document.getElementById("load_share_player_window").style.visibility = "visible"
		}
	else
		{
		alert("Grrrrr! a popup blocker appears to be preventing stashSpace from opening the share page.  If you have a popup blocker try disabling it to open your share.")
		}
	}
else
	{
	share_window.focus()
	}
window.clearInterval(win_check_timer);
win_check_timer = 0
}



function update_cookie()
{
 // done at load - parse_cookie()
// hold_chr = (code+"~"+pass+"~"+cust_type+"~")
 hold_chr = ("~"+code+"~"+pass+"~"+cust_type+"~")
 var expdate = new Date ();
 FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
 expdate.setTime (expdate.getTime() + (365 * 24 * 60 * 60 * 1000)); // 1 year from now 
 if(hold_chr.length > 4)
   {
    //SetCookie ("homemovie", "hmc", expdate);
    SetCookie ("homemovie", hold_chr, expdate, "/", cookie_domain, "");
   }
}


function set_special_cookie(cookie_name,cookie_value,how_long)
{

 var expdate = new Date ();
 FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
 expdate.setTime (expdate.getTime() + (how_long * 24 * 60 * 60 * 1000)); // 1 year from now 
  SetCookie (cookie_name,cookie_value, expdate, "/", cookie_domain, "");
  if(how_long == -1)
  	{
	SetCookie (cookie_name,cookie_value, expdate, "/", "", "");
  	}

}
hold_chr = new String(document.location.search)

var options = 'dependent=no,status=no,menubar=no,scrollbars=no,resizable=no,toolbar=no,width=775,height=735';
var win_url = "/my-stash/shares.stm" + hold_chr
if(top && String(top.document.location).indexOf("stashspace.com/index.stm") != -1)
	{
	document.write('<div id=\"load_share_player_window\" style=\"position: absolute; width: 745px; height: 306px; z-index: 805; visibility: hidden; left: 25px; top: 50px; overflow-x:hidden; overflow-y:hidden\">')
	if(String(top.document.location).indexOf("~S~") != -1)
		{
		document.write('<table id= \"share_table\" class = "timeline" onClick =\"javascript: share_window = window.open('+'\''+win_url+'\''+ ',\''+"share_window"+'\''+ ',\''+options+'\''+ ');document.getElementById(\'load_share_player_window\').style.visibility=\'hidden\';win_check_timer = window.setInterval(\'popup_blocked()\',1000)\" background=\"../images/load_album_share.gif\" width=\"748\" height=\"305\">')		
		}
	else
		{
		document.write('<table id= \"share_table\" class = "timeline" onClick =\"javascript: share_window = window.open('+'\''+win_url+'\''+ ',\''+"share_window"+'\''+ ',\''+options+'\''+ ');document.getElementById(\'load_share_player_window\').style.visibility=\'hidden\';win_check_timer = window.setInterval(\'popup_blocked()\',1000)\" background=\"../images/load_movie_share.gif\" width=\"748\" height=\"305\">')		
		}	
	document.write('<tr><td>')
	//document.write('Load your <a href="#" ">stashBox</a>')
	document.write('</td></tr></table></div>')
	}

//var exists = 0
//var my_time = window.setInterval("where_cust()",1000) 

if(hold_chr.length > 0)
	{
	var string_sort = (hold_chr.replace("?","")).split('~')

	if (string_sort.length == 3)
		{
		 if (string_sort[0].length == 10 && isFinite(string_sort[0]))
		 	{
		 	code = string_sort[0]
		 	pass = string_sort[1]
		 	cust_type = string_sort[2]
			if(code == "" || pass == "")
				{
				alert("There is something wrong with your login & password.  Please call stashSpace customer service at (877) 576-8777.")
				}
			else
				{
				update_cookie()
				}
		 	}
		 }
	else if (string_sort.length > 1)
		{
		if (string_sort[0] == "share")
			{
			if(string_sort[1] != "")
				{
				if(partner == "")
					{
					if(document.location.pathname == "/index.stm")
						{
						if(!GetCookie("homemovie"))
							{
							set_special_cookie("vs_guest",string_sort[1],365)
							}
						//open a new window	
						try
							{
							if(share_window == null)
								{
								share_window = window.open(win_url,'share_window',options)
								}
							else if(share_window.closed == false)
								{
								share_window.focus()	
								}
							else
								{
								share_window = window.open(win_url,'share_window',options)
								}
							}
						catch(err)
							{
							share_window = window.open(win_url,'share_window',options)
							}
						if(share_window)share_window.focus()
						win_check_timer = window.setInterval("popup_blocked()",1000)
						//javascript: alert(share_window)
						//http://dev.stashspace.com/index.stm?share~6ds30h60@0~d~5099963163_1213412429075
						//document.location = "/my-stash/shares.stm" + hold_chr
						//http://dev.stashspace.com/index.stm?share~6ds30h60@0~d~5099963163_1209669811665
						}
					}
				else
					{
					if(document.location.pathname == partner_folder + "/index.stm")
						{
						if(!GetCookie("homemovie"))
							{
							set_special_cookie("vs_guest",string_sort[1],365)
							}
						document.location = partner_folder + "/my-stash.stm" + hold_chr
						}
					}
				}
			}
		else if (string_sort[1].length == 10 && isFinite(string_sort[1]))
			{
			code = string_sort[1]
			pass = string_sort[2]
			cust_type = string_sort[3]
			if(code == "" || pass == "")
				{
				alert("There is something wrong with your login & password.  Please call stashSpace customer service at (877) 576-8777.")
				}
			else
				{
				update_cookie()
				}
			}
		}
	}
else
	{
	parse_cookie()
	if(code == "" || pass == "")
		{
		//       var href = ("http://www.homemovie.com/vp3_badlogin.htm")
		//     top.location = href
		}
	else
		{
		if(code!="0101010101")
			{
			update_cookie()
			}
		}
	}

