/* 
// +----------------------------------------------------------------------+
// | Javascript                                                           |
// +----------------------------------------------------------------------+
// | Copyright (c) 2008 Herman Veluwenkamp                                |
// +----------------------------------------------------------------------+
// | Authors: Herman Veluwenkamp <hermanV@fastmail.fm>                    |
// |                                                                      |
// +----------------------------------------------------------------------+
//
*/

//var HV = window.HV || {};

var HV = new Object;

HV.Array = {
  indexExists: function(array_search, array_index) {
    var found = false;
    for (var i=0; i<array_search.length; i++){
      if (array_index == array_search[i]) {
        found = true;
        break;
      }
    }
    return found;
  }
}


HV.getArgs = function() {
  var args  = new Object(  );
  var query = location.search.substring(1);     
  var pairs = query.split("&");

  for(var i = 0; i < pairs.length; i++) {
    var pos = pairs[i].indexOf('=');
    if (pos == -1) continue;
    var argname = pairs[i].substring(0,pos);
    var value = pairs[i].substring(pos+1);
    args[argname] = decodeURIComponent(value);
  }
  return args;
}


HV.Window = {
  
  newWindowWidth:  400,
  newWindowHeight: 400,
  
  //newWindowOptions: 'dependent=yes',
  //newWindowOptions: 'dialog ',
  newWindowOptions: 'alwaysRaised=yes',
  
  close:          function()    { window.close(); },
  
  refresh:        function()    { window.document.location.reload();        },
	reloadParent:   function()    { parent.window.document.location.reload(); },
	
  redirect:       function(url) { window.document.location.href = url;          },
  replace:        function(url) { window.document.location.replace(url);        },
  redirectParent: function(url) { parent.window.document.location.href = url;   },
  replaceParent:  function(url) { parent.window.document.location.replace(url); },

  open: function(url, width, height) {
    try {
      if (width==undefined)  width  = HV.Window.newWindowWidth;
      if (height==undefined) height = HV.Window.newWindowHeight;
      
      var window_name = url.slice(0, url.indexOf('?'));
      
      var options = 'resizable,scrollbars,width='+width+',height='+height;
      
      if (HV.Window.newWindowOptions) options = options + ',' + HV.Window.newWindowOptions;
      
      //alert(options); return;
      
      newwin = window.open(url, window_name, options);
      
      //newwin.focus();
            
    } catch (e) { }
  }
  
}


HV.sendToErrorWindow = function(message) {
  var win = window.open('', 'win', 'width=600,height=400,resizable,scrollbars');
  win.document.open('text/html');
  win.document.write('<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"\><title>Error</title></head><body>');
  //win.document.write(Utf8.encode(message));
  win.document.write(message);
  win.document.write("</body></html>");
  win.document.close();
}


HV.Logger = {
  
  log: function(data, category, source) {
    HV.Debug.append(data, category, source); return;
  },
  
  logObj: function(obj) {
    HV.Debug.appendObj(data, category, source); return;
  }
  
}


HV.selectOnSingleResult = function() {
  //_p('selectOnSingleResult');
  
  HV.Form.resetStatusBar();
  
  var metadata_status = $('urn:status:result');
  //_p(metadata_status);
  
  if (metadata_status==null) {
    setTimeout(HV.selectOnSingleResult, 100);
    
  } else {
    
    //HV.Form.tree_select= $([HV.Form.form_name, HV.Form.config_name+'_tree_select']);
    //_p(HV.Form.tree_select.view.rowCount);
    //_po(HV.Form.tree_select.view);
    
    
    if (!HV.Form.tree_select.view) { // for some reason tree view is null after record is selected
      HV.Animate.setMode('select');
      return;
      
    } else {
      
      var row_count = HV.Form.tree_select.view.rowCount;
      if (row_count==1) {
        try {
          var col       = HV.Form.tree_select.columns[HV.Form.field_list_id];
          var record_id = HV.Form.tree_select.view.getCellText(0, col);
          //_p(record_id);
          HV.Form.selectRecord(record_id);
        } catch(e) {
          alert(HV.Form.field_list_id)
        }

      } else {
        HV.Animate.setMode('select');
      }
    }
  }
}



/**
*
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Utf8 = {
 
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}




