//open new window or if window exists and is open bring to foreground
// params:: window name, window url, height, width, window options
function openWin(nm,url,h,w,opts) {
   //default vals :: set for large artwork view
   if(!nm) nm = 'detailsWin'
   if(!url) url = 'details.asp'
   if(!h) h = 250
   if(!w) w = 250
   if(!opts) opts = 'scrollbars=auto, resizable=yes'

   if(window[nm] && !window[nm].closed){
      window[nm].document.location=url
      window[nm].focus();
   } else {
      var options = ''
      options += 'width='+w+','
      options += 'height='+h+','
      options+=opts
      eval(nm+"=window.open('"+url+"','"+nm+"','"+options+"')")
   }

   //center new window
  if(window.screen) {
      var x = (screen.width-w)/2
      var y = (screen.height-h)/2
      window[nm].moveTo(x,y)
  }

}

/*
function strToDOM(str) {

	if(env.IE) {
		var el = document.createElement('DIV');
		el.innerHTML = str;
	} else {
		var tp = new DOMParser();
		return tp.parseFromString(str,"text/xml");
	}

}
*/

function cancelBubble(e) {
	if(env.IE) {
		event.cancelBubble=true;
	} else {
		if(!e || !e.preventBubble) return;
		e.preventBubble();
	}
	
}

function strcmp(a,b) {
	if(a==b) return 0;
	var c=0;
	while(a.charAt(c)==b.charAt(c)) {
		++c;
	}
	if(a.charAt(c) > b.charAt(c)) return 1;
	else return -1;
}

function yesno(str,title,buttons) {

	var vbOKCancel = 1;
	var vbInformation = 64;
	var vbExlamation = 48
	var vbQuestion = 32
	var vbYesNo = 4
	var vbCancel = 2;
	var vbYesNoCancel = 3


	if(!buttons) buttons = vbYesNo
	//var WSHShell = new ActiveXObject("WScript.Shell");
	//return ((WSHShell.Popup(str,0,((title)? title:''),vbYesNo)==6)? true:false);
	var ret = makeMsgBox(((title)? title:''),str,0,buttons,0,1)
	return ((ret==7)? false:((buttons==4)? true:ret));
}

function formatTime(dt) {
	var ampm=(dt.getHours()>12)? 'pm':'am';
	var hour = (dt.getHours()>12)? dt.getHours()-12:dt.getHours()
	if(!hour) hour = 12
	var min = (String(dt.getMinutes()).length < 2)? '0'+dt.getMinutes():dt.getMinutes()
	return hour+':'+min+' '+ampm
}


//formats a passed in date object into d/m/yr h:mm AM/PM
function formatDate(d,dateAdjust){
	if(!d) return '';

	var dt = new Object();

	dt.hours = d.getHours()
	dt.ampm = (dt.hours >= 12)? 'PM':'AM'
	if(dt.hours > 12) {
		dt.hours = dt.hours -12
	}

	dt.minutes = d.getMinutes()
	dt.month = (d.getMonth()+1)
	dt.day = d.getDate()
	dt.year = d.getFullYear()

	if(dateAdjust) {
		for(var i in dateAdjust) {
			if(!isNaN(dt[i])) {
				dt[i] += dateAdjust[i]
			}
		}
	}

	if(String(dt.minutes).length<2) dt.minutes = '0'+String(dt.minutes)
	
	var str = dt.month+'/'+dt.day+'/'+dt.year;
	if(dt.hours && d.getMinutes()) str+= ' '+dt.hours+':'+dt.minutes+' '+dt.ampm
	return str;
}

////load panel
//function panel(panel,w,h,args){
//  if(!w) w = 300
//  if(!h) h = 300
//  panel = urlEncode(panel)
//  return showModalDialog('panel.'+PD_PROCESSING_ENGINE+'?file='+panel+'&'+lt(),args,'center:yes; dialogWidth:'+w+'px; dialogHeight:'+h+'px;');
//}

function urlEncode(str){
	if(!str) return ''
	return escape(str).replace(/\//g,'%2F').replace(/\\/g,'%5C').replace(/\+/g,'%2B')
}

function urlDecode(str){
	if(!str) return ''
	return unescape(str).replace(/\%2F/g,'/').replace(/\%5C/g,'\\').replace(/\%2B/g,'+')
}

function lt(){
	return new Date().getTime()
}

//make str a safe directory or filename and return it
function makeSafePath(str){
	str = str.replace(/ /g,'_').replace(/[^a-zA-Z0-9_\-.]/g,'')
	return str;
}

//******************************************
// ObjToXML
//	recursively takes an object as parameter
//	"obj" and parses it into XML.
//	*if optional [cont] parameter is passed in
//	the object will be wrapped in the container tags.
//	*if object is an array, the "setTag" parameter should also be passed in
//	to tell the function what tag to wrap the array elements with
//******************************************
function ObjToXML(obj,setTag,cont){

	var xml = (cont)? '<'+cont+'>\n':'' //wrap xml in "cont" tags if cont is not null
	for(var i in obj) {
			if(obj[i] && typeof obj[i] == 'object') {
				var tag = (setTag)? setTag:i
				if((obj.length && obj.length > 1) || !obj.length) xml += '<'+tag+'>\n'
				//if object is an array set the tag to be 'item' instead of the default int which is invalid xml
				xml += ObjToXML(obj[i],((obj[i] && obj[i].sort && obj[i].reverse)? 'item':''))
				if((obj.length && obj.length > 1) || !obj.length) xml += '</'+tag+'>\n'  //if object property is an object, run ObjToXML on that object (recursive)
			} else {
				var tag = (setTag)? setTag:i
				xml += 	'<'+tag+'><![CDATA['+XMLescape(obj[i])+']]></'+tag+'>\n'
			}
	}
	xml += (cont)? '</'+cont+'>\n':'' //wrap xml in "cont" tags if cont is not null
	return xml
}

//******************************************
// XMLescape
//	escape any special xml characters
//******************************************
function XMLescape(str){
	if(!str || !String(str)) return '';
	return String(str).replace(/&/g,'&amp;').replace(/'/g,'&apos;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;')
}

//******************************************
// XMLunescape
//	unescape any special xml characters
//******************************************
function XMLunescape(str){
	if(!String(str)) return '';
	return String(str).replace(/&amp;/g,'&').replace(/&apos;/g,'\'').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"')
}




