/* appQuery */

/*
 * 
 * general vars
 * _result_
 * 
 * 
 */
	var _jSON_information = "";
	var _result_ = "";
	jQuery().ajaxStart(function()
	{
   		_result_ = ""; 
 	});
	
/* v.0.1 */
/*
 * @param {Object} size
 * @param {Object} _i_
 */
function exe_setbysize(size,_i_)
{
	_getsessionproducto(true, "_set_size('"+size+"','"+_i_+"')" ) ;
}
function _set_size(_size_,_i_)
{
	try 
	{
		if (_result_ != "")
		{
			var item = _search_("id",_result_);
			if (item != false) 
			{
				var _color_ = _jSON_information[item]['color'];
				var item_ = _search_("size", _size_, "color", _color_);
				if (item_ != false) 
				{
					_setsessionproducto(_jSON_information[item_]['id'], true, "_set_parameters_for_(_result_,'"+_i_+"')");
					$("#_img_addtocart"+_i_).attr( "src","/images/btadd2cart.gif" );
				}else{
					if (_jSON_information[1]['id'] != null) 
					{
						_setsessionproducto(_jSON_information[1]['id'], true, "_set_parameters_for_(_result_,'" + _i_ + "')");
						$("#_img_addtocart"+_i_).attr( "src","/images/btadd2cart.gif" );
					}else{
						$("#_img_addtocart"+_i_).attr( "src","/images/btadd2cart_off.gif" );
					}
				}
			}
		}
	}catch (E){
		alert( E );
	}
}
/* 
 * @param {Object} _i_ -> DOM
 */
function _exe_setbycolor(_i_)
{
	_getsessionproducto(true, "_set_color('"+$("#_color_"+_i_).val()+"','"+_i_+"')" ) ;
}

function _exe_setbycolor_box(_color_,_i_)
{
	_getsessionproducto(true, "_set_color('"+_color_+"','"+_i_+"')" ) ;
}

function _set_color(_color_,_i_)
{
	try 
	{
		if (_result_ != "")
		{
			var item = _search_("id",_result_);
			if (item != false) 
			{
				var _size_ = _jSON_information[item]['size'];
				var item = _search_("color", _color_, "size", _size_);
				
				if (item != false) 
				{
					_setsessionproducto(_jSON_information[item]['id'], true, "_set_parameters_for_(_result_,'"+_i_+"')");
					$("#_img_addtocart"+_i_).attr( "src","/images/btadd2cart.gif" );
				}else{
					 $("#_img_addtocart"+_i_).attr( "src","/images/btadd2cart_off.gif" );
				}
			}
		}
	}catch (E){
		alert( E );
	}		
}
/*
 * @param {Object} idproducto
 */
function _set_parameters_for_(idproducto,_i_)
{
	_get_images(idproducto,_i_);
	_get_image_main(idproducto,_i_);
	_exe_price(idproducto,"_price_producto_"+_i_);
	_set_size_infomation(idproducto,_i_);
	_set_color_information(idproducto,_i_);
	_set_color_information_box(idproducto,_i_);
}
/*
 * @param {Object} idproducto
 */
function _get_images(idproducto,_i_)
{
	try 
	{
		var _URL_= "include/functions.php";
		var XhtmlHttp = $.getJSON(_URL_, { idproducto: idproducto ,i: _i_ ,action: "_get_images" }, 
			function(json)
			{
				if (json.ERRORMSG == null)
				{
					$("#thumbs"+_i_).attr( "innerHTML",json.HTML );
				}else{
					alert( json.ERRORMSG );
				}
			});
	}catch (E){
		alert( E );
	}		
}
/*
 * @param {Object} idproducto
 */
function _get_image_main(idproducto,_i_)
{
	try 
	{
		var _URL_= "include/functions.php";
		var XhtmlHttp = $.getJSON(_URL_, { idproducto: idproducto ,i: _i_ ,action: "_get_image_main" }, 
			function(json)
			{
				if (json.ERRORMSG == null)
				{
					$("#main_image_"+_i_).attr( "src",json.IMAGE );
				}else{
					alert( json.ERRORMSG );
				}
			});
	}catch (E){
		alert( E );
	}		
}
/* 
 * @param {Object} param1 -> ITEM SKU
 * @param {Object} param2 -> ID DOM
 */
function _exe_price(param1,param2)
{
	try 
	{
		if (param1 != "") 
		{
			var _URL_= "include/functions.php";
			var XhtmlHttp = $.getJSON(_URL_, { idproducto: param1 ,action: "_getprice" }, 
				function(json)
				{
					if (json.ERRORMSG == null)
					{
						if (param2 != "")
						{
							$("#"+param2).attr("innerHTML","$"+ json.PRICE );
						}else{
							alert( json.PRICE );
						}
					}else{
						alert( json.ERRORMSG );
					}
				});
		}
	}catch (E){
		alert( E );
	}
}
/*
 * @param {Object} idproducto
 */
function _set_size_infomation(idproducto,_i_)
{
	var item = _search_("id",idproducto);
	if (item != false)
	{
		var _size_ = _jSON_information[item]['size'];
		for (var item in _jSON_information) 
		{
			if (_jSON_information[item]['size'] == _size_) 
			{
				$("#_gr_"+_i_+"_"+_jSON_information[item]['size']).attr( "class","groupon" );
			}else{
				$("#_gr_"+_i_+"_"+_jSON_information[item]['size']).attr( "class","size" );
			}
		}
	}
}
/*
 * @param {Object} idproducto
 * @param {Object} _i_
 */
function _set_color_information(idproducto,_i_)
{
	var item = _search_("id",idproducto);
	if (item != false) 
	{
		var _URL_= "include/functions.php";
		var XhtmlHttp = $.getJSON(_URL_, { idproducto: idproducto ,i: _i_ ,action: "_get_color_information",color: _jSON_information[item]['color'],size: _jSON_information[item]['size'] }, 
			function(json)
			{
				if (json.ERRORMSG == null)
				{
					$("#_color_"+_i_).attr( "innerHTML",json.HTML );
				}else{
					alert( json.ERRORMSG );
				}
			});
	}
}
/* 
 * @param {Object} idproducto
 * @param {Object} _i_
 */
function _set_color_information_box(idproducto,_i_)
{
	var item = _search_("id",idproducto);
	if (item != false) 
	{
		var _URL_= "include/functions.php";
		var XhtmlHttp = $.getJSON(_URL_, { idproducto: idproducto ,i: _i_ ,action: "_get_color_information_box",color: _jSON_information[item]['color'],size: _jSON_information[item]['size'] }, 
			function(json)
			{
				if (json.ERRORMSG == null)
				{
					$("#colores"+_i_).attr( "innerHTML",json.HTML );
				}else{
					alert( json.ERRORMSG );
				}
			});
	}
}
/*
 * @param {Object} param1 -> ITEM SKU
 */
function exeInformation(param1)
{
	try 
	{
		if (param1 != "") 
		{
			var _URL_= "include/functions.php";
			var XhtmlHttp = $.getJSON(_URL_, {	SKU: param1 , action: "_getinformation" }, 
				function(json)
				{
					if (json.ERRORMSG == null)
					{
						/*
						 * json.PRICE ;
						 * json.SIZE ;
						 * json.COLOR ;
						 * json.OFFICIAL_PRICE ;
						 * json.WEIGHT ;
						 * json.NAME ;
						 * json.SKU ;
						 */
						
					}else{
						alert( json.ERRORMSG );
					}
				});
		}
	}catch (E){
		alert( E );
	}	
}

/*
 * 
 */
function _getsessionproducto(_exec_,_fun_)
{
	try 
	{
		var _URL_= "include/functions.php";
		var XhtmlHttp = $.getJSON(_URL_, { action: "_getsessionproducto" }, 
			function(json)
			{
				if (json.ERRORMSG == null)
				{
					_result_ = json.IDPRODUCTO ;
					if (_exec_)
					{
						eval( _fun_ );
					}
				}else{
					alert( json.ERRORMSG );
				}
			});
	}catch (E){
		alert( E );
	}	
}

function _setsessionproducto(idproducto,_exec_,_fun_)
{
	try 
	{
		var _URL_= "include/functions.php";
		var XhtmlHttp = $.getJSON(_URL_, { action: "_setsessionproducto" ,idproducto: idproducto }, 
			function(json)
			{
				if (json.ERRORMSG == null)
				{
					_result_ = json.IDPRODUCTO ;
					if (_exec_)
					{
						eval( _fun_ );
					}
				}else{
					alert( json.ERRORMSG );
				}
			});
	}catch (E){
		alert( E );
	}	
}

function _search_(opt1,needle1,opt2,needle2,opt3,needle3)
{
	for (var item in _jSON_information) 
	{
		if (_jSON_information[item][opt1] == needle1)
		{
			if (opt2 != "")
			{
				if (_jSON_information[item][opt2] == needle2)
				{
					if (opt3 != "")
					{
						if (_jSON_information[item][opt3] == needle3)
						{
							return item;
						}
					}else{
						return item;
					}
				}
			}else{
				return item;
			}
		}
	}
	
	return false;
}

function _s_popup(_i_)
{
	var _URL_=  "/include/_zoom.php";
	jQuery.facebox('<iframe id="faceboxframe" width="675px" height="520px" scroll="no" src="'+_URL_+'"></iframe>');
}

function _set_yml(_i_)
{
	var _cont_ = 0;
	for(i=1;i<3;i++)
	{
		if( $("#_pyml_"+i+"_"+_i_).attr( "checked" ) == true )
		{
			_cont_++;
		}
	}
	$("#_cont_"+_i_).attr( "innerHTML",_cont_ );
}
function _cls_ymal(_i_)
{
	for(i=1;i<3;i++)
	{
		$("#_pyml_"+i+"_"+_i_).attr( "checked",false );
	}
	$("#_cont_"+_i_).attr( "innerHTML","0");
}

function _set_image(DOM,_img_)
{
	$("#"+DOM).attr( "src" ,_img_ );
}

function _exe_add2_more(_i_)
{
	_getsessionproducto(true,"_add_2_more_(_result_,'"+_i_+"')");	
}
function _add_2_more_(idproducto,_i_)
{
	if ( $("#_img_addtocart"+_i_).attr( "src" ) == "/images/btadd2cart.gif")
	{
		var _id_productos = idproducto + ",";
		var _qty_productos = $("#qty"+_i_).val() + ",";
		for(i=1;i<3;i++)
		{
			if( $("#_pyml_"+i+"_"+_i_).attr( "checked" ) == true )
			{
				_id_productos += $("#_pyml_"+i+"_"+_i_).val() + ",";
				_qty_productos += "1,"
			}
		}
		
		try 
		{
			var _URL_= "include/functions.php";
			var XhtmlHttp = $.getJSON(_URL_, { action: "_add_2_more" ,idproductos: _id_productos ,qtys: _qty_productos }, 
				function(json)
				{
					if (json.ERRORMSG == null)
					{
						alert(" ADD : " + json.PRODUCTNAME);
						document.location.href = "/view.html";
					}else{
						alert( json.ERRORMSG );
					}
				});
		}catch (E){
			alert( E );
		}	
	}
}
function _exe_addtocart(_i_)
{
	_getsessionproducto(true,"_add_to_cart_(_result_,'"+_i_+"')");
}

function _add_to_cart_(idproducto,_i_)
{
	if ( $("#_img_addtocart"+_i_).attr( "src" ) == "/images/btadd2cart.gif")
	{
		try 
		{
			var _URL_= "include/functions.php";
			var XhtmlHttp = $.getJSON(_URL_, { action: "_add_to_cart" ,idproducto: idproducto ,qty: $("#qty"+_i_).val() }, 
				function(json)
				{
					if (json.ERRORMSG == null)
					{
						alert(" ADD : " + json.PRODUCTNAME);
						document.location.href = "/view.html";
					}else{
						alert( json.ERRORMSG );
					}
				});
		}catch (E){
			alert( E );
		}	
	}
}

/* v.0.1 */



/* View */

function exeCoupon()
{
	var _URL_= "include/Coupon.php";
		var XhtmlHttp = $.getJSON(_URL_,{ formCoupon: $("#formCoupon").val() , Subtotal: $("#subtotal").val() } ,
						function(json){
							if (json.ErrorCouponMsg != null)
							{
								$("#CouponError").attr("innerHTML",json.ErrorCouponMsg);
							}else{
								$("#LabelCoupon").attr("innerHTML","Coupon (" + $("#formCoupon").val() + ") <em> $ " + json.CouponValue + "</em>" );
								$("#LabelCoupon").css({ display: "" , visibility: ""});
								var Subtl = $("#subtotal").val();
								var Shipping = $("#abShipping").val();
								var Tax = $("#abTax").val();
								if (Tax != "")
								{
									$("#total").attr("innerHTML","$" + aQRound( ( parseFloat(Subtl) + parseFloat(Shipping) + parseFloat(Tax) - parseFloat(json.CouponValue) ),2 ) );
									$("#abTotal").val( aQRound( ( parseFloat(Subtl) + parseFloat(Shipping) + parseFloat(Tax) - parseFloat(json.CouponValue) ),2 ) );
								}else{
									$("#total").attr("innerHTML","$" + aQRound( ( parseFloat(Subtl) + parseFloat(Shipping) - parseFloat(json.CouponValue) ),2 ) );
									$("#abTotal").val( aQRound( ( parseFloat(Subtl) + parseFloat(Shipping) - parseFloat(json.CouponValue) ),2 ) );
								}
							}
						});
}

function exeCountry()
{
	var CountryCode = $("#country").val() ;
	var weightnew = parseInt(weight)+1;
	if (CountryCode != "none")
	{
		if (CountryCode == "US")
		{
			var _URL_= "include/Shipping.php";
			var XhtmlHttp = $.get(_URL_,
							function(data){ 
								$("#tdstate").attr("innerHTML",data); 
								$("#ArrowShip").attr("class","state"); 
								$("#ArrowShip").attr("src","images/sstate3.gif"); 
							});
		}else{
			$("#tdstate").attr("innerHTML","");
			$("#ArrowShip").attr("class","country"); 
			$("#ArrowShip").attr("src","images/white_arrow.gif");  
			var _URL_= "include/Shipping.php";
			var XhtmlHttp = $.getJSON(_URL_,{ Country: $("#country").val(), Weight: weightnew , Subtotal: $("#subtotal").val() , State: "" },
							function(json){ 
								$("#shipping").attr("innerHTML","$" + aQRound( (parseFloat(json.Shipping) + parseFloat(json.Handling)),2) );
								$("#ship").val( aQRound( (parseFloat(json.Shipping) + parseFloat(json.Handling)),2) );
								$("#abShipping").val( aQRound( (parseFloat(json.Shipping) + parseFloat(json.Handling)),2) );
								$("#shipCheckout").val( aQRound( (parseFloat(json.Shipping) + parseFloat(json.Handling)),2) );
								$("#shipservices").val( json.ShipServices );
								
								var Coupon = $("#abCoupon").val();
								if (Coupon == "")
								{
									$("#abTotal").val( aQRound( (parseFloat(json.Shipping) + parseFloat(json.Handling) + parseFloat(json.Subtotal)) ,2 ) );
									$("#total").attr("innerHTML","$" + aQRound( (parseFloat(json.Shipping) + parseFloat(json.Handling) + parseFloat(json.Subtotal)),2 ) );
								}else{
									$("#abTotal").val( aQRound( (parseFloat(json.Shipping) + parseFloat(json.Handling) + parseFloat(json.Subtotal) - parseFloat(Coupon) ) ,2 ) );
									$("#total").attr("innerHTML","$" + aQRound( (parseFloat(json.Shipping) + parseFloat(json.Handling) + parseFloat(json.Subtotal) - parseFloat(Coupon) ),2 ) );
								}
								$("#tax").val( "00.00" );
								$("#abCountry").val( $("#country").val() );
								operationStatus = "2";
							});
		}
		operationStatus = "1";
	}
}

function exeState()
{
	var weightnew = parseInt(weight) + 1;
	if ($("#state").val() != "none")
	{
		var estado = states( $("#state").val() );
		var _URL_ = "include/Shipping.php";
		var XhtmlHttp = $.getJSON(_URL_,{ ST: $("#state").val(), Country: $("#country").val(), Weight: weightnew , Subtotal: $("#subtotal").val() , State: estado },
					function(json){ 
						$("#shipping").attr("innerHTML","$" + aQRound((parseFloat(json.Shipping) + parseFloat(json.Handling)),2));
						$("#ship").val( aQRound( (parseFloat(json.Shipping) + parseFloat(json.Handling)),2) );
						$("#shipCheckout").val( aQRound( (parseFloat(json.Shipping) + parseFloat(json.Handling)),2) );
						$("#abShipping").val( aQRound( (parseFloat(json.Shipping) + parseFloat(json.Handling)),2) );
						$("#taxCheckout").val( parseFloat(json.Tax) );
						$("#abTax").val( parseFloat(json.Tax) );
						$("#tax_cart").val( parseFloat(json.Tax) );
						$("#tax").attr( "innerHTML","$" + parseFloat(json.Tax) );
						$("#shipservices").val( json.ShipServices );
						
						var Coupon = $("#abCoupon").val();
						if (Coupon == "")
						{
							$("#abTotal").val( aQRound( (parseFloat(json.Shipping) + parseFloat(json.Handling) + parseFloat(json.Subtotal) + parseFloat(json.Tax)) ,2 ) );
							$("#total").attr("innerHTML","$" + aQRound( (parseFloat(json.Shipping) + parseFloat(json.Handling) + parseFloat(json.Subtotal) + parseFloat(json.Tax)) ,2 ) );
						}else{
							$("#abTotal").val( aQRound( (parseFloat(json.Shipping) + parseFloat(json.Handling) + parseFloat(json.Subtotal) + parseFloat(json.Tax) - parseFloat(Coupon) ) ,2 ) );
							$("#total").attr("innerHTML","$" + aQRound( (parseFloat(json.Shipping) + parseFloat(json.Handling) + parseFloat(json.Subtotal) + parseFloat(json.Tax) - parseFloat(Coupon) ) ,2 ) );
						}
						$("#abCountry").val( $("#country").val() );
						$("#abState").val( $("#state").val() );
						operationStatus = "2";
					});
		operationStatus = "1";
	}
}

function validarEntero(txt)
{
     var valor = txt.value;
     valor = parseInt(valor);
     if (isNaN(valor)) 
     {
           txt.value = "1";
           return true;
     }
}

function exeUpdate()
{
	var Cantidad =  $("#CadenaId").val() ;
	var i ;
	var Datos = new Array();
	Datos = Cantidad.split(",");
	for(i = 0;i < Datos.length ;i++)
	{
		eval("var e = document.miForm.t" + Datos[i] + " ;");
		eval("var ex = document.formUpdate.x" + Datos[i] + " ;");
		ex.value = e.value;
	}
	document.formUpdate.submit();
}

/* View */


/* all Site */

function isEmail(s){var i=1;var sLength=s.length;while((i<sLength)&&(s.charAt(i)!="@")){i++}if((i>=sLength)||(s.charAt(i)!="@"))return false;else i+=2;while((i<sLength)&&(s.charAt(i)!=".")){i++}if((i>=sLength-1)||(s.charAt(i)!="."))return false;else return true;}
function validateform(){x=document.order;str="";if(x.name.value==""){str="** Please enter Name.\n";}if(x.address1.value==""){str+="** Please enter Street address.\n";}if(x.city.value==""){str+="** Please enter City.\n";}if(x.state.value==""){str+="** Please enter State.\n";}if(x.country.value==""){str+="** Please enter Country.\n";}if(x.zip.value==""){str+="** Please enter Zip/Postal code.\n";}if(x.email.value!=""){if(!isEmail(x.email.value)){str+="** Please enter Correct Email.\n";}}if(x.sname.value==""){str+="** Please enter Shipping Name.\n";}if(x.saddress1.value==""){str+="** Please enter shipping Street address.\n";}if(x.scity.value==""){str+="** Please ship enter City.\n";}if(x.sstate.value==""){str+="** Please enter ship State.\n";}if(x.scountry.value==""){str+="** Please enter ship Country.\n";}if(x.szip.value==""){str+="** Please enter Ship Zip/Postal code.\n";}if(x.cardnumber.value==""){str+="** Please enter your card number.\n";}if(x.cardexpmonth.value==""){str+="** Please enter Expiry Month.\n";}if(x.cardexpyear.value==""){str+="** Please enter Expiry Year.\n";}if(str!=""){alert(str);return false;}else return true;}
function fillInfo(){x=document.order;if(x.sameas.checked){x.sname.value=x.name.value;x.saddress1.value=x.address1.value;x.saddress2.value=x.address2.value;x.scity.value=x.city.value;x.sstate.value=x.state.value;x.scountry.value=x.country.value;x.szip.value=x.zip.value;}else{x.sname.value="";x.saddress1.value="";x.saddress2.value="";x.scity.value="";x.sstate.value="";x.scountry.value="";x.szip.value="";}}
function states(state){switch(state){case"AL":return"35000";break;case"AK":return"99500";break;case"AZ":return"85000";break;case"AR":return"71600";break;case"CA":return"90007";break;case"CO":return"80000";break;case"CT":return"06000";break;case"DC":return"20000";break;case"DE":return"19700";break;case"FL":return"32000";break;case"GA":return"30000";break;case"HI":return"96700";break;case"ID":return"83200";break;case"IL":return"60000";break;case"IN":return"46000";break;case"IA":return"50000";break;case"KS":return"66000";break;case"KY":return"40000";break;case"LA":return"70000";break;case"MD":return"20600";break;case"ME":return"03900";break;case"MA":return"01000";break;case"MI":return"48000";break;case"MN":return"55000";break;case"MS":return"38600";break;case"MO":return"63000";break;case"MT":return"59000";break;case"NE":return"68000";break;case"NV":return"88900";break;case"NH":return"03000";break;case"NJ":return"07000";break;case"NM":return"87000";break;case"NY":return"10000";break;case"NC":return"27000";break;case"ND":return"58000";break;case"OH":return"43000";break;case"OK":return"73000";break;case"OR":return"97000";break;case"PA":return"15000";break;case"PR":return"00600";break;case"RI":return"02800";break;case"SC":return"29000";break;case"SD":return"57000";break;case"TN":return"37000";break;case"TX":return"75000";break;case"UT":return"84000";break;case"VT":return"05000";break;case"VA":return"20100";break;case"WA":return"98000";break;case"WV":return"24700";break;case"WY":return"82000";break;case"WI":return"53000";break;default:}}


function aQdump(arr,level) 
{
	var dumped_text = "";
	if(!level) level = 0;
	
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') 
	{ 
		for(var item in arr) 
		{
			var value = arr[item];
			
			if(typeof(value) == 'object') { 
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { 
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function aQRound(number,X) 
{
	X = (!X ? 2 : X);
	return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
} 

/* all Site */


/* appQuery */