var colorImages = [];
var colorThumbs = [];

function change_image_set(color,product_id) {
//	alert(colorImages[product_id][color]);
//	alert(colorThumbs[product_id][color]);
	if(document.getElementById('image_'+product_id)) {
		document.getElementById('image_'+product_id).src = '/take_action/uploads/images/thumbs/'+colorThumbs[product_id][color];
	}
	if(document.getElementById('zoom_'+product_id)) {
		document.getElementById('zoom_'+product_id).href = '/take_action/uploads/images/'+colorImages[product_id][color];
	}
}
function doSubmit(){
	if(document.getElementById('current_item'))
	{
		item_id = document.getElementById('current_item').value;
		if(item_id != '')
		{
		    form = document.getElementById("form_"+item_id);
		    setTimeout("form.submit()", 0 ); // Hack for ie juan.lopez@globant.com
		}
	}
	else
	{
		if(document.getElementById('items_ids'))
		{
			if(document.getElementById('items_ids').value!='') 
			{
		        form = document.getElementById("form");
		        setTimeout("form.submit()", 0 ); // Hack for ie juan.lopez@globant.com
			}
		}
	}
    return false;
}

function userValidation(item_id, form_id, agree_id)
{
	if(item_id == "no")
	{
		if (document.getElementById('items_ids')) // carbon offset
		{
			if (document.getElementById('items_ids').value!='') 
				tb_show(null,'/take_action/content/showText/id/'+ agree_id +'/item_id/'+form_id+'/agree/1?width=700&height=490',false);
			else
			{
				alert("Please select a plan.")
				return false;
			}
		}
		else
			return true; //other items
	}
	else
	{
		if (validate_qty(item_id))
		{
			if(agree_id == "no") {
				document.getElementById('form_' + form_id).submit();
			}
			else {
				tb_show(null,'/take_action/content/showText/id/'+ agree_id +'/item_id/'+form_id+'/agree/1?width=700&height=490',false);
			}
		}
		else {
			return false;
		}
	}
}


 function updateJSON(request, json)
 {
   $(json[0][0]).innerHTML = json[0][1] //lbs_text
   $(json[1][0]).innerHTML = json[1][1] //price_text   
   $(json[2][0]).value = json[2][1] //lbs
   $(json[3][0]).value = json[3][1] //price   
 }


 function updatePCL(request, json)
 {
   var nbElementsInResponse = json.length;
   $(json[0][0]).href = json[0][1] //comp_label
   $(json[1][0]).src = json[1][1] //comp_thumb
   $(json[2][0]).href = json[2][1] //comp_thumb_link
   $(json[3][0]).innerHTML = json[3][1] //lbs_text
   $(json[4][0]).innerHTML = json[4][1] //price_text
   $(json[5][0]).value = json[5][1] //lbs
   $(json[6][0]).value = json[6][1] //price         
 }

// Cross-browser implementation of element.addEventListener()
 function addListener(element, type, expression, bubbling)
 {
   bubbling = bubbling || false;
  	if(window.addEventListener) { // Standard
  		element.addEventListener(type, expression, bubbling);
  		return true;
  	} else if(window.attachEvent) { // IE
  		element.attachEvent('on' + type, expression);
  		return true;
  	} else return false;
 }

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function validate_qty(item_id)
{
	qty = document.getElementById('qty_'+item_id).value
	num = new Number(qty)
	if (isNaN(num) || num<=0)
	{
		alert("Please enter a valid quantity.");
		return false;
	}
	return true;
}

	function selectItem(obj)
	{
		group_id = document.getElementById("group_" + obj.value).value
		if (selectGroup(group_id, obj.checked))
		{
    		var array = new Array();
    		items = document.getElementById("items_ids").value;
    		array = items.split(",");
    		flag = false;
    		if (obj.checked) //adding
    		{
        		for (x = 0; x < array.length; x++)
        		{
        			if (array[x] == obj.value)
        			{
        				flag = true;
        				break; //already in
        			}
        		}
       			if (!flag)
       			{
       				array[array.length] = obj.value;
       				addTotal(obj.value);
       			}
    		}
    		else //deleting
    		{
        		for (x = 0; x < array.length; x++)
        		{
          			if (array[x] == obj.value) 
          			{
          				array.splice(x,1);
          				flag = true;
          				delTotal(obj.value);
          				break;
          			}
        		}
    		}
    		document.getElementById("items_ids").value = array.join(",");
//    		alert(document.getElementById("items_ids").value)
    		return true;
    	}
    	else
   		{
    	//uncomment this code to enable the group selection restriction
    		alert("Please select just one plan of each group.");
    		return false;
    	}
	}

	function selectGroup(group_id, flag)
	{
		var array = new Array();
		items = document.getElementById("groups_ids").value;
		array = items.split(",");
		ret = true;
		deleted = false;
   		for (x = 0; x < array.length; x++)
   		{
   			if (flag) //adding
   			{
     			if (array[x] == group_id)
     			{
     				ret = false;
     				break; //already in
     			}
   			}
   			else
   			{
     			if (array[x] == group_id)
     			{
     				array.splice(x,1);
     				deleted = true;
     				break; //already in
     			}  			
   			}
   		}
		if (ret && !deleted)
			array[array.length] = group_id;
   		
		document.getElementById("groups_ids").value = array.join(",");
//		alert(document.getElementById("groups_ids").value)
		return ret;
	}
	
	function addTotal(item_id)
	{
		total = new Number(document.getElementById("total").value)
		price = new Number(document.getElementById("price_"+item_id).value)  
		document.getElementById("total").value = total + price;
	}

	function delTotal(item_id)
	{
		total = new Number(document.getElementById("total").value)
		price = new Number(document.getElementById("price_"+item_id).value)    
		document.getElementById("total").value = total - price; 
	}
