var _browser = navigator.userAgent.toLowerCase();
var _browser_ver = navigator.appVersion.substring(0,1);
var _browser_version = navigator.appVersion;
var _request;	// ajax
var t_code = '';		// items temp





  function move(selectId, incr) {
       // var objSelect = document.myForm.ts;
       var objSelect = document.getElementById(selectId);
       var index = objSelect.selectedIndex;
       if (index+incr >= objSelect.options.length || index+incr < 0) {
            if (index+incr >= objSelect.options.length) {
                 alert("Cannot move bottom item down");
            } else {
                 alert("Cannot move top item up");
            }
       } else {
            var objOption = objSelect.options[index+incr];
            var text = objOption.text
            var value = objOption.value
            var oldOption = new Option(text, value);
            var newOption = objSelect.options[index];
            objOption.text = newOption.text;
            objOption.value = newOption.value;
            newOption.text = oldOption.text;
            newOption.value = oldOption.value;
            oldOption = null;
            objSelect.selectedIndex = index+incr;
       }
  }
  
  function dlte(selectId) {
    var objSelect = document.getElementById(selectId);
    var index = objSelect.selectedIndex;
    objSelect.remove(index);
    return true;
  }
  
  function selectAllOptions(selectId) {
  	a=document.getElementById(selectId); 
  	for(i=0;i<a.length;i++) { 
  		a.options[i].selected=true;
  	}
  	return (true);
  }
  
  function addOption(selectId, val) {
  	a = document.getElementById(selectId);
  	if (val != null) {
  		// a.options[1+a.length] = new Option(val,val);
  		o = document.createElement("option");
  		o.setAttribute("value", val);
  		o.appendChild(document.createTextNode(val));
  		a.appendChild(o);
  		return (true);
  	}
  	return (false);
  }



function wndo(wurl, iw, ih) {
    window.open(wurl,'xsfwin',
              'width='+ iw +', height='+ ih 
                    +', scrollbars=no, resizable=no, location=no, menubar=no, status=yes, toolbar=no');
    return (false);
}


function toggle(what) 
{
	var b = document.getElementById (what)
	var a = b.style.display
	if (a=="block")
	{ b.style.display = "none";	}
	else
	{ b.style.display = "block"; }
}

function expand(what)
{
	var b = document.getElementById (what)
	b.style.display = "block";
}


function hide(what)
{
	var b = document.getElementById (what)
	b.style.display = "none";
}

/*

 ajax_addtocart(code, quant=1) {
 	// call server w/ add to cart request
 	// update item msg area {added to your cart, already in your cart}
 	// update microcart area
 }
 
 
 ajax_updatemicrocart() {
 	// call server w/ get microcart items request
 	// update microcart area
 	
 	//
 	// request may need to supply name of callback function,
 	// e.g. callback_updatemicrocart($content) 
 	//
 	
 	if (ajaxHtml = ajax_request('microcart','')) {
 		divMicrocart.innerHtml = '';
 		divMicrocart.innerHtml = ajaxHtml;
 	}
 }
 
 _browser_ok() {
 	// if browser is supported, return true, else false
 }
 

*/
 
 

function createRequest() {
  try {
    _request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      _request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        _request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        _request = false;
      }
    }
  }
  // debug
  // if (!request) alert("Error initializing XMLHttpRequest!");
}

function ajax_dwait(elementId, wait, icon) {
	// if (wait) document.getElementById(elementId).className = 'wait';
	// else document.getElementById(elementId).className = 'nowait';
	
	var el = document.getElementById(elementId);
	el.className = 'nowait';
	
	if (wait) {
		el.innerHTML = '<img src="'+_webbase+'theme/i/xwait.gif" alt="" border="0" />';
	} else {
		if (icon==null) {
			el.innerHTML = '&nbsp;';
		} else {
			el.innerHTML = '<img src="'+_webbase+'theme/i/icon_'+icon+'.gif" alt="" border="0" />';
		}
	}
}

function ajax_updateshippingtable(providerId) {
	var co = document.getElementById('coid');
	var i_coid = co.options[co.selectedIndex].value;
	// alert (i_coid);					// debug
	// document.getElementById("ship_wait").innerHTML = '...';
	ajax_dwait('ship_wait', true);
	ajax_getshippingtable(i_coid, providerId);
}


function ajax_getshippingtable(coid, providerId) {
	var url = _webbase+"ajaxhtml.php?cmd=shipping&arg=coid%3d"+coid+";;;provider%3d"+providerId;
	// alert ('calling: '+url);			// debug
	
	createRequest();
	_request.open("GET",url,true);
	_request.onreadystatechange = ajax_updateshippinginfo;
	_request.send(null);
}


function ajax_updateshippinginfo() {
	// alert('readystate: '+_request.readyState);	// debug
	if (_request.readyState == 4) {
		if (_request.status == 200) {		// all is good
			var response = _request.responseText;
			// alert (response);
			document.getElementById('shipinfodiv').innerHTML = response;
			ajax_dwait('ship_wait', false);
		}
	}
	
}

function ajax_addtowl(code) {
	if (t_code == '') {
		t_code = code;
		ajax_dwait('wait_'+code, true);
		var url=_webbase+"ajaxhtml.php?cmd=wishlist&arg=act%3dadd;;;code%3d"+code;
		ajax_get(url, ajax_updateitemwlmsg);
	}
}




function ajax_addtocart(code) {
	if (t_code == '') {
		t_code = code;
		ajax_dwait('wait_'+code, true);
		var url;
		if (_sf_per_label_id) {
			url = _webbase+"ajaxhtml.php?cmd=cart&perlid="+_sf_per_label_id+"&arg=act%3dadd;;;code%3d"+code;
		} else
			url = _webbase+"ajaxhtml.php?cmd=cart&arg=act%3dadd;;;code%3d"+code;
			
		ajax_get(url, ajax_updateitemmsg);
	}
}


function ajax_updateitemmsg() {
	if (_request.readyState ==4) { 
		if (_request.status == 200) {
		var resp = _request.responseText;
		document.getElementById('msg_'+t_code).innerHTML = resp;
		if (resp.toLowerCase().indexOf('cart') > -1) {
			ajax_dwait('wait_'+t_code, false); 
		} else {
			ajax_dwait('wait_'+t_code, false); 
		}
		ajax_updatemicrocart();
		t_code = '';
	} else {
		t_code = '';	// so just release t_code 
	}
	}	// rs 4
}


function ajax_updateitemwlmsg() {
	if (_request.readyState ==4) { 
		if (_request.status == 200) {
			var resp = _request.responseText;
			document.getElementById('wlmsg_'+t_code).innerHTML = resp;
			if (resp.toLowerCase().indexOf('cart') > -1) {
				ajax_dwait('wait_'+t_code, false); 
			} else {
				ajax_dwait('wait_'+t_code, false); 
			}
			// ajax_updatemicrowl();	
			t_code = '';
		} else {
			t_code = '';	// s..t happened so just release t_code 
		}
	}	// rs 4
}

function ajax_updatemicrocart() {
	var url=_webbase+"ajaxhtml.php?cmd=microcart&perlid="+_sf_per_label_id;
	ajax_get(url, ajax_updatemicrocart_rs);
}


function ajax_updatemicrocart_rs() {
	if (_request.readyState ==4) { if (_request.status == 200) {
		document.getElementById('divMicrocart').innerHTML = _request.responseText;
	} }
}


function ajax_get(url, callback) {
	createRequest();
	_request.open("GET", url, true);
	_request.onreadystatechange = callback;
	_request.send(null);
}



function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}



function ajax_enableaddtocart() {
	
	var al = getElementsByClass('addtocart');
	var code='';
	var ie = _browser.indexOf('msie')>0;
	for (var i=0;i<al.length;i++) {
		code= al[i].id.substring(4);
		if (!ie) {
			al[i].setAttribute('onclick', "ajax_addtocart('" + code + "')");
			al[i].href = "#"+code;
		} else {
			al[i].href = "javascript:ajax_addtocart('" + code + "')";
		}
		
	}
}


function ajax_enableaddtowl() {
	// try {
		var al = getElementsByClass('addtowishlist');
		var code='';
		var ie = _browser.indexOf('msie')>0;
		for (var i=0;i<al.length;i++) {
			code= al[i].id.substring(5);
			if (!ie) {
				al[i].setAttribute('onclick', "ajax_addtowl('" + code + "')");
				al[i].href = "#"+code;
			} else al[i].href = "javascript:ajax_addtowl('" + code + "')";
		}
	// } catch (ex) { alert (ex); }
}

/*
function ajax_addevent(elmnt,evnt,hndlr) {
	if( elmnt.attachEvent ){
   		elmnt.attachEvent (evnt, hndlr);
	} else {
   		elmnt.setAttribute(evnt, hndlr); 
	}
}
*/

/*

 getelementsbyclass {
 	foreach addtocart as l
 	code = l.id (omit add_)
 	onclick = ajax_addtocart(code)
 }

*/


function okBrowser() {
	var ua = navigator.userAgent.toLowerCase();
	
	// returns false if found msie 5.1, netscape 6, or generally macintosh and os9. 
	// as pc browsers generally behave better.
	// this list may grow with time
	
	// g m
	
	var result = 
		!(
			((ua.indexOf('msie') > -1)  && (navigator.appVersion.substring(0,1)<=6))
			|| 
			(ua.indexOf('aol') > -1) 
			|| 
			(ua.indexOf('netscape') > -1)	
			||
			((ua.indexOf('macintosh')> -1) && (ua.indexOf('os x') < 0))	
		)
	;
	
	// alert(result);	// debug
	return (result);
}


function wrapin(boxid, tagname) {
		var dc=document.getElementById(boxid);
		
		if (dc.selectionStart||dc.selectionEnd) {
			dc.value = dc.value.substring(0,dc.selectionStart) 
			+ '<'+tagname+'>'
			+ dc.value.substring(dc.selectionStart,dc.selectionEnd)
			+ '</'+tagname+'>'
			+ dc.value.substring(dc.selectionEnd,dc.value.length)
		} else 
		{
			alert ('Please select some text to format first'); 
		}
		return (false);
}

function embedplayer(src) {
	document.write('<object id="rvobj" classid="clsid:cfcdaa03-8be4-11cf-b84b-0020afbbccfa" width="99%" height="28">');
	document.write('<param name="src" value="'+src+'" />');
	document.write('<param name="controls" value="ControlPanel" />');
	document.write('<param name="autostart" value="true" />');
	document.write('<embed type="audio/x-pn-realaudio-plugin" width="99%" height="28" autostart="true" nojava="true" src="'+src+'" controls="ControlPanel" />');
	return (false);
}


function pnavigate(catno) {
	window.opener.focus();
	window.opener.location.href = _webbase+'release/?cat='+escape(catno);
}

function pwishme(catno) {
	window.opener.focus();
	window.opener.location.href = _webbase+'wishlist/?act=add&cat='+escape(catno);
}

 function paddtocart(catno) {
	window.opener.focus();
	window.opener.location.href = _webbase+'order/?flash=add,'+escape(catno);
 }

 
 
 document.onkeydown = function(e){ 	
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				switch (keycode) {
					
					case 27:
						if (mclose = document.getElementById('a_mclose')) modal_remove();
						break;
					/* case 190:
						alert('right');
						break;
					case 188:
						alert('left'); */
						
				}
			};


 function modal_remove() {
 	olay=document.getElementById('overlay');olay.removeChild(document.getElementById('modal'));olay.style.visibility='hidden';return false;
 }
			
 
 function sfhtml(link, cat, imgsrc, description) {

 	
 	
 	// alert(link);
 	// alert(cat);
 	// alert(description);
 	
 	thediv = document.createElement("div");
 	thediv.id = "modal";
 	thediv.style.height="400px";
 	thediv.style.width="400px";
 	
 	link = link + "/~"+escape(cat);
 	 
 	thehtml = "<br /><a href=\"" +link + "\"'><img src=\""+imgsrc+"\" /></a><br /><a href=\"" +link + "\"'>" + description+"</a><br />";
 					
 	thediv.innerHTML += "<h2>Post on your site</h2>";
 	
 	thediv.innerHTML += 	"<div>Click this field, then copy to your clipboard:<br /><textarea onclick='this.select();' style='background: #ff9; font-size:12px; height: 100px; width: 380px; margin: 0 auto;'>" 
 					+ thehtml +"</textarea></div>";
 	thediv.innerHTML += "<p style=\"width:380px;\" align=\"right\"><a id=\"a_mclose\" href=\"#\" onclick=\"return (modal_remove());\">Close (ESC)</a></p><hr />";
	thediv.innerHTML += "<p style='width:380px;'>Preview:"+thehtml+"</p>";
 	
 	
 	// xmain = document.getElementById("xmain");
 	// document.getElementById("mainlayout").insertBefore(thediv,xmain);
 	olay = document.getElementById("overlay");
 	olay.appendChild(thediv);
 	olay.style.visibility="visible";
 	
 	return (false);
 
 }