var selectedSection = 0;

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if(cents < 10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
	return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}

function intval( mixed_var, base ) {
	
    var tmp;
 
	if( typeof( mixed_var ) == 'string' ) {
		tmp = parseInt(mixed_var*1);
		if(isNaN(tmp) || !isFinite(tmp)) {
			return 0;
		} else {
			return tmp.toString(base || 10);
		}
	} else if ( typeof( mixed_var ) == 'number' && isFinite(mixed_var) ) {
		return Math.floor(mixed_var);
	} else {
		return 0;
	}
}

function updateAmounts( row, table, tablenum ) {
	var itemHash = new Hash();

	if (!tablenum) tablenum = "";

	var type = document.getElementByid
	var times = $('times-' + row).value;
	var amount = $('amount-' + row).value;
	var typeElem = $('type-' + row);
	var yearly = formatCurrency(times * amount.toString().replace(/\$|\,/g,''));

	itemHash.set('key', table);
	itemHash.set('item[' + row + '][times]', times);
	itemHash.set('item[' + row + '][amount]', amount.replace(/\$|\,/g,''));
	if (typeof(typeElem.selectedIndex) != 'undefined') {
		itemHash.set('item[' + row + '][type]', typeElem.options[typeElem.selectedIndex].value);
	} else {
		itemHash.set('item[' + row + '][type]', typeElem.value);
	}

	if ($('yearly-' + row)) { 
		$('yearly-' + row).innerHTML = yearly;
	}
	$('monthly-' + row + (tablenum ? '-' + tablenum : '')).innerHTML = formatCurrency(yearly.toString().replace(/\$|\,/g,'') / 12);
	updateTotal(tablenum);

	new Ajax.Request('updateTotals.ajax.php', {
		parameters:	itemHash
		});
}

function updateTotal(tablenum) {
	var total = 0.00;
	var thisAmount = 0.00;
	for (var i = 0; i < 20; i++) {
		//alert(document.getElementById('monthly-' + i).innerHTML);
		if (!$('monthly-' + i + (tablenum ? '-' + tablenum : '')) || isNaN(document.getElementById('monthly-' + i + (tablenum ? '-' + tablenum : '')).innerHTML.replace(/\$|\,/g,''))) {
			continue;
		}
		thisAmount = parseFloat(document.getElementById('monthly-' + i + (tablenum ? '-' + tablenum : '')).innerHTML.replace(/\$|\,/g,''));
		total = total + thisAmount;
		//alert(total);
		//alert(document.getElementById('monthly-' + i + (tablenum ? '-' + tablenum : '')).innerHTML.replace(/\$|\,/g,''));
	}
	//alert(tablenum);
	document.getElementById('tabletotal' + tablenum).value = total;
	document.getElementById('total').innerHTML = formatCurrency(total);

	if (tablenum) {
		document.getElementById('tabletotal').value = parseInt($('tabletotal1').value) + parseInt($('tabletotal2').value) + parseInt($('tabletotal3').value); 
		document.getElementById('total').innerHTML = formatCurrency($('tabletotal').value); 
	}
}

function replaceLinkWithPanel(linkelem, panelid) {
	linkelem.style.display = "none";
	document.getElementById(panelid).style.display = "block";
}

/* SITE ADMIN PANEL */

Ajax.Responders.register({
	onComplete:function(request, transport, json) {
			if (transport.status == 403) {
				window.location.href = 'login.php?go=' + encodeURIComponent(window.location.href) + "&error=timeout";
			}
		}
});

function drawTiers() {
	new Ajax.Updater('tiersControl', 'drawTiers.ajax.php');
}

function getCouponList() {
	new Ajax.Updater('couponList', 'getcoupons.ajax.php');
}

function getSponsorList() {
	new Ajax.Updater('sponsorList', 'getsponsors.ajax.php');
}
function activateCoupon(couponID) {
	new Ajax.Request('couponActivation.ajax.php', {
		onSuccess:	function(transport) {
					elem = $('couponActive-' + couponID);
					elem.update('Deactivate');
					elem.stopObserving('click');
					elem.onclick = null;
					Event.observe(elem, 'click', function() { deactivateCoupon(couponID) });
				},
		parameters:	{
					id:	couponID,
					active: 1
				}
		});
	return false;
}

function deactivateCoupon(couponID) {
	new Ajax.Request('couponActivation.ajax.php', {
		onSuccess:	function(transport) {
					elem = $('couponActive-' + couponID);
					elem.update('Activate');
					elem.stopObserving('click');
					elem.onclick = null;
					Event.observe(elem, 'click', function() { activateCoupon(couponID) });
				},
		parameters:	{
					id:	couponID,
					active: 0
				}
		});
	return false;
}

function drawTree(sectionID,showTree,courseID) {
	var ajaxUrl = 'drawTree.ajax.php';
	
	if((showTree == 'show') && (courseID > 0))
	{
		ajaxUrl = 'drawTree.ajax.php?st=s&cid='+courseID;
	}
	
	if (!sectionID) {
		if (selectedSection) {
			new Ajax.Updater('treeControl', ajaxUrl ,
				{parameters:	{id:	selectedSection}}
			);
		} else {
			new Ajax.Updater('treeControl', ajaxUrl);
		}
	} else {
		selectedSection = sectionID;
		new Ajax.Updater('treeControl', ajaxUrl ,
			{parameters:	{id:	sectionID}}
		);
	}
}

/* TIER ADMIN PANEL */

function promptRename(tierSpanID, tierID) {
	if ($(tierSpanID).firstChild.nodeValue) {
		var tierName = prompt("Rename this tier to:", $(tierSpanID).firstChild.nodeValue);
		if (tierName) {
			renameTier(tierID, tierName);
		}
	}
	return false;
}

function promptAddTier() {
	var tierName = prompt("Name of the new tier:");
	if (tierName) {
		addTier(tierName);
	}
	return false;
}

function moveTierUp(tierID) {
	new Ajax.Request('moveTierUp.ajax.php', {
		onSuccess:	function(transport) {
					drawTiers();
				},
		parameters:	{id:	tierID}
		});
	return false;
}

function moveTierDown(tierID) {
	new Ajax.Request('moveTierDown.ajax.php', {
		onSuccess:	function(transport) {
					drawTiers();
				},
		parameters:	{id:	tierID}
		});
	return false;
}

function addTier(tierName) {
	new Ajax.Request('addTier.ajax.php', {
		onSuccess:	function(transport) {
					drawTiers();
				},
		parameters:	{name:	tierName}
		});
	return false;
}

function renameTier(tierID, tierName) {
	new Ajax.Request('renameTier.ajax.php', {
		onSuccess:	function(transport) {
					drawTiers();
				},
		parameters:	{id:	tierID, name:	tierName}
		});
	return false;
}

function deleteTier(tierID) {
	new Ajax.Request('deleteTier.ajax.php', {
		onSuccess:	function(transport) {
					drawTiers();
				},
		parameters:	{id:	tierID}
		});
	return false;
}

/* TREE ADMIN PANEL */

function promptRenameSection(sectionElemID, sectionID) {
	if ($(sectionElemID).firstChild.nodeValue) {
		var sectionName = prompt("Rename this section to:", $(sectionElemID).firstChild.nodeValue);
		if (sectionName) {
			renameSection(sectionID, sectionName);
		}
	}
	return false;
}

function promptAddSection(parentID) {

	var sectionName = prompt("Name of the new section:");
	if (sectionName) {
		addSection(parentID, sectionName);
	}
	return false;
}

function moveSectionUp(sectionID) {
	new Ajax.Request('moveSectionUp.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function moveSectionDown(sectionID) {
	new Ajax.Request('moveSectionDown.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function addSection(parentID, sectionName) {
	new Ajax.Request('addSection.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{pid:	parentID, name:	sectionName}
		});
	return false;
}

function drawNotes(userID) {
	var notesElem = $('notes');
	new Ajax.Request('getUserNotes.ajax.php', {
		onSuccess:  function(transport) {
						if (notesElem) {
							notesElem.update(transport.responseText);
						}
				},
		parameters: {uid:	userID}
	});
}

function confirmSectionDelete() {
	return confirm('Are you sure you want to delete this section?  All sub-sections will be deleted as well, and the user enrollment for the deleted sections will be cleared.');
}

function deleteSection(sectionID) {
	new Ajax.Request('deleteSection.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function renameSection(sectionID, sectionName) {
	new Ajax.Request('renameSection.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID, name:	sectionName}
		});
	return false;
}

function attachPage(sectionID) {
	new Ajax.Request('attachPage.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function detachPage(sectionID) {
	new Ajax.Request('detachPage.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function setSectionPublic(sectionID) {
	new Ajax.Request('setSectionPublic.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function removeSectionPublic(sectionID) {
	new Ajax.Request('removeSectionPublic.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function setSectionOptional(sectionID) {
	new Ajax.Request('setSectionOptional.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function removeSectionOptional(sectionID) {
	new Ajax.Request('removeSectionOptional.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function updateUserCourse(id,cid){
	var updateStatus	=	false;
	if(document.getElementById("checkCourse"+ cid).checked)
	{
		var status	=	'complete';
		if(confirm('Are you sure want to make the course completed?'))
		{
			updateStatus	=	true;
		}
	}
	else
	{
		var status	=	'uncomplete';
		if(confirm('Are you sure want to make the course uncompleted?'))
		{
			updateStatus	=	true;
		}
	}
	
	if(updateStatus)
	{
		new Ajax.Request('updateCourseStatus.ajax.php', {
		onSuccess:	function(transport) {
			alert(transport.responseText);
		},
		onFailure:	function(transport) {
			alert('Problem?');
		},
		parameters:	{id: id,cid : cid, status : status}
		});
	}
}

function drawUserCourseList(userid, enrolledelem, availableelem, isAdmin) {
	
	
	new Ajax.Request('getEnrolledCourses.ajax.php', {
		onSuccess:	function(transport) {
					//alert('Enrolled response: ' + transport.responseText);
					var enrolledCourses = transport.responseText.evalJSON();
					
					if (enrolledCourses.size() > 0) {
						$(enrolledelem).update();
						//alert('After enrolled clear: ' + $(enrolledelem).innerHTML);
						var checkboxstatus;
						
						enrolledCourses.each(function(item) {
							//alert("Item: " + item.name + ", ID: " + item.course_id);
							checkboxname	=	"checkCourse"+ item.course_id;

							if (isAdmin == 1) { 
								
								op = Builder.node("a", {"onclick":  "removeCourse(" + userid + ", " + item.course_id + ", '" + enrolledelem + "', '" + availableelem + "')", "class": "tinylinkbutton"}, item.name + " CODE:" + item.code);
								
								if(item.completed ==1){
									chk = Builder.node("input", {"type": "checkbox","id": checkboxname ,"name": checkboxname ,"value":"1","onclick":  "updateUserCourse(" + userid + ", " + item.course_id + ")", "checked": "checked"});
								}
								else
								{
									chk = Builder.node("input", {"type": "checkbox","id": checkboxname ,"name": checkboxname ,"value":"1","onclick":  "updateUserCourse(" + userid + ", " + item.course_id + ")"});
								}
								//lbl = Builder.node("LABEL", {"for":"chk1"},'Mark Completed');
							} else { 
								op = Builder.node("a", {"class": "tinylinkbutton"}, item.name + " CODE:" + item.code);
							}
							
							
							$(enrolledelem).appendChild(op);
							$(enrolledelem).appendChild(chk);

						});
						//alert('After enrolled buttons: ' + $(enrolledelem).innerHTML);
					} else {
						$(enrolledelem).update("(no courses)");
						//alert('After no enrolled: ' + $(enrolledelem).innerHTML);
					}
				},
		onFailure:	function(transport) {
					alert('Problem?');
				},
		parameters:	{id:	userid}
		});

	new Ajax.Request('getAvailableCourses.ajax.php', {
		onSuccess:	function(transport) {
					//alert(transport.responseText);
					var availableCourses = transport.responseText.evalJSON();

					if (availableCourses.size() > 0) {
						$(availableelem).update();
						//alert('After available clear: ' + $(availableelem).innerHTML);
						availableCourses.each(function(item) {
							op = Builder.node("a", {"onclick":  "addCourse(" + userid + ", " + item.course_id + ", '" + enrolledelem + "', '" + availableelem + "')", "class": "tinylinkbutton"}, item.name);
							$(availableelem).appendChild(op);
						});
						//alert('After available buttons: ' + $(availableelem).innerHTML);
					} else {
						$(availableelem).update("(no courses)");
						//alert('After no available: ' + $(availableelem).innerHTML);
					}
				},
		onFailure:	function(transport) {
					alert('Problem?');
				},
		parameters:	{id:	userid}
		});
}

function addCourse(userid, courseid, enrolledelem, availableelem) {
	new Ajax.Request('addUserCourse.ajax.php', {
		onSuccess:	function(transport) {
					drawUserCourseList(userid, enrolledelem, availableelem);
				},
		parameters:	{uid:	userid, cid:	courseid}
		});
}

function removeCourse(userid, courseid, enrolledelem, availableelem) {
	new Ajax.Request('removeUserCourse.ajax.php', {
		onSuccess:	function(transport) {
					drawUserCourseList(userid, enrolledelem, availableelem);
				},
		parameters:	{uid:	userid, cid:	courseid}
		});
}

function drawRegions(groupid, regionelem, stateelem, countyelem) {
	new Ajax.Updater(regionelem, 'drawRegions.ajax.php', {
		parameters: { gid: groupid, relem: regionelem, selem: stateelem, celem: countyelem }
	});
}

function addCounty(groupid, countyid, regionelem, stateelem, countyelem) {
	new Ajax.Request('addCounty.ajax.php', {
		onSuccess:	function(transport) {
					drawRegions(groupid, regionelem, stateelem, countyelem);
					$(countyelem).update('&nbsp;');
					getLeftoverStatesDropdown(stateelem, countyelem, groupid);
				},
		parameters:	{gid:	groupid, cid:	countyid}
		});
}

function deleteCounty(groupid, countyid, regionelem, stateelem, countyelem) {
	new Ajax.Request('deleteCounty.ajax.php', {
		onSuccess:	function(transport) {
					drawRegions(groupid, regionelem, stateelem, countyelem);
					$(countyelem).update('&nbsp;');
					getLeftoverStatesDropdown(stateelem, countyelem, groupid);
				},
		parameters:	{gid:	groupid, cid:	countyid}
		});
}

function getLeftoverStatesDropdown(parentelem, countyelem, groupid) {
	new Ajax.Request('getStates.ajax.php', {
		onSuccess:	function(transport) {
					var states = transport.responseText.evalJSON();
					if (states.size() == 0) {
						return;
					}

					var optionsList = $A();

					op = Builder.node('option', {value: '#'}, '-- select state --');
					optionsList.push(op)

					states.each(function(item) {
						//alert(item.state);
						op = Builder.node('option', {value: item.state}, item.state);
						optionsList.push(op);
					});
					var stateSelect = Builder.node('select', {id: 'states', onchange: "getLeftoverCountiesDropdown(this.value, '" + countyelem + "', " + groupid + ");"}, optionsList);
					$(parentelem).update(stateSelect);
					$('states').selectedIndex = 0;
				},
		onFailure:	function(transport) {
					//alert('Had a problem?'); 
				},
		parameters:	{gid:	groupid}
		});
}

function getLeftoverCountiesDropdown(state, parentelem, groupid) {
	new Ajax.Request('getCounties.ajax.php', {
		onSuccess:	function(transport) {
					var counties = transport.responseText.evalJSON();
					if (counties.size() == 0) {
						return;
					}

					var optionsList = $A();

					op = Builder.node('option', {value: '#'}, '-- select county --');
					optionsList.push(op)

					counties.each(function(item) {
						//alert(item.county_id);
						op = Builder.node('option', {value: item.county_id}, item.county);
						optionsList.push(op);
					});
					var countySelect = Builder.node('select', {id: 'counties'}, optionsList);
					$(parentelem).update(countySelect);
					$('counties').selectedIndex = 0;
				},
		onFailure:	function(transport) {
					//alert('Had a problem?'); 
				},
		parameters:	{st:	state, gid:	groupid}
		});
}

function getCountiesDropdown(state, parentelem, frame_name) {
	var is_frame = 0;
 
	new Ajax.Request('getCounties.ajax.php', {
					
		onSuccess:	function(transport) {
			
					if (frames[frame_name]) {
						is_frame = 1;
					}

					var counties = transport.responseText.evalJSON();
					if (counties.size() == 0) {
						return;
					}

					var optionsList = $A();

					op = Builder.node('option', {value: '#'}, '-- select county --');
					optionsList.push(op)

					counties.each(function(item) {
						//alert(item.county_id);
						if (is_frame) {
							op = Builder.node('option', {value: 'orglist.php?c=' + item.county_id + '&quiet'}, item.county);
						} else {
							op = Builder.node('option', {value: 'orglist.php?c=' + item.county_id}, item.county);
						}
						optionsList.push(op);
					});
					if (is_frame) {
						var countySelect = Builder.node('select', {className: 'clsSelectbox', id: 'counties', onchange: "goToDropdownPage($('counties'), '" + frame_name + "');"}, optionsList);
					} else {
						var countySelect = Builder.node('select', {className: 'clsSelectbox', id: 'counties'}, optionsList);
					}
					$(parentelem).update(countySelect);
					$('counties').selectedIndex = 0;
				},
		onFailure:	function(transport) {
					//alert('Had a problem?'); 
				},
		parameters:	{st:	state}
		});
}

function goToDropdownPage(drop_down, frame_name) {
	
	var number = drop_down.selectedIndex;
	if (number) {
		if (frames[frame_name]) {
			$(frame_name).show();
			frames[frame_name].location.href = drop_down.options[number].value;
		} else {
			location.href = drop_down.options[number].value;
		}
	}
}

function confirmUserDelete() {
	return confirm("Are you sure you want to delete this user?  All of the user's progress / test answers will be lost.");
}

function confirmGroupDelete() {
	return confirm("Are you sure you want to delete this group?  The group information, logo, etc. wil be lost.\n\nNOTE: The group must be empty (ie. no users)");
}

var Popup = {
  open: function(options)
  {
    this.options = {
      url: '#',
      width: 600,
      height: 500,
      name:"_blank",
      location:"no",
      menubar:"no",
      toolbar:"no",
      status:"yes",
      scrollbars:"yes",
      resizable:"yes",
      left:"",
      top:"",
      normal:false
    }
    Object.extend(this.options, options || {});

    if (this.options.normal){
        this.options.menubar = "yes";
        this.options.status = "yes";
        this.options.toolbar = "yes";
        this.options.location = "yes";
    }

    this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
    this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
    var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
    if (this.options.top!="")openoptions+=",top="+this.options.top;
    if (this.options.left!="")openoptions+=",left="+this.options.left;
    window.open(this.options.url, this.options.name,openoptions );
    return false;
  }
}

/* added SK */

function fnSetRequestObject(){
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType){
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject){ // IE
		try {
			
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	return http_request;
}
	function showdeadcenterdiv(Xwidth,Yheight,divid) {
	// First, determine how much the visitor has scrolled
	
	showDetails();
	
	var scrolledX, scrolledY;
	if( self.pageYoffset ) {
	scrolledX = self.pageXoffset;
	scrolledY = self.pageYoffset;
	} else if( document.documentElement && document.documentElement.scrollTop ) {
	scrolledX = document.documentElement.scrollLeft;
	scrolledY = document.documentElement.scrollTop;
	} else if( document.body ) {
	scrolledX = document.body.scrollLeft;
	scrolledY = document.body.scrollTop;
	}

	// Next, determine the coordinates of the center of browser's window
	
	var centerX, centerY;
	if( self.innerHeight ) {
	centerX = self.innerWidth;
	centerY = self.innerHeight;
	} else if( document.documentElement && document.documentElement.clientHeight ) {
	centerX = document.documentElement.clientWidth;
	centerY = document.documentElement.clientHeight;
	} else if( document.body ) {
	centerX = document.body.clientWidth;
	centerY = document.body.clientHeight;
	}

	//document.getElementById('fade').style.display='block';
	
	document.getElementById('fade').style.width = '990px';
	document.getElementById('fade').style.height = document.body.scrollHeight;
	var d=document.getElementById('fade');
	var s=d.style;
	var opacity = 0.8;
	s.filter = " alpha(opacity: " + opacity + ");zoom: 1;"; // IE
	//s.KHTMLOpacity = opacity / 100; // Konqueror
	//s.MozOpacity = opacity / 100; // Mozilla (old)
	//s.opacity = opacity / 100;
	
		
	// Xwidth is the width of the div, Yheight is the height of the
	// div passed as arguments to the function:
	var leftoffset = (scrolledX + (centerX - Xwidth) / 2 - 100);
	var topoffset = scrolledY + (centerY - Yheight) / 2;
	// The initial width and height of the div can be set in the
	// style sheet with display:none; divid is passed as an argument to // the function
	var o=document.getElementById(divid);
	var r=o.style;
	r.position='absolute';
	r.top = topoffset + 'px';
	r.left = leftoffset + 'px';
	r.display = "block";
} 
	
	function showDetails()
	{
		document.getElementById('light').style.display	=	'block';
		document.getElementById('fade').style.display='block';
	}
	
	function hideDetails()
	{
	
		document.getElementById('light').style.display	=	'none';
		document.getElementById('fade').style.display='none';
	}
		 

   /*function markAsWorksheet(pageid)
   {
		var	url 		=	"ajax_make_worksheet.php";
		
		if(document.getElementById('chkworksheet').checked)
		{
			var str_action		=	"mws";
			var str_confirm_msg	=	"Are you sure to make this page as worksheet?";
		}
		else
		{
			var str_action	=	"rws";
			var str_confirm_msg	=	"Are you sure to make page as not worksheet?";
		}
		
		var parameters	=	"pid=" + pageid + "&action=" + str_action;

	   	if(confirm(str_confirm_msg))
		{
			ajaxRequestObject();
			http_request.onreadystatechange = markWorksheetResponse();
			ajaxRequestHeader(url,parameters);
		}
		else
		{
			return false;
		}
	}
	
	function ajaxRequestObject()
	{
		  http_request = false;
		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
			 http_request = new XMLHttpRequest();
			 if (http_request.overrideMimeType) {
				// set type accordingly to anticipated content type
				//http_request.overrideMimeType('text/xml');
				http_request.overrideMimeType('text/html');
			 }
		  } else if (window.ActiveXObject) { // IE
			 try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			 } catch (e) {
				try {
				   http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			 }
		  }
		  if (!http_request) {
			 alert('Cannot create XMLHTTP instance');
			 return false;
		  }
	}
	
	function ajaxRequestHeader(url,parameters)
	{
	  http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
	}
	
	function markWorksheetResponse()
	{
		if (http_request.readyState == 4)
		{
			if (http_request.status == 200) 
			{
				alert('Comes Back');
			   	result = http_request.responseText;
            	//alert(result);
			}
			else
			{
				alert('There was a problem with the request.');
			}
		}
	}*/
	
	var http_request = false;
   function makeWorksheet(pageid)
   {
	   	var	url 		=	"ajax_make_worksheet.php";
		
		if(document.getElementById('chkworksheet').checked)
		{
			var str_action		=	"mws";
			var str_confirm_msg	=	"Are you sure to make this page as worksheet?";
		}
		else
		{
			var str_action	=	"rws";
			var str_confirm_msg	=	"Are you sure to make page as not worksheet?";
		}
		
		var parameters	=	"pid=" + pageid + "&action=" + str_action;

	   	if(confirm(str_confirm_msg))
		{
		  http_request = false;
		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
			 http_request = new XMLHttpRequest();
			 if (http_request.overrideMimeType) {
				// set type accordingly to anticipated content type
				//http_request.overrideMimeType('text/xml');
				http_request.overrideMimeType('text/html');
			 }
		  } else if (window.ActiveXObject) { // IE
			 try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			 } catch (e) {
				try {
				   http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			 }
		  }
		  if (!http_request) {
			 alert('Cannot create XMLHTTP instance');
			 return false;
		  }
		  
		  http_request.onreadystatechange = function(){
		  
		   if (http_request.readyState == 4) {
			 if (http_request.status == 200) {
				// alert('Comes return');
				//alert(http_request.responseText);
				result = http_request.responseText;
				alert(result);
			   // document.getElementById(display_block_id).innerHTML = result;
						   
			 } else {
				alert('There was a problem with the request.');
			 }
		  }
		  }
		  
		  http_request.open('POST', url, true);
		  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		  http_request.setRequestHeader("Content-length", parameters.length);
		  http_request.setRequestHeader("Connection", "close");
		  http_request.send(parameters);
		}
		else
		{
			return false;
		}

   }
	function validEmail(txt)
	{
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(txt))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
/* added SK */
