
checkbox_checked_status = false;

function ajaxRequest(callback, url,queryString) {

	  // Obtain an XMLHttpRequest instance
	  var req = newXMLHttpRequest();

	  // Set the handler function to receive callback notifications
	  // from the request object
	  if(callback != null) {
		  var handlerFunction = getReadyStateHandler(req, callback);
		  req.onreadystatechange = handlerFunction;
	  }
	  
	  // Open an HTTP POST connection to the shopping cart servlet.
	  // Third parameter specifies request is asynchronous.
	  req.open("POST", url, true);

	  // Specify that the body of the request contains form data
	  req.setRequestHeader("Content-Type", 
	                       "application/x-www-form-urlencoded");

	  // Send form encoded data stating that I want to add the 
	  req.send(queryString);
}

function selectAllNone(the_form)
{
    in_form = eval("document.forms['" + the_form + "']");

    for(var i=0; i<in_form.elements.length; i++){
	curr_elem = in_form.elements[i];
	
	if(curr_elem.type == 'checkbox'){
		curr_elem.checked = !checkbox_checked_status;
	}
    }

    checkbox_checked_status = !checkbox_checked_status;
}


function selectChildren(the_form,field_name,start,end)
{
    p_checkbox_checked_status = false;

    for(var i=start; i<=end; i++){

	curr_parent    = eval("document." + the_form + "." + field_name);
        curr_elem = eval("document." + the_form + "." + field_name + "_p" + i);

        if (!curr_parent.checked) {
	   p_checkbox_checked_status =true;
        }

        if(curr_elem.type == 'checkbox') {
                curr_elem.checked = !p_checkbox_checked_status;
        }       
    }
}



function openfile(page) {
    top.location.href = page;
}

function updatehidden (the_form, field_name, field_value) {
    element_name = eval("document." + the_form + "." + field_name);
    element_name.value = field_value;
}

function PreFillElement (the_form, element_from, element_to) {    
    element_to = eval("document." + the_form + "." + element_to);
    element_to.value = element_from.value;
}

function PreFillField (the_form, element_from, element_to) {    

    element_to = eval("document.forms[\"" + the_form + "\"].elements[\"" + element_to + "\"]");
    element_from = eval("document.forms[\"" + the_form + "\"].elements[\"" + element_from + "\"]");
    element_to.value = element_from.value;
}

function UpdateField (the_form, field_name, field_value) {
    element_name = eval("document.forms[\"" + the_form + "\"].elements[\"" + field_name + "\"]");
    element_name.value = field_value;
}

function preloadImages(the_image_array)
{
  for(loop=0;loop<the_image_array.length;loop++)
  {
    var an_image = new Image();
    an_image.src = the_image_array[loop];
  }
}    

//to open new popup window
function openWindow(page,width,height) {
	windowFeatures = "";
	window_width = width;
	window_height = height;
	window_top = (screen.availHeight-window_height)/2;
	window_left = (screen.availWidth-window_width)/2;
	windowFeatures += "width=" + window_width + ",height=" + window_height + ",top=";
	windowFeatures += window_top;
	windowFeatures += ",left=";
	windowFeatures += window_left;
	windowFeatures += ",status=no,scrollbars=1";

	return window.open(page,"mywindow", windowFeatures);
}

function updateParent(newURL) {
  opener.document.location = newURL;
}

function checkAll(the_form)
{
    var checked_count =0;
    var programs ="";
    in_form = eval("document.forms['" + the_form + "']");

    for(var i=0; i<in_form.elements.length; i++){
	curr_elem = in_form.elements[i];
	
	if ( (curr_elem.type == 'checkbox') && (curr_elem.checked) ){
		curr_elem_name = eval("document." + the_form + "." + curr_elem.name + "_name");
		programs += curr_elem_name.value +"\n";
		
		checked_count++;
	}
    }

  if (checked_count ==0) {
    alert("You must select an item to delete first");
    return false;
  } else {
    return confirmIt(programs);
  }
    
}


function CheckItems(f,content) {

    var cnt = 0;
    var msg="";

    alert(f.length);

    for (var i=0 ; i < f.length ; i++) {

	var e=f.elements[i];

	if (e.checked) {
	    cnt++;
	}
    }

    if (cnt == 0) {
	msg+="Sorry, you have not selected " + content +".\n";
	alert(msg);
	return false;
    }
}

function CheckOptionSubmit(the_form,field_name,content) {
  check_element_value  = eval("document." + the_form + "." + field_name);

 if (check_element_value.checked) {
   return confirmIt(content);
 } else {
   return true;
 }
}

function confirmIt(content) {

  var agree=confirm("You are about to " + content + "\n\nOK?");
  if (agree)
    return true
  else
    return false
}

function browsepreviewimage(in_dir,in_section,in_form,in_name,in_path) {

  var dialogWidth  ="28";
  var dialogHeight ="32";

  element_to = eval("document." + in_form + "." + in_name);
  var link = "selectimage.html?dir_name=" + in_dir + "&section=" + in_section + "&image_name=" + element_to.value;

  var str = showModalDialog(link, "", "font-family:Verdana; font-size:10; dialogWidth:" + dialogWidth + "em; dialogHeight:" + dialogHeight + "em");

  if(str == null || str == "" || str =="undefined") {
  } else {
        var output_str = str.split(in_path);

        if (!(output_str[1] =="preview.gif")) {
           element_to.value = output_str[1];
        }
  }

}

function enableList(form,listName){
    var element = eval("document." + form + "." + listName);
    if(element != null){
        element.disable='false';
    }
}

function imagepreview() {
    SelImage.value=this.options[this.selectedIndex].value
    RGB.src = SelImage.value;
}

function submitForm(in_form,action,button){
  document_action = eval("document." + in_form);
    if (action) {
     document_action.action.value=action;
    }
    
    button.value='Processing...';
    button.disabled = 'true';
    document_action.submit();
}

/**
    Finds form element
*/
function findFormElement(form,elname){
    var elements = eval("document." + form + ".elements");
    for(var i = 0; i < elements.length; i++){
        if(elements[i].name == elname)
            return elements[i];
    }
    
    return null;
}

function selectOption(selectElement,value){
		//debug("before for loop " + value);
		//loop through option
		for( var optionIndex = 0; optionIndex < selectElement.options.length; optionIndex ++) {
			//debug("option " +optionIndex + " within for loop value is " + selectElement.options[optionIndex].value);
			//find matching value
			if(selectElement.options[optionIndex].value == value)
			{
				//select
				selectElement.selectedIndex = optionIndex;
				selectElement.focus();

				//try again if not set
				if(selectElement.selectedIndex != optionIndex);
				{
					//select
					selectElement.selectedIndex = optionIndex;
					selectElement.focus();
				}
				
				//loose focus
				selectElement.blur();
				
				//exit loop
				break;
			}
		}
	}

	/**
	 * Creates new dom element
	 * @param clientId
	 * @return
	 */
	function htmlElement(clientId) {
		this.clientId = clientId;
		this.el = document.getElementById(clientId);
	}
	
	htmlElement.prototype.show = function() {
		this.el.style.display='block';
	}
	
	htmlElement.prototype.hide = function() {
		this.el.style.display='none';
	}
	
	htmlElement.prototype.setContent = function(content) {
		this.el.innerHTML = content;
	}