

function getElementsByID(sId)
 {

//Hacked to accommodate partial id name match
    var outArray = new Array(); 
        if(typeof(sId)!='string' || !sId)
        {
                return outArray;
        };
     /*   
        if(document.evaluate)
        {
                var xpathString = "//*[@id='" + sId.toString() + "']"
                var xpathResult = document.evaluate(xpathString, document, null, 0, null);
                while ((outArray[outArray.length] = xpathResult.iterateNext())) { }
                outArray.pop();
        }
        else if(document.all)
        {
                
                for(var i=0,j=document.all[sId].length;i<j;i+=1){
                outArray[i] =  document.all[sId][i];}
                
        }
*/
	 if(document.getElementsByTagName)
        {
        
                var aEl = document.getElementsByTagName( '*' ); 
                for(var i=0,j=aEl.length;i<j;i+=1){
                
		var re = new RegExp(sId ,"g");
		
                        if((aEl[i].id).search(re) !== -1 )
                        {
                                outArray.push(aEl[i]);
                        };
                };      
                
        };
        
        return outArray;
 }

function drawDropDowns()
{
	if(!$('shirtTypeDropDown'))
	{
		workQueue(true);
		var url = baseDir+'_includes/ajax/dropDown_shirtType.php?designID='+designID+'&shirtTypeID='+dd_shirtTypeValue;
		new Ajax.Updater('shirtType', url, { method: 'get', onSuccess: workQueue, onFailure: workFailed, onComplete: drawDropDowns });
	}
	else
	{
		var dd_newShirtTypeValue = $('shirtTypeDropDown').options[$('shirtTypeDropDown').selectedIndex].value;
		if(!$('colorDropDown') || dd_newShirtTypeValue != dd_shirtTypeValue)
		{
			dd_shirtTypeValue = dd_newShirtTypeValue;
			workQueue(true);
			var url = baseDir+'_includes/ajax/dropDown_color.php?designID='+designID+'&shirtTypeID='+dd_shirtTypeValue+'&colorID='+dd_colorValue;
			new Ajax.Updater('shirtColor', url, { method: 'get', onSuccess: workQueue, onFailure: workFailed, onComplete: drawDropDowns });
			forceRedraw = true;
		}
		else
		{
			var dd_newColorValue = $('colorDropDown').options[$('colorDropDown').selectedIndex].value;
			if(!$('sizeDropDown') || dd_newColorValue != dd_colorValue || forceRedraw)
			{
				forceRedraw = false;
				dd_colorValue = dd_newColorValue;
				workQueue(true);
				var url = baseDir+'_includes/ajax/dropDown_shirtSize.php?shirtSizeID='+dd_sizeValue+'&shirtTypeID='+dd_newShirtTypeValue+'&colorID='+dd_colorValue;
				new Ajax.Updater('shirtSize', url, { method: 'get', onSuccess: workQueue, onFailure: workFailed, onComplete: drawDropDowns });
				
			}
			else
			{
				dd_sizeValue = $('sizeDropDown').options[$('sizeDropDown').selectedIndex].value;
				
				processImage();
				
				processPrice();
				
				processOrder();
				
				buildLinks();
			}
		}
	}
}



function processImage()
{
	var color;
	if(dd_colorValue == '#')
		color = defaultColor;
	else
		color = dd_colorValue;
	
	if(dd_side==""){ dd_side="front";}
	workQueue(true);
	//alert(dd_shirtTypeValue);
	var url = baseDir+'_includes/ajax/processDesignImage.php?designID='+designID+'&selectDesignColor='+color+'&defaultColor='+defaultColor+'&type='+dd_shirtTypeValue+'&side='+dd_side;
	new Ajax.Updater('designImage', url, { method: 'get', onSuccess: workQueue, onFailure: workFailed });
	
//	workQueue(true);
//	var url = baseDir+'_includes/ajax/processShirtTypeImage.php?designID='+designID+'&selectDesignType='+dd_shirtTypeValue;
//	new Ajax.Updater('shirtTypeImage', url, { method: 'get', onSuccess: workQueue, onFailure: workFailed });
}


function processPrice()
{
	workQueue(true);
	var url = baseDir+'_includes/ajax/processPrice.php?designID='+designID+'&selectDesignType='+dd_shirtTypeValue+'&sizeID='+dd_sizeValue;
	new Ajax.Updater('shirtPrice', url, { method: 'get', onSuccess: workQueue, onFailure: workFailed });
}

function processOrder()
{
	if(dd_shirtTypeValue != '' && dd_shirtTypeValue != 'undefined')
		if(dd_colorValue != '' && dd_colorValue != 'undefined')
			if(dd_sizeValue != '' && dd_sizeValue != 'undefined')
				return enableSubmit();
	disableSubmit();				
}

function disableSubmit()
{
	$('submitBtn').disabled = true;
	$('submitBtn').src = offSubmitBtnSource;
	$('submitBtn').onmouseup = 'return false';
}
function enableSubmit()
{
	$('submitBtn').disabled = false;
	$('submitBtn').src = onSubmitBtnSource;
	$('submitBtn').onmouseup = function()
	{
		document.shirtCustomizeForm.submitted.value="true";
		document.shirtCustomizeForm.submit();
	} 
}
function workQueue(workingBool)
{
	if(workingBool == true)
	{
		if(working == 1)
		{
			workStart();
		}
		working++;
	}
	else
	{
		working--;
		if(working < 1)
			workComplete();	
	}	
}
workStart = function()
{
	statusDiv.innerHTML = "<div id='statusDivText'>Loading... Please Wait.</div>";
}
workComplete = function()
{
	statusDiv.innerHTML = "<div id='statusDivText'>Loading Complete</div>";
	new Effect.Fade($('statusDivText'), {delay: .8});
}
workFailed = function()
{
	statusDiv.innerHTML = "<div id='statusDivText' style='color: #900; font-weight: bold;'>Loading Error</div>";
}

function buildLinks()
{
	var custShirtValue = new Array('#', '#', '#');
	if(dd_shirtTypeValue)
		custShirtValue[0] = dd_shirtTypeValue;
		
	if(dd_colorValue)
		custShirtValue[1] = dd_colorValue;
		
	if(dd_sizeValue)
		custShirtValue[2] = dd_sizeValue;
		
	$('customizedID').value = custShirtValue[0]+'_'+custShirtValue[1]+'_'+custShirtValue[2];
	
	//var linkNode = $('customShirtLink');
	//alert("Test");
	//linkNode.innerHTML = '<span style="color:#CCC">'++'/</span>';
	
	var sLink = $('sendToFriendLink');
	sLink.href = baseDir+'sendToFriend.php?id='+categoryID+'/'+designID+'/'+($('customizedID').value);
	
}

function submitComment()
{
	
	if($('commentName').value != '' && $('commentBody').value != '')
	{
		document.commentForm.action = scriptRoot+$('customizedID').value;
		return true;
	}
	else
	{
	
		$('commentError').innerHTML = "<div id='commentErrorText' class='ajaxError'>! Please fill in both fields.</div>";
		new Effect.Fade($('commentErrorText'), {delay: 2});
		return false;
	}
}


function uploadPhoto(me)
{
	if(me.myphoto.value != '')
	{
		me.action = scriptRoot+$('customizedID').value;
		var filestrname = me.myphoto.value;
		var fileext = filestrname.substr((filestrname.length - 3), 3);
		if(fileext.toLowerCase() == 'jpg' || fileext.toLowerCase() == 'gif' || fileext.toLowerCase() == 'jpeg')
		{
			if(me.agree.checked)
			{
				document.commentForm.action = scriptRoot+$('customizedID').value;
				return true;
			}
			else
				alert("You must agree to the Terms of Use to upload an image.");
		}
		else
			alert("Invalid Image Extension");
			
	}
	return false;
}
