var base_url = 'http://www.sittingbullusa.com/zzz_new/';
//var notesWindow;
//var notesWindow_on = 0;

function loader(div_name,height_x){
	var chkElement = document.getElementById(div_name);
	if (chkElement != null){
		document.getElementById(div_name).innerHTML='<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" '+height_x+'><tr><td width="100%" align="center" valign="middle"><img src="sas/pics/loader.gif" border="0"></td></tr></table>';
	}
}

function f_logout(){
	document.logout_form.submit();
}

function bg_img_toggle(img_a,img_w,img_h) {
	//var newImg = new Image();
	//newImg.src = img_a;
	//var img_w = newImg.width;
	//var img_h = newImg.height;
	document.getElementById("bg_img").innerHTML='<img border="0" src="'+img_a+'" width="'+img_w+'" height="'+img_h+'">';
}

function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'width=600,height=500,scrollbars=yes');
return false;
}

function popup2(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'width=800,height=660,scrollbars=yes');
return false;
}
/*
function alertt(){
	alert('alert test');
}

function popup2(windowname, goToLink){
	//alert('start');
	notesWindow = window.open('',windowname,'resizable=no,scrollbars=yes,width=800,height=660');
	notesWindow.document.body.innerHTML = '<html>'
		+'<head>'
		+'<title>Welcome to Sittingbull USA</title>'
		+'<link type="text/css" rel="stylesheet" href="'+base_url+'sas/css/txtstyle.css">'
		+'<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">'
		+'</head>'
		+'<body link="#333333" vlink="#333333" alink="#333333">'
		+'<div id="layer_1_main_content">!</div>'
		+'<a href="javascript:window.opener.alertt();">test</a>'
		+'</body>'
		+'</html>';
	
	notesWindow.focus();
	
	//notesWindow.document.close()
	notesWindow_on = 1;
	
	goToLink = goToLink.replace("\\", "")
	//alert(goToLink);
	eval(goToLink);
}*/

function getTab(div_name){
	//alert('start');
	if(div_name=='div_ztab_product'){
        //$(".show_hide").show();
		$("#div_product_category_menu_small").animate({width: 'hide', opacity: 0}, 1000, "linear", function(){});
		$("#div_ztab_product_setup_media").animate({width: 'hide', opacity: 0}, 1000, "linear", function(){});
		
		$('.menuProductHide').click(function() {
			/*
			$("#div_product_category_menu_large").animate({width:'hide', opacity: 0}, 1000, "linear", function(){});
			//animate({width: 'toggle'});
			//document.getElementById('div_product_category_menu_large').style.display='none';
			$("#div_product_category_menu_small").animate({width: 'show', opacity: 1}, 1000, "linear", function(){});
			*/
			$(".leftProductMenu").animate({"left": "-=150px"}, "slow");
		});
		$('.menuProductShow').click(function() {
			$("#div_product_category_menu_large").animate({width: 'show', opacity: 1}, 1000, "linear", function(){});
			$("#div_product_category_menu_small").animate({width: 'hide', opacity: 0}, 1000, "linear", function(){});
		});
	}
	
	var ztabs=new Array("div_ztab_site","div_ztab_media","div_ztab_product");
	for (x=0; x<ztabs.length; x++){ 
		//alert('compare '+div_name+' to '+ztabs[x]);
		if(div_name==ztabs[x]){
			//alert('yes');
			document.getElementById(ztabs[x]).style.display='';
			//alert('set yes');
		}else{
			//alert('no');
			document.getElementById(ztabs[x]).style.display='none';
			//alert('set no');
		}
	}
}


function f_process(target, menu_id, template_id, prior, edit, editvalue, popup){ //,topmenu,var_1,var_2,var_3
	//alert('target='+target+', menu_id='+menu_id+', template_id='+template_id+', prior='+prior+', edit='+edit+', editvalue='+editvalue+', popup='+popup);
	max_top_menu_num = document.getElementById('menu_' + target + '_topnum').value;
	//alert('max_top_menu_num='+max_top_menu_num);
	for(i=1;i<=max_top_menu_num;i++){
		if(prior == i){
			document.getElementById('menu_' + target + '_num_' + i).className = "nav_green";
		}else{
			document.getElementById('menu_' + target + '_num_' + i).className = "prod_nav";
		}
	}
	
	//alert(target + '|' + menu_id + '|' + template_id + ' for TARGET : layer_' + target + '_main_content');
	if(popup!='popup'){
		loader('layer_' + target + '_main_content','height="400px"');
	}
	
	if(edit=='edit'){
		urlxmlList = 'sas/php/process.php?zone=menu&menu_id=' + menu_id + '&template_id=' + template_id + '&target=' + target + '&prior=' + prior + '&edit_info=' + editvalue;
	}else{
		urlxmlList = 'sas/php/process.php?zone=menu&menu_id=' + menu_id + '&template_id=' + template_id + '&target=' + target + '&prior=' + prior + '&edit_info=0';
	}
	
	//alert(urlxmlList+' with :'+popup);
	
	var xmlList = createRequestObject();
	xmlList.onreadystatechange=function handleStateChange(){
    	if(xmlList.readyState == 4 || xmlList.readyState=="complete"){
			var resultText=xmlList.responseText;
			//alert(resultText);
			
			var SplitArray = new Array();
			SplitArray = resultText.split("|||||SPLIT|||||");
			
			//alert(SplitArray[0]);
			if(popup=='popup'){
				SplitArray[0] = SplitArray[0].replace(/f_zadm/g, "window.opener.f_zadm");
				notesWindow.document.getElementById('layer_1_main_content').innerHTML = SplitArray[0];
				//alert(SplitArray[0]);
			}else{
				document.getElementById('layer_' + target + '_main_content').innerHTML = SplitArray[0]; //resultText;
			}
			
			
			
			if(SplitArray[1] != '' && SplitArray[1] != 'wymeditor'){
				var SplitArray_2 = new Array();
				SplitArray_2 = SplitArray[1].split("|s|");
				f_process(SplitArray_2[0],SplitArray_2[1],SplitArray_2[2],SplitArray_2[3]);
			}
			
			//alert(SplitArray[1]);
			if(SplitArray[1] == 'wymeditor'){
				/*if(SplitArray[2] == 'init'){
					setTimeout(start_editor(' '),1000);
				}else{
					setTimeout(start_editor(SplitArray[2]),1000);
				}*/
				//eval(SplitArray[2]);
				//alert(SplitArray[0]);
				setTimeout(start_JQeditor(editvalue),2000);
			}
			
			//ResizableColumns();
		}
	}
	xmlList.open("GET",urlxmlList,true);
	xmlList.send(null);
}

function start_JQeditor(menu_id){
	//alert("textarea_input_"+menu_id);
	//["h1", "h2", "h3", "h4", "h5", "h6"],
	if(menu_id=='textarea_input_ne'){
		div_name = menu_id;
	}else{
		div_name = 'textarea_input_'+menu_id;
	}
	
	$(function(){
		$("#"+div_name).htmlarea({
			// Override/Specify the Toolbar buttons to show
			toolbar: [
				["html"], ["bold", "italic", "underline", "|", "forecolor"],
				["increaseFontSize","decreaseFontSize"],
				["justifyLeft","justifyCenter","justifyRight"],
				["link", "unlink", "|", "image"],
				[{
					css: "h_prodname",
					text: "Product Name",
					action: function(btn) {
						this.bold();
						this.forecolor("#CC0000");
						this.fontSize("24");
					}
				}]
			],
			 css: "sas/scripts/style//jHtmlArea.Editor.css"
		});
	});
}

function start_editor(dialogImageHtml_code){
	//alert('wymediting');
	if(dialogImageHtml_code==' '){
		 dialogImageHtml_code = "<body class='wym_dialog wym_dialog_image'"
								   + " onload='document.formrefresh.submit();WYMeditor.INIT_DIALOG(" + WYMeditor.INDEX + ")'"
								   + ">"
								   + "<script type=\"text/javascript\">"
									+ "function startCallback() {"
									+ "alert('callback started');"
									+ "return true;"
									+ "}"
									+ "function completeCallback(response) {"
									+ "alert('callbacked');"
									+ "}"
									+ "</script>"
								   + "<form name='formrefresh' action='sas/scripts/wymeditor/wymeditorajax.php?type=display_all_media' method='post' enctype='multipart/form-data' >"
									+ "<fieldset>"
									+ "<legend>New image</legend>"
									+ "<div style='display:none;'>"
									+ "<input type='hidden' name='wym_editor_index' value='"+WYMeditor.INDEX+"'>"
									+ "<input type='file' name='uploadfile'>"
									+ "<input type='submit'"
									+ " value='{Submit}' />"
									+ "</div>"
									+ "<div>"
								   + "loading..."
								   + "</div>"
									+ "</fieldset>"
									+ "</form>"
								   + "<form name='lm_imgform'>"
								   + "<fieldset>"
								   + "<input type='hidden' class='wym_dialog_type' value='"
								   + WYMeditor.DIALOG_IMAGE
								   + "' />"
								   + "<legend>Available images</legend>"
								   + "<div class='row' id='lm_img_select_div' style='display:none;'>"
								   + "<select id='lm_select' onchange='document.lm_imgform.wym_src.value=this.value;'><option></option></select>"
								   + "</div>"
								   + "<div>"
								   + "loading..."
								   + "</div>"
								   + "</fieldset>"
								   + "<fieldset>"
								   + "<legend>{Image}</legend>"
								   + "<div style='display:none;'>"
								   + "<label>{URL}</label>"
								   + "<input type='text' class='wym_src' value='' size='40' />"
								   + "</div>"
								   + "<div style='display:none;'>"
								   + "<label>{Alternative_Text}</label>"
								   + "<input type='text' class='wym_alt' value='' size='40' />"
								   + "</div>"
								   + "<div style='display:none;'>"
								   + "<label>{Title}</label>"
								   + "<input type='text' class='wym_title' value='' size='40' />"
								   + "</div>"
								   + "<div>"
								   + "loading..."
								   + "</div>"
								   + "<div class='row row-indent' style='display:none;'>"
								   + "<input class='wym_submit' type='button'"
								   + " value='{Submit}' />"
								   + "<input class='wym_cancel' type='button'"
								   + "value='{Cancel}' />"
								   + "</div>"
								   + "</fieldset>"
								   + "</form>"
								   + "</body>";
		htmlInsert = '';
	}else{
		htmlInsert = dialogImageHtml_code;
		//alert(htmlInsert);
		dialogImageHtml_code = "<body class='wym_dialog wym_dialog_image'"
								   + " onload='document.formrefresh.submit();WYMeditor.INIT_DIALOG(" + WYMeditor.INDEX + ")'"
								   + ">"
								   + "<script type=\"text/javascript\">"
									+ "function startCallback() {"
									+ "alert('callback started');"
									+ "return true;"
									+ "}"
									+ "function completeCallback(response) {"
									+ "alert('callbacked');"
									+ "}"
									+ "</script>"
								   + "<form name='formrefresh' action='sas/scripts/wymeditor/wymeditorajax.php?type=display_all_media' method='post' enctype='multipart/form-data' >"
									+ "<fieldset>"
									+ "<legend>New image</legend>"
									+ "<div style='display:none;'>"
									+ "<input type='hidden' name='wym_editor_index' value='"+WYMeditor.INDEX+"'>"
									+ "<input type='file' name='uploadfile'>"
									+ "<input type='submit'"
									+ " value='{Submit}' />"
									+ "</div>"
									+ "<div>"
								   + "loading..."
								   + "</div>"
									+ "</fieldset>"
									+ "</form>"
								   + "<form name='lm_imgform'>"
								   + "<fieldset>"
								   + "<input type='hidden' class='wym_dialog_type' value='"
								   + WYMeditor.DIALOG_IMAGE
								   + "' />"
								   + "<legend>Available images</legend>"
								   + "<div class='row' id='lm_img_select_div' style='display:none;'>"
								   + "<select id='lm_select' onchange='document.lm_imgform.wym_src.value=this.value;'><option></option></select>"
								   + "</div>"
								   + "<div>"
								   + "loading..."
								   + "</div>"
								   + "</fieldset>"
								   + "<fieldset>"
								   + "<legend>{Image}</legend>"
								   + "<div style='display:none;'>"
								   + "<label>{URL}</label>"
								   + "<input type='text' class='wym_src' value='' size='40' />"
								   + "</div>"
								   + "<div style='display:none;'>"
								   + "<label>{Alternative_Text}</label>"
								   + "<input type='text' class='wym_alt' value='' size='40' />"
								   + "</div>"
								   + "<div style='display:none;'>"
								   + "<label>{Title}</label>"
								   + "<input type='text' class='wym_title' value='' size='40' />"
								   + "</div>"
								   + "<div>"
								   + "loading..."
								   + "</div>"
								   + "<div class='row row-indent' style='display:none;'>"
								   + "<input class='wym_submit' type='button'"
								   + " value='{Submit}' />"
								   + "<input class='wym_cancel' type='button'"
								   + "value='{Cancel}' />"
								   + "</div>"
								   + "</fieldset>"
								   + "</form>"
								   + "</body>";
	}
	
	jQuery(function() {
		jQuery("textarea.wymeditor").wymeditor(
			{
           		skin: 'silver',
      			stylesheet: 'sas/scripts/wymeditor/stylesheet.css',
				toolsItems: [
					{'name': 'Bold', 'title': 'Bold', 'css': 'wym_tools_strong'}, 
					{'name': 'Italic', 'title': 'Emphasis', 'css': 'wym_tools_emphasis'},
					{'name': 'Superscript', 'title': 'Superscript', 'css': 'wym_tools_superscript'},
					{'name': 'Subscript', 'title': 'Subscript', 'css': 'wym_tools_subscript'},
					{'name': 'InsertOrderedList', 'title': 'Ordered_List', 'css': 'wym_tools_ordered_list'},
					{'name': 'InsertUnorderedList', 'title': 'Unordered_List', 'css': 'wym_tools_unordered_list'},
					//{'name': 'Indent', 'title': 'Indent', 'css': 'wym_tools_indent'},
					//{'name': 'Outdent', 'title': 'Outdent', 'css': 'wym_tools_outdent'},
					{'name': 'Undo', 'title': 'Undo', 'css': 'wym_tools_undo'},
					{'name': 'Redo', 'title': 'Redo', 'css': 'wym_tools_redo'},
					//{'name': 'CreateLink', 'title': 'Link', 'css': 'wym_tools_link'},
					//{'name': 'Unlink', 'title': 'Unlink', 'css': 'wym_tools_unlink'},
					{'name': 'InsertImage', 'title': 'Image', 'css': 'wym_tools_image'},
					//{'name': 'InsertTable', 'title': 'Table', 'css': 'wym_tools_table'},
					{'name': 'Paste', 'title': 'Paste_From_Word', 'css': 'wym_tools_paste'},
					{'name': 'ToggleHtml', 'title': 'HTML', 'css': 'wym_tools_html'},
					{'name': 'Preview', 'title': 'Preview', 'css': 'wym_tools_preview'}
				],
				 containersItems: [
					{'name': 'P', 'title': 'Paragraph', 'css': 'wym_containers_p'},
					{'name': 'H1', 'title': 'Heading_1', 'css': 'wym_containers_h1'},
					{'name': 'H2', 'title': 'Heading_2', 'css': 'wym_containers_h2'},
					{'name': 'H3', 'title': 'Heading_3', 'css': 'wym_containers_h3'},
				],
				 boxHtml:   "<div class='wym_box'>"
				  + "<div class='wym_area_top'>" 
				  + WYMeditor.TOOLS
				  + WYMeditor.CONTAINERS
				  + WYMeditor.CLASSES
				  + "</div>"
				  + "<div class='wym_area_left'></div>"
				  + "<div class='wym_area_right'></div>"
				  + "<div class='wym_area_main'>"
				  + WYMeditor.HTML
				  + WYMeditor.IFRAME
				  + WYMeditor.STATUS
				  + "</div>"
				  + "<div class='wym_area_bottom'>"
				  + WYMeditor.LOGO
				  + "</div>"
				  + "</div>",
				 dialogImageHtml: dialogImageHtml_code,
			     html: htmlInsert,
				 logoHtml: ''
   			}
		);
		//{ stylesheet: 'sas/scripts/wymeditor/stylesheet.css' }
		}
	);
}

function f_zadm(action, val_1, val_2, val_3, val_4, val_5, val_6){
	xajax_method = 'GET';
	var urlxmlList_Appendix;
	
	if(action=='get_related_product'){
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&val_1=' + val_1 + '&val_2=' + val_2 + '&val_3=' + val_3 + '&val_4=' + val_4 + '&val_5=' + val_5 + '&val_6=' + val_6;
		//alert(urlxmlList);
		//loader(val_5,'');
		//alert(val_5);
	}
	if(action == 'rearrange_menu'){
		var menu_reff = val_1;
		var menu_layer = val_2;
		var menu_prior = val_3;
		var reprior_action = val_4;
		var refresh_link = val_5;
		var target = val_6;
		
		if(target=='all'){
			loader('div_all_content','height="400px"');
		}else{
			loader('layer_' + target + '_main_content','height="400px"');
		}
		
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&menu_reff=' + menu_reff + '&menu_layer=' + menu_layer + '&menu_prior=' + menu_prior + '&reprior_action=' + reprior_action + '&refresh_link=' + refresh_link;
		//alert(urlxmlList);
	}
	
	if(action=='addmenu'){
		var new_name = document.getElementById(val_1).value;
		var new_reff = val_2;
		var new_layer = val_3;
		var refresh_link = val_4;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&new_name=' + new_name + '&new_reff=' + new_reff + '&new_layer=' + new_layer + '&refresh_link=' + refresh_link;
	}
	
	if(action=='delmenu'){
		if(val_3=='1'){
			var alert_msg = 'The template and all related submenus and info\nwill be deleted\n\nAre you sure ?';
		}else{
			var alert_msg = 'The menu and all related submenus and info\nwill be deleted\n\nAre you sure ?';
		}
		
		var answer = confirm (alert_msg);
		if (answer){
			var remove_menu_id = val_1;
			var refresh_link = val_2;
			var template_remove = val_3;
			urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&remove_menu_id=' + remove_menu_id + '&refresh_link=' + refresh_link + '&template_remove=' + template_remove;
		}else{
			return;
		}
	}
	
	if(action=='addtemplate'){
		//alert ('1');
		/*if(notesWindow_on == 1){
			notesWindow_on = 0;
			notesWindow.close();
		}*/
		
		var menu_id = val_1;
		var refresh_link = val_2;
		var template_id = val_3;
		var link_target = val_4;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&menu_id=' + menu_id + '&refresh_link=' + refresh_link + '&template_id=' + template_id + '&link_target=' + link_target;
		//alert (urlxmlList);
	}
	
	/*if(action=='deletetemplate'){
		var menu_id = val_1;
		var refresh_link = val_2;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&menu_id=' + menu_id + '&refresh_link=' + refresh_link;
	}*/
	
	if(action=='savetext'){
		//alert('1');
		var menu_id = val_1;
		var refresh_link = val_2;
		
		var edit_info = val_3;
		//$.wymeditors(0).update();
		
		var target = val_4;
		
		if(edit_info!=''){
			var new_text = document.getElementById('textarea_input_'+edit_info).value;
		}else{
			var new_text = document.getElementById('textarea_input_'+menu_id).value;
			edit_info = '1';
		}
		
		new_text = encodeURIComponent(new_text);
		//var new_text = $.wymeditors(0).xhtml(); //document.getElementById('textarea_input_'+val_1).value;
		//alert('2');
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&menu_id=' + menu_id + '&refresh_link=' + refresh_link + '&edit_info=' + edit_info;
		urlxmlList_Appendix = 'new_text=' + new_text;
		
		loader('layer_' + target + '_main_content','height="400px"');
		
		xajax_method = 'POST';
	}
	if(action=='addimgcat'){
		var name = document.getElementById(val_1).value;
		var reff_id = val_2;
		
		if(name == ''){
			alert('Enter category name');
			return;
		}
		
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&name=' + name + '&reff_id=' + reff_id;
		loader('div_img_cat_'+reff_id,'');
	}
	if(action=='manage_category_img'){
		var reff_id = val_1;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&reff_id=' + reff_id;
		loader('div_ztab_media_workbench','height="400px"');
	}
	if(action=='manage_category_deattach'){
		var img_id = val_1;
		var reff_id = val_2;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&img_id=' + img_id + '&reff_id=' + reff_id;
		loader('div_ztab_media_workbench','height="400px"');
	}
	if(action=='manage_category_attach'){
		var img_id = val_1;
		var reff_id = val_2;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&img_id=' + img_id + '&reff_id=' + reff_id;
		loader('div_ztab_media_workbench','height="400px"');
	}
	
	
	
	
	if(action=='addproductcat'){
		var name = document.getElementById(val_1).value;
		
		if(name == ''){
			alert('Enter category name');
			return;
		}
		
		var reff_id = val_2;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&name=' + name + '&reff_id=' + reff_id;
		loader('div_product_cat_'+reff_id,'');
	}
	if(action=='manage_category_product'){
		var reff_id = val_1;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&reff_id=' + reff_id;
		loader('div_ztab_product_workbench','height="400px"');
	}
	
	if(action=='addeditproduct'){
		document.getElementById('div_ztab_product_setup_media').style.display = "none";
		var prod_id = val_1;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&prod_id=' + prod_id;
		loader('div_ztab_product_workbench','height="400px"');
	}
	
	if(action=='ne_editor_to_html'){
		for(x=0;x<20;x++){
			alert('x'+x + ':'+$.wymeditors(x).xhtml())
		}
		/*
		$.wymeditors(0).update();
		var new_text = $.wymeditors(0).xhtml();
		document.getElementById('div_product_info').innerHTML = new_text;
		document.getElementById('div_product_info_button').innerHTML = '<a class="prod_nav" href="javascript:f_zadm(\'html_to_ne_editor\')">Edit text</a>';
		*/
		return;
	}
	if(action=='html_to_ne_editor'){
		var new_text = document.getElementById('div_product_info').innerHTML;
		document.getElementById('div_product_info').innerHTML = '<textarea id="textarea_input_ne" class="wymeditor"></textarea>'
		setTimeout(start_editor(new_text),1000);
		document.getElementById('div_product_info_button').innerHTML = '<a class="prod_nav" href="javascript:f_zadm(\'ne_editor_to_html\')">Accept text</a>';
		return;
	}
	if(action=='PreGenerateMedia'){
		prod_id_reff = val_1;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&prod_id_reff=' + prod_id_reff;
		loader('div_ztab_product_setup_media','200px');
		//alert(urlxmlList);
	}
	
	if(action=='generateInsertMedia'){
		var reff_id = val_1;
		var tip_id = document.getElementById('selectPreGenerate').value;
		if(tip_id=='0'){
			alert('Please select Media Category');
			return;
		}
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&reff_id=' + reff_id + '&tip_id=' + tip_id;
		loader('div_ztab_product_setup_media','200px');
	}
	if(action=='linkProductMedia'){
		document.getElementById('div_ztab_product_setup_media').style.display = "none";
		
		var prod_id = val_1;
		var media_id = val_2;
		var media_tip = document.getElementById('linkMediaType').value;
		
		if(media_tip==''){
			alert('Click on the image that should be changed.');
			return;
		}
		
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&prod_id=' + prod_id + '&media_id=' + media_id + '&media_tip=' + media_tip;
		loader('div_ztab_product_setup_media','200px');
	}
	if(action=='unlinkProductMedia'){
		var prod_id = val_1;
		var del_prod_media_link_id = val_2;
		var media_tip = val_3;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&prod_id=' + prod_id + '&del_prod_media_link_id=' + del_prod_media_link_id + '&media_tip=' + media_tip;
		loader('div_ztab_product_setup_media','200px');
	}
	if(action=='deleteimg'){
		var answer = confirm ('Delete Media from DataBase ?');
		if (answer){
			var media_id = val_1;
			urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&media_id=' + media_id;
			loader('div_ztab_media_workbench','height="400px"');
		}else{
			return;
		}	
	}
	
	
	if(action=='saveeditproduct'){
		//alert('start');
		var prod_id = val_1;
		//alert('1');
		var ne_product_name = document.getElementById('ne_product_name').value;
		//alert('2');
		var ne_product_info = document.getElementById('textarea_input_ne').value;
		//alert('3');
		var ne_product_price = document.getElementById('ne_product_price').value;
		//alert('4');
		var ne_product_stock = document.getElementById('ne_product_stock').value;
		//alert('5');
		var ne_product_category = document.getElementById('ne_product_category').value;
		
		var ne_product_hash_units = document.getElementById('ne_product_hash_units').value;
		var ne_product_active_state = document.getElementById('ne_product_active_state').value;
		
		//alert('6');
		if(ne_product_name=='[[new]]'){
			alert('Please name your product');
			document.getElementById('ne_product_name').focus();
			return;
		}
		
		if(ne_product_category=='0'){
			alert('Select a Product Category');
			document.getElementById('ne_product_category').focus();
			return;
		}
		
		if(ne_product_hash_units=='0'){
			alert('Product Hash Units can not be 0');
			document.getElementById('ne_product_hash_units').focus();
			return;
		}
		
		ne_product_info = encodeURIComponent(ne_product_info);
		
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&prod_id=' + prod_id + '&ne_product_name=' + ne_product_name  + '&ne_product_price=' + ne_product_price + '&ne_product_stock=' + ne_product_stock + '&ne_product_category=' + ne_product_category + '&ne_product_hash_units=' + ne_product_hash_units + '&ne_product_active_state=' + ne_product_active_state;
		
		urlxmlList_Appendix = 'ne_product_info=' + ne_product_info;
		//alert(urlxmlList_Appendix);
		
		loader('div_ztab_product_workbench','height="400px"');
		
		xajax_method = 'POST';
	}
	if(action=='canceleditproduct'){
		var prod_id = val_1;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&prod_id=' + prod_id;
		loader('div_ztab_product_workbench','height="400px"');
	}
	if(action=='deleteproduct'){
		var answer = confirm ('Delete product from DataBase ?');
		if (answer){
			var prod_id = val_1;
			urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&prod_id=' + prod_id;
			loader('div_ztab_product_workbench','height="400px"');
		}else{
			return;
		}
			
	}
	
	if(action=='manage_category_deattach_product'){
		var prod_id = val_1;
		var reff_id = val_2;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&prod_id=' + prod_id + '&reff_id=' + reff_id;
		loader('div_ztab_product_workbench','height="400px"');
	}
	if(action=='manage_category_attach_product'){
		var prod_id = val_1;
		var reff_id = val_2;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&prod_id=' + prod_id + '&reff_id=' + reff_id;
		loader('div_ztab_product_workbench','height="400px"');
	}
	
	if(action=='deleteCategory'){
		var answer = confirm ('Delete Category and Subcategories ?\nAll subunits will be detached.\n\nContinue ?');
		if (answer){
			var delCat_id = val_1;
			var delCat_tip = val_2;
			urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&delCat_id=' + delCat_id + '&delCat_tip=' + delCat_tip;
			loader('div_product_cat_0','');
		}else{
			return;
		}	
	}
	
	if(action=='link_product_to_template'){
		var prod_id = val_1;
		var info_id = val_2;
		var loader_target = val_3;
		var refresh_link = val_4;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&prod_id=' + prod_id + '&info_id=' + info_id + '&refresh_link=' + refresh_link + '&loader_target=' + loader_target;
		//alert(urlxmlList);
		loader(loader_target,'height="400px"');
	}
	
	if(action=='unlink_product_from_template'){
		var info_id = val_1;
		var loader_target = val_2;
		var refresh_link = val_3;
		urlxmlList = 'sas/php/process.php?zone=zadm&action=' + action + '&info_id=' + info_id + '&refresh_link=' + refresh_link + '&loader_target=' + loader_target;
		loader(loader_target,'height="400px"');
	}
	
	//alert(urlxmlList);
	
	if(xajax_method == 'GET'){
		var xmlList = createRequestObject();
		xmlList.onreadystatechange=function handleStateChange(){
			if(xmlList.readyState == 4 || xmlList.readyState=="complete"){
				var resultText=xmlList.responseText;
				//alert('3');
				//alert(resultText);
				var SplitArray = new Array();
				SplitArray = resultText.split("|||||SPLIT|||||");
				
				if(action=='get_related_product'){
					//alert(resultText);
					document.getElementById('related_products').innerHTML = SplitArray[0];
				}
				
				if(action == 'rearrange_menu'){
					//alert(resultText);
					if(SplitArray[0] == 'window'){
						window.location.reload();
					}else{
						eval(SplitArray[0]);
					}
				}
				if(action=='addmenu'){
					//alert(resultText);
					if(SplitArray[0] == 'window'){
						window.location.reload();
					}else{
						eval(SplitArray[0]);
					}
				}
				if(action=='delmenu'){
					//alert(resultText);
					if(SplitArray[0] == 'window'){
						window.location.reload();
					}else{
						eval(SplitArray[0]);
					}
				}
				if(action=='addtemplate'){
					//alert(resultText);
					if(popup!='popup'){				
						if(SplitArray[0] == 'window'){
							window.location.reload();
						}else{
							eval(SplitArray[0]);
						}
					}else{
						window.location.reload();
					}
				}
				/*if(action=='deletetemplate'){
					if(SplitArray[0] == 'window'){
						window.location.reload();
					}else{
						eval(SplitArray[0]);
					}
				}*/
				
				if(action=='addimgcat'){
					document.getElementById('div_img_cat_'+reff_id).innerHTML = SplitArray[0];
				}
				if(action=='manage_category_img'){
					document.getElementById('div_ztab_media_workbench').innerHTML = SplitArray[0];
				}
				if(action=='manage_category_deattach'){
					f_zadm('manage_category_img',SplitArray[0]);
				}
				if(action=='manage_category_attach'){
					f_zadm('manage_category_img',SplitArray[0]);
				}
				if(action=='addproductcat'){
					document.getElementById('div_product_cat_'+reff_id).innerHTML = SplitArray[0];
				}
				if(action=='manage_category_product'){
					document.getElementById('div_ztab_product_workbench').innerHTML = SplitArray[0];
				}
				if(action=='addeditproduct'){
					//document.getElementById('div_ztab_product_workbench').style.height="750px"
					//collapse1.contentheight=500
					//alert(SplitArray[0]);
					
					document.getElementById('div_ztab_product_workbench').innerHTML = SplitArray[0];
					
					$('.insertMedia').click(function(){
						//$(".slidingDiv").slideToggle();
						 $("#div_ztab_product_setup_media").animate({height: 'show', opacity: 1}, 1000, "linear", function(){});
						 //$("#div_product_info_ne").animate({width: 'hide', opacity: 0}, 1000, "linear", function(){});
						 //$("#div_product_category_menu_large").animate({width: 'hide', opacity: 0}, 1000, "linear", function(){});
						 //$("#div_product_category_menu_small").animate({width: 'show', opacity: 1}, 1000, "linear", function(){});
						document.getElementById('linkMediaType').value = 'img';
						 f_zadm('PreGenerateMedia',SplitArray[3]);
					});
					
					$('.insertMediaThumb').click(function(){
						//$(".slidingDiv").slideToggle();
						 $("#div_ztab_product_setup_media").animate({height: 'show', opacity: 1}, 1000, "linear", function(){});
						 //$("#div_product_info_ne").animate({width: 'hide', opacity: 0}, 1000, "linear", function(){});
						 //$("#div_product_category_menu_large").animate({width: 'hide', opacity: 0}, 1000, "linear", function(){});
						 //$("#div_product_category_menu_small").animate({width: 'show', opacity: 1}, 1000, "linear", function(){});
						 document.getElementById('linkMediaType').value = 'thumb';
						 f_zadm('PreGenerateMedia',SplitArray[3]);
					});
					
					if(SplitArray[3] != ''){
						 //alert('f_zadm(\'generateInsertMedia\','+SplitArray[3]+');')
						 f_zadm('PreGenerateMedia',SplitArray[3]);
					}
					
					/*if(SplitArray[1] == 'wymeditor'){
						if(SplitArray[2] == 'init'){
							setTimeout(start_editor(' '),1000);
						}
					}*/
					if(SplitArray[1] == 'wymeditor'){
						setTimeout(start_JQeditor('textarea_input_ne'),2000);
					}		
				}
				if(action=='PreGenerateMedia'){
					//alert(resultText);
					document.getElementById('div_ztab_product_setup_media').innerHTML = SplitArray[0];
				}
				if(action=='generateInsertMedia'){
					document.getElementById('div_ztab_product_setup_media').innerHTML = SplitArray[0];
					
					$('.cancelMedia').click(function(){
						//$(".slidingDiv").slideToggle();
						 $("#div_ztab_product_setup_media").animate({height: 'hide', opacity: 0}, 1000, "linear", function(){});
						 //$("#div_product_info_ne").animate({width: 'show', opacity: 1}, 1000, "linear", function(){});
						 //$("#div_product_category_menu_large").animate({width: 'show', opacity: 1}, 1000, "linear", function(){});
						 //$("#div_product_category_menu_small").animate({width: 'hide', opacity: 0}, 1000, "linear", function(){});
					});
				}
				if(action=='linkProductMedia'){
					var prod_id_frw = SplitArray[0];
					//alert(prod_id_frw);
					if(prod_id_frw!='maximum'){
						f_zadm('addeditproduct',prod_id_frw);
						//f_zadm('generateInsertMedia',prod_id);
					}
					if(prod_id_frw=='maximum'){
						alert('Maximum images per product assigned.');
						return;
					}
				}
				if(action=='unlinkProductMedia'){
					var prod_id_frw = SplitArray[0];
					//alert(prod_id_frw);
					f_zadm('addeditproduct',prod_id_frw);
				}
				if(action=='deleteimg'){
					//alert(resultText);
					f_zadm('manage_category_img','all');
				}
				if(action=='saveeditproduct'){
					//alert(resultText);
					f_zadm('addeditproduct','0');
				}
				if(action=='canceleditproduct'){
					f_zadm('addeditproduct','0');
				}
				if(action=='deleteproduct'){
					f_zadm('addeditproduct','0');
				}
				if(action=='manage_category_deattach_product'){
					f_zadm('manage_category_product',SplitArray[0]);
				}
				if(action=='manage_category_attach_product'){
					f_zadm('manage_category_product',SplitArray[0]);
				}
				
				if(action=='deleteCategory'){
					//alert(resultText);
					window.location.reload();
				}
				
				if(action=='link_product_to_template'){
					//alert(resultText);
					if(SplitArray[0] == 'window'){
						window.location.reload();
					}else{
						eval(SplitArray[0]);
					}
				}
				if(action=='unlink_product_from_template'){
					if(SplitArray[0] == 'window'){
						window.location.reload();
					}else{
						eval(SplitArray[0]);
					}
				}
			}
		}
		xmlList.open("GET",urlxmlList,true);
		xmlList.send(null);
	}
	if(xajax_method == 'POST'){
		var saveHttp = createRequestObject();
		var saveHttpUrl = urlxmlList;
		var params = urlxmlList_Appendix;
		saveHttp.open("POST", saveHttpUrl, true);
		
		//Send the proper header information along with the request
		saveHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		saveHttp.setRequestHeader("Content-length", params.length);
		saveHttp.setRequestHeader("Connection", "close");
		
		saveHttp.onreadystatechange = function() {//Call a function when the state changes.
			if(saveHttp.readyState == 4 && saveHttp.status == 200) {
				var resultText=saveHttp.responseText;
				var SplitArray = new Array();
				SplitArray = resultText.split("|||||SPLIT|||||");
				
				//alert(resultText);
				if(action=='savetext'){
					if(SplitArray[0] == 'window'){
						window.location.reload();
					}else{
						eval(SplitArray[0]);
					}
				}
			}
		}
		saveHttp.send(params);
	}
}

function cancelMediaBtn(){
	 $("#div_ztab_product_setup_media").animate({height: 'hide', opacity: 0}, 1000, "linear", function(){});
}

function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var request_o=null;
	try
	 {
	 // Firefox, Opera 8.0+, Safari
	 request_o=new XMLHttpRequest();
	 }
	catch (e)
	 {
	 //Internet Explorer
	 try
	  {
	  request_o=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	 catch (e)
	  {
	  request_o=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	 }
	return request_o;
	//}
}

function requestTableResize (tbname){
	new ColumnResize(document.getElementById(tbname));
}








//==========================================================================================================================================================================
//==========================================================================================================================================================================
//==========================================================================================================================================================================
//==========================================================================================================================================================================
//==========================================================================================================================================================================
function showhideDiv (obj,trg) {
	if(document.getElementById(obj).style.display=='none'){
		document.getElementById(obj).style.display='';
	}else{
		document.getElementById(obj).style.display='none';
	}
}

function showhideCatDiv (obj,trg) {
	//document.getElementById('div_ztab_media_workbench').innerHTML = 'Linking to : '+trg;
	if(document.getElementById(obj).style.display=='none'){
		document.getElementById(obj).style.display='';
	}else{
		document.getElementById(obj).style.display='none';
	}
}

	
function splitSelectValuesToInputs(varValue){
	//alert(document.getElementById('wym_img_id').value);
	var SplitArray = new Array();
	SplitArray = varValue.split("|");
	document.getElementById('wym_src').value = SplitArray[0];
	document.getElementById('wym_title').value = SplitArray[1];
	document.getElementById('wym_alt').value = SplitArray[2];
	
	//document.getElementById('wym_cat').options[document.getElementById('wym_cat').selectedIndex].value
	
	for (var i=0; i < document.getElementById('wym_cat').length; i++) {
		if (document.getElementById('wym_cat').options[i].value == SplitArray[3]) {
			document.getElementById('wym_cat').options[i].selected = true;
			//alert(document.wym_cat[i].value);
		}
	}
	
	document.getElementById('wym_img_id').value = SplitArray[4];
	//alert(document.getElementById('wym_img_id').value);
	
	if(document.getElementById('wym_src').value != ''){
		document.getElementById('img_show_div').innerHTML = '<img src="'+document.getElementById('wym_src').value+'" border="0">';
	}else{
		document.getElementById('img_show_div').innerHTML = '';
	}
}

function saveCreateNewImageForm(file){
	//alert('1');
	extArray = new Array(".jpg", ".png", ".gif");
	var fileChecked = checkFileExtension(extArray, file);
	//alert('2');
	if(fileChecked==false){
		return;
	}
	
	var save_alt = document.getElementById('niu_alt').value;
	var save_title = document.getElementById('niu_title').value;
	var save_category = document.getElementById("niu_cat").options[document.getElementById("niu_cat").selectedIndex].value;
	
	//alert('fileChecked : '+fileChecked);
	if(save_title == ''){
		alert('Please type image title');
		document.getElementById('niu_title').focus();
		return;
	}else{
		//alert('1');
		if(save_alt == '' || save_category == '0'){
			var answer = confirm ('The form is incomplete.\nContinue save image ?');
			if (answer){
				document.niu_form.submit();
			}else{
				return;
			}
		}else{
			document.niu_form.submit();
			//alert('3');
		}
	}
}


function checkFileExtension(extArray, file) {
	allowSubmit = false;
	if (!file){
		alert('Please select an image');
		return false;
	}
	
	//while (file.indexOf("\\") != -1)
	//	file = file.slice(file.indexOf("\\") + 1);
	//	ext = file.slice(file.indexOf(".")).toLowerCase();
	if( file.length == 0 ){
		return false;
	}
	
	var dot = file.lastIndexOf("."); 
	if( dot == -1 ){
		return false;
	}
	
	var ext = file.substr(dot,file.length); 
	
	for (var i = 0; i < extArray.length; i++){
		if(extArray[i] == ext){
			allowSubmit = true;
		}
	}
	
	if(allowSubmit == true){
		return true;
	}else{
		alert("Please only upload files that end in types:  "
		+ (extArray.join("  ")) + "\nPlease select a new "
		+ "file to upload and submit again.");
		return false;
	}
}

function saveImageForm(){
	//alert('Saving');
	var save_alt = document.getElementById('wym_alt').value;
	var save_title = document.getElementById('wym_title').value;
	var save_category = document.getElementById("wym_cat").options[document.getElementById("wym_cat").selectedIndex].value;
	var type = 'save';
	
	var save_id = document.getElementById('wym_img_id').value;
	
	if(save_id == '0'){
		alert('Image not selected');
		return;
	}else{
		if(save_title == '' || save_title == '[[Newly_uploaded_image]]'){
			save_title = 'TITLE NOT SET BY USER';
		}
	}
	
	httpObject = getHTTPObject();
	if (httpObject != null) {
		httpObject.open("GET", base_url+"sas/scripts/wymeditor/wymeditorajax.php?type="+type + "&save_alt="+save_alt + "&save_title="+save_title + "&save_category="+save_category + "&save_id="+save_id);
		httpObject.onreadystatechange = function () {
			if (httpObject.readyState == 4  || httpObject.readyState=="complete") {
				var response = httpObject.responseText;
				//alert(response);
				alert('Image should be added.');
			}
		};
		httpObject.send(null);
	}
}

// Get the HTTP Object
function getHTTPObject() {
	if (window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		return new XMLHttpRequest();
	else {
		alert("Your browser does not support AJAX.");
		return null;
	}
}

var httpObject = null;


function wymeditorAjax(type) {
	httpObject = getHTTPObject();
	if (httpObject != null) {
		httpObject.open("GET", "sas/scripts/wymeditor/wymeditorajax.php?type="+type);
		httpObject.onreadystatechange = function () {
			if (httpObject.readyState == 4  || httpObject.readyState=="complete") {
				var response = httpObject.responseText;
				alert('1\n'+response); //NOT GETTING CALLED
				//eval(response);
				start_editor(response)
				//document.getElementById('lm_img_select_div').innerHTML = response;
			}
		};
		httpObject.send(null);
	}
}
/**
*
*  AJAX IFRAME METHOD (AIM)
*  http://www.webtoolkit.info/
*
**/
 
AIM = {
 
	frame : function(c) {
 
		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
		document.body.appendChild(d);
 
		var i = document.getElementById(n);
		if (c && typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}
 
		return n;
	},
 
	form : function(f, name) {
		f.setAttribute('target', name);
	},
 
	submit : function(f, c) {
		AIM.form(f, AIM.frame(c));
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},
 
	loaded : function(id) {
		var i = document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
			var d = window.frames[id].document;
		}
		if (d.location.href == "about:blank") {
			return;
		}
 
		if (typeof(i.onComplete) == 'function') {
			i.onComplete(d.body.innerHTML);
			eval(d.body.innerHTML);
		}
	}
 
}

//==========================================================================================================================================================================
//==========================================================================================================================================================================
//==========================================================================================================================================================================
//==========================================================================================================================================================================
//==========================================================================================================================================================================
//
// Resizable Table Columns.
//  version: 1.0
//
// (c) 2006, bz
//
// 25.12.2006:  first working prototype
// 26.12.2006:  now works in IE as well but not in Opera (Opera is @#$%!)
// 27.12.2006:  changed initialization, now just make class='resizable' in table and load script
//
function preventEvent(e) {
	var ev = e || window.event;
	if (ev.preventDefault) ev.preventDefault();
	else ev.returnValue = false;
	if (ev.stopPropagation)
		ev.stopPropagation();
	return false;
}

function getStyle(x, styleProp) {
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

function getWidth(x) {
	if (x.currentStyle)
		// in IE
		var y = x.clientWidth - parseInt(x.currentStyle["paddingLeft"]) - parseInt(x.currentStyle["paddingRight"]);
		// for IE5: var y = x.offsetWidth;
	else if (window.getComputedStyle)
		// in Gecko
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue("width");
	return y || 0;
}

function setCookie (name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}
// main class prototype
function ColumnResize(table) {
	if (table.tagName != 'TABLE') return;

	this.id = table.id;

	// ============================================================
	// private data
	var self = this;

	var dragColumns  = table.rows[0].cells; // first row columns, used for changing of width
	if (!dragColumns) return; // return if no table exists or no one row exists

	var dragColumnNo; // current dragging column
	var dragX;        // last event X mouse coordinate

	var saveOnmouseup;   // save document onmouseup event handler
	var saveOnmousemove; // save document onmousemove event handler
	var saveBodyCursor;  // save body cursor property

	// ============================================================
	// methods

	// ============================================================
	// do changes columns widths
	// returns true if success and false otherwise
	this.changeColumnWidth = function(no, w) {
		if (!dragColumns) return false;

		if (no < 0) return false;
		if (dragColumns.length < no) return false;

		if (parseInt(dragColumns[no].style.width) <= -w) return false;
		if (dragColumns[no+1] && parseInt(dragColumns[no+1].style.width) <= w) return false;

		dragColumns[no].style.width = parseInt(dragColumns[no].style.width) + w +'px';
		if (dragColumns[no+1])
			dragColumns[no+1].style.width = parseInt(dragColumns[no+1].style.width) - w + 'px';

		return true;
	}

	// ============================================================
	// do drag column width
	this.columnDrag = function(e) {
		var e = e || window.event;
		var X = e.clientX || e.pageX;
		if (!self.changeColumnWidth(dragColumnNo, X-dragX)) {
			// stop drag!
			self.stopColumnDrag(e);
		}

		dragX = X;
		// prevent other event handling
		preventEvent(e);
		return false;
	}

	// ============================================================
	// stops column dragging
	this.stopColumnDrag = function(e) {
		var e = e || window.event;
		if (!dragColumns) return;

		// restore handlers & cursor
		document.onmouseup  = saveOnmouseup;
		document.onmousemove = saveOnmousemove;
		document.body.style.cursor = saveBodyCursor;

		// remember columns widths in cookies for server side
		var colWidth = '';
		var separator = '';
		for (var i=0; i<dragColumns.length; i++) {
			colWidth += separator + parseInt( getWidth(dragColumns[i]) );
			separator = '+';
		}
		var expire = new Date();
		expire.setDate(expire.getDate() + 365); // year
		document.cookie = self.id + '-width=' + colWidth +
			'; expires=' + expire.toGMTString();

		preventEvent(e);
	}

	// ============================================================
	// init data and start dragging
	this.startColumnDrag = function(e) {
		var e = e || window.event;

		// if not first button was clicked
		//if (e.button != 0) return;

		// remember dragging object
		dragColumnNo = (e.target || e.srcElement).parentNode.parentNode.cellIndex;
		dragX = e.clientX || e.pageX;

		// set up current columns widths in their particular attributes
		// do it in two steps to avoid jumps on page!
		var colWidth = new Array();
		for (var i=0; i<dragColumns.length; i++)
			colWidth[i] = parseInt( getWidth(dragColumns[i]) );
		for (var i=0; i<dragColumns.length; i++) {
			dragColumns[i].width = ""; // for sure
			dragColumns[i].style.width = colWidth[i] + "px";
		}

		saveOnmouseup       = document.onmouseup;
		document.onmouseup  = self.stopColumnDrag;

		saveBodyCursor             = document.body.style.cursor;
		document.body.style.cursor = 'w-resize';

		// fire!
		saveOnmousemove      = document.onmousemove;
		document.onmousemove = self.columnDrag;

		preventEvent(e);
	}

	// prepare table header to be draggable
	// it runs during class creation
	for (var i=0; i<dragColumns.length; i++) {
		dragColumns[i].innerHTML = "<div style='position:relative;height:100%;width:100%'>"+
			"<div style='"+
			"position:absolute;height:100%;width:5px;margin-right:-5px;"+
			"left:100%;top:0px;cursor:w-resize;z-index:10;'>"+
			"</div>"+
			dragColumns[i].innerHTML+
			"</div>";
			// BUGBUG: calculate real border width instead of 5px!!!
			dragColumns[i].firstChild.firstChild.onmousedown = this.startColumnDrag;
		}
}

// select all tables and make resizable those that have 'resizable' class
var resizableTables = new Array();
function ResizableColumns() {
	alert('Resize started');
	var tables = document.getElementsByTagName('table');
	for (var i=0; tables.item(i); i++) {
		if (tables[i].className.match(/resizable/)) {
			// generate id
			if (!tables[i].id) tables[i].id = 'table'+(i+1);
			// make table resizable
			resizableTables[resizableTables.length] = new ColumnResize(tables[i]);
		}
	}
//	alert(resizableTables.length + ' tables was added.');
}
// init tables
/*
if (document.addEventListener)
	document.addEventListener("onload", ResizableColumns, false);
else if (window.attachEvent)
	window.attachEvent("onload", ResizableColumns);
*/

/*USER IC
try {
	window.addEventListener('load', ResizableColumns, false);
} catch(e) {
	window.onload = ResizableColumns;
}
USER IC*/

//document.body.onload = ResizableColumns;

//============================================================
//
// Usage. In your html code just include the follow:
//
//============================================================
// <table id='objectId'>
// ...
// </table>
// < script >
// var xxx = new ColumnDrag( 'objectId' );
// < / script >
//============================================================
//
// NB! spaces was used to prevent browser interpret it!
//
//============================================================
//==========================================================================================================================================================================
//==========================================================================================================================================================================
//==========================================================================================================================================================================
//==========================================================================================================================================================================
//==========================================================================================================================================================================
