    $(document).ready(function(){  
        if (document.forms[1] != null) { 
            document.forms[1][0].focus();
        }
    });

//<![CDATA[
var d = document;

function $a(id)
{
	return d.getElementById(id);
}

function addEventsAction(element, event, action, useCapture)
{
	if(!action)
		action = function(){};

	if(element.addEventListener)
		element.addEventListener(event, action, useCapture);
	else if(element.attachEvent)
		element.attachEvent('on' + event, action);
	else
		alert('Error Occur...');
}

function createElement(name, attr, val, text, _alert_)
{
	var element, i;

	if(!document.all)
	{
		element = d.createElement(name);

		if(attr)
		{
			for(i=0; i<attr.length; ++i)
			{
				if(attr[i].substr(0, 2) == 'on')
					eval('element.' + attr[i] + '=' + val[i]);
				else
					element.setAttribute(attr[i], val[i]);
			}
		}
	}
	else
	{
		if(name == 'input')
		{
			for(i=0; i<attr.length; ++i)
			{
				if(attr[i] == 'name')
				{
					element = d.createElement('<input name="' + val[i] + '">');
					attr.splice(i, 1);
					attr.splice(i, 1);
					break;
				}
			}
		}
		else
		{
			element = d.createElement(name);
		}

		if(attr)
		{
			for(i=0; i<attr.length; ++i)
			{
				switch(attr[i])
				{
					case 'class':
						attr[i] = 'className';
						break;
					case 'for':
						attr[i] = 'htmlFor';
						break;
					case 'maxlength':
						attr[i] = 'maxLength';
						break;
					case 'colspan':
						attr[i] = 'colSpan';
						break;
					case 'style':
						continue;
						break;
					case 'name':
						//need to check for not input elements
						continue;
					case '':
						continue;
				}

				element.setAttribute(attr[i], val[i]);
/*				if(attr[i].substr(0,2) != 'on')
					eval('element.' + attr[i] + "='" + val[i] + "'");
//				else if(val[i].substr(0,11) != 'function(){')
//					eval('element.' + attr[i] + '=' + val[i]);
				else
					eval('element.' + attr[i] + '=function(){' + val[i] + '}');
-*/			}
		}
	}

	if(text)
		element.appendChild(d.createTextNode(text));

	return element;
}

/**
 * check vaild "input text" value that need to be number formated and convrt to number if not.
 *
 * @param inputElement - pointer to the "input text" elemnt.
 * @param isInt - set to true to disable float value, default is allow float values.
 * @param allowPositive - set to boolean false if you want to disallow positive value. default is true.
 * @param limitMax - the limit of the maximu, value. if the value is bigger from this limit, the function is set the value to this limit. please not set this parameter or set to false to not limit.
 * @param limitMin - the limit of the minimum value. if the value is smaller from this limit, the function is set the value to this limit. please not set this parameter or set to false to not limit.
 * @param decimalDigits - set the number of maximum decimal digits. default is 2.
 *
 * @return true on success (or if the value is empty) otherwise false.
 **/
function form_convertNum(inputElement, isInt, allowPositive, limitMax, limitMin, decimalDigits)
{
	try{
		if(allowPositive !== false) allowPositive=true;
		limitMax = parseFloat(limitMax);
		limitMin = parseFloat(limitMin);
		decimalDigits = parseInt(decimalDigits);
		if(!decimalDigits) decimalDigits = 2;

		var val = inputElement.value;

		if(!val) return true;
		if(val=='-' && allowPositive) return true;

		if(!isInt)
		{
			if(val=='0' || val=='0.')
				return true;

			if(val=='.')
			{
				val = '0.';
				return true;
			}

			if(val=='-.' && allowPositive)
			{
				val = '-0.';
				return true;
			}

			if(val.indexof('.') == val.substr(val.length-1)) // +"d." -"d.." -"d.d." -".."
				return true;

		//	if(val.substr(val.length-1)=='.' /* "99." */ || val.substr(val.length-1)=='0' /* "0" */)
		//		return true;
		}

		if(isNaN(val = isInt ? parseInt(val) : parseFloat(val))) val = '';
		if(!allowPositive && val<0) val = val-val*2;

		inputElement.value = val;

		return true;
	}catch(e){
		return false;
	}
}

function form_rangeSelect(textObj, from, to)
{
	if(!textObj.value)
		return;
	else if(!from){
		from = 0;
		to = textObj.value.length;
	}
	else if(from >= to)
		return;

	if(textObj.setSelectionRange)
	{
		textObj.setSelectionRange(from, to);
	}
	else if(textObj.createTextRange)
	{
		var range = textObj.createTextRange();
		range.collapse(true);
		range.moveEnd('character', to);
		range.moveStart('character', from);
		range.select();
	}
}

var form_words_limit_max = 40;
function form_words_limit(obj)
{
	arr = obj.value.replace('  ', ' ').split(' ');

	if(arr.length <= words_limit_max)
		return;

	alert('עברת את מגבלת 40 המילים');

	while(arr.length > words_limit_max)
		arr.pop();

	obj.value = arr.join(' ');
}


function productEdit_checkForm()
{
	return true;
}

function product_delete_picture(picture_id, picture_link)
{
    $.post("/product/del_pic",
    { pic_id: picture_id },
    function(data) {
     if (data == "LOGINREQUIRED") alert("Login is required.");
     else if (data == "INVALIDPICTUREID") alert("Invalid picture ID.");
     else if (data == "DOESNTEXIST") alert("Picture doesnt exist.");
     else if (data == "DOESNTBELONGTOYOU") alert("Picture doesnt belong to one of your products.");
     else { 
         $(picture_link.parentNode.parentNode).fadeOut("slow");
     }
    }
    );
}

var productAdd_addPic_obj;
var productAdd_addPic_obj_last_child;
var productAdd_addPic_num = 0;
var productAdd_addPic_max = 5;
function productAdd_addPic()
{
	if(productAdd_addPic_num != productAdd_addPic_max)
		productAdd_addPic_obj.insertBefore(createElement('input', ['type', 'name'], ['file', 'pic'+(productAdd_addPic_num++)]), productAdd_addPic_obj_last_child);
	else
		alert('The Maximum alowed Pictures is ' + productAdd_addPic_max);

}


function product_image_popup(pic_name)
{
	window.open('http://www.wibizz.com/images/products/'+ pic_name);
}

function product_delete(product_id, product_link)
{
	if (confirm('Are you sure you want to delete the Product from the list?'))
        {
                $.post("/product/del",
                { product_id: product_id },
                function(data) {
                    if (data == "LOGINREQUIRED") alert("Login is required.");
                    else if (data == "INVALIDPRODUCTID") alert("Invalid picture ID.");
                    else if (data == "DOESNTEXIST") alert("Picture doesnt exist.");
                    else if (data == "DOESNTBELONGTOYOU") alert("Picture doesnt belong to one of your products.");
                    else $(product_link.parentNode).fadeOut("slow");
                }
                );
        } return false;
}
//]]>
