//////////////////////////////////////////////////////////////////////////////////////////////////////////
// INFOSOFT SA DE CV
// Date:   2011-09-11
// Updates:2011-09-11
// Author:Rodolfo Machon
// File Name: control.js BSB v2.0
// Description:
//////////////////////////////////////////////////////////////////////////////////////////////////////////
var id;
var xmlHttp;
var xmlHttpStream;
var openwindow;
var isIE = document.all?true:false;
var isNS = document.layers?true:false;
var OW   = new Object();
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// LEVEL 1
function GetPageData(){
    GetParams();
}
function GetParams(){
    var query   = window.location.search.substring(1);
    var params  = query.split('&');
    if(params[0]){var la = params[0].split('=');lan   = la[1];}	
    if(params[1]){var ap = params[1].split('=');app   = ap[1];}
    if(params[2]){var cl = params[2].split('=');clase = cl[1];}
    if(params[3]){var ii = params[3].split('=');id = ii[1];}
}
function GetClase(id){
    //alert(id);
    var URL	= "/cgi-bin/"+app+"/bin/controlp.exe?lan="+lan+"&app="+app+"&clase="+clase+"&id="+id;
    xmlHttp	= GetXmlHttpObject(SC_GetClase);
    xmlHttp.open("GET",URL,true) ;
    xmlHttp.send(null);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// LEVEL 2
function GetXmlHttpObject(handler){
	var objXmlHttp=null;	
	if(window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		objXmlHttp = new XMLHttpRequest();
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 		
	}
	if(window.ActiveXObject){// code for IE6, IE5
		objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		objXmlHttp.onreadystatechange=handler 		
	}
	if(objXmlHttp==null){alert("Your Browser Does Not Suppport AJAX")}
	return objXmlHttp;
}
function SC_GetClase(){
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
	xmlHttpStream = xmlHttp.responseText;
	if(xmlHttpStream!=""){
	    if (document.getElementById('clase_ul_div')){
		var CUD   = document.getElementById('clase_ul_div')
		CUD.innerHTML = xmlHttpStream;
		//alert(CUD.innerHTML);
	    }
	}
    } 
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
function OpenOption(containertype,component,componenttype,clase,formulario,id_field,win_properties){
    var id_value = "";
    var type     = (componenttype=="EXE") ? 'bin':componenttype;
    var command  = component+"."+componenttype;
    var win_name = clase+"_"+component;
    if(id_field!=""){
	id_value = eval("document."+formulario+"."+id_field+".value");
	if(id_value==""){
	    alert('Please Choose ID Firts');
	    return;
	}
    }	
    if (!OW[win_name] || OW[win_name].closed){
	if(containertype=="PW"){
	    win ="window.open('/cgi-bin/"+app+"/"+type+"/"+command+"?lan="+lan+"&app="+app+"&clase="+clase+"&id="+id_value+"','"+component+clase+"','"+win_properties+"')"
	    OW[win_name]=eval(win);
	}
	if(containertype=="TW"){
	    win ="window.open('/cgi-bin/"+app+"/"+type+"/"+command+"?lan="+lan+"&app="+app+"&clase="+clase+"&id="+id_value+"','_blank')"
	    eval(win);
	}
    }else{OW[win_name].focus()}
}
function addEventSimple(obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt,fn);
}
function removeEventSimple(obj,evt,fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)
		obj.detachEvent('on'+evt,fn);
}
dragDrop = {
	keyHTML: '<a href="#"></a>',
	keySpeed: 10, // pixels per keypress event
	initialMouseX: undefined,
	initialMouseY: undefined,
	startX: undefined,
	startY: undefined,
	dXKeys: undefined,
	dYKeys: undefined,
	draggedObject: undefined,
	initElement: function (element) {
		if (typeof element == 'string')
			element = document.getElementById(element);
		element.onmousedown = dragDrop.startDragMouse;
		element.innerHTML += dragDrop.keyHTML;
		var links = element.getElementsByTagName('a');
		var lastLink = links[links.length-1];
		lastLink.relatedElement = element;
		lastLink.onclick = dragDrop.startDragKeys;
	},
	startDragMouse: function (e) {
		dragDrop.startDrag(this);
		var evt = e || window.event;
		dragDrop.initialMouseX = evt.clientX;
		dragDrop.initialMouseY = evt.clientY;
		addEventSimple(document,'mousemove',dragDrop.dragMouse);
		addEventSimple(document,'mouseup',dragDrop.releaseElement);
		return false;
	},
	startDragKeys: function () {
		dragDrop.startDrag(this.relatedElement);
		dragDrop.dXKeys = dragDrop.dYKeys = 0;
		addEventSimple(document,'keydown',dragDrop.dragKeys);
		addEventSimple(document,'keypress',dragDrop.switchKeyEvents);
		this.blur();
		return false;
	},
	startDrag: function (obj) {
		if (dragDrop.draggedObject)
			dragDrop.releaseElement();
		dragDrop.startX = obj.offsetLeft;
		dragDrop.startY = obj.offsetTop;
		dragDrop.draggedObject = obj;
		obj.className += ' dragged';
	},
	dragMouse: function (e) {
		var evt = e || window.event;
		var dX = evt.clientX - dragDrop.initialMouseX;
		var dY = evt.clientY - dragDrop.initialMouseY;
		dragDrop.setPosition(dX,dY);
		return false;
	},
	dragKeys: function(e) {
		var evt = e || window.event;
		var key = evt.keyCode;
		switch (key) {
			case 37:	// left
			case 63234:
				dragDrop.dXKeys -= dragDrop.keySpeed;
				break;
			case 38:	// up
			case 63232:
				dragDrop.dYKeys -= dragDrop.keySpeed;
				break;
			case 39:	// right
			case 63235:
				dragDrop.dXKeys += dragDrop.keySpeed;
				break;
			case 40:	// down
			case 63233:
				dragDrop.dYKeys += dragDrop.keySpeed;
				break;
			case 13: 	// enter
			case 27: 	// escape
				dragDrop.releaseElement();
				return false;
			default:
				return true;
		}
		dragDrop.setPosition(dragDrop.dXKeys,dragDrop.dYKeys);
		if (evt.preventDefault)
			evt.preventDefault();
		return false;
	},
	setPosition: function (dx,dy) {
		dragDrop.draggedObject.style.left = dragDrop.startX + dx + 'px';
		dragDrop.draggedObject.style.top = dragDrop.startY + dy + 'px';
	},
	switchKeyEvents: function () {
		// for Opera and Safari 1.3
		removeEventSimple(document,'keydown',dragDrop.dragKeys);
		removeEventSimple(document,'keypress',dragDrop.switchKeyEvents);
		addEventSimple(document,'keypress',dragDrop.dragKeys);
	},
	releaseElement: function() {
		removeEventSimple(document,'mousemove',dragDrop.dragMouse);
		removeEventSimple(document,'mouseup',dragDrop.releaseElement);
		removeEventSimple(document,'keypress',dragDrop.dragKeys);
		removeEventSimple(document,'keypress',dragDrop.switchKeyEvents);
		removeEventSimple(document,'keydown',dragDrop.dragKeys);
		dragDrop.draggedObject.className = dragDrop.draggedObject.className.replace(/dragged/,'');
		dragDrop.draggedObject = null;
	}
}
function OpenDiv(divID) {
	dragDrop.initElement(document.getElementById(divID));
}