function getLanguage(vchar)
{
	document.mainform.lang.value = vchar;
	document.mainform.action = '';
	document.mainform.target = '';
	document.mainform.submit();
}

function getCategories(id)
{
	document.mainform.categoryid.value = id;
	document.mainform.action = 'category_list.php';
	document.mainform.target = '';
	document.mainform.submit();
}

function getSubCategories(id,parentid)
{
	document.mainform.categoryid.value = id;
	document.mainform.parentid.value = parentid;
	//document.mainform.categoryname.value = name;
	document.mainform.action = 'category_explore.php';
	document.mainform.target = '';
	document.mainform.submit();
}

function GoMenu(start,page)
{
	document.mainform.start.value = start;
	document.mainform.page.value = page;

	document.mainform.action = '';
	document.mainform.target = '';
	document.mainform.submit();
}

function GoJumpMenu(start,page)
{
	var ostart = ( start * page ) - page;

	document.mainform.start.value = ostart;
	document.mainform.page.value = start;

	document.mainform.action = '';
	document.mainform.target = '';
	document.mainform.submit();
}

function doSearch()
{
	var condition = document.mainform.filter.value;
	
	document.mainform.action = '';
	document.mainform.target = '';
	document.mainform.submit();
}

function getItem(itemid)
{
	document.mainform.itemid.value = itemid;
	document.mainform.action = 'item_details.php';
	document.mainform.target = '';
	document.mainform.submit();
}

function viewItem(itemid,categoryid,parentid)
{
	document.mainform.itemid.value = itemid;
	document.mainform.categoryid.value = categoryid;
	document.mainform.parentid.value = parentid;
	document.mainform.action = 'item_details.php';
	document.mainform.target = '';
	document.mainform.submit();
}

function getItemSearch(itemid,parentid,categoryid)
{
	document.mainform.itemid.value = itemid;
	document.mainform.parentid.value = parentid;
	document.mainform.categoryid.value = categoryid;
	document.mainform.action = 'item_details.php';
	document.mainform.target = '';
	document.mainform.submit();
}

function doAdvancedSearch()
{
	var keyword = document.mainform.keyword;
	var categories = document.mainform.categories;
	
	if ( !checkblank(keyword.value) ) {
		if ( checkSearchIn() ) {
			if ( !checkblank(categories.value) ) {
				var oconfirm = confirm("Are you Sure?");
				if ( oconfirm )
				{
					document.mainform.hsearch.value = 1;
					return true;
				}
			}
			else
				alert("Please select a category");
		}
		else
			alert("Please select search in");  
	}
	else
	{
		alert("Please enter a valid keyword to search.");
		keyword.focus();
	}
	
	return false;
}

function checkSearchIn()
{
	var chk = document.mainform["searchin[]"];
	
	if ( chk.length > 0 ) 
	{
		for ( var i=0; i<chk.length; i++ )
		{
			if	( chk[i].checked )
				return true;
		}
	}
	
	return false;
}

function gosearch(lang)
{
	var message = '';
	if ( lang == "en" )
		message = "please enter a value to search.";
	else
		message = "Por favor ingrese el valor a buscar.";
	
	var filter = document.searchform.filter;
	
	if ( !checkblank(filter.value) )
		return true;
	else
	{
		alert(message);
		filter.focus();
	}
	
	return false;
}

function sendContact()
{
	var lang = document.mainform.lang.value; 
	
	if ( lang == "en" )
	{
		var txtcontact = "Please enter your Contact Name";
		var txtemail = "Your Email Address or Phone Number are required.";
		var txtphone = "Please enter a valid Email Address.";
		var txtmessage = "Please enter a Message.";
	}
	else
	{
		var txtcontact = "Por favor ingrese su Nombre";
		var txtemail = "La direccion de correo o su numero de telefono son requeridos.";
		var txtphone = "Por favor ingrese una direccion de correo valida.";
		var txtmessage = "Por favor ingrese un mensaje.";
	}
	
	var contact = document.mainform.contact;
	var email = document.mainform.email;
	var phone = document.mainform.phone;
	var message = document.mainform.message;
	
	if ( !checkblank(contact.value) ) {
		if ( !checkblank(email.value) || !checkblank(phone.value) ) {
			if ( checkEmail(email.value) ) {
				if ( !checkblank(message.value) ) {
					document.getElementById("div_hframe").innerHTML = '<iframe name="hframe" style="width:400px; height:300px"></iframe>';
					document.mainform.doaction.value = "contacts";
					document.mainform.action = 'admin/send_email.php';
					document.mainform.target = 'hframe';
					document.mainform.submit();
				}
				else
				{
					alert(txtmessage);
					message.focus();
				}
			}
			else
			{
				alert(txtphone);
				email.focus();
			}
		}
		else
		{
			alert(txtemail);
			email.focus();
		}
	}
	else
	{
		alert(txtcontact);
		contact.focus();
	}
	
	return false;
}


function goPrint(itemid)
{
	document.mainform.itemid.value = itemid;
	document.mainform.action = 'item_print.php';
	document.mainform.target = 'blank';
	document.mainform.submit();
}

function doSubcategories(categoryid)
{
	if ( !checkblank(categoryid) )
	{
		var url = "admin/get_server.php";
		var params = "doaction=getsubcategories&categoryid=" + categoryid;
		
		var loader = new net.ContentLoader(url,
			function() {
				var xmldoc = this.req.responseXML;
				document.getElementById("div_hframe").innerHTML = '<textarea style="width:500px; height:300px">' + this.req.responseText + '</textarea>';
				doSubMenu(xmldoc);
				
			},
			function() { alert('something went wrong...') } ,
			"POST",
			params
		);
	}
	else
	{
		document.getElementById("div_sub").style.display = 'none';
		document.getElementById("div_subcategory").innerHTML = '';	
		document.getElementById("div_brand").style.display = 'none';
	}
}

function doSubMenu(xmldoc)
{
	document.getElementById("div_sub").style.display = 'none';
	document.getElementById("div_subcategory").innerHTML = '';
	
	/*document.getElementById("td_brand").innerHTML = '';
	document.getElementById("div_brand").style.display = 'none';
	*/
	
	if ( typeof(xmldoc) == "object" )
	{
		var TheB = '';
		
		var categories = xmldoc.getElementsByTagName("category");
		
		if ( categories.length > 0 )
		{
			for ( var i=0; i<categories.length; i++ )
			{
				var category = categories[i];
				var category_name = category.getAttribute("name");
				var category_id = category.getAttribute("id");
				var typeid = category.getAttribute("typeid");
				
				var option = category.getElementsByTagName("option");
				
				if ( option.length > 0 )
				{
					TheB += '<optgroup label="' + category_name + '">';
					for ( var j=0; j<option.length; j++ )
					{
						var optid = option[j].getAttribute("id");
						var optname = option[j].getAttribute("value");
						
						TheB += '<option value="' + optid + '">' + optname + '</option>';
					}
					TheB += '</optgroup>';
				}
				else
					TheB += '<option value="' + category_id + '">' + category_name + '</option>';
				
				if ( typeid == "2" )
				{
					document.getElementById("td_brand").innerHTML = '';
					document.getElementById("div_brand").style.display = 'none';	
				}
				else
				{
					document.getElementById("td_brand").innerHTML = TheBR;
					document.getElementById("div_brand").style.display = '';	
				}
			}
			
			if ( TheB != "" )
			{	
				TheB = '<select name="subcategory" id="subcategory" size="7" style="width:220px">' + TheB + '</select>';
				document.getElementById("div_subcategory").innerHTML = TheB;
				document.getElementById("div_sub").style.display = '';
			}
		}
		else
		{
			document.getElementById("td_brand").innerHTML = TheBR;
			document.getElementById("div_brand").style.display = '';
		}
			
	}
}

function validateLogin()
{
 	document.getElementById("div_error").innerHTML = '';
	document.getElementById("div_error").className = '';
	
	var email = document.login.email;
	var password = document.login.password;
	
	if ( checkEmail(email.value) ) {
		if ( checkValid(password.value) ) {
			document.getElementById("div_hframe").innerHTML = '<iframe name="hframe" style="width:400px; height:300px"></iframe>';
			document.login.target = 'hframe';
			return true;
		}
		else
		{
			alert("Please enter a valid Password");
			password.focus();
		}
	}
	else
	{
		alert("Please enter a valid Email Address");
		email.focus();
	}
	
	return false;
}

function validateRegister()
{
	document.getElementById("span_email").innerHTML = '';
	document.getElementById("div_error").innerHTML = '';
	
	if ( validateUser() ) {
		if ( validateContact() ) {
			if ( validateAddress() ) {
				var oconfirm = confirm("Are you sure?");
				if ( oconfirm ) {
					document.getElementById("div_hframe").innerHTML = '<iframe name="hframe" style="width:400px; height:300px"></iframe>';
					document.register.action = 'register_save.php';
					document.register.target = 'hframe';
					document.register.submit();
				}
			}
		}
	}
	
	return false;
}

function validateAddress()
{
	var address = document.register.address;
	var city = document.register.city;
	var country = document.register.country;
	var state = document.register.state;
	var zipcode = document.register.zipcode;
	
	if ( !checkblank(address.value) ) {
		if ( !checkblank(city.value) ) {
			if ( checkCountry(country,state,zipcode) ) {
				return true;
			}
		}
		else
		{
			alert("Please enter a valid City.");
			city.focus();
		}
	}
	else
	{
		alert("Please enter a valid Address.");
		address.focus();
	}
}

function checkCountry(country,state,zipcode)
{
	if ( !checkblank(country.value) ) {
		if ( country.value == "United States" ) {
			if ( !checkblank(state.value) ) {
				if ( !checkblank(zipcode.value) ) {
					return true;
				}
				else
				{
					alert("Please enter a valid Zipcode.");
					zipcode.focus();
				}
			}
			else
			{
				alert("Please select a State.");
				state.focus();
			}
		}
		else
			return true;
	}
	else
	{
		alert("Please select a Country.");
		country.focus();
	}
	
	return false;
}

function validateContact()
{
	var fname = document.register.fname;
	var lname = document.register.lname;
	var phone = document.register.phone;
	
	if ( !checkblank(fname.value) ) {
		if ( !checkblank(lname.value) ) {
			if ( !checkblank(phone.value) ) {
				return true;
			}
			else
			{
				alert("Please enter a valid Phone Number.");
				phone.focus();
			}
		}
		else
		{
			alert("Please enter your Last Name.");
			lname.focus();
		}
	}
	else
	{
		alert("Please enter your First Name.");
		fname.focus();
	}
	
	return false;
}

function validateUser()
{
	var email = document.register.useremail;
	var password = document.register.password;
	var cpassword = document.register.cpassword;
	
	if ( checkEmail(email.value) ) {
		if ( checkValid(password.value) ) {
			if ( checkpassword(password,cpassword) ) {
				return true;
			}
		}
		else
		{
			alert("Your password must be minimum 5 characters alphanumerics in length.");
			password.focus();
		}
	}
	else
	{
		alert("Please enter a valid user email address.");
		email.focus();
	}
}

function checkpassword(p1,p2)
{
	if ( p1.value == p2.value )	
		return true;
	else
	{
		alert("Passwords do not match");
		p1.value = '';
		p2.value = '';
		p1.focus();
	}
	
	return false;
}

function getErrorHtml(id,message)
{
	var obj = document.getElementById(id);
	//obj.className = "error";
	obj.innerHTML = message;
	
}

function validateUpdate()
{
		
}

function getErrorEmail()
{
	var html = '<img src="images/ico_error.jpg" align="absmiddle" style="padding-right:7px"><span style="color:#FF0000">An error was encountered while sending the email. Please try again.</span>';
	document.getElementById("div_error").innerHTML = html;
}

function getErrorLogin()
{
	var lang = document.mainform.lang.value;
	
	var message = "The username or password you entered is incorrect.";
	if ( lang == "es" )
		message = "El correo electronico o la contraseña ingresada no son correctas.";
	
	var obj = document.getElementById("div_error");
	obj.className = "error";
	obj.innerHTML = message;
	
	var password = document.login.password;
	password.value = '';
	password.focus();
	
	return false;
}


function addWishList(return_url,itemid,contactid)
{
	if ( !checkblank(contactid) )
	{
		var msg = ( document.mainform.lang.value == "en" ) ? "This item has been added." : "Este item ha sido adicionado.";
		$.jGrowl('<br />' + msg, { life:400 } );
		
		var categoryid = document.mainform.categoryid.value;
		var parentid = document.mainform.parentid.value;
		
		var url = "admin/get_server.php";
		var params = 'doaction=addwishlist&categoryid=' + categoryid + '&parentid=' + parentid + '&itemid='+ itemid + '&contactid=' + contactid;
		
		var loader = new net.ContentLoader(url,
			function() {
				var oreturn = this.req.responseText;
				
				if ( oreturn != "OK" )
					alert("An unexpected error was encountered while adding this item to the wish list.");
				
				document.getElementById("div_hframe").innerHTML = '<textarea style="width:500px; height:300px">' + this.req.responseText + '</textarea>';
				//doContacts(xmldoc);
			},
			function() { alert('something went wrong...') } ,
			"POST",
			params
		);
	}
	else
	{
		document.mainform.action = "login.php?return_url=" + return_url;
		document.mainform.target = '';
		document.mainform.submit();
	}
}

function return_Location()
{
	var return_url = document.mainform.return_url.value;
	
	if ( checkblank(return_url) ) 
		return_url = "index.php";
		
	document.mainform.action = return_url;
	document.mainform.target = '';
	document.mainform.submit();
}

function remove_list(id)
{
	var oconfirm = confirm("Are you sure you want delete this item?");
	if ( oconfirm )
	{
		var element = "div_" + id;
		
		var url = "admin/get_server.php";
		var params = 'doaction=delwishlist&id=' + id;
			
		var loader = new net.ContentLoader(url,
			function() {
				var oreturn = this.req.responseText;
				
				if ( oreturn == "OK" )
				{
					var div_items = document.getElementById("div_items");
					var obj = document.getElementById(element);
					obj.parentNode.removeChild(obj);
					
					if ( div_items.childNodes.length == 0 )
					{
						var div = document.createElement("div");
						div.innerHTML = '<div style="width:99%; height:25px;border:#ccc 1px solid;padding:5px; line-height:25px; text-align:center">Your Wish List is empty.</div>';
						div_items.appendChild(div);
						
						var div_request = document.getElementById("div_request");
						div_request.parentNode.removeChild(div_request);
					}
				}
				else
					alert("An unexpected error was encountered while deleting this item to the wish list.");
					
				document.getElementById("div_hframe").innerHTML = '<textarea style="width:500px; height:300px">' + this.req.responseText + '</textarea>';
				//doContacts(xmldoc);
			},
			function() { alert('something went wrong...') } ,
			"POST",
			params
		);
	}
}

function quote_request()
{
	var obj = document.getElementById("div_message");
	obj.innerHTML = '';
	obj.className = '';
	
	document.mainform.request.disabled = true;	
	document.getElementById("div_hframe").innerHTML = '<iframe name="hframe" style="width:400px; height:300px"></iframe>';
	document.mainform.action = 'scripts.php';
	document.mainform.doaction.value = "quoterequest";
	document.mainform.target = 'hframe';
	document.mainform.submit();
}

var img_chk = new Image();
var img_error = new Image();

img_chk.src = "images/ico_check.jpg";
img_error.src = "images/ico_error.jpg";

function return_request(oreturn)
{
	var obj = document.getElementById("div_message");
	obj.style.display = "block";
	
	document.mainform.request.disabled = false;
	window.scrollTo(0,0);
	
	var lang = document.mainform.lang.value;
	
	switch ( oreturn )
	{
		case "0":
			var msg = ( lang == "en" ) ? "An unexpected error was encountered while sending a Quote Request. Please try again" : "Un error inesperado se encontró al enviar esta solicitud de cotización";
			obj.innerHTML = '<div style="color:#000"><img src="' + img_error.src + '" align="absmiddle" style="padding-right:4px">' + msg + '</div>';
			break;
		
		case "1":
			var msg = ( lang == "en" ) ? "Your quote request has been submitted." : "Su solicitud de cotizacion ha sido enviada." //, the <strong>quote status</strong> can be checked on the Quote List."; //, the <strong>quote status</strong> can be checked on the Quote List.
			obj.innerHTML = '<div style="color:#000"><img src="' + img_chk.src + '" align="absmiddle" style="padding-right:4px">' + msg + '</div>';
			break;
			
		case "2":
			var msg = ( lang == "en" ) ? "Your quote request already has been submitted." : "Su solicitud de cotizacion ya ha sido enviada." 
			obj.innerHTML = '<div style="color:#000"><img src="' + img_chk.src + '" align="absmiddle" style="padding-right:4px">' + msg + '</div>';
			break;
	}
			
	return false;
}

var img_disabled = new Image();
img_disabled.src = "images/disabled_bar.gif";

var back1 = new Image();
back1.src = "images/specials_bar.jpg";

var back2 = new Image();
back2.src = "images/injection_bar.jpg";

var back3 = new Image();
back3.src = "images/molds_bar.jpg";

var back4 = new Image();
back4.src = "images/auctions_bar.jpg";

var background = Array();
background[0] = back1.src;
background[1] = back2.src;
background[2] = back3.src;
background[3] = back4.src;

function changeBackground(id, flag)
{
	var obj = document.getElementById("td" + id);
		
	if ( flag == 0 )
		obj.style.background = 'url(' + img_disabled.src + ')';
	else
		obj.style.background = 'url(' + background[id] + ')';
	
}

