//-------------------------------------------
//	ON DISPLAY MEN AJAX AND GENERAL JS FUNCTIONS
//
//-------------------------------------------

//------------------------------------------

//Browser Support Code
function initAjax(){
	var ajaxRequest;  
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("I'm sorry, Your browser is not compatible with this function.");
				return false;
			}
		}
	}
	return ajaxRequest;
}

//------------------------------------------
//	General Functions
//------------------------------------------

function ForEach(array, fn, objThis)
{
  objThis = objThis || this;
  var len = array.length;
  for (var n = 0; n < len; n++)
  {
    var r = fn.call(objThis, array[n], n);
    if (r !== undefined)
      return r;
  }
}

function SumArray(array)
{
  var nSum = 0;
  ForEach(array, function (n) { nSum += n; });
  return nSum;
}

function FindInArray(array, find)
{
  var index = ForEach(array,
    function (item, n)
    {
      if (item == find)
        return n;
    });
  return index === undefined ? -1 : index;
}

//------------------------------------------

function checkLoggedIn(){
	var ajaxRequest; var loginResponse = "test";
	try{ ajaxRequest = initAjax(); } catch (e){ alert("I'm sorry, the ajax function isnt working."); return false; }

	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState == 4){
			loginResponse = ajaxRequest.responseText; // use global
			if(loginResponse != "test"){document.forms["vars"].elements["loginStatus"].value = loginResponse;}
		}	
	}
	ajaxRequest.open("GET", "includes/getLoginStatus.php", true);
	ajaxRequest.send(null);
	
}

function isMember(){
	return true;
}


function voteForMe(hot_not,id){
	vote = hot_not; model = id;
	var ajaxRequest;  loginStatus = "not";
	try{
		ajaxRequest = initAjax();
	} catch (e){
		alert("I'm sorry, the ajax function isnt working.");
		return false;
	}

	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4)
		{
			var ajaxDisplay = document.getElementById('voteResponseDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
			//
			checkLoggedIn(); 
			var loginResponse = document.forms["vars"].elements["loginStatus"].value;
			if(loginResponse=="logged in"){showHotNotIndicator(model,vote);}
			else{showbox();}
		}
	}
	var fullURL = parent.document.URL;
	var queryString = "?id=" + id + "&hot_not=" + hot_not + "&formaction=" + fullURL;
	ajaxRequest.open("GET", "./vote.php" + queryString, true);
	ajaxRequest.send(null);
}

function showHotNotIndicator(n,hot_not){
	var i = "hot_not_indicator" + n;
	if (hot_not == "hot"){ var img = "images/hotflame_on.gif"; }
	else{ var img = "images/notbutton_on.gif"; }
	
	if(document.getElementById(i)){
	document.getElementById(i).src = img;
	}
}

function addToCart(product_id,option){
	
	var ajaxRequest;  loginStatus = "not";
	try{
		ajaxRequest = initAjax();
	} catch (e){
		alert("I'm sorry, the ajax function isnt working.");
		return false;
	}

	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4)
		{
			var theResponse = ajaxRequest.responseText;
			
			checkLoggedIn(); 
			var loginResponse = document.forms["vars"].elements["loginStatus"].value;
			if(loginResponse=="logged in"){showHotNotIndicator(model,vote);}
			else{showbox();}
		}
	}
	var queryString = "?id=" + id + "&hot_not=" + hot_not;
	ajaxRequest.open("GET", "./vote.php" + queryString, true);
	ajaxRequest.send(null);
}

function replaceText(sId, sText)
{
	var el;
	if (document.getElementById && (el = document.getElementById(sId)))
	{
		while (el.hasChildNodes()){
			el.removeChild(el.lastChild);
			el.appendChild(document.createTextNode(sText));
		}	
	}
}

function getDisplayProperty( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
  {
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  	if(vis.display==''||vis.display=='block'){ return 'block'; }else { return 'none'; }
  }
}

function toggleLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

function toggle(obj) {
	var el = document.getElementById(obj);
	
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

function isDisplayed(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		return true;
	}
	else {
		return false;
	}
}

function toggleBetween(a,b){
	toggle(a); toggle(b);
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.Images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
