// JavaScript Document
// Searchs for an element
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_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// Confirms a deletion
function confirmDelete()
{
var agree=confirm("Seguro que quieres eliminar?");
if (agree)
	return true ;
else
	return false ;
}


function checkAll(theForm)
{
	for (i = 0; i < theForm.elements.length; i++)
	{
		if ( theForm.elements[i].type == 'checkbox' )
		{
			theForm.elements[i].checked = true ;
		}
	}
	//theForm.getElementsById('checkbox_label').className .= ' checkbox_checked';
}

function uncheckAll(theForm)
{
	for (i = 0; i < theForm.elements.length; i++)
	{
		if ( theForm.elements[i].type == 'checkbox' )
		{
			theForm.elements[i].checked = false ;
		}
	}
	//theForm.getElementById('checkbox_label').className .= ' checkbox_unchecked';
}

function addComment(assetId)
{
	window.open('addComment.php?assetId='+assetId,'Add your comment','width=400,height=300');
}


//To get all a elements in the document with a “info-links” class.
//    getElementsByClassName(document, "a", "info-links");
//To get all div elements within the element named “container”, with a “col” and a “left” class.
//    getElementsByClassName(document.getElementById("container"), "div", ["col", "left"]);
function getElementsByClassName(oElm, strTagName, oClassNames){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if(typeof oClassNames == "object"){
		for(var i=0; i<oClassNames.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\s)" + oClassNames[i].replace(/-/g, "\-") + "(\s|$)"));
		}
	}
	else{
		arrRegExpClassNames.push(new RegExp("(^|\s)" + oClassNames.replace(/-/g, "\-") + "(\s|$)"));
	}
	var oElement;
	var bMatchesAll;
	for(var j=0; j<arrElements.length; j++){
		oElement = arrElements[j];
		bMatchesAll = true;
		for(var k=0; k<arrRegExpClassNames.length; k++){
			if(!arrRegExpClassNames[k].test(oElement.className)){
				bMatchesAll = false;
				break;
			}
		}
		if(bMatchesAll){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}



// THIS FUNCTION RESIZES IMAGES TO RATIO! - USEFUL FOR IE6
//USSGE: 
// <img id="media_asset" src="<?=$row['filename'];?>" alt="" title=" onload="getRelativeSize(this.id,340,330,this.width,this.height)"  />
function getRelativeSize( ele, maxw, maxh, imgw, imgh )
{
	var scalew = imgw / maxw;
	var scaleh = imgh / maxh;
	
	var orientation = (imgw > imgh) ? 'landscape' : 'portrait';

	var eleclass = MM_findObj(ele).className = orientation;
	
	if (( scaleh > 1 ) || (scalew > 1))
	{
		var scale = (scaleh > scalew) ? scaleh : scalew;
		//alert("Image Resized to \nWidth:" + imgw + ">" + Math.floor(imgw / scale) + "\nHeight:" + imgh + ">" + Math.floor(imgh / scale) + "\nOrientation: " + orientation );
	}
	else 
	{
		scale = 1;
	}
	
	var elew = MM_findObj(ele).width = Math.floor(imgw / scale);
	var eleh = MM_findObj(ele).height = Math.floor(imgh / scale);
}


// Appends an option to a select box
function appendOptionLast(element, value, text)
{
	var newOption = document.createElement('option');
	newOption.text = text;
	newOption.value = value;
	try
	{
		$(element).add(newOption, null); // standards compliant; doesn't work in IE
	}catch(ex)
	{
		$(element).add(newOption); // IE only
	}

}

// Get sizes for each color
function getTallas(element)
{	
	var stockId;
	stockId = element.options[element.selectedIndex].value;
	if(stockId != "")
	{
		// Clear select box options
		$("tallas").options.length=0;
	
	
		new Ajax.Request('http://www.arvendetta.com/services/getTallas.php', {
			parameters: {id:stockId},
		  	method:'post',
			onSuccess: function(transport) {
				var response = transport.responseText;
				if(response=="0")
					appendOptionLast("tallas", "", "sin existencias"); 
				else
				{
					response = response.split("&");	
					for (var i=0; i< response.length; i++)
					{
						values= response[i].split("=");
						appendOptionLast("tallas", values[0], values[1]); 
					}
				}
			}
		});
	}
}

// Changes image for any element by giving its id and image to change
function changeImage(id, image)
{
	$(id).style.paddingTop = 10;	
}