// Core Titan JS functions

//Base Namespace
if (Titan == undefined) var Titan = {}; // Make sure the base namespace exists.

Titan.Core = function() {
	
	this.util = new Titan.Util();
	this.unsavedChanges = false;
	
	// make sure the prototype.js is loaded before titan_core.js
	Event.observe(window, 'beforeunload',  this.checkForUnsavedChanges.bind(this));
	
}


Titan.Core.prototype = {
	
	setUnsavedChangesFlag: function() {
		this.unsavedChanges = true;
	},
		
	clearUnsavedChangesFlag: function() {
		this.unsavedChanges = false;
	},

	hasUnsavedChanges: function() {
		return this.unsavedChanges;
	},

	checkForUnsavedChanges: function (ev) 
	{
		if ( this.unsavedChanges ) 
		{
		 	ev.returnValue = "You have unsaved information.";
			//return "It appears you have made changes to data on this page. If you do not save, your changes will be lost.  Are you sure you want to exit this page?";
		}
	}
	
}


Titan.Util = function() {}

Titan.Util.prototype = {

	/*	
	var num_fields = "";
	var alpha_fields = "";
	var namechar_fields = "";
	var graphic_fields = "";
	var creditcard_fields = "";
	var creditcardexp_fields = "";
	var email_fields = "";
	var money_fields = "";
	var phone_fields = "";
	var forceSelect_fields = ""; 
	var zip_fields = "";
	var anyall_fields = "";
	 */

	isSimpleDate: function(sVal) {
		return !isNaN(Date.parse(sVal));
	},
	
	isDateTime: function(sVal) {
		// add regex check later
		return true;
	},
	
	isEmail: function(sVal) {
		// this could cause problems with some emails?
     	var regex = new RegExp("^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$");
 		return regex.test(sVal);
	},
	
	isNumeric: function(sVal) {
		return !isNaN(sVal);
	},
	
	isInteger: function(s) {
      var i;

      if (this.isEmpty(s)) {
	      if (this.isInteger.arguments.length == 1) return 0;
    	  else return (this.isInteger.arguments[1] == true);
      }

      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);

         if (!this.isDigit(c)) return false;
      }

      return true;
   },

	isEmpty: function(s) {
		return ((s == null) || (s.length == 0))		
	},


   isDigit: function(c) {
      return ((c >= "0") && (c <= "9"))
   },
	
	
	
	Struct: function() {
		this.members = $H( {} );
		this.structFind = function(_key) { return this.members.get(_key); };
		this.getItems = function() { return this.members; };
		this.structInsert = function(_key,_value ) { this.members.set(_key,_value); };
		this.structCount = function() {	return this.members.size(); };
		this.structDelete = function(_key) { var old = this.members.remove(_key); }; //change to unset if we upgrade prototype	
		/* reuse for structSort? 
		valueList = Object.keys(thisItem.valueList.value);
		var sortArray = new Array();		
		for(var i=0; i<valueList.length; i++){
		sortArray[i] = {ordernum:thisItem.valueList.value[valueList[i]].ordernum, thisid:valueList[i]};
		}		
		sortArray.sort(
		function(a,b){
		var x = a.ordernum;
		var y = b.ordernum;
		return x - y}
		) ;		
		for(var i=0; i<sortArray.length; i++){
		addValueListItem(thisItem.valueList.value[sortArray[i].thisid],sortArray[i].thisid);
		}
		*/		
	},
	
	
	addDoubleClick: function(id,target,callback) {
		var oItem = $(id);
		var oTarget = $(target);
		oItem.ondblclick = function() { callback(oTarget); }
	},
			
	removeDoubleClick: function(id) {
		var item = $(id);
		item.ondblclick = null;
	},
	
	
	listFind: function(l,v,d) {
		if(!d){d = ",";}
		var r = 0;
		var listToArray = l.split(d);
		for (var i = 0; i < listToArray.length; i++) {
			if (listToArray[i] == v) {
				r = i + 1;
				break;
			}
		}
		return r;	
	},
	
	
	listAppend: function(l, v, d){
		if(!d){d = ",";}
		var r = ""; 
		if (this.ListLen(l) && l != ""){
			r = l + d + v;
		} else {
			r = v;
		}
		return r;
	},


	listLen: function(l,d){
		if(!d){d = ",";}		
		return l.split(d).length;
	}
	
	
		
}


// create an instance of TitanCore for use in files downstream
titan = new Titan.Core();


function goto(redirect_url){
	
	// make AJAX call to retrieve temporary token
	var token_url = '?event=auo.token';

	// no longer appending authToken
	document.location.href = redirect_url;

	new Ajax.Request(
		token_url,
		{
			  method: 'get'
			, onSuccess: function(transport) 
				{ 
					var token = transport.responseText;
					
					//alert(token.length);
					return false;
					
					/*
					if( transport.responseText.length > 0 && transport.responseText.length < 40 ) {
						document.location.href = redirect_url + '&authToken=' + token;
					}
					else {
						document.location.href = redirect_url;
					}
					*/
					
				}
			, onFailure: function(){ document.location.href = redirect_url; }
		}, this
	);
	
	//return false;
}



