// 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;
 }
}

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 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);
}

//Collects form name/value pairs
function formdata(match){
 var saveString='';
 var f='';
 for (var i=0;i<document.forms.length;i++){
  if (document.forms[i].name.indexOf(/match/i)!=-1){
  f=document.forms[i].name;
  }
 }
 if (f==''){
  f=document.forms[0].name;
 }
 var x=document.getElementById(f);
 for (var i=0;i<x.length;i++){
  saveString+=x.elements[i].name+'=';
  saveString+=x.elements[i].value+'&';
 }
 document.getElementById('tmp').value=saveString;
}

//Browser Support Code
function ajaxFunction(saveString,divele,app){
	if (divele==null){
	var divele='ajresponse';
	}
	if (app==null){
	var app='/$thisapp';
	}
	var e=document.getElementById(divele);
	e.style.display='block';
	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
				e.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){
				e.innerHTML = ajaxRequest.responseText;
			}
			else {
				e.innerHTML = '';
			}
		}
	}
	e.innerHTML='<center><span style="width:100%; align:center; text-align:center; text-decoration:blink;"><img src="/images/indicator48x48.gif" width=48 align=center height=48 border=0 alt="processing"><br><I>processing your request...</I></span></center>';
//	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); 
}

