// JavaScript Document


function confirmDelete() {
	if (confirm('Are you sure you want to delete?\n\nAll information associated with this item will be deleted!\n\nThis action is irreversable!')) {
		/*if (!confirm('You can still cancel this action.\n\nWould you like to cancel now? \n(OK will cancel, Cancel will continue to delete.)')) {
			return true;
		} else {
			return false;
		}*/
	} else {
		return false;
	}
}

function confirmCancel() {
	if (confirm('Are you sure you want to cancel?\n\nThis action is irreversable!')) {
		return true;
	} else {
		return false;
	}
}


function insertAtCursor(myField, myValue, myValue2) {
//IE support
	var scrollT = myField.scrollTop;
	var startPos = 0;
	var endPos = 0;
	var selectText = "";

	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		
		selectText = sel.text;
		startPos = sel.offsetLeft;
		endPos = sel.offsetLeft + sel.text.length;
		sel.text = myValue + selectText + myValue2;
		
		
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		startPos = myField.selectionStart;
		endPos = myField.selectionEnd;
		selectText = myField.value.substring(startPos, endPos);

		myField.value = myField.value.substring(0, startPos)
			+ myValue + selectText + myValue2
			+ myField.value.substring(endPos, myField.value.length);
		
		
	} else {
		//myField.value += myValue;
		alert('Not Supported!');
	} 
	
	myField.focus();
	
	setCaretTo(myField, endPos+myValue2.length+myValue.length);
	
	myField.scrollTop = scrollT;
	//alert(startPos + " " + endPos + " " + selectText);
}

function setCaretTo(obj, pos) { 
    if(obj.createTextRange) { 
        /* Create a TextRange, set the internal pointer to
           a specified position and show the cursor at this
           position
        */ 
        var range = obj.createTextRange(); 
        range.move("character", pos); 
        range.select(); 
    } else if(obj.selectionStart) { 
        /* Gecko is a little bit shorter on that. Simply
           focus the element and set the selection to a
           specified position
        */ 
        obj.focus(); 
		obj.setSelectionRange(pos, pos); 
    } 
} 

// Add this code to use the indexOf function
// It works in all browsers
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++) 
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}	

var loadJS = function(file) {
	var script = document.createElement('script');
	script.src = file;
	script.type = 'text/javascript';
	script.language = 'javascript';
	document.getElementsByTagName('head')[0].appendChild(script);
};

function ajaxCall(command, target, read, type) {
	var client = new HttpClient();
	client.isAsync = true;
	
	client.callback = function(result) {
		
		if (target != 'none') {
			var tg = target.split('|');
			
			for (var i = 0; i < tg.length; i++) {
				if (tg[i] == +tg[i]) {
					var comp = ('component' + tg[i]) + "";
				} else {
					var comp = tg[i];
				}
				
				
				
				switch (type) {
					case 'value':
						document.getElementById(comp).value = result;
					break;
					
					case 'none':
						//do nothing
					break;
					
					case 'text':
						document.getElementById(comp).text = result;
					break;
					
					default:
						document.getElementById(comp).innerHTML = result;
					break;
				}
			}
		}
	}
	
	var arg = '';
	
	if (read != '') {
		var readField = read.split('&');
		for (var j = 0; j < readField.length; j++) {
			switch (target) {
				case 'text':
					arg += "&"+readField[j]+"="+document.getElementById(readField[j]).text;
				break;
				
				default:
					try {
						arg += "&"+readField[j]+"="+document.getElementById(readField[j]).value;
					} catch (err) {
						try {
							arg += "&"+readField[j]+"="+document.getElementById(readField[j]).text;
						} catch (err2) {
							alert ("could not find value " + readField[j]);
						}
					}
				break;
			}
		}
	}
	
	client.makeRequest("/call.php?arg="+command+arg, null);
}

function getSelectionValue(me, target) {
	document.getElementById(target).value = me.value;
}

function shc(obj) {
	var me = document.getElementById(obj);
	if (me.style.display == 'none') {
		me.style.display = 'block';
	} else {
		me.style.display = 'none';
	}
}
































