//Initial variables - edit these to match your setup

//picwala_url: Full url for the main picwala program
var picwala_url = 'http://www.picwala.com/picwala2.0/picwala_main.php';
//picwala_css: Full url for the css file
var picwala_css = 'http://www.picwala.com/picwala2.0/picwala.css';

//picwala_mydiv: ID of the div where picwala is inserted
//This is in the parent page like:
//	<div id="picwala_div">
//	<SCRIPT language="JavaScript" SRC="http://www.picwala.com/picwala2.0/picwala.js"></SCRIPT> 
//	</div>
//If you change the name of picwala_mydiv, you will need to adjust picwala.css!
var picwala_mydiv = 'picwala_div';

//End setup
//Do not edit beyound this point!
////////////////////////////////////////////////////////////////////////////////////////////////

function picwala_LoadMyDiv(strURL) {
//Do an XMLHttpRequest to strURL
//and replace the contents of picwala_mydiv with the response
	var mydiv=document.getElementById(picwala_mydiv);
	var xmlHttpReq = false;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
                xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
	xmlHttpReq.open('POST', strURL, true);
	xmlHttpReq.onreadystatechange = function() {
                if (xmlHttpReq.readyState == 4) {
                	mydiv.innerHTML=xmlHttpReq.responseText;
			picwala_drawExecJS();
                }
        }
       xmlHttpReq.send(null);
}

function picwala_Reload(argArray){
//Reloads Picwala
//argArray elments become url arguments
	var urlargs = '';
	for(var i = 0; i < argArray.length; i++){ 
		if(i == 0){
			urlargs = '?';
		} else {
			urlargs += '&';
		}
		urlargs += argArray[i];
	}
	picwala_LoadMyDiv(picwala_url+urlargs);
}

function picwala_doUpdate(UpdateUrl, ReloadUrl){
//Makes an Ajax call to UpdateUrl
//On completion, if ReloadUrl, reloads picwala page with ReloadUrl
	var xmlHttpReq = false;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
                xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
	xmlHttpReq.open('POST', UpdateUrl, true);
	xmlHttpReq.onreadystatechange = function() {

                if (xmlHttpReq.readyState == 4) {
			//alert(xmlHttpReq.responseText);
			if(ReloadUrl != ''){
               			picwala_LoadMyDiv(ReloadUrl);
			}
		}
       }
       xmlHttpReq.send(null);
}

function picwala_readSelect(){
//Call with any number of IDs as arguments
//Outputs only   selected options in an array with element as name[]=value
//Also appends picwala_RefObjChecked[] for picwala_archiveChecked, picwala_unfiledChecked if any
//and always picwala_name and picwala_pw

	var myOutput = [];
	var argv = picwala_readSelect.arguments;
	var argc = argv.length;
	for(var a = 0; a < argc; a++){ 
		var mySelect = document.getElementById(argv[a]);
		for(var i = 0; i<mySelect.options.length; i++){
			if(mySelect.options[i].selected)myOutput.push(mySelect.name+"="+mySelect.options[i].value);
		}
	}
	var NewCheck = document.getElementById('picwala_newChecked');
	if(NewCheck.checked)myOutput.push(["picwala_RefObjChecked[]="+NewCheck.value]);

	var ArchiveChecked = document.getElementById('picwala_archiveChecked');
	if(ArchiveChecked && ArchiveChecked.checked)myOutput.push(["picwala_RefObjChecked[]="+ArchiveChecked.value]);

	var UnfiledChecked = document.getElementById('picwala_unfiledChecked');
	if(UnfiledChecked && UnfiledChecked.checked)myOutput.push(["picwala_RefObjChecked[]="+UnfiledChecked.value]);

	myOutput.push(["picwala_name="+document.getElementById('picwala_name').value]);
	myOutput.push(["picwala_pw="+document.getElementById('picwala_pw').value]);
	//alert('Output: '+myOutput);
	return myOutput;
}

function picwala_readSelectArgsOnly(){
//Call with any number of IDs as arguments
//Outputs only selected options in a STRING with element as &name[]=value
	var myOutput = '';
	var argv = picwala_readSelectArgsOnly.arguments;
	var argc = argv.length;
	for(var a = 0; a < argc; a++){ 
		var mySelect = document.getElementById(argv[a]);
		for(var i = 0; i<mySelect.options.length; i++){
			if(mySelect.options[i].selected)myOutput += ('&'+mySelect.name+"="+mySelect.options[i].value);
		}
	}
	//alert('Output: '+myOutput);
	return myOutput;
}

function picwala_selectLoginOpen(){
	//Supports dialog to input name and pw in the search page, called once to initiate.
	//Assumes a div with id picwala_login_div has been created including input picwala_name, and is hidden.
	//Unhides picwala_login_div and sets focus to picwala_name
	document.getElementById('picwala_login_div').style.visibility='visible';
	document.getElementById('picwala_name').focus();
}

function picwala_drawExecJS(){
//After inserting html via ajax, new javascript is not executable.
//This makes the new javascript executable
	var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
	var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
	var bMoz = (navigator.appName == 'Netscape');
	function execJS(node) {
		var st = node.getElementsByTagName('SCRIPT');
		var strExec;
		for(var i=0;i<st.length; i++) {
			if (bSaf) {
				strExec = st[i].innerHTML;
			}
			else if (bOpera) {
				strExec = st[i].text;
			}
			else if (bMoz) {
				strExec = st[i].textContent;
			}
			else {
				strExec = st[i].text;
			}
			try {
				eval(strExec.split("<!--").join("").split("-->").join(""));
			} catch(e) {
				alert(e);
			}
		}
	}
}

//Load css only once
if( typeof(picwala_cssLoaded)=='undefined') {
	document.write("<link REL='stylesheet' HREF='"+picwala_css+"' TYPE='text/css'>");
	var picwala_cssLoaded = 1;
}

//Load picwala
if( typeof(picwala_newload)=='undefined') {
	picwala_Reload([]);
}




