/**
cd C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\flyswat
java -jar minify.jar include/functions.js -o functions-min.js
*/


  

function centerElementHoriz(container, el, offset){
    if(typeof offset == "undefined"){
       offset = 0;
    }
    //alert(el.tagName + " " + js_get_elementWidth(container) + " " + js_get_elementWidth(el));
    YAHOO.util.Dom.setStyle(el, 'position','relative');   
    YAHOO.util.Dom.setStyle(el, 'left', (js_get_elementWidth(container)/2) - (js_get_elementWidth(el)/2) + offset + 'px');
}
       
function browserSupportsAjax(){
      
      //Mozilla-based browsers
      if(window.XMLHttpRequest){ 
          return true;
      } 
      else if (window.ActiveXObject){ // IE
          request=new ActiveXObject("Msxml2.XMLHTTP");
          if (! request){ 
              request=new ActiveXObject("Microsoft.XMLHTTP");
              if(request){
                  return true;
              }
          }
      }
      return false;

}

function addURLParam(sURL, sParamName, sParamValue) {
	sURL += (sURL.indexOf("?") == -1 ? "?" : "&");
	sURL += encodeURIComponent(sParamName) + "=" + encodeURIComponent(sParamValue);
	return sURL;
}
	
function onPostFormSuccessCallback(formSubmittedSuccessCallback){
      return (function(oRequest){
          formSubmittedSuccessCallback(oRequest.responseText);
      });
}
   
function onPostFormFailureCallback(){
      return (function(oRequest){
          FLYSWAT.widget.alertBox("Form submission error. Please try again " + oRequest.statusText);
      });
}     

function submitForm(frm, submitUrl, formSubmittedSuccessCallback){
    try{
       YAHOO.util.Connect.setForm(frm);
       var successcallback = onPostFormSuccessCallback(formSubmittedSuccessCallback);
       var failurecallback = onPostFormFailureCallback();
       FLYSWAT.util.Connect.send(successcallback, failurecallback, submitUrl, "", "POST");
    }
    catch(err){
       alert(err + ':submitForm');
    }
}

function onSubmitFormClick(frm, submitUrl, formSubmittedSuccessCallback){
   return (function(e){
       try{
          if(e){
             YAHOO.util.Event.stopEvent(e); // stop submit button from submitting form
          }
          submitForm(frm, submitUrl, formSubmittedSuccessCallback);
       }
       catch(err){
          alert(err + 'onSubmitFormClick');
       }
   });   
}

/**
 * Searches subject for a match to the regular expression given in pattern.
 * 
 * @param string pattern
 * @param string subject
 * @param array matches
 * @return int Number of times pattern matches.
 * ref: http://www.devshed.com/c/a/JavaScript/Understanding-the-JavaScript-RegExp-Object/7/
 */
function preg_match(pattern, subject, matches){
 
    regObj = new RegExpr(pattern);
    matches = regObj.execute(subject);
    return matches.length; 
 
}

/**
* Gets the middle vertical point of the page
*/
function js_get_screenCenterY(){
   return ( window.outerHeight - js_get_scrollTop())/2;
}

/**
* Gets the middle horizontal point of the page.
*/
function js_get_screenCenterX(){
   return (window.outerWidth - js_get_scrollLeft())/2;
}

/**
* Gets the vertical position of an element.
*
* Credit: http://www.aspandjavascript.co.uk/javascript/javascript_api/get_element_top_left.asp
*/
function getElementTop(elem) {

    yPos = elem.offsetTop;
    tempEl = elem.offsetParent;
    while (tempEl != null) {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}

/**
* Gets the horizontal position of an element.
*
* Credit: http://www.aspandjavascript.co.uk/javascript/javascript_api/get_element_top_left.asp
*/
function getElementLeft(elem) {

    xPos = elem.offsetLeft;
    tempEl = elem.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;

}

/**
* Gets the height of an element.
*
* Credit: http://www.aspandjavascript.co.uk/javascript/javascript_api/get_element_width_height.asp
*/
function js_get_elementHeight(elem) {

    if (elem.style.pixelHeight) {
       xPos = elem.style.pixelHeight;
    } 
    else {
       xPos = elem.offsetHeight;
    }
    return xPos;

}

/**
* Get the width of an element
*
* Credit: http://www.aspandjavascript.co.uk/javascript/javascript_api/get_element_width_height.asp
*/
function js_get_elementWidth(elem) {

    if (elem.style.pixelWidth) {
       xPos = elem.style.pixelWidth;
    } 
    else {
      xPos = elem.offsetWidth;
    }
    
    return xPos;

}

/**
* Sets an element's opacity.
* @param obj el.
* @param int val - value from 0 to 10.
* Credit: <a href="http://www.quirksmode.org/js/opacity.html">quirksmode.org</a>
*/
function js_set_opacity(el,value){
    el.style.opacity = value/10; 
    el.style.filter = 'alpha(opacity=' + value*10 + ')'; // IE.
}



/**
* Gets the amount the page has been scrolled down.
*
* Credit:  http://www.quirksmode.org/viewport/compatibility.html
*/
function js_get_scrollTop(){

    if (self.pageYOffset) // all except Explorer
    {
        y = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
        // Explorer 6 Strict
    {
        y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
        y = document.body.scrollTop;
    }

   return y;

}

/**
* Gets how much the page has been scrolled left.
*
* Credit: http://www.quirksmode.org/viewport/compatibility.html
*
*/
function js_get_scrollLeft(){

    if (self.pageYOffset) // all except Explorer
    {
        x = self.pageXOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
        // Explorer 6 Strict
    {
        x = document.documentElement.scrollLeft;
    }
    else if (document.body) // all other Explorers
    {
        x = document.body.scrollLeft;
    }

    return x;

}

/**
* Move element to vertical point.
*
* @param object element
* @param int yPoint
*/
function moveToY(element, yPoint){
     element.style.position = 'absolute';
     element.style.top = yPoint;
}

/**
* Move element to horizontal point.
*
* @param object element
* @param int xPoint
*/
function moveToX(element, xPoint){
     element.style.position = 'absolute';
     element.style.left = xPoint;
}

/**
* Move element horizontally.
*
* @param object element
* @param int shiftHorizontal
*/
function moveHoriz(element, shiftHoriz){
     element.style.position = 'relative';
     element.style.left = shiftHoriz;
}

/**
* Move element vertically.
*
* @param object element
* @param int shiftVertical
*/
function moveVert(element, shiftVertical){
     element.style.position = 'relative';
     element.style.top = shiftVertical;
}



/*
http://www.quirksmode.org/js/events_properties.html
*/
function getMousePos(e){

	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	
	return new Array(posx, posy);


}

function ajax(successcallback, failurecallback, uri, query, method){

   //var successcallback = onSaveChangesSuccessCallback(indicator);
   //var failurecallback = onSaveChangesFailureCallback(indicator);
   // Assign callback functions.
   
   var callback =
   {
      success:successcallback,
      failure:failurecallback
   };

    // Do the ajax call.
    if(method.toUpperCase()=="GET"){
        uri = uri+"?"+query;
    }
    var req = YAHOO.util.Connect.asyncRequest(method, uri, callback, query);

}

/**
* Ref: http://lists.firepipe.net/pipermail/cwe-lug/2004-September/001934.html
*/
function print_r(obj, cont, tab){

        var propertyList = '';
        var val = '';
        for (prop in obj){
                eval("val = 'obj.' + prop")
                propertyList += prop + '=&gt;' + eval(val);
                propertyList +=  "\n";
        }
        
        return propertyList;

}

function toggleDisableItem(item){
   item.disabled = item.disabled?false:true;
}
   
function nukeSelValues(sel){
   sel.options.length = 0;
}
   
function getTarget(e){
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
    return targ;
}

function initSelect(sel, def, optValues, optStrings){
    var i=0;
    var opt = null;
    var str = null;
    
    //alert(optValues + optStrings + " " + def);

    for(i=0;i<optValues.length;i++){
        opt = document.createElement("OPTION");
        opt.setAttribute("value", optValues[i]);
        if(def==optValues[i]){
           opt.setAttribute("selected",true);
        }
        str = document.createTextNode(optStrings[i]);
        opt.appendChild(str);
        sel.appendChild(opt);
    }

}	

   
	function highlightInputElement(element){
	    element.style.border = "2px solid #ff0000";
	    element.focus();
	}

function toCurrency(val){
  if(!val) {
	  return false;
  }
  
  if(isNaN(val)){
	  return false;
  }
  val=""+Math.round(100*val); 
  var dec_point=val.length-2;
  var first_part=val.substring(0,dec_point);
  var second_part=val.substring(dec_point,val.length);
  var result=first_part+"."+second_part;

  // "3" + decimal point + "14"
  return result;
}


function removefromwatchlist(id, loggedIn, msgNotLoggedIn, msgRemoved) {					

        if(!loggedIn){
            FLYSWAT.widget.alertBox(msgNotLoggedIn);
        }
        else{
			var sURL = "ajax-validation.php?run=watchlist-remove&id=" + id;
			var json;
			var oRequest = new XMLHttpRequest();
			oRequest.open("get", sURL, true);
				
			oRequest.onreadystatechange = function() {
					
				if(oRequest.readyState == 4) {
					if(oRequest.status == 200 ){
							
						json = "json=" + oRequest.responseText;
						json = eval(json);
							
						if (json[0].text=='yes') { 
						    //YAHOO.util.Dom.setStyle(container, "display", "none");
							FLYSWAT.widget.alertBox(msgRemoved); 
							document.location.reload();
						} 
						else {  FLYSWAT.widget.alertBox("Error removing auction"); }
						
							
					}  // if(oRequest.status == 200 ){
					
				} // if(oRequest.readyState == 4) {
	
			} // oRequest.onreadystatechange = function()
			oRequest.send(null);
		}
} // email.onblur = function(

function addwatchlist(id, loggedIn, msgNotLoggedIn, msgAdded, msgInBuzzList) {					

        if(!loggedIn){
            FLYSWAT.widget.alertBox(msgNotLoggedIn);
        }
        else{
			var sURL = "ajax-validation.php?run=watchlist-add&id=" + id;
			var json;
			var oRequest = new XMLHttpRequest();
			oRequest.open("get", sURL, true);
				
			oRequest.onreadystatechange = function() {
					
				if(oRequest.readyState == 4) {
					if(oRequest.status == 200 ){
						//alert(oRequest.responseText);	
						json = "json=" + oRequest.responseText;
						json = eval(json);
							
						if (json[0].text=='yes') { FLYSWAT.widget.alertBox(msgAdded); } else {  FLYSWAT.widget.alertBox(msgInBuzzList); }
						
							
					}  // if(oRequest.status == 200 ){
					
				} // if(oRequest.readyState == 4) {
	
			} // oRequest.onreadystatechange = function()
			oRequest.send(null);
		}
} // email.onblur = function()
	     
       
function type(s){
   return s.constructor;
}

