
// ==============================================================
// telepark.cms

// This Software is copyright (c) 2007 by telepark, 
// Inh. Patrick Thomas, www.telepark.de. 
// All rights reserved. 

// You may not modify, extend, alter, reverse engineer or emulate
// the functionality, or create derivative works of the 
// Software in parts or it's entirety without the prior
// written consent of telepark.
// ==============================================================

function getfocus() {
	self.focus();
}

// open new window
function newWindow(url,name,width,height) {
	fenster = window.open (
	url,
	name, 					// name
	'toolbar=0' 			// tool bar 
	+',location=0' 			// address bar
	+',directories=0' 		// additional bars
	+',status=0' 			// status bar
	+',menubar=0' 			// menu bar
	+',scrollbars=1' 		// scroll bars
	+',resizable=1' 		// window resizable
	+',width=' + width 		// window width in pixels
	+',height=' + height 	// window height in pixels
	);
}

// create http request object
function createHttpRequest() {
	var xmlhttp=false;
 	try {
  		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} catch (e) {
  		try {
   			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  		} catch (E) {
   			xmlhttp = false;
  		}
	}
	return xmlhttp;
}

// refresh manager email
function refreshManagerEmail(userid) {
	var xmlhttp = createHttpRequest();

	if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
	  xmlhttp = new XMLHttpRequest();
	}
	
	targetURL = "../ajax/refreshManagerEmail.php";
	
	xmlhttp.open("GET", targetURL + "?userid=" + userid, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
			//alert(xmlhttp.responseText);
				eval(xmlhttp.responseText);
			}
		}
		catch(e) {
			status = '';
		}
	  }
	}
	xmlhttp.send(null);
}

// reload shop list
function reloadShopList(selectedItem) {
	var xmlhttp = createHttpRequest();

	if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
	  xmlhttp = new XMLHttpRequest();
	}
	
	targetURL = "../ajax/refreshShopList.php";
	
	xmlhttp.open("GET", targetURL + "?selected=" + selectedItem, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
				eval(xmlhttp.responseText);
			}
		}
		catch(e) {
			status = '';
		}
	  }
	}
	xmlhttp.send(null);
}

// reload product list
function reloadProductList(shopid,searchstring) {
	var s = '';
	if (searchstring!='') var s = encodeURI(searchstring);
	
	var xmlhttp = createHttpRequest();

	if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
	  xmlhttp = new XMLHttpRequest();
	}
	
	targetURL = "../ajax/reloadProductList.php";
	
	xmlhttp.open("GET", targetURL + "?shopid=" + shopid + "&searchstring=" + s, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
				eval(xmlhttp.responseText);
			}
		}
		catch(e) {
			status = '';
		}
	  }
	}
	xmlhttp.send(null);
}

// reload product list (frontend)
function reloadProductList_frontend(shopid,searchstring,start) {
	var s = '';
	if (searchstring!='') var s = encodeURI(searchstring);
	
	var xmlhttp = createHttpRequest();

	if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
	  xmlhttp = new XMLHttpRequest();
	}
	
	targetURL = "admin/modules/shop/ajax/reloadProductList_frontend.php";
	
	xmlhttp.open("GET", targetURL + "?shopid=" + shopid + "&searchstring=" + s + "&sp=" + start, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
				eval(xmlhttp.responseText);
			}
		}
		catch(e) {
			status = '';
		}
	  }
	}
	xmlhttp.send(null);
}

// delete shop
function deleteShop(shopid) {
	if (shopid == 0 || shopid == 'undefined') {
		alert(selectshopfirst);
	}
	else {
		var c = confirm(deleteshopconfirm);
		if (c == true) {
			var xmlhttp = createHttpRequest();
	
			if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
	  			xmlhttp = new XMLHttpRequest();
			}
			xmlhttp.open("GET", "../ajax/deleteshop.php?shopid=" + shopid , true);
			xmlhttp.onreadystatechange=function() {
			  // readyState==4 - meaning the load is complete
			  if (xmlhttp.readyState==4) {
			  	// only if "OK"
				var status = '';
				try {
					status = xmlhttp.status;
					if (status==200) {
						eval(xmlhttp.responseText);
						reloadShopList('');
					}
				}
				catch(e) {
					status = '';
					reloadShopList('');
				}
			  }
			}
			xmlhttp.send(null);
		}
		else
			return false;
	}
}

// delete product
function deleteProduct(productid) {
	var c = confirm(deleteproductconfirm);
	if (c == true) {
		var xmlhttp = createHttpRequest();

		if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
  			xmlhttp = new XMLHttpRequest();
		}
		xmlhttp.open("GET", "../ajax/deleteproduct.php?productid=" + productid , true);
		xmlhttp.onreadystatechange=function() {
		  // readyState==4 - meaning the load is complete
		  if (xmlhttp.readyState==4) {
		  	// only if "OK"
			var status = '';
			try {
				status = xmlhttp.status;
				if (status==200) {
					eval(xmlhttp.responseText);
					reloadProductList('','');
				}
			}
			catch(e) {
				status = '';
				reloadProductList('','');
			}
		  }
		}
		xmlhttp.send(null);
	}
	else
		return false;
}

// delete picture
function deletePicture(picture) {
	var c = confirm(deletepictureconfirm);
	if (c == true) {
		var xmlhttp = createHttpRequest();

		if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
  			xmlhttp = new XMLHttpRequest();
		}
		xmlhttp.open("GET", "../ajax/deletepicture.php?productid=" + picture , true);
		xmlhttp.onreadystatechange=function() {
		  // readyState==4 - meaning the load is complete
		  if (xmlhttp.readyState==4) {
		  	// only if "OK"
			var status = '';
			try {
				status = xmlhttp.status;
				if (status==200) {
					eval(xmlhttp.responseText);
				}
			}
			catch(e) {
				status = '';
			}
		  }
		}
		xmlhttp.send(null);
	}
	else
		return false;
}

// add product to cart
function addToCart(shopid,productid,amount,referer) {
	var xmlhttp = createHttpRequest();

	if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
	  xmlhttp = new XMLHttpRequest();
	}
	if (referer=='front') targetURL = "admin/modules/shop/ajax/addToCart.php";
	else targetURL = "../ajax/addToCart.php";
	
	xmlhttp.open("GET", targetURL + "?shopid=" + shopid + "&productid=" + productid + "&amount=" + amount, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
				eval(xmlhttp.responseText);
			}
		}
		catch(e) {
			status = '';
		}
	  }
	}
	xmlhttp.send(null);
}

// show cart
function showCart(shopid) {
	var xmlhttp = createHttpRequest();

	if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
	  xmlhttp = new XMLHttpRequest();
	}
	
	targetURL = "admin/modules/shop/ajax/showCart.php";
	
	xmlhttp.open("GET", targetURL + "?shopid=" + shopid, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
				eval(xmlhttp.responseText);
			}
		}
		catch(e) {
			status = '';
		}
	  }
	}
	xmlhttp.send(null);
}

// display product amount
function ProdAmount(productid,shopid,mode) {
	var xmlhttp = createHttpRequest();

	if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
	  xmlhttp = new XMLHttpRequest();
	}
	
	targetURL = "admin/modules/shop/ajax/ProdAmount.php";
	
	xmlhttp.open("GET", targetURL + "?productid=" + productid + "&shopid=" + shopid + "&mode=" + mode, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		var status = '';
		try {
			status = xmlhttp.status;
			if (status==200) {
				eval(xmlhttp.responseText);
			}
		}
		catch(e) {
			status = '';
		}
	  }
	}
	xmlhttp.send(null);
}

// remove product
function ProdRemove(productid,shopid) {
	//var c = confirm(conftext);
	//if (c == true) {
	
		var xmlhttp = createHttpRequest();
	
		if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
		  xmlhttp = new XMLHttpRequest();
		}
		
		targetURL = "admin/modules/shop/ajax/ProdRemove.php";
		
		xmlhttp.open("GET", targetURL + "?productid=" + productid + "&shopid=" + shopid + "&reload=1", true);
		xmlhttp.onreadystatechange=function() {
		  // readyState==4 - meaning the load is complete
		  if (xmlhttp.readyState==4) {
			// only if "OK"
			var status = '';
			try {
				status = xmlhttp.status;
				if (status==200) {
					eval(xmlhttp.responseText);
				}
			}
			catch(e) {
				status = '';
			}
		  }
		}
		xmlhttp.send(null);
	//}
}

// discard cart
function discardCart(shopid,conftext,cartURI) {
	var c = confirm(conftext);
	if (c == true) {
	
		var xmlhttp = createHttpRequest();
	
		if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
		  xmlhttp = new XMLHttpRequest();
		}
		
		targetURL = "admin/modules/shop/ajax/discardCart.php";
		
		xmlhttp.open("GET", targetURL + "?shopid=" + shopid, true);
		xmlhttp.onreadystatechange=function() {
		  // readyState==4 - meaning the load is complete
		  if (xmlhttp.readyState==4) {
			// only if "OK"
			var status = '';
			try {
				status = xmlhttp.status;
				if (status==200) {
				  eval(xmlhttp.responseText);	
				}
			}
			catch(e) {
				status = '';
			}
		  }
		}
		xmlhttp.send(null);
	}
	else return false;
}

// check manager email
function checkManager(conftext,conftext2) {
	if (document.getElementById('shop_manageremail').value=='') {
		alert(conftext);
		return false;
	}
	//else if (document.getElementById('shop_recipientemail').value=='') {
	//	alert(conftext2);
	//	return false;
	//}
	else document.shoporder.submit();
}

// switch input field
function switchInput(pid,amount,shopid) {
	var xmlhttp = createHttpRequest();
	
	if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
	  xmlhttp = new XMLHttpRequest();
	}
	
	xmlhttp.open("GET", "admin/modules/shop/ajax/amountinput.php?pid=" + pid + "&amount=" + amount + "&shopid=" + shopid, true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		if (xmlhttp.status==200) {
		 eval(xmlhttp.responseText);
		 document.getElementById('amount').focus();
		}
	  }
	}
	xmlhttp.send(null);
	return false;
}

// submit input field with return key
function submitter_input(pid, amount, shopid, evt)	{
  var keyCode = document.layers ? evt.which : document.all ? evt.keyCode : evt.keyCode;
  if (keyCode == 13) {
  	saveAmountForm(pid,shopid,amount);
  }
}

// save product amount
function saveAmountForm(pid,shopid,newamount) {
	var xmlhttp = createHttpRequest();
	
	if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
	  xmlhttp = new XMLHttpRequest();
	}
	
	xmlhttp.open("GET", "admin/modules/shop/ajax/saveamountform.php?pid=" + pid + "&shopid=" + shopid + "&amount=" + newamount , true);
	xmlhttp.onreadystatechange=function() {
	  // readyState==4 - meaning the load is complete
	  if (xmlhttp.readyState==4) {
		// only if "OK"
		if (xmlhttp.status==200) {
		 eval(xmlhttp.responseText);
		}
	  }
	}
	xmlhttp.send(null);
	return false;
}

function newAddToCart(productId,shopId) {
	
	var pre = "";
	if(TP_URL)
		pre = TP_URL;	
	var targetURL = pre + "admin/modules/shop/ajax/addToCart.php";
	var query = "shopid=" + shopId + "&productid=" + productId + "&amount=1&newCartFront=1";
	$.ajax({
		type: "GET",
  		url: targetURL,
  		data: query,
  		cache: false,
  		success: function(html){
	    	//$('#imgGreenCheck_' + productId).remove();
			//$('#hrefaddToCart_' + productId).before('<img style="cursor:pointer; margin-bottom:-10px; positon:relative; margin-right:6px;" onclick="newRemoveShopSelect(\''+productId+'\',\''+shopId+'\')" id="imgGreenCheck_'+productId+'" src="'+imgSrc+'" width="24" height="24"  />');
			//$('#hrefaddToCart_' + productId).replaceWith('in cart' );
			$('#td_image_' + productId).html('<img src="'+pre+'admin/modules/shop/imgs/green_checkmark.png" width="16" height="16" />&nbsp;in cart');
			//$('#hrefaddToCartSec_' + productId).replaceWith('<a href="javascript:void(0)" id="hrefaddToCartSec_'+productId+'" onclick="newRemoveShopSelect(\''+productId+'\',\''+shopId+'\')" style="font-size:11px;color:green;" title="Remove from cart" >Remove</a>' );
			//$('#inCart_'+ productId).html('In cart');
			//document.getElementById('img_cart_basket_' + productId).src = pre + "admin/modules/shop/imgs/green_checkmark.png";
			//document.getElementById('img_cart_basket2_' + productId).src = pre + "admin/modules/shop/imgs/green_checkmark.png";
			
		/*$('#hrefaddToCart_' + productId).text('Is in cart');
		$('#hrefaddToCart_' + productId).css('color','green');	
		
		$('#hrefaddToCartSec_' + productId).text('Is in cart');
		$('#hrefaddToCartSec_' + productId).css('color','green');	*/
  		}
	});
}



function newRemoveShopSelect(productId, shopId) {
	
	var pre = "";
	if(TP_URL)
		pre = TP_URL;
	var targetURL = "admin/modules/shop/ajax/ProdRemove.php";	
	var query = "productid=" + productId + "&shopid=" + shopId + "&reload=1";
	$.ajax({
		type: "GET",
  		url: targetURL,
  		data: query,
  		cache: false,
  		success: function(html){
	    	//$('#imgGreenCheck_' + productId).remove();	
	    	$('#hrefaddToCart_' + productId).replaceWith('<a href="javascript:void(0)" id="hrefaddToCart_'+productId+'" onclick="newAddToCart(\''+productId+'\',\''+shopId+'\')" style="font-size:11px;font-weight:normal;"  >Add to cart</a>' );
		//	$('#hrefaddToCartSec_' + productId).replaceWith('<a href="javascript:void(0)" id="hrefaddToCartSec_'+productId+'" onclick="newAddToCart(\''+productId+'\',\''+shopId+'\')" style="font-size:11px;" >Add to cart</a>' );
	    	/*$('#hrefaddToCart_' + productId).html('Add to cart');
		    $('#hrefaddToCart_' + productId).css('color','#5F82C2');*/
	    		document.getElementById('img_cart_basket_' + productId).src = pre + "admin/modules/shop/imgs/cart_16.gif";
			   // document.getElementById('img_cart_basket2_' + productId).src = pre + "admin/modules/shop/imgs/cart_16.gif";
			   $('#inCart_'+ productId).html('&nbsp;');
  		}
	});
}