function testKey(e)
{
  // Make sure to use event.charCode if available
  var key = (typeof e.charCode == 'undefined' ? e.keyCode : e.charCode);

  // Ignore special keys
  if (e.ctrlKey || e.altKey || key < 32)
    return true;

  key = String.fromCharCode(key);
  return /\d/.test(key);
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ' ' + '$2');
	}
	return x1 + x2;
}


// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function setCookie( name, value, expires, path, domain, secure ) {
    var today = new Date();
    today.setTime( today.getTime() );
    if ( expires ) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    document.cookie = name+'='+escape( value ) +
        ( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
        ( ( path ) ? ';path=' + path : '' ) +
        ( ( domain ) ? ';domain=' + domain : '' ) +
        ( ( secure ) ? ';secure' : '' );
}


function deleteCookie( name, path, domain ) {
    if ( getCookie( name ) ) document.cookie = name + '=' +
            ( ( path ) ? ';path=' + path : '') +
            ( ( domain ) ? ';domain=' + domain : '' ) +
            ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

 
function incNumber(str,inum) {
	var cookiecode = document.getElementById("code" + str).innerHTML;
	var cookiename = "simplecaddy[" + document.getElementById("code" + str).innerHTML + "]";
	//var prodname = escape(document.getElementById("name" + str).innerHTML);
	var a = parseFloat(document.getElementById("my_number" + str).value * 1 + inum);
	var b = parseFloat(document.getElementById("price" + str).innerHTML.replace(/\s*/g, ''));
	var resnum = a * b;
	var format = addCommas(resnum.toFixed(2));
	var plwidth = cookiename.split("/");
	if (inum == "5") {
		var prodtype = "pl";
	} else {
		var prodtype = "ak";
	}
	cookiearray = Get_Cookie(cookiename);
	cookieval = cookiearray.split(",");
	//alert(cookieval[2]);
	document.getElementById("alert").innerHTML = "<font color=Red><strong>Вы изменили количество товара, не забудьте пересчитать заказ!</strong></font>";
    document.getElementById("my_number" + str).value = document.getElementById("my_number" + str).value * 1 + inum;
    document.getElementById("summ" + str).innerHTML = format.replace(/[.]/, ",");
    var cookievalue = cookiecode + "," + a + "," + cookieval[2] + "," + b + "," + plwidth[1] + "," + prodtype;
    deleteCookie(cookiename);
    setCookie(cookiename, cookievalue, 3000);
    
}

function decNumber(str,dnum) {
	var cookiecode = document.getElementById("code" + str).innerHTML;
	var cookiename = "simplecaddy[" + document.getElementById("code" + str).innerHTML + "]";
	var a = parseFloat(document.getElementById("my_number" + str).value * 1 - dnum);
	var b = parseFloat(document.getElementById("price" + str).innerHTML.replace(/\s*/g, ''));
	var resnum = a * b;	
	var format = addCommas(resnum.toFixed(2));
	var plwidth = cookiename.split("/");
	if (dnum == "5") {
		var prodtype = "pl";
	} else {
		var prodtype = "ak";
	}
	cookiearray = Get_Cookie(cookiename);
	cookieval = cookiearray.split(",");
    if (document.getElementById("my_number" + str).value * 1 > dnum) {
		document.getElementById("my_number" + str).value = document.getElementById("my_number" + str).value * 1 - dnum;
		document.getElementById("summ" + str).innerHTML = format.replace(/[.]/, ",");
	document.getElementById("alert").innerHTML = "<font color=Red><strong>Вы изменили количество товара, не забудьте пересчитать заказ!</strong></font>";		
    var cookievalue = cookiecode + "," + a + "," + cookieval[2] + "," + b + "," + plwidth[1] + "," + prodtype;
    deleteCookie(cookiename);
    setCookie(cookiename, cookievalue, 3000);

    }
}
//########################################################################
// name - ��� cookie
// [path] - ����, ��� �������� cookie �������������
// [domain] - �����, ��� �������� cookie �������������
function deleteCookie(name, path, domain) {
        if (Get_Cookie(name)) {
                document.cookie = name + "=" + 
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                "; expires=Thu, 01-Jan-70 00:00:01 GMT"
        }
}
        
//########################################################################        
// name - ��� cookie
// value - �������� cookie
// [expires] - ���� ��������� �������� cookie (�� ��������� - �� ����� ������)
// [path] - ����, ��� �������� cookie ������������� (�� ��������� - ��������, � ������� �������� ���� �����������)
// [domain] - �����, ��� �������� cookie ������������� (�� ��������� - �����, � ������� �������� ���� �����������)
// [secure] - ���������� ��������, ������������ ��������� �� ���������� �������� �������� cookie





function setCookie1(name, value, expires) {
        var curCookie = name + "=" + escape(value) +
                ((expires) ? "; expires=" + expires.toGMTString() : "");
                
//        if (!caution || (name + "=" + escape(value)).length <= 4000)
//                document.cookie = curCookie
//        else
//                if (confirm("Cookie ��������� 4KB � ����� ������� !"))
//                        document.cookie = curCookie
}


function incNumberList(str,inum) {
    document.getElementById("my_number" + str).value = (document.getElementById("my_number" + str).value * 1) + inum;
}

function decNumberList(str,dnum) {
	if ((document.getElementById("my_number" + str).value * 1) > dnum) {
    	document.getElementById("my_number" + str).value = (document.getElementById("my_number" + str).value * 1) - dnum;
	}
}




//function incNumberList(str) {
//	var a = parseFloat(document.getElementById("my_number" + str).value * 1 + 1);
//	var b = parseFloat(document.getElementById("price" + str).innerHTML.replace(/\s*/g, ''));
//	//var repl = b.replace(/\s*/g, '');
//	var resnum = a * b;
//	//alert(b);
//	var format = addCommas(resnum.toFixed(2));
//    document.getElementById("my_number" + str).value = document.getElementById("my_number" + str).value * 1 + 1;
//    document.getElementById("summ" + str).innerHTML = format.replace(/[.]/, ",");
//}
//
//function decNumberList(str) {
//	var a = parseFloat(document.getElementById("my_number" + str).value * 1 - 1);
//	var b = parseFloat(document.getElementById("price" + str).innerHTML.replace(/\s*/g, ''));
//	var resnum = a * b;	
//	var format = addCommas(resnum.toFixed(2));
//    if (document.getElementById("my_number" + str).value * 1 > 1) {
//		document.getElementById("my_number" + str).value = document.getElementById("my_number" + str).value * 1 - 1;
//		document.getElementById("summ" + str).innerHTML = format.replace(/[.]/, ",");
//    }
//}

