/**
 * @author dallas
 */
Ext.ns('Rebul');
Ext.ns('Rebul.util');
Ext.BLANK_IMAGE_URL = 'http://js.bokkaassets.com/libraries/ext/resources/images/default/s.gif';

Rebul.ROWLIMIT = 20;

// overrides used
Ext.override(Ext.Panel, {
	hideTbar: function() {        
		this.tbar.setVisibilityMode(Ext.Element.DISPLAY);
		this.tbar.hide();
		this.syncSize();
		if(this.ownerCt)
			this.ownerCt.doLayout();
	}
});

Rebul.Store = function(config){
	var config = config || {};
	Ext.applyIf(config, {
		root: 'data',
		totalProperty: 'recordcount',
		id: 'INTID',
		remoteSort: true
	});
	
	Rebul.Store.superclass.constructor.call(this,config);	
};
Ext.extend(Rebul.Store, Ext.data.JsonStore);

getQSVar = function(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable)
			return pair[1];
	}  
}

findLabel = function(field) {
    var wrapDiv = null;
    var label = null;
    wrapDiv = field.getEl().up('div.x-form-item');
    if(wrapDiv)
        label = wrapDiv.child('label');        
    if(label)
        return label;
}

addWorkingDays = function(myDate,days) {
	var temp_date = new Date();
	var i = 0;
	var days_to_add = 0;
	while (i < (days)){
		temp_date = new Date(myDate.getTime() + (days_to_add*24*60*60*1000));
		if ((temp_date.getDay() != 0) && (temp_date.getDay() != 6)) 
			i+=1;
		days_to_add += 1;
	}
	return new Date(myDate.getTime() + days_to_add*24*60*60*1000);
}

dumpProps = function(obj, parent) {
   // Go through all the properties of the passed-in object
   for (var i in obj) {
      // if a parent (2nd parameter) was passed in, then use that to
      // build the message. Message includes i (the object's property name)
      // then the object's property value on a new line
      if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
      // Display the message. If the user clicks "OK", then continue. If they
      // click "CANCEL" then quit this level of recursion
      if (!confirm(msg)) { return; }
      // If this property (i) is an object, then recursively process the object
      if (typeof obj[i] == "object") {
         if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
      }
   }
}

$import = function(src){
  var scriptElem = document.createElement('script');
  scriptElem.setAttribute('src',src);
  scriptElem.setAttribute('type','text/javascript');
  document.getElementsByTagName('head')[0].appendChild(scriptElem);
}

$importNoCache = function(src){
  var ms = new Date().getTime().toString();
  var seed = "?" + ms; 
  $import(src + seed);
}

loadExtraFiles = function(){
	if(!Rebul.Item){
		$import('http://scripts.rebul.com.au/min/?f=Item.js');
	}
	if(!Rebul.Order){
		$import('http://scripts.rebul.com.au/min/?f=Order.js');
	}
	//if(!Ext.ux.form.XDateField){
	//	$import('http://scripts.rebul.com.au/min/?f=libraries/ext/ux/form/XDateField.js');
	//}
}

checkSession = function(){
	Ext.Ajax.request({
		url:'/jsonSession.cfm',
		failure:function(){
			location.href = '/login.cfm?expired';
		}
	});
}

