// JavaScript Document
function get(id){
	return document.getElementById(id);
}
function checkext(str, id, list){
	/*var oas = new ActiveXObject("Scripting.FileSystemObject");
	var e = oas.getFile(str);
	var f = e.size;
	alert(f/1000 + "Kb");*/
	
	if(str>''){
		if(in_string(str.substring(str.length,str.length-4), list)==false){
			get('file'+id+'_c').style.display='inline';
			get('file'+id).value = '';
			return;
		}
	}
	get('file'+id+'_c').style.display='none';
}
function in_string(value, list){
	var mySplitResult = list.split(",");
	
	for(i = 0; i < mySplitResult.length; i++){
		if(mySplitResult[i]==value){
			return true;
		}
	}
	return false;
}
function toggle(id, id2){
	var box = document.getElementById(id);
	var box2 = document.getElementById(id2);
	if(box){
		if(box.style.display=='none'){
			box.style.display = 'inline';
		}else{
			box.style.display = 'none';
		}
	}
	if(box2){
		if(box2.style.display=='none'){
			box2.style.display = 'inline';
		}else{
			box2.style.display = 'none';
		}
	}
}

function preview(url){
	window.popupWindow(url,'preview',600,600,'directories=yes,status=yes,resizable=yes,scrollbars=yes,location=1,menubar=yes,left=0,top=0');
}
function popupWindow(url, windowName, width, height, customparameters)
{
	//Determine the browser if positioning of pop-up window is supported
	theversion=navigator.appVersion.substring(0,3);
	
	if (navigator.appName == "Netscape" && navigator.appVersion.substring(0,1) == '4')
	{
		X = screenX + myscreen(outerWidth-width)/2;
		myscreenY = screenY + (outerHeight-height)/2;
	}
	else
	{
		//IE & Nav3 don't do positioning, variables just have to exist...
		myscreenX = 0;
		myscreenY = 0;
	}
	
	if (customparameters)
	{
		windowparams = customparameters
	}
	else
	{
		windowparams = "directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no"
	}
	
	//Default window name if not passed to function
	if (!windowName)
	{
		windowName = "popup_window";
	}
	
	popupWindowObj = window.open(url, windowName, "toolbar=no,width=" + width + ",height=" + height + ",screenX=" + myscreenX + ",screenY=" + myscreenY + "'," + windowparams) ;
	popupWindowObj.focus() ;
}
function nl2br (str, is_xhtml) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Philip Peterson
    // +   improved by: Onno Marsman
    // +   improved by: Atli Þór
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brettz9.blogspot.com)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: nl2br('Kevin\nvan\nZonneveld');
    // *     returns 1: 'Kevin<br />\nvan<br />\nZonneveld'
    // *     example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
    // *     returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
    // *     example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
    // *     returns 3: '<br />\nOne<br />\nTwo<br />\n<br />\nThree<br />\n'
 
    var breakTag = '';
 
    breakTag = '<br />';
    if (typeof is_xhtml != 'undefined' && !is_xhtml) {
        breakTag = '<br>';
    }
 
    return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}

function localcheck(site, checked){
	if(checked){
		
	}else{
		toggle('site'+site);
	}
}
function updatelocal(site, product, lno, form){
	var price = form.price.value;
	var surcharge = form.surcharge.value;
	var stock = form.stock.value;
	var vat = form.vat.value;
	var status = '';//form.status.value;
	var availability = '';
	if(form.availability){
		availability = form.availability.value;
	}
	var unlimited = 0;
	if(form.unlimited.checked){
		unlimited = 1;
	}
	var active = 0;
	//var divid = 'active_'+site;
	divid = 'site'+site+'_status';
	if(form.active.checked){
		active = 1;
		divid = 'site'+site+'_status';
		//get('site'+site).style.display='inline';
	}else{
		//get('site'+site).style.display='none';
	}
	ajax('product_edit_ajax.php?cmd=updatelocal&site='+site+'&product='+product+'&active='+active+'&price='+price+'&surcharge='+surcharge+'&unlimited='+unlimited+'&stock='+stock+'&vat='+vat+'&status='+status+'&availability='+availability, divid);

	if(get('su_'+lno)){
		get('su_'+lno).value = surcharge;
		get('pr_'+lno).value = price;
		get('st_'+lno).value = stock;
		get('un_'+lno).checked = form.unlimited.checked;
		if(active==1){
			get('lactive'+lno).innerHTML = '<img src="../images/tick.gif" />';
		}else{
			get('lactive'+lno).innerHTML = '<img src="../images/error.gif" />';
		}
	}
	return false;
}
function gtotal(form){
	form = get(form);
	if(form){
		form.total.value = (parseFloat(form.price.value)+(parseFloat(form.price.value)/100*parseFloat(form.vat.value))).toFixed(2);
		form.vattotal.value = (parseFloat(form.price.value)/100*parseFloat(form.vat.value)).toFixed(2);
	}
}
function addcategory(product){
	var parent = document.getElementById('newcat_parent').value;
	var MParentCategory = document.getElementById('MParentCategory').value;
	var name = document.getElementById('newcat_name').value;
	if(name>''){
		ajax('product_edit_ajax.php?MParentCategory='+MParentCategory+'&product='+product+'&add=category&parent='+parent+'&name='+encodeURIComponent(name), 'category')
	}else{
		alert('You must enter a name.');
	}
	return false;
}