 if(typeof HTMLElement!="undefined" && !
HTMLElement.prototype.insertAdjacentElement){
	HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode){
		switch (where){
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this)
			break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild);
			break;
		case 'beforeEnd':
			this.appendChild(parsedNode);
			break;
		case 'afterEnd':
			if (this.nextSibling) 
this.parentNode.insertBefore(parsedNode,this.nextSibling);
			else this.parentNode.appendChild(parsedNode);
			break;
		}
	}
	HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr){
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	}
	HTMLElement.prototype.insertAdjacentText = function(where,txtStr){
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	}
}



// productSizeCode.php //////////////////////////////////////////////

 function confirmDeleteSizeCode(id, product_id, i){
 	var content = '<div class="alert_in">Varmista kohteen poisto!<br>';
	content += '<input type="button" value="&nbsp;&nbsp;OK&nbsp;&nbsp;" class="delete_button" onclick="deleteSizeCode(\''+id+'\',\''+product_id+'\',\''+i+'\')" /> <input type="button" value="Peru" class="button" onclick="openClose(\'alert_delete_sizecode\')" /></div>';
	document.getElementById('alert_delete_sizecode').innerHTML=content;
	new Effect.BlindDown($('alert_delete_sizecode'), {duration:0.5, scaleContent: false});
 }
 function deleteSizeCode(id, product_id, i){
 	new Ajax.Request('modules/deleteSizeCode.php', {	
			method: 'post',
			parameters: {del: 1, id: id, product_id: product_id },
			onSuccess: function(transport) {
				if(transport.responseText=="no"){
					document.getElementById('alert_delete_sizecode').innerHTML="Kohteen poisto ei onnistunut";
				} else {
					new Effect.BlindUp($('alert_delete_sizecode'), {duration:1.0, scaleContent: false});
					new Effect.BlindUp($('row_'+i), {duration:1.0, scaleContent: false});
					new Effect.BlindUp($('a_row_'+i), {duration:1.0, scaleContent: false});
					window.setTimeout(function (text) {
						document.getElementById('sizecode_wrap').innerHTML=transport.responseText;
						Sortable.create('rows', { constraint:'vertical' });
						document.getElementById('editSizeCode').style.display='block';
					},1200,transport.responseText);
				}
			}
		});
 }
 function newSizeCode(){
 	var content = '<div id="newsizecode" style="display:none;float:left;clear:both;width:100%;"><input type="text" id="new_size" name="new_size[]" value="" class="input_sizecode" />';	
		content += '<input type="text" id="new_code" name="new_code[]" value="" class="input_sizecode" /></div>';
 	document.getElementById('rows').insertAdjacentHTML('afterEnd', content);
	new Effect.BlindDown($('newsizecode'), {duration:0.5, scaleContent: false});
 }
 
 function updateSizeCode(product_id){	
 	document.getElementById('ajax_loader').style.display="block";
 	var form = document.getElementById('sizecodeForm');
	var id = "";
	var size = "";
	var code = "";
	var new_size = "";
	var new_code = "";
	var elements = new Array();
	var new_elements = new Array();
	for(var k=0; k<form.length; k++){
		if(form.elements[k].id=="id"){
			id += form.elements[k].value+",";
			elements.push(id);
		} else if(form.elements[k].id=="edit_size"){
			size += form.elements[k].value+",";
		} else if(form.elements[k].id=="edit_code"){
			code += form.elements[k].value+",";
		} else if(form.elements[k].id=="new_size"){
			new_size += form.elements[k].value+",";
			new_elements.push(new_size);
			elements.push(new_size);
		} else if(form.elements[k].id=="new_code"){
			new_code += form.elements[k].value+",";
		}
	}
	elements = elements.length;
	new_elements = new_elements.length;
	var olds = elements-new_elements;
  	new Ajax.Request('modules/saveSizeCode.php', {	
			method: 'post',
			parameters: {update: 1, product_id: product_id, id: id, size: size, code: code, new_size: new_size, new_code: new_code },
			onSuccess: function(transport) {
					document.getElementById('ajax_loader').style.display="none";
					document.getElementById('sizecode_wrap').innerHTML=transport.responseText;
					for(var i=1; i<=elements; i++){
						if(i>olds){
							document.getElementById('a_row_'+i).style.display='none';
								new Effect.BlindDown($('a_row_'+i), {duration:0.5, scaleContent: false});
						}
					}
					document.getElementById('editSizeCode').style.display='block';
					Sortable.create('rows', { constraint:'vertical' });				
			}
		});
 }



////// leftImages.php //////////////////////////////////////////////////////////////
function changeImage(url, i){
	var img1 = document.getElementById('img_1').src;
	var content = '<img id="img_1x" src="'+url+'" width="230" border="0" style="position:absolute;left:0;top:0; display:none" />';
	document.getElementById('images').insertAdjacentHTML('afterEnd', content);
	document.getElementById('firstimage').innerHTML = '<img id="img_1" src="'+url+'" width="230" border="0" />';
}
function editImages(){
	var edit = document.getElementById('edit_images');
	Appears(edit);
}
function confirmDeleteImage(image_id,i,id){
	var Alert = 'alert_image_'+i;
	var content = '<div class="alert_in">Varmista kohteen poisto!<br>';
	content += '<input type="button" onclick="deleteImage(\''+image_id+'\',\''+id+'\',\''+i+'\')" value="Poista" class="delete_button" /> <input type="button" value="Peru" class="button" onclick="openClose(\''+Alert+'\')" /></div>';
	document.getElementById(Alert).innerHTML=content;
	openClose(Alert);
}
function deleteImage(image_id,id,i){
	new Ajax.Request('modules/deleteImage.php', {	
			method: 'post',
			parameters: {del:1, image_id: image_id, id: id },
			onSuccess: function(transport) {
				new Effect.BlindUp($('edit_'+i), {duration:0.5, scaleContent: false});
				//new Effect.BlindUp($('image_'+i), {duration:0.5, scaleContent: false});
				fadeInOut('image_'+i);
				window.setTimeout(function (text) {
					document.getElementById('images').innerHTML=transport.responseText;
					document.getElementById('edit_images').style.display='block';
					so1.write("curve_top_left");
					so2.write("curve_imagebottom");
					
					Sortable.create('images_ol', { constraint:'vertical' });
				},1200,transport.responseText);
			}
	});
}


function saveImageOrder(){
	document.getElementById('ajax_loader').style.display="block";
	var x = document.getElementById('editImagesForm');
	var images = "";
	for (var i=0;i<(x.length-2);i++){
  		images += x.elements[i].value+',';
	}
	new Ajax.Request('modules/updateOrder.php', {	
			method: 'post',
			parameters: {type: 'image', ids: images },
			onSuccess: function(transport) {
				document.getElementById('ajax_loader').style.display="none";
				document.getElementById('images').innerHTML=transport.responseText;
				document.getElementById('edit_images').style.display="block";
				so1.write("curve_top_left");
				so2.write("curve_imagebottom");
				Sortable.create('images_ol', { constraint:'vertical' });
				
		}
	});

}




////// products.php ////////////////////////////////////////////////////////////////////////////////

function EditProduct(){
	document.getElementById('edit_name').style.display='block';
	document.getElementById('edit_legend').style.display='block';
	document.getElementById('edit_text').style.display='block';
	document.getElementById('name').style.display='none';
	document.getElementById('legend').style.display='none';
	document.getElementById('text').style.display='none';
	document.getElementById('peru_button').style.display='block';
	makeWW();
}
function closeEditProduct(){
	document.getElementById('edit_name').style.display='none';
	document.getElementById('edit_legend').style.display='none';
	document.getElementById('edit_text').style.display='none';
	document.getElementById('name').style.display='block';
	document.getElementById('legend').style.display='block';
	document.getElementById('text').style.display='block';
	document.getElementById('peru_button').style.display='none';
}
function confirmDeleteProduct(id){
	var content = 'Haluatko varmasti poistaa kohteen?<br /><br />';
	content += '<input type="button" class="delete_button" value="Poista" onclick="deleteProduct('+id+')"> &nbsp; <input type="button" class="button" value="Peru" onclick="Fades(\'alert\')">';
	document.getElementById('alert').innerHTML=content;
	Appears('alert');
}
function deleteProduct(id){
	new Ajax.Request('modules/deleteProduct.php', {	
			method: 'post',
			parameters: {id: id },
			onSuccess: function(transport) {
				if(transport.responseText=="yes"){
					var content = "Kohde on poistettu!<br /><br />";
					content += '<input type="button" class="button" value="&nbsp;&nbsp; OK &nbsp;&nbsp;" onclick="afterDelete()">';
					document.getElementById('alert').innerHTML=content;
					Appears('alert');
				} else {
					var content = 'kohteen poisto ei onnistunut?<br /><br />';
					content += transport.responseText+"<br><br>";
					content += '&nbsp; <input type="button" class="button" value="OK" onclick="Fades(\'alert\')">';
					document.getElementById('alert').innerHTML=content;
					Appears('alert');
				}
		}
	});

}
function afterDelete(){
	window.location="http://www.kir-fix.fi/index_dimedia.php";
}


//////////  productManual.php/////////////////////////////////////////////////////////////////////////////////////////

function confirmDeleteManual(product_id){
	var content = '<div class="alert_in">Varmista kohteen poisto!<br>';
	content += '<input type="button" value="Poista" onclick="deleteManual(\''+product_id+'\')" class="delete_button" /> <input type="button" value="Peru" onclick="openClose(\'alert_manual\')" class="button" />';
	document.getElementById('alert_manual').innerHTML = content;
	openClose('alert_manual');
}
function deleteManual(product_id){
	new Ajax.Request('modules/productManual.php', {	
			method: 'post',
			parameters: {del: '1', product_id: product_id },
			onSuccess: function(transport) {
				//if(transport.responseText==""){
					openClose('manual_1');
					openClose('alert_manual');
				//} else {
				//	document.getElementById('manual').innerHTML = transport.responseText;
				//}
			}
	});
}

//////// product / adminNavigation.php /////////////////////////////////////////////////////////////////////////////////////////


function openNavi(id){
	if(document.getElementById('order_1')){
		Sortable.destroy('order_1');
	}
	if(document.getElementById('order_2_'+id)){
		Sortable.create('order_2_'+id, { constraint:'vertical' });
	}
	if(document.getElementById('title_'+id).className=="title1"){
		document.getElementById('title_'+id).className="title1B";
	}
	var a = ""+document.getElementById('title_'+id).href;
	a = a.replace(/openNavi/, 'closeNavi');
	document.getElementById('title_'+id).href=a;
	new Effect.BlindDown($('navi_'+id), {duration:.50, scaleContent: false, queue: 'end'});

}
function closeNavi(id){
	if(document.getElementById('order_2_'+id)){
		Sortable.destroy('order_2_'+id);
	}
	new Effect.BlindUp($('navi_'+id), {duration:.50, scaleContent: false});
	if(document.getElementById('title_'+id).className=="title1B"){
		document.getElementById('title_'+id).className="title1";
	}
	var a = ""+document.getElementById('title_'+id).href;
	a = a.replace(/closeNavi/, 'openNavi');
	document.getElementById('title_'+id).href=a;
	if(document.getElementById('order_1')){
		Sortable.create('order_1', { constraint:'vertical', queue: 'end' });
	}
}
function openSubNavi(id){
	if(document.getElementById('naviTitle_'+id).className=="title2"){
		document.getElementById('naviTitle_'+id).className="title2B";
	}
	var a = ""+document.getElementById('naviTitle_'+id).innerHTML;
	a = a.replace(/openSubNavi/, 'closeSubNavi');
	document.getElementById('naviTitle_'+id).innerHTML=a;
	new Effect.BlindDown($('subnavi_'+id), {duration:.50, scaleContent: false});
}
function closeSubNavi(id){
	new Effect.BlindUp($('subnavi_'+id), {duration:.50, scaleContent: false});
	if(document.getElementById('naviTitle_'+id).className=="title2B"){
		document.getElementById('naviTitle_'+id).className="title2";
	}
	var a = ""+document.getElementById('naviTitle_'+id).innerHTML;
	a = a.replace(/closeSubNavi/, 'openSubNavi');
	document.getElementById('naviTitle_'+id).innerHTML=a;
}
function Edit(id){
	if(document.getElementById('title_edit_'+id).style.display=="none"){
		new Effect.BlindDown($('title_edit_'+id), {duration:.50, scaleContent: false});
		if(document.getElementById('legend_edit_'+id)){
			document.getElementById('legend_edit_'+id).style.height=document.getElementById('legend_'+id).offsetHeight+20+"px";
			new Effect.BlindUp($('legend_'+id), {duration:.50, scaleContent: false});
		}
	} else {
		new Effect.BlindUp($('title_edit_'+id), {duration:.50, scaleContent: false});
		new Effect.BlindDown($('legend_'+id), {duration:.50, scaleContent: false});
	}
}

function newCategory(id, parent_id){
	var elm = 'productNavigation';
	var content = '<div id="newProductCategory_'+id+'" class="newProductCategory" style="display:none;"><div style="padding:16px 0 10px 0;">';
		content += '<form action="" method="post">';
    	content += '<a class="title1"><input type="text" id="new_category_name_'+id+'" name="new_category" class="input" /> </a><input type="button" value="Tallenna" class="save_button" onclick="saveCategory('+id+','+parent_id+')" />';
		var id2 = 'newProductCategory_'+id;
		content += '&nbsp;<input type="button" value="Peru" class="button" onclick="openClose(\''+id2+'\')" /></form></div></div>';
		document.getElementById(elm).innerHTML=content+document.getElementById(elm).innerHTML;
	//new Effect.BlindDown($('newProductCategory_'+id), {duration:0.5, scaleContent: false});
	openClose('newProductCategory_'+id);
}

function newSubCategory(id, parent_id){
	var elm = 'navi_'+id;
	var content = '<div id="newProductCategory_'+id+'" class="newSubProductCategory" style="display:none;"><div style="padding:16px 0 10px 0;">';
	//	content += '<form action="" method="post" style="padding:0; margin:0;">';
    	content += '<input type="text" id="new_category_name_'+id+'" name="new_category" class="input" /> <input type="button" value="Tallenna" class="save_button" onclick="saveCategory('+id+','+parent_id+')" />';
		content += '<input type="button" value="Peru" class="button" onclick="openClose(\'newProductCategory_'+id+'\')" /></form></div></div>';
//	var old = document.getElementById(elm).innerHTML;
//		document.getElementById(elm).innerHTML=content+old;
		document.getElementById(elm).insertAdjacentHTML('afterBegin', content);
	if(document.getElementById(elm).style.display=='none'){
		document.getElementById('newProductCategory_'+id).style.display='block';
		openNavi(id);
	} else {
		new Effect.BlindDown($('newProductCategory_'+id), {duration:0.5, scaleContent: false});
	}
}



function saveCategory(id, parent_id){	
	document.getElementById('ajax_loader').style.display="block";
	var new_category = document.getElementById('new_category_name_'+id).value;
	new Ajax.Request('modules/saveCategory.php', {	
			method: 'post',
			parameters: {parent_id: parent_id, new_category: new_category, element_id: id },
			onSuccess: function(transport) {
				document.getElementById('ajax_loader').style.display="none";
				document.getElementById('productNavigation').innerHTML=transport.responseText;
		}
	});
}
function updateCategory(id, category_id, parent_id){	
	document.getElementById('ajax_loader').style.display="block";
	var update_category = document.getElementById('category_name_'+id).value;
	if(document.getElementById('legend_edit_'+id)){
		var legend = document.getElementById('legend_edit_'+id).value;
	} else {
		var legend = "";
	}
	var display = document.getElementById('display_'+id).value;
	new Ajax.Request('modules/saveCategory.php', {	
			method: 'post',
			parameters: {category_id: category_id, parent_id: parent_id, update_category: update_category, legend: legend, display: display, element_id: id},
			onSuccess: function(transport) {
				document.getElementById('ajax_loader').style.display="none";
				document.getElementById('productNavigation').innerHTML=transport.responseText;
				//Edit(id);				
		}
	});
}
	
function openEdit(id){
	document.getElementById('editbuttons_'+id).style.display="block";
	
}
function closeEdit(id){
	document.getElementById('editbuttons_'+id).style.display="none";
}


function Appears(id){
	$(id).appear({ duration: 0.5 });
}
function Fades(id){
	$(id).fade({ duration: 0.5 });
}

function fadeInOut(id){
	if(document.getElementById(id).style.display=='none'){
		$(id).appear({ duration: 0.5 });
	} else {
		$(id).fade({ duration: 0.5 });
	}
}

function openClose(id){
	if(document.getElementById(id).style.display=='none'){
		new Effect.BlindDown($(id), {duration:0.5, scaleContent: false});
	} else {
		new Effect.BlindUp($(id), {duration:0.5, scaleContent: false});
	}
}

function confirmDeleteCategory(id, name, category_id, n){
	new Ajax.Request('modules/deleteCategory.php', {	
			method: 'post',
			parameters: {check: 1, category_id: category_id, n: n },
			onSuccess: function(transport) {
				var Alert = 'cat_alert_'+id;
				if(transport.responseText=="match_product"){
					var content = '<div class="alert_in"><strong>'+name+'</strong> sisältää tuotteita, joten sitä ei voi poistaa!';
					content += '<input type="button" class="button" value="&nbsp;&nbsp; OK &nbsp;&nbsp;" style="margin-left:20px;" onclick="openClose(\''+Alert+'\')"></div>';
					document.getElementById(Alert).innerHTML=content;
					openClose(Alert);
				} else 	if(transport.responseText=="match_category"){
					var content = '<div class="alert_in"><strong>'+name+'</strong> sisältää alakategorioita, joten sitä ei voi poistaa! ';
					content += '<input type="button" class="button" value="&nbsp;&nbsp; OK &nbsp;&nbsp;" style="margin-left:20px;" onclick="openClose(\''+Alert+'\')"></div>';
					document.getElementById(Alert).innerHTML=content;
					openClose(Alert);
				} else if(transport.responseText=="no_match"){
					var content = '<div class="alert_in">Haluatko varmasti poistaa <strong>'+name+'</strong>? ';
					content += '<input type="button" class="delete_button" value="Poista" style="margin-left:20px;" onclick="DeleteCategory(\''+id+'\','+category_id+','+n+')"> &nbsp; <input type="button" class="button" value="Peru" onclick="openClose(\''+Alert+'\')"></div>';
					document.getElementById(Alert).innerHTML=content;
					openClose(Alert);
				}
		}
	});
}

function DeleteCategory(element_id,category_id,n){
	new Ajax.Request('modules/deleteCategory.php', {	
			method: 'post',
			parameters: {del: 1, category_id: category_id, element_id: element_id },
			onSuccess: function(transport) {
				new Effect.BlindUp($('navigation_'+element_id), {duration:0.5, scaleContent: false});
				openClose('cat_alert_'+element_id);
				//document.getElementById('productNavigation').innerHTML=transport.responseText;
		}
	});
	
}


function updateOrder(){	
	document.getElementById('ajax_loader').style.display="block";
  	new Ajax.Request('modules/updateCategoryOrder.php', {	
			method: 'post',
			parameters: {update: 1, place: Sortable.serialize('order_1') },
			onSuccess: function(transport) {
					document.getElementById('ajax_loader').style.display="none";
					document.getElementById('productNavigation').innerHTML=transport.responseText;
					Sortable.create('order_1', { constraint:'vertical' });
			}
		});
 }	
			
function updateSubOrder(category_id, element_id){	
	document.getElementById('ajax_loader').style.display="block";
  	new Ajax.Request('modules/updateCategoryOrder.php', {	
			method: 'post',
			parameters: {update: 1, category_id: category_id, element_id: element_id, place: Sortable.serialize('order_2_'+element_id) },
			onSuccess: function(transport) {
					document.getElementById('ajax_loader').style.display="none";
					document.getElementById('productNavigation').innerHTML=transport.responseText;
					Sortable.create('order_2_'+element_id, { constraint:'vertical' });
			}
		});
 }





// Pages ////////////////////////////////////////////////////////////////////////////////////////

function EditPage(){
	document.getElementById('edit_text').style.display='block';
	document.getElementById('text').style.display='none';
	document.getElementById('save_button').style.display='block';
	document.getElementById('peru_button').style.display='block';
	makeWW();
}
function closeEditPage(){
	document.getElementById('edit_text').style.display='none';
	document.getElementById('text').style.display='block';
	document.getElementById('save_button').style.display='none';
	document.getElementById('peru_button').style.display='none';
}