<!-- STATPACKS SCRIPT LIBRARY v0.1 -->

var navitrail=""; //"Navigational breadcrumb trail"
function setTrail(val) {
	navitrail=val;
	document.getElementById('subText').innerHTML=val;
}

// PRELOADING OF IMAGES
/*
var myimages=new Array()
function preloadimages(){
  for (i=0;i<preloadimages.arguments.length;i++){
    myimages[i]=new Image()
    myimages[i].src=preloadimages.arguments[i]
  }
}
*/
//preloadimages("img/topLogo.gif","img/img0Off.gif","img/img0On.gif","img/img1Off.gif","img/img1On.gif","img/img2Off.gif","img/img2On.gif","img/img3Off.gif","img/img3On.gif","img/img4Off.gif","img/img4On.gif","img/img5Off.gif","img/img5On.gif","img/img6Off.gif","img/img6On.gif","img/img7Off.gif","img/img7On.gif","img/backDrop.gif","img/backptn.gif","img/topBack.gif","img/cdiv.gif","img/bl.gif","img/divi.gif");

// URL PROCESSING
function go(url) {
  document.location = url;//"http://www.webstringdesign.com/statpacks/"+url;
}

function goPop(url) {
  //var url = obj.href;
  window.open(url, 'myCustWin', 'toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=yes, scrollbars=yes, height=550, width=550');
}


function goWin(url) {
  //var url = obj.href;
  window.open(url, 'myWin', 'toolbar=yes, directories=no, location=yes, status=yes, menubar=no, resizable=yes, scrollbars=yes');
}

function goStory(url) {
  //var url = obj.href;
  window.open(url, 'myPulseWin', 'toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=yes, scrollbars=yes, height=550, width=610');
}

// ROLLOVER FUNCTIONALITY
/*
Rollover Content:

BUY
Link to your preferred distributor or buy direct from the StatPacks on-line store

PRODUCTS
Browse packs and modules by category and application

COMPANY
About StatPacks: history, core beliefs, contact info

MEDIA KIT
For media professionals: news, company details, and hi-res photos
 
THE PULSE
Headlines and ideas from the field, the industry, and the StatPacks team
*/


function doRollover(obj) {
	var id = obj.id;
	var text = "";
	  
	if(id=="buy") { text = "Link to your preferred distributor or buy direct from the StatPacks on-line store"; }
	if(id=="products") { text = "Browse packs and modules by category and application"; }
	if(id=="company") { text = "About StatPacks: history, core beliefs, contact info"; }
	if(id=="mediakit") { text = "For media professionals: news, company details, and hi-res photos"; }
	if(id=="thepulse") { text = "Headlines and ideas from the field, the industry, and the StatPacks team"; }
	document.getElementById('subText').innerHTML=text;
}

function clearRollover() {
	document.getElementById('subText').innerHTML=navitrail;
}


// MENU FUNCTIONALITY
var closeIm = new Array(true, true, true, true, true, true, true, true); //timer array: image links
var closeDD = new Array(true, true, true, true, true, true, true, true); //timer array: drop downs

var waitTime = 100; // in milliseconds
function closeImageLink(val) {
  if(closeIm[val]==true && closeDD[val]==true) { 
	//document.getElementById("linkImg"+val).src="img/img"+val+"Off.gif"; 
	document.getElementById("menuContent"+val).style.display="none";
  }
  else {
	var func = "closeImageLink("+val+")";
	setTimeout(func, 250);
  }
}

function swapMainNav(obj, state)	{
	var name = obj.getAttribute("name");
	var val = name.substr(7,1);
	if(val == "t") {
		//Process menuContentX
		val = name.substr(11,1);
		if (state == 0)		{
			closeDD[val]=true;
			var func = "closeImageLink("+val+")";
			setTimeout(func, waitTime );
		}
		if (state == 1)		{ 
			closeDD[val]=false;	
		}
	}
	else {
		//Process linkImgX
		if (state == 0)		{
			closeIm[val]=true;
			var func = "closeImageLink("+val+")";
			setTimeout(func, waitTime );
		}
		if (state == 1)		{
			closeIm[val]=false;
			//obj.src= "img/img"+val+"On.gif";
			document.getElementById("menuContent"+val).style.display="block";
		}
	}
}

function doSubmit(obj) {
  var objid=obj.id;
  document.getElementById("formname").value=objid;
  try {
  	document.getElementById("form").submit();
  } catch(err) {
	  txt="FORM SUBMIT Error: " + err.description + "\n"
	  txt+="Possibly a file upload name/path was invalid. Click OK to continue.\n\n"
	  alert(txt)
  }

}

function doDelCat(obj,id) {
	if(confirm("Are you sure you want to delete this category?")) {
		document.getElementById('catID').value=id;
		doSubmit(obj);
	}
}
function doModifyCat(obj,id) {
	//document.getElementById("formname").value="modifycat";
	document.getElementById('catID').value=id;
	document.getElementById('catNAME').value=document.getElementById('catname'+id).value;
	//document.getElementById('catTAC').value=document.getElementById('catname'+id).value;
	if(document.getElementById('catsite_tac'+id).checked) {
		document.getElementById('catTAC').value=1;
	}
	if(document.getElementById('catsite_ems'+id).checked) {
		document.getElementById('catEMS').value=1;
	}
	doSubmit(obj);
}
function doModifyProdCat(obj,id) {
	document.getElementById('prodID').value=id;
	doSubmit(obj);
}
function doModifyProduct(obj,id) {
	document.getElementById('prodID').value=id;
	doSubmit(obj);
}
function doAddProduct(obj) {
	//document.getElementById('prodID').value=id;
	doSubmit(obj);
}
function doAddProductImage(obj) {
	//alert('This button is disabled... Please notify the System Administrator if you want to use this functionality.');
	if(document.getElementById('prodimagefile').value!='') {
		doSubmit(obj);
	}
}
function doAddAllProductImages(obj) {
	if(document.getElementById('prodimagefile_S').value!='' || document.getElementById('prodimagefile_M').value!='' || document.getElementById('prodimagefile_L').value!='' || document.getElementById('prodimagefile_XL').value!='') {
		doSubmit(obj);
	}
}
function doDelProduct(id) {
	//document.getElementById('prodID').value=id;
	if(confirm("Are you sure you want to delete this product? Images associated with this Product will also be deleted.")) {
		document.getElementById("formname").value="delprod";
		document.getElementById('prodID').value=id;
		document.getElementById("form").submit();
	}
}
function doDelProductImage(id) {
	//document.getElementById('prodID').value=id;
	if(confirm("Are you sure you want to delete this product's image?")) {
		document.getElementById("formname").value="delprodimg";
		document.getElementById('pimg_version').value=id;
		document.getElementById("form").submit();
	}
}
function doSetDefImage(id) {
  //product id already set in hidden field
	document.getElementById("formname").value="setdefaultproductimage";
	document.getElementById("pimg_version").value=id;
	document.getElementById("form").submit();
}
function assignProdImg2Site(version,site,assign) {
	if(assign==1) {
		document.getElementById("formname").value="assignsiteimage";
	} else {
		document.getElementById("formname").value="unassignsiteimage";
	}
	document.getElementById("pimg_version").value=version;
	document.getElementById("site").value=site;
	document.getElementById("form").submit();
}
function doModifyAd(obj,id) {
	document.getElementById('adID').value=id;
	doSubmit(obj);
}
function activeAD(obj,id) {
	document.getElementById('adID').value=id;
	doSubmit(obj);
}
function doModifyStory(obj,id) {
	document.getElementById('storyID').value=id;
	doSubmit(obj);
}
function doModifyHomeImgLeft(obj) {
	if(document.getElementById('userfileLeft').value != "") {
		doSubmit(obj);
	} else {
		alert('Please click Browse to choose an image for replacement');
	}
}
function doModifyHomeImgBotLeft(obj) {
	if(document.getElementById('userfileBotLeft').value != "") {
		doSubmit(obj);
	} else {
		alert('Please click Browse to choose an image for replacement');
	}
}
function doModifyHomeImgMiddle(obj) {
	if(document.getElementById('userfileMiddle').value != "") {
		doSubmit(obj);
	} else {
		alert('Please click Browse to choose an image for replacement');
	}
}
function doModifyHomeImgBotRight(obj) {
	if(document.getElementById('userfileBotRight').value != "") {
		doSubmit(obj);
	} else {
		alert('Please click Browse to choose an image for replacement');
	}
}
function doModifyHomeImgMain(obj) {
	if(document.getElementById('userfileMain').value != "") {
		doSubmit(obj);
	} else {
		alert('Please click Browse to choose an image for replacement');
	}
}
function doSubmitWithImage(imgname,obj) {
	if(document.getElementById(imgname).value != "") {
		doSubmit(obj);
	} else {
		alert('Please click Browse to choose an image for replacement');
	}
}


function doSearch() {
	var sp_search = document.getElementById('sp_search').value;
	if(sp_search!='') {
  	document.location='search.php?q='+escape(sp_search);
	}
}

function doLogin(site) {
	document.location='../admin/index.php?s='+site;
}


// AJAX:

function HttpClient() { }
HttpClient.prototype = {
	// type GET,POST passed to open
	requestType:'GET',
	// when set to true async calls are made
	isAsync:true,

	// where an XMLHttpRequest instance is stored
	xmlhttp:false,

	// what is called when a successful async call is made
	callback:false,

	// what is called when send is called on XMLHttpRequest
	// set your own function to onSend to have a custom loading effect
	onSend:function() {
		//document.getElementById('HttpClientStatus').style.display = 'block';
	},

	// what is called when when readyState 4 is reached, this is called before your callback
	onLoad:function() {
		//document.getElementById('HttpClientStatus').style.display = 'none';
	},

	// what is called when an http error happens
	onError:function(error) {
		alert(error.message);
	},
	
	// method to initialize an xmlhttpclient
	init:function() {
		try {
		    // Mozilla / Safari
		    this.xmlhttp = new XMLHttpRequest();
		} catch (e) {
			// IE
			var XMLHTTP_IDS = new Array(
			'MSXML2.XMLHTTP.5.0',
			'MSXML2.XMLHTTP.4.0',
			'MSXML2.XMLHTTP.3.0',
			'MSXML2.XMLHTTP',
			'Microsoft.XMLHTTP' );
			var success = false;
			for (var i=0;i < XMLHTTP_IDS.length && !success; i++) {
				try {
					this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
					success = true;
				} catch (e) {}
			}
			if (!success) {
				throw new Error('Unable to create XMLHttpRequest.');
			}
		}
	},

	// method to make a page request
	// @param string url  The page to make the request too
	// @param string payload  What your sending if this is a POST request
	makeRequest: function(url,payload) {
		if (!this.xmlhttp) {
			this.init();
		}
		this.xmlhttp.open(this.requestType,url,this.isAsync);

		// set onreadystatechange here since it will be reset after a completed call in Mozilla
		var self = this;
		this.xmlhttp.onreadystatechange = function() { self._readyStateChangeCallback(); }

		this.xmlhttp.send(payload);

		if (!this.isAsync) {
			return this.xmlhttp.responseText;
		}
	},
	

	// internal method used to handle ready state changes
	_readyStateChangeCallback:function() {
		switch(this.xmlhttp.readyState) {
			case 2:
				this.onSend();
				break;
			case 4:
				this.onLoad();
				if (this.xmlhttp.status == 200) {
					this.callback(this.xmlhttp.responseText);
				}
				else {
					this.onError(new Error('HTTP Error Making Request: ['+this.xmlhttp.status+'] '+this.xmlhttp.statusText));
				}
			break;
		}
	}
}
var client = new HttpClient();
var s=Math.random();
//client.isAsync = true;

function doReq(params,target) {
	client.callback = function(result) { 
		document.getElementById(target).innerHTML = result; 
	}
	s++;
	client.makeRequest('../inc/ajax.php?s='+s+'&'+params,null);
	//client.makeRequest('http://cruzer/statpacks/dev/ajax.php?'+params,null);
}

