// This code developed by Craig Richards LLC 
// Copyright 2010. All rights reserved worldwide.
// The above lines must be included in code that is 
// reused or adapted by anyone
// This code provides AJAX functionality.

var oneClick=1;
function singleClick(){
 if (oneClick==1){
  oneClick=0;
  return true;
 } else {
  setTimeout('oneClick=1;',3000);
  return false;
 }
}

var reloaded=1;

function popup(url){
	var win_w, win_h;
	win_w = screen.availWidth; 
	win_h = (screen.availHeight - 100); 
	myWindow = window.open(url, "", "top=1, left=1, width=" + win_w + ", height=" + win_h + ", toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,resizable=yes");
	if (!myWindow.opener) myWindow.opener = self;
}
function toggle_visibility(id,view){
 var e=document.getElementById(id);
 var vw=null;
 vw=view;
 if (vw != null){
  if (vw == 'off'){
   e.style.display=(e.style.display = 'none');
  }
  else {
   e.style.display=(e.style.display='block');
  }
 }
 else {
   e.style.display=(e.style.display != 'none' ? 'none' : 'block');
 }
}

function show(id,view){
 var e=document.getElementById(id);
 var vw=null;
 vw=view;
 if (vw != null){
  if (vw == 'off'){
   e.style.display=(e.style.display = 'none');
  }
  else {
   e.style.display=(e.style.display='block');
  }
 }
}
function setOpacity(value){
	d.style.opacity=value / 10;
	d.style.filter='alpha(opacity=' + value * 10 + ')';
	value='';
}

function fadeIn(op) {
	d=document.getElementById(op);
	openDIV(d);
	for(var i=0; i <= 100; i++){
		setTimeout('setOpacity('+(i / 10)+')',12 * i);
	}
}

function fadeOut(fo){d='';
	d=document.getElementById(fo);
	for(var i=0; i <= 100; i++){
	setTimeout('setOpacity('+(10-i / 10)+')',8 * i);
 }
	setTimeout('closeDIV()',600);
}

function closeDIV(){
	d.style.display="none";
}

function openDIV(op){d='';
	d=document.getElementById(op);
	d.style.display='block';
	d.style.visibility='visible';
	setOpacity(10);
}

function saveSelect(val){
	var saveString = "";
	document.pics.primary.value=val;
	saveString='primary=' + val;
	openDIV('ajresponse');
	ajaxFunction(saveString);
	setTimeout("fadeOut('ajresponse')",4000);
}

function deleteSelect(dSel){
	if(confirm('Permanently delete this photo?')){
	}else{
		alert('Delete Cancelled');
		return false
	}
	var saveString = "";
	saveString='delete=' + dSel;
    openDIV('ajresponse');
	ajaxFunction(saveString);
	fadeOut('p'+dSel);
	setTimeout("fadeOut('ajresponse')",6000);
}

//Browser Support Code
function ajaxFunction(saveString,divele,app){
	if (divele==null){
	var divele='ajresponse';
	}
	if (app==null){
//	var app='/$thisapp';
	}
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try {
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
		ajaxRequest.overrideMimeType('text/xml');
	}
	catch (e) {
		// Internet Explorer Browsers
		try {
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			ajaxRequest.overrideMimeType('text/xml');
		}
		catch (e) {
			try {
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				ajaxRequest.overrideMimeType('text/xml');
			}
		catch (e) {
				// Something went wrong
				document.getElementById(divele).innerHTML = 'Sorry, there was an error with your request.';
//				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			if (ajaxRequest.status==200){
				document.getElementById(divele).innerHTML = ajaxRequest.responseText;
			}
		}
		else {
				document.getElementById(divele).innerHTML = '';
		}
	}
	var e=document.getElementById(divele);
	e.style.display='block';
//	e.innerHTML='<div align=center style="width:100%; height:300px; text-align:center; text-decoration:blink;"><img src="/images/indicator24x24.gif" width=24 height=24 align=center border=0 alt="processing changes"><br><I>processing your request...</I></div>';
//	if POST, change the request header with this line:
//	ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajaxRequest.open('GET',app+'?ajax=yes&'+saveString,true);
	ajaxRequest.send(null); 
}
