/*
 * File:					main.js
 * Title:					General JS Functions
 * Description:		Contains our general,visual/DOM, and non-AJAX functions
 * Author:				Dan Bair and Kevin Huisman, Florentine Design (www.florentinedesign.com)
 * NOTE:					This file (main.js) MUST be attached BEFORE ajax.js
 */


 /*	Visual, DOM, and Data Functions
--------------------------------------------------------->>
--------------------------------------------------------->>
 */



// these variables will hold the row ID of the selected rows in each box
var selectedItem = Array();
var selectedCustomer = Array();

function encodeSearch(query){
	var SAFECHARS = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "-_.!~*'()";
	var HEX 			= "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < query.length; i++ ) {
		var ch = query.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

return encoded;
}

function autoItemSelect(field, select, property, forcematch) {
	var found = false;
	for (var i = 0; i < select.options.length; i++) {
		if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
			found=true; break;
		}
	}
	if (found){
		select.selectedIndex = i;
	}
	else {
		select.selectedIndex = -1;
	}
	if (field.createTextRange) {
		if (forcematch && !found) {
			field.value=field.value.substring(0,field.value.length-1);
			return;
		}
		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		if (cursorKeys.indexOf(event.keyCode+";") == -1) {
			var r1 = field.createTextRange();
			var oldValue = r1.text;
			var newValue = found ? select.options[i][property] : oldValue;
			if (newValue != field.value) {
				field.value = newValue;
				var rNew = field.createTextRange();
				rNew.moveStart('character', oldValue.length) ;
				rNew.select();
			}
		}
	}
}

var phone_field_length = 0;
function TabNext(obj,event,len,next_field) {
	if (event == "down") {
		phone_field_length=obj.value.length;
	}
	else if (event == "up") {
		if (obj.value.length != phone_field_length) {
			phone_field_length=obj.value.length;
				if (phone_field_length == len) {
					next_field.focus();
				}
		}
	}
}
function reportError(e) {
  alert(e);
}

function highlight(targetDIV){
	new Effect.Highlight(targetDIV);
}

function show(what){
	Element.show(what);
}

function hide(what){
	Element.hide(what);
}

function showLoader(){
	Element.show('loading');
}

function hideLoader(){
	Element.hide('loading');
}

function showError(){
	Element.show('error');
}

function hideError(){
	Element.hide('error');
}

function scannerActive(){
		Element.hide('hideScanner');
		Element.show('showScanner');
}
function scannerInactive(){
		Element.hide('showScanner');
		Element.show('hideScanner');
}

function clearBoxForm( form ){
var clear = confirm( "Are you sure you wish to clear all current data? Once data is cleared, it may not be retrieved." );
if(clear)
	Form.reset(form);
else
	return false;
}

function confirmConfirm(type,URL){ //example: confirmConfirm('change','?action=editSomething&id=85')
var send = confirm("Confirm this " + type + "?"); // Output might be: "Confirm this change?"
if(send)
	window.location = url;
else
	return false;
}

function popWindow(URL){ // this function will later be adjusted according to needs
	var win = null;
	LeftPosition = (screen.width) ? (screen.width-216)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-216)/2 : 0;
	address = URL;
	settings ='height=216,width=216,top= ' + TopPosition + ',left= ' + LeftPosition + ',scrollbars=no,resizable=1';
	win = window.open(address,'Preview',settings);
}


/// customer object to control customer input, selection, etc.

custObj = {
  _url : "index.php",
  _id : null,
  _csort : "last",

  getCustomerBrowse : function (_sort) {
    this._csort = (_sort == undefined) ? "last" : _sort ;
    this.doAjax("getCustomerBrowse") ;
  },

  getCustomerProfile : function (_id) {
    this._id = _id ;
    this.doAjax("getProfile") ;
  },
  doAjax : function (_type) {
    var params = "" ;
    var ret = {} ;
    switch (_type) {
      case "getProfile" :
        ret = { success: 'customer_profile' } ;
        params = 'action=getCustomerProfile&cid=' + this._id ;
        hide('customer_browse');
        show('edit');
        break ;
      case "getCustomerBrowse" :
        ret = { success: 'customer_browse_data' } ;
        params = 'action=getCustomerBrowser&csort=' + this._csort ;
        break ;
    }

		var ajax = new Ajax.Updater(ret,	this._url,
		  {
    		method      : 'get',
    		evalScripts	: true,
    		onComplete	: function(request){custObj.performResponse(request, _type)},
  		  onLoading	  : function(request){},
  		  parameters  : params,
  		  onFailure   : function(request){}
  		 }
  	);
  },
  performResponse : function(request, _type) {

    switch (_type) {
      case "getProfile" :
        break ;
      case "getCustomerBrowse" :
        break ;
    }
  }

}

/*
 * Carosel Stuff for color choices
 */

function initCarousel_html_carousel() {
	carousel = new Carousel('html-carousel',
	  { animHandler:animHandler,
	    animParameters:{duration:0.5},
		  buttonStateHandler:buttonStateHandler,
		  nextElementID:'next-arrow',
		  prevElementID:'prev-arrow', size:31}) ;
};


function buttonStateHandler(button, enabled) {
 if (button == "prev-arrow")
   $('prev-arrow').src = enabled ? "images/left3-enabled.gif" : "images/left3-disabled.gif" ;
 else
   $('next-arrow').src = enabled ? "images/right3-enabled.gif" : "images/right3-disabled.gif" ;
}

function animHandler(carouselID, status, direction) {
  var region = $(carouselID).down(".carousel-clip-region") ;
  if (status == "before") {
    Effect.Fade(region, {to: 0.3, queue: { position:'end', scope: "carousel" }, duration: 0.2}) ;
  }
  if (status == "after") {
    Effect.Fade(region, {to: 1, queue: { position:'end', scope: "carousel" }, duration: 0.2}) ;
  }
}
