//Main Setup
if (!datafilepath) {
	
	var datafilepath = "http://www.sethleedy.name/"; // Make sure a slash is on the end!
}

var temp=window.location.href;
var filenameresult = getfilename(temp);

var htmlout = "";
var n=navigator.appName;
var ns=(n=="Netscape");
var ie=(n=="Microsoft Internet Explorer");
var pathanfile = "", whichdivglobal = 0, checkieopen;
var idx = document.URL.indexOf('?');
var params = new Array();
var nameVal = new Array();
var menu1width = 100;
var gopage = "";
var checktaginterval = "";
var filesopened = 0;
var error_code = "";

// For PayPal Cart Items Count
//var pcartcount = readCookie("blackcreek_pcartcount");
//End Vars

// test what the script sees the browser as
//alert(n);

if (!ns && !ie) {

 ns = true;

}

// Do startup routines
runstartup();

// Any URL parsing ?
if (idx != -1) {

 var pairs = document.URL.substring(idx+1,document.URL.length).split('&');
 var i;
 
 for (i=0; i < pairs.length; i++) {

  nameVal = pairs[i].split('=');
  params[i] = nameVal[i + 1];

 }

}

if (params[0] != null) {

 var gopage = "";

 gopage = unescape(params[0]);
 gopage = datafilepath + gopage;

// status = test_file_exists(gopage);
// if (status == true) {

  loadpages(gopage,0);

// }
}

//End Setup

//Main Loop
function loadpages(pathanfile,whichdiv) {

iinc = 0;
filesopened = 1;
whichdivglobal = whichdiv;
datafile = pathanfile;

if (datafile == "") {
 return;
}

if (ns) {
 if (!nsopen()) {
  //alert("Bad File Name");
 }
 
 // Write to Div Tag
 writecode();
} else {
 ieopen();

 do {
 	iinc=iinc+1; //Waiting for IE Page to load
	//document.getElementById('debug').innerHTML = iinc;
 } while (waitonie() == false || iinc <= 100)

   // Write to Div Tag
   if (!htmlout) {alert("Bad File Name");}
   writecode();

} // End If of if (ns)


filesopened = 0;
return;

} // End loadpages()

function mempages(pathanfile) {

iinc = 0;
filesopened = 1;
datafile = pathanfile;

if (pathanfile == "") {
 return;
}

if (ns) {
 nsopen();

} else {
 ieopen();

 do {
 
    iinc=iinc+1;
 
 //Waiting for IE Page to load
 } while (waitonie() == false || iinc <= 500)
 
} // End If of if (ns)

filesopened = 0;
return htmlout;

} // End mempages()     for the weird reason that if I want a return var in loadpages it will update the screen to show it.

function waitonie() {
 if (objXml.readyState == 4) {
	clearTimeout(checkieopen);
        if (objXml.status != 0 && objXml.status != 200) {
	      	error_code = "1";
		//alert("Error: " + objXml.status);
		//alert("Error: " + objXml.statusText);
		htmlout = "";
	      	//Call function to show error somewhere.
	      	
	      	return false;
	} else {
		getcodein(); // Get code.
		return true;
	}

  
 } // End If of if (objXml.readyState == 4)
 return false;
} // End WaitOnIE()

function nsopen() {
// All Netscape Code
     objXml = new XMLHttpRequest();
     try{
      error_return = objXml.open("GET",datafile,false);
      error_return = objXml.send(null);
      htmlout = objXml.responseText;
      }catch(error_return){
         //alert(errorObject.description );
         error_code = "1";
      }
      if (objXml.status != 0 && objXml.status != 200) {
      	error_code = "1";
			//alert("Error: " + objXml.status);
			//alert("Error: " + objXml.statusText);
			htmlout = "";
      	//Call function to show error somewhere.
      }
 return htmlout;

} // End nsopen

function ieopen() {

// All Internet Exploder Code

 objXml = new ActiveXObject("Microsoft.XMLHTTP");
 objXml.open("GET", datafile, false);

 objXml.send(null);
      
 return;

} // End ieopen

function getcodein() {

 htmlout = objXml.responseText; // Get code into htmlout.
 return htmlout;
}


// Write Code to Div Tags.
function writecode() {

if (whichdivglobal == "null") {return;}

if (document.getElementById('maininfo') != null && whichdivglobal == 0) {

  document.getElementById('maininfo').innerHTML = htmlout;

}

if (document.getElementById('submenu1') != null && whichdivglobal == 1) {

  document.getElementById('submenu1').innerHTML = htmlout;

}

if (document.getElementById('submenu2') != null && whichdivglobal == 2) {

  document.getElementById('submenu2').innerHTML = htmlout;

}

if (document.getElementById('maininfo') != null && whichdivglobal == 4) {

  document.getElementById('maininfo').innerHTML = '<iframe width="1500" height="1500" frameborder="0" scrolling="no" marginheight="2" marginwidth="2" src="' + datafile + '" style="color:#0000FF;text-align:left">';

}

if (document.getElementById('mp3') != null && whichdivglobal == 5) {

  document.getElementById('mp3').innerHTML = htmlout;

}

if (document.getElementById(whichdivglobal) != null && whichdivglobal != "") {

  document.getElementById(whichdivglobal).innerHTML = htmlout;

}

return;
} // End writecode


//Show or Hide a Div Tag
function hidediv(ID) {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById(ID).style.visibility = 'hidden';
}
else {
if (document.layers) { // Netscape 4
document.ID.visibility = 'hidden';
}
else { // IE 4
document.all.ID.style.visibility = 'hidden';
}
}
}

function showdiv(ID) {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById(ID).style.visibility = 'visible';
}
else {
if (document.layers) { // Netscape 4
document.ID.visibility = 'visible';
}
else { // IE 4
document.all.ID.style.visibility = 'visible';
}
}
}  // End Show or Hide a Div Tag

function divlabel(tag,txttag){

//alert(tag + "+" + txttag);

// No sense in continuing if nothing in txttag...
//if (txttag == "") { return; }

// If the Div Tags are empty when calling this function to change a var inside them,
//	create the html code first.
if (document.getElementById(tag) && tag != "") {
 document.getElementById(tag).innerHTML = txttag;
}

// Now, in this block of code, if the Div Tags have something in them and we call the
// function with a div tag($tag) and without a txttag value, we will delete the div html.
// Kinda like an ON/OFF for blocks of html code.

if (tag != "" && txttag == "" && document.getElementById(tag).innerHTML != "") {
 document.getElementById(tag).innerHTML = "";
}

return;
} // End divlabel function


// Load/Run some things on first visit to page
function runstartup() {

// Check to see if we are using local server or not
querylocal=check_domain_local();
query_url=prep_url();
//alert(querylocal);alert(querywww);

if (!querylocal && !query_url) {
	window.location = datafilepath;
} else {
	datafilepath="";
}

}

function test_file_exists(testfile) {


} // End test_file_exists()

function prep_url() {

var barurl2 ="";
var prefix="";
var newurl="";
barurl2 = window.location; // barurl2 turns into a object here
barurl2 = barurl2.toString().toLowerCase(); // So use this alt method of string...
newurl=barurl2;
http_result = barurl2.indexOf("http://");

if (http_result == -1) {
	prefix1="http://";
	newurl=prefix1+barurl2;
}

var barurl = newurl;
www_result = barurl.indexOf("www.");

if (www_result == -1) {
	prefix2="www";

}

// See if we have to splice in the www...
if (www_result == -1) {
	var newurltemp = barurl.slice(7);
	newurl = "http://www." + newurltemp;
}

// check if newurl contains a filename and if so strip it out.
if (getfilename(newurl)) {
 newurltemp = xtractfile(newurl);
 newurl = newurltemp["path"];
}

if (newurl != datafilepath) {
//alert(newurl + " : " + datafilepath);
	window.location = newurl;
}
// If it returns true then it never changed the url and refreshed. So good to go.
return true;

}

function check_domain_local() {
// This function will allow me to use my local server for a file path instead of the set domain path in var datafilepath.
// It detects an error if this file was loaded from the local server and trying to load a file(function loadpages()) off
// the domain name on the web. If error then rewrite the var datafilepath into "" for local access. 
 var chk=location.hostname;
 
 if (chk == "127.0.0.1" || chk == "localhost") {
     returncode = true;
 } else {
	//alert("false");
	returncode = false; 
 }
 return returncode;
}

//For submiting the add to cart button.
function submitdata(myindex,selcat,filetype,filename,customid,itemindex) {

  // Calling this page allows the sessions information to be stored into the MySQL Database.
  loadpages('/products/'+filetype+'/'+myindex+'?selcat='+selcat+'&filetype='+filetype+'&filename='+filename+'&customid='+customid+'&itemindex='+itemindex+'&pcartcount='+pcartcount,0,'','','');

  // Save the number of cart items in a cookie for later viewing
  pcartcount = readCookie("blackcreek_pcartcount");
  //pcartcount = pcartcount + 1;
  //createCookie("blackcreek_pcartcount",pcartcount,5);

  showhide("cart_items",pcartcount,"");

return;
}

// If deleting a cart item add a value to the $_session array info for that item
// saying that this item is to be skipped when submiting to PayPal and displaying the cart.
function changecartitems(itemnum) {

// Check if delete is checked -[0]-
// If so mark deleted {actully hiding it, not removing}
stritemnum = itemnum.toString();
itemnum_check = "document.form_items[" + stritemnum + "].delete_chk[0].checked";

 if (itemnum_check) {
  loadpages("paypalcart.php?delitem="+itemnum,0,'','','');
  return true;
 } else {
  return false;
 }

}

// Send all data to PayPal via the cart API
function cartcheckout() {

// Check to see if at least one item is in cart to checkout
ifstat = "document.form_items[0].update_btn[0].value";
if (ifstat) {

  ppwindow = window.open('http://www.blackcreekcyberstore.com/paypalcart.php?submitcart=1', 'paypal', 'status=yes,dependent,resizable=yes,scrollbars=yes,height=480,width=640', false);
  // Max the screen size
  ppwindow.moveTo(0,0);
  ppwindow.outerHeight = screen.availHeight - 75;
  ppwindow.outerWidth = screen.availWidth;
  // Submit all PayPal Cart information
  //setTimeout("paypal.document.forms[0].submit()",1500);
  paypal.document.forms[0].submit()

  return true;
}

}

// Javascript functions for creating/reading/erasing cookies
function createCookie(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 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 eraseCookie(name) {
	createCookie(name,"",-1);
}

function iframe_string(url) {

return "<iframe class='iframe_1' width='100%' src='" + url + "'></iframe>"

}

function tag_count(tag,srcstring) {
// Count how many tags/strings are in a string

var count = srcstring.split(tag).length - 1;


return count;

}

//Full Name and path
function xtractfile(data) {
//alert("data: "+data);
    var m = data.match(/(.*)[\/\\]([^\/\\]+\.\w+)$/);
    if (m != null) {
    	var datareturn = {path: m[1] + "/", file: m[2]};
    	return datareturn; // Returns an array
    } else {
    	return {path: "", file: ""};
    }
}

function getfilename(path){

	var fn = path.match(/\/([a-z0-9_-]+\.\w+)$/i);
	return (fn == null)? "" : fn[fn.length-1];

}

// For dhtmlLoadScript() function
var e = {};
var dhtml_counter = 0; // All start @ 0.

function dhtmlLoadScript(url,pos)
{

	if (!pos) {
		var pos="head";
	}

	e[dhtml_counter] = document.createElement('script');
	e[dhtml_counter].src = url;
	e[dhtml_counter].language="JavaScript";
	e[dhtml_counter].type="text/javascript";
	document.getElementsByTagName(pos)[0].appendChild(e[dhtml_counter]);

//alert(e[dhtml_counter].src);
	// Increase count by 1 for next run
	dhtml_counter = dhtml_counter + 1;

}

//The dhtmlLoadScript needs a delay in order for the appended code to attach and run.
// Some functions within the appended code will not be availiable unless I do this.
// 1000 msecs = 1 Second.
function wait(msecs)
{
var start = new Date().getTime();
var cur = start
while(cur - start < msecs)
{
cur = new Date().getTime();
}
} 
