/**
 *  PHPMass Shopping Cart
 * Copyright (C) 2008  PHPMass.com
 * 
 *  This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
var newWholesaleLine = new Template(
'<tr style="font-size:9px;" id="wholesaleDiv#{Index}">'+
'<td><input name="wholesaleUnitsAmount[]" type="text" class="textinput" id="wholesaleAmount#{Index}" size="2" value="#{Qty}" /></td>'+
'<td class="su"><input name="wholesaleUnitPrice[]" type="text" class="textinput" id="wholesalePrice#{Index}" size="20" value="#{Price}" /></td>'+
'<td>&nbsp;</td>'+
'<td><a href="#" onclick="$(\'wholesaleDiv#{Index}\').remove()">'+SiteLang['delete']+'</a></td>'+
'</tr>'
);
var newAccLine = new Template (
'<tr style="font-size:9px;" id="selectedUsefullAccDiv#{Index}">'+
'<td>#{ProductID}<input type="hidden" name="selectedUsefullAcc[#{ProductID}]" value="selected" /></td>'+
'<td class="su">#{Name}</td>'+
'<td>#{Price} '+SiteLang['CURRENCY']+'</td>'+
'<td><a href="#" onclick="$(\'selectedUsefullAccDiv#{Index}\').remove()">'+SiteLang['delete']+'</a></td>'+
'</tr>'
);
var newTopProdLine = new Template (
'<tr style="font-size:9px;" id="selectedUsefullAccDiv#{Index}">'+
'<td>#{ProductID}<input type="hidden" name="selectedUsefullAcc[#{ProductID}]" value="selected" /></td>'+
'<td class="su">#{Name}</td>'+
'<td>#{Price} '+SiteLang['CURRENCY']+'</td>'+
'<td><a href="#" onclick="removeTopProd(#{ProductID},#{CatID},#{Index});">'+SiteLang['delete']+'</a></td>'+
'</tr>'
);
var newTopProdManufLine = new Template (
'<tr style="font-size:9px;" id="selectedUsefullAccDiv#{Index}">'+
'<td>#{ProductID}<input type="hidden" name="selectedUsefullAcc[#{ProductID}]" value="selected" /></td>'+
'<td class="su">#{Name}</td>'+
'<td>#{Price} '+SiteLang['CURRENCY']+'</td>'+
'<td><a href="#" onclick="removeTopManProd(#{ProductID},#{ManufID},#{Index});">'+SiteLang['delete']+'</a></td>'+
'</tr>'
);
var newRelatedProLine = new Template (
'<tr style="font-size:9px;" id="relatedProductDiv#{Index}">'+
'<td>#{ProductID}<input type="hidden" name="relatedProduct[#{ProductID}]" value="selected" /></td>'+
'<td class="su">#{Name}</td>'+
'<td>#{Price} '+SiteLang['CURRENCY']+'</td>'+
'<td><a href="#" onclick="$(\'relatedProductDiv#{Index}\').remove()">'+SiteLang['delete']+'</a></td>'+
'</tr>'
);
var newfreeProdLine = new Template (
'<tr style="font-size:9px;" id="freeAccDiv#{Index}">'+
'<td>#{ProductID}<input type="hidden" name="freeAccID[#{ProductID}]" value="selected" /></td>'+
'<td class="su">#{Name}</td>'+
'<td>#{Price} '+SiteLang['CURRENCY']+'</td>'+
'<td><input type="text" size="3" value="1" name="freeAccAmount[#{ProductID}]" class="textinput" /></td>'+
'<td><span id="fullFreePrice#{Index}">#{Price} '+SiteLang['CURRENCY']+'</span></sup></td>'+
'<td><a href="#" onclick="$(\'freeAccDiv#{Index}\').remove()">'+SiteLang['delete']+'</a></td>'+
'</tr>'
);
var newOptionLine = new Template (
'<tr>'+
'<td><input type="text" size="2" name="OptionOrder[#{CatID}][]" value="10" class="textinput"/></td>'+
'<td><em>#{Name}</em> <input type="hidden" name="OptionName[#{CatID}][]" value="#{Name}" /></td>'+
'<td><input type="text" size="6" name="OptionPrice[#{CatID}][]" value="#{Price}" class="textinput"/>#{Type}<input type="hidden" name="OptionType[#{CatID}][]" value="#{Value}" /></td>'+
'<td><a href="#" onclick="removeOptionLine(this)">'+SiteLang['delete']+'</a></td>'+
'</tr>'
);
var newCatOptionLine = new Template (
'<tr class="adminproductoption">'+
'<td>&nbsp;</td>'+
'<td><div style="float:left"><strong>#{Name}</strong> <input type="hidden" name="CatOptionName[#{CatID}]" value="#{Name}" /> </div>'+
'</td>'+
'<td>&nbsp;</td>'+
'<td><a href="#" onclick="removeCatOption(this)">'+SiteLang['delete']+'</a></td>'+
'</tr>'+
'<tr style="font-size:9px; background:#ECF1F2" class="addNewCatOptions">'+
'<td><em>'+SiteLang['Add_option']+'</em></td>'+
'<td class="su"><em>'+SiteLang['Option_Name']+'</em> <input type="text" size="20" class="textinput"/> <em>'+SiteLang['Price']+'</em> <input type="text" size="6" class="textinput"/> '+
'<select class="selectinput" ><option value="%">%</option><option value="$" selected="selected">'+SiteLang['CURRENCY']+'</option></select>'+
'</td>'+
'<td><a href="#" onclick="addOption(this,#{CatID})">'+SiteLang['Add_option']+'</a></td>'+
'<td>&nbsp;</td>'+
'</tr>'
);
var banIpLine = new Template (
'<tr><td><input name="banip[]" type="text" class="textinput" id="banip[]"/>&nbsp;<a href="#" onclick="removeOptionLine(this)">-remove</a></td></tr>'
);
var banWordLine = new Template (
'<tr><td><input name="words[]" type="text" class="textinput" id="words[]"/>&nbsp;<a href="#" onclick="removeOptionLine(this)">-remove</a></td>'
);
var priceLine = new Template ('<tr><td><input type="text" class="textinput" name="pricelist[]" /> <a href="#" onclick="newPriceLine()">+</a> <a href="#" onclick="removePriceLine(this)">-</a></td></tr>');
// This function makes tabs on the product administration page. a_ (div id) is the tab id of the respective table id
function admprodtabs(divid)
{
	var divArray = ['primaryinfo', 'secondaryimages', 'specifications', 'shippingandpayment', 'shippingandpayment', 'admprodoptions', 'wholesale', 'freeaccessories', 'usefullaccesories', 'relatedproducts', 'productother'];
	for (var index = 0; index < divArray.length; ++index) {
		if(divid == divArray[index]){
			if($(divArray[index])){
				$(divArray[index]).show();
				$('a_'+divArray[index]).addClassName('selected');
			}
		}
		else{
			if($(divArray[index])){
				$(divArray[index]).hide();
				$('a_'+divArray[index]).removeClassName('selected');
			}
		}
	}
}
// This function makes tabs on the user administration page. a_ (div id) is the tab id of the respective table id
function admusrtabs(divid)
{
	var divArray = ['usrInfo', 'usrAddresses', 'usrCompany'];
	for (var index = 0; index < divArray.length; ++index) {
		if(divid == divArray[index]){
			$(divArray[index]).show();
			$('a_'+divArray[index]).addClassName('selected');
		}
		else{
			$(divArray[index]).hide();
			$('a_'+divArray[index]).removeClassName('selected');
		}
	}
}
// This creates the url. It takes the string from one input to another
function makeUrlFromTo(fromInput,toInput)
{
	var fromValue = $(fromInput).value;
	fromValue = fromValue.toLowerCase()

	$(toInput).value = cleanURLString(fromValue);

}
function cleanURLString(URLString)
{
	URLString = URLString.strip();
	var rgx = new RegExp('[^0-9A-Za-z]','g');
	URLString = URLString.replace(rgx,'-');
	return URLString;
}

var newwindow;
function winPopUp(url,width,height)
{
	newwindow=window.open(url,'name','height='+height+',width='+width+',status=yes,resizable=yes');
	if (window.focus) {newwindow.focus()}
}
function closeReloadParent()
{
	opener.location.reload(true);
	self.close();
}
function IsDecimal(strString)
//  check for valid numeric strings
{
	var strValidChars = "0123456789.";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
}
function IsInteger(strString)
//  check for valid numeric strings
{
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
}

function toogleSelectAll(checkbox,selectbox){
	if($F(checkbox)){
		selectBox = $(selectbox);
		if (selectBox.type == "select-multiple") {
			for (var i = 0; i < selectBox.options.length; i++) {
				selectBox.options[i].selected = true;
			}
		}
	}
	else{
		selectBox = $(selectbox);
		if (selectBox.type == "select-multiple") {
			for (var i = 0; i < selectBox.options.length; i++) {
				selectBox.options[i].selected = false;
			}
		}
	}
}
function toogleEnabledInput(checkbox,input){
	if($F(checkbox)){
		$(input).disabled = false;
	}
	else{
		$(input).disabled = true;
		$(input).value = '';
	}
}

function addNewWholesale()
{
	WholeSaleIndex++;
	var Price = $F('wholesalePrice')
	var Qty = $F('wholesaleQty')
	var show = {Price: Price, Qty: Qty, Index: WholeSaleIndex};
	var html = newWholesaleLine.evaluate(show);
	new Insertion.Before('addWholesale', html)
}

function getFreeAcc(){
	var code = $F('freeAccCode');
	var name = $F('freeAccname');
	var cat = $F('freeAccCat');
	var	URL = '/admin/product.php?Ajax=on&Action=FreeAcc';
	var opt = {
		method: 'post',
		parameters:'code='+code+'&name='+name+'&cat='+cat,
		onSuccess: function(transport) {
			$('freeAccSearchTable').innerHTML = transport.responseText;
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText + '\n'+SiteLang['Contact_Sys_Admin']);

		}
	}
	new Ajax.Request(URL, opt);

}
function selectThisFreeAcc(ProdID,ProdName,ProdPrice){
	UseFullFreeAccIndex++;
	var show = {Price: ProdPrice, ProductID: ProdID, Name: ProdName, Index: UseFullFreeAccIndex};
	var html = newfreeProdLine.evaluate(show);
	new Insertion.Before('freeAccSearch', html)

}


function getUsefullAcc(){
	var code = $F('usefullAccCode');
	var name = $F('usefullAccName');
	var cat = $F('usefullAccCat');
	var	URL = '/admin/product.php?Ajax=on&Action=UsefullAcc';
	var opt = {
		method: 'post',
		parameters:'code='+code+'&name='+name+'&cat='+cat,
		onSuccess: function(transport) {
			$('usefullAccSearcTable').innerHTML = transport.responseText;
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText + '\n'+SiteLang['Contact_Sys_Admin']);

		}
	}
	new Ajax.Request(URL, opt);

}
function selectThisAcc(ProdID,ProdName,ProdPrice){
	UseFullAccIndex++;
	var show = {Price: ProdPrice, ProductID: ProdID, Name: ProdName, Index: UseFullAccIndex};
	var html = newAccLine.evaluate(show);
	new Insertion.Before('searchAccessories', html)

}
function getrelatedProducts(){
	var code = $F('relatedProductCode');
	var name = $F('relatedProductsName');
	var cat = $F('relatedProductsCat');
	var	URL = '/admin/product.php?Ajax=on&Action=UsefullProd';
	var opt = {
		method: 'post',
		parameters:'code='+code+'&name='+name+'&cat='+cat,
		onSuccess: function(transport) {
			$('usefullProdSearcTable').innerHTML = transport.responseText;
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText + '\n'+SiteLang['Contact_Sys_Admin']);

		}
	}
	new Ajax.Request(URL, opt);

}
function selectThisProd(ProdID,ProdName,ProdPrice){
	var show = {Price: ProdPrice, ProductID: ProdID, Name: ProdName, Index: UseFullAccIndex};
	var html = newRelatedProLine.evaluate(show);
	new Insertion.Before('searchRelated', html)
	UseFullAccIndex++;
}
function resetImage(ProdID,ImageID){
	var	URL = '/admin/product.php?Ajax=on&Action=DelImg';

	var opt = {
		method: 'post',
		parameters:'ProductID='+ProdID+'&ImageID='+ImageID,
		onSuccess: function(transport) {
			$(ImageID).src = '/images/image-na-big.jpg';
			//alert(transport.responseText);
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText + '\n'+SiteLang['Contact_Sys_Admin']);

		}
	}
	new Ajax.Request(URL, opt);
}
function resetFile(ProdID){
	var	URL = '/admin/product.php?Ajax=on&Action=DelFile';
	var opt = {
		method: 'post',
		parameters:'ProductID='+ProdID,
		onSuccess: function(transport) {
			$('fileDiv').hide();
			alert(transport.responseText);
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText + '\n'+SiteLang['Contact_Sys_Admin']);

		}
	}
	new Ajax.Request(URL, opt);
}

function addOption(div,CatID){
	var tr = div.parentNode.parentNode
	var name = tr.getElementsByTagName('input')[0];
	var price = tr.getElementsByTagName('input')[1];
	var type = tr.getElementsByTagName('select')[0];
	var show = {CatID: CatID, Price: price.value, Name: name.value, Type: type.options[type.selectedIndex].text, Value: type.options[type.selectedIndex].value};
	var html = newOptionLine.evaluate(show);
	new Insertion.Before(tr, html)
}
function addCatOption(div){

	optionsCatIndex++;
	var tr = div.parentNode.parentNode;
	var name = tr.getElementsByTagName('input')[0];
	var show = {CatID: optionsCatIndex, Name: name.value};
	var html = newCatOptionLine.evaluate(show);
	new Insertion.Before(tr, html);
}
function removeOptionLine(div){
	var td = $(div).parentElement.parentElement;
	$(td).remove();
}
function removeCatOption(div){
	var tr = $(div).parentElement.parentElement;
	var NextChild = $(tr).nextSibling;


	var finished = false;
	while(finished == false){
		if($(NextChild).hasClassName('addNewCatOptions')){
			$(NextChild).remove();
			finished = true;
		}
		else{
			NextChild = $(NextChild).nextSibling;
			RemoveChild = $(NextChild).previousSibling;
			$(RemoveChild).remove();
		}
	}
	$(tr).remove();
}
function getProductForDiscounts(){
	var code = $F('relatedProductCode');
	var name = $F('relatedProductsName');
	var cat = $F('relatedProductsCat');
	var	URL = '/admin/discounts.php?Ajax=usefullProd';
	var opt = {
		method: 'post',
		parameters:'code='+code+'&name='+name+'&cat='+cat,
		onSuccess: function(transport) {
			$('usefullProdSearcTable').innerHTML = transport.responseText;
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText + '\n'+SiteLang['Contact_Sys_Admin']);

		}
	}
	new Ajax.Request(URL, opt);

}
function selectDiscountedProd(ProdID,ProdName,ProdPrice){
	$('discountProductID').value = ProdID;
	$('selectedProdName').innerHTML = ProdName;
	$('selectedProdPrice').innerHTML = ProdPrice;
	$('usefullProdSearcTable').innerHTML = '';
}
function getUsersForDiscounts(){
	var relUserID = $F('relatedUserID');
	var relUsername = $F('relatedUserName');
	var relFullName = $F('relatedFullName');
	var	URL = '/admin/discounts.php?Ajax=relatedUsers';
	var opt = {
		method: 'post',
		parameters:'userid='+relUserID+'&username='+relUsername+'&fullname='+relFullName,
		onSuccess: function(transport) {
			$('usefullProdSearcTable').innerHTML = transport.responseText;
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText + '\n'+SiteLang['Contact_Sys_Admin']);

		}
	}
	new Ajax.Request(URL, opt);

}
function selectDiscountedUsers(UserID,Username,UserFullName){
	$('discountUserID').value = UserID;
	$('discountUserName').innerHTML = Username;
	$('discountUserFullName').innerHTML = UserFullName;
	$('usefullProdSearcTable').innerHTML = '';
}
function addToList(from,to){
	if($F(from).length > 0) $(to).value = $F(to) + $F(from) + "\n";
}
function getUsersForNewsletter(){
	var relUserID = $F('relatedUserID');
	var relUsername = $F('relatedUserName');
	var relFullName = $F('relatedFullName');
	var	URL = '/admin/newsletter.php?Ajax=relatedUsers';
	var opt = {
		method: 'post',
		parameters:'userid='+relUserID+'&username='+relUsername+'&fullname='+relFullName,
		onSuccess: function(transport) {
			$('usefullProdSearcTable').innerHTML = transport.responseText;
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText + '\n'+SiteLang['Contact_Sys_Admin']);

		}
	}
	new Ajax.Request(URL, opt);

}
function addEmailToNewsletter(email){
	if(email.length > 0) $('newsletterOthers').value = $F('newsletterOthers') + email + "\n";
}
function switchFormUsersType(curent){
	var curentClass = curent + 'Form';
	var divArray = ['profileForm', 'addressForm', 'companyForm'];
	for (var index = 0; index < divArray.length; ++index) {
		if(curentClass == divArray[index]){
			toogleFormOfClass(divArray[index],'enable');
		}
		else{
			toogleFormOfClass(divArray[index],'disable');
		}
	}
}
function toogleFormOfClass(classname,action){
	document.getElementsByClassName(classname).each(function(elm) {
		if(action == 'disable'){
			elm.disable();
			elm.value = '';
		}
		if(action == 'enable'){
			elm.enable();
		}
	});
}
function getTopProd(currentCat){
	var code = $F('usefullAccCode');
	var name = $F('usefullAccName');
	var cat = $F('usefullAccCat');
	var	URL = '/admin/categories.php?Ajax=on&Action=UsefullAcc';
	var opt = {
		method: 'post',
		parameters:'code='+code+'&name='+name+'&cat='+cat+'&currentCat='+currentCat,
		onSuccess: function(transport) {
			$('usefullAccSearcTable').innerHTML = transport.responseText;
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText + '\n'+SiteLang['Contact_Sys_Admin']);

		}
	}
	new Ajax.Request(URL, opt);

}
function getTopProdManuf(manuf){
	var code = $F('usefullAccCode');
	var name = $F('usefullAccName');
	var cat = $F('usefullAccCat');
	var	URL = '/admin/manufacturers.php?Ajax=on&Action=GetProds';
	var opt = {
		method: 'post',
		parameters:'code='+code+'&name='+name+'&cat='+cat+'&manuf='+manuf,
		onSuccess: function(transport) {
			$('usefullAccSearcTable').innerHTML = transport.responseText;
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText + '\n'+SiteLang['Contact_Sys_Admin']);

		}
	}
	new Ajax.Request(URL, opt);

}
function selectBestProd(ProdID,ProdName,ProdPrice,CatID){
	UseFullAccIndex++;
	var show = {Price: ProdPrice, ProductID: ProdID, Name: ProdName, Index: UseFullAccIndex, CatID: CatID};
	var html = newTopProdLine.evaluate(show);
	new Insertion.Before('searchAccessories', html)

	var	URL = '/admin/categories.php?Ajax=on&Action=setSpecProd';
	var opt = {
		method: 'post',
		parameters:'prodid='+ProdID+'&cat='+CatID,
		onSuccess: function(transport) {
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText + '\n'+SiteLang['Contact_Sys_Admin']);

		}
	}
	new Ajax.Request(URL, opt);

}
function selectBestManufProd(ProdID,ProdName,ProdPrice,ManufID){
	UseFullAccIndex++;
	var show = {Price: ProdPrice, ProductID: ProdID, Name: ProdName, Index: UseFullAccIndex, ManufID: ManufID};
	var html = newTopProdManufLine.evaluate(show);
	new Insertion.Before('searchAccessories', html)

	var	URL = '/admin/manufacturers.php?Ajax=on&Action=setSpecProd';
	var opt = {
		method: 'post',
		parameters:'prodid='+ProdID+'&ManufID='+ManufID,
		onSuccess: function(transport) {
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText + '\n'+SiteLang['Contact_Sys_Admin']);

		}
	}
	new Ajax.Request(URL, opt);

}
function removeTopProd(ProdID,CatID,IndexDiv){
	var	URL = '/admin/categories.php?Ajax=on&Action=delSpecProd';
	var opt = {
		method: 'post',
		parameters:'prodid='+ProdID+'&cat='+CatID,
		onSuccess: function(transport) {
			$('selectedUsefullAccDiv'+IndexDiv).remove();
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText + '\n'+SiteLang['Contact_Sys_Admin']);
		}
	}
	new Ajax.Request(URL, opt);
}
function removeTopManProd(ProdID,ManufID,IndexDiv){
	var	URL = '/admin/manufacturers.php?Ajax=on&Action=delSpecProd';
	var opt = {
		method: 'post',
		parameters:'prodid='+ProdID+'&ManufID='+ManufID,
		onSuccess: function(transport) {
			$('selectedUsefullAccDiv'+IndexDiv).remove();
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText + '\n'+SiteLang['Contact_Sys_Admin']);
		}
	}
	new Ajax.Request(URL, opt);
}
function addNewIpLine(){
	var show = {};
	var html = banIpLine.evaluate(show);
	new Insertion.Before('newIpLine', html);
}
function addNewWordLine(){
	var show = {};
	var html = banWordLine.evaluate(show);
	new Insertion.Before('newWordLine', html);
}
function tooglePrice(checkbox){
	if ($(checkbox).checked) $('catprices').innerHTML = '<table><tr><td><strong>'+SiteLang['Min']+'</strong></td></tr><tr><td><input type="text" class="textinput" name="pricelist[]"/> <a href="#" onclick="newPriceLine()">+</a> <a href="#" onclick="removePriceLine(this)">-</a></td></tr><tr id="maxprice"><td><strong>'+SiteLang['Max']+'</strong></td></tr></td></table>';
	else $('catprices').innerHTML = '';
}
function newPriceLine(){
	var show = {};
	var html = priceLine.evaluate(show);
	new Insertion.Before('maxprice', html);
}
function removePriceLine(div){
	var td = $(div).parentElement.parentElement;
	$(td).remove();
}
