var ajax = new sack();

// get sizes
function getSizesList(p_id, c_id)
{
	document.getElementById('sizeLoader').style.display = 'inline';
	document.getElementById('quantityLoader').style.display = 'none';
	ajax.requestFile = 'product_detail_sizes.php?product_id='+p_id+'&color_id='+c_id;	// Specifying which file to get
	ajax.onCompletion = createSizes;	// Specify function that will be executed after file has been found
	ajax.runAJAX();		// Execute AJAX function
}

function createSizes()
{
	document.getElementById('sizeLoader').style.display = 'none';
	var obj = document.getElementById('size_id');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code
}

// get quantity
function getQuantityList(p_id, c_id, s_id)
{
	document.getElementById('quantityLoader').style.display = 'inline';
	ajax.requestFile = 'product_detail_quantity.php?product_id='+p_id+'&color_id='+c_id+'&size_id='+s_id;	// Specifying which file to get
	ajax.onCompletion = createQuantity;	// Specify function that will be executed after file has been found
	ajax.runAJAX();		// Execute AJAX function
}

function createQuantity()
{
	document.getElementById('quantityLoader').style.display = 'none';
	var obj = document.getElementById('quantity');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code
}


// colour swatch rollover
function swatchOver(type,id,c_name){
	document.getElementById("optionColour").innerHTML = c_name;
	document.getElementById(type+'Swatch_' + id).className='hover';
}

// colour swatch rollout
function swatchOut(type,id){
	document.getElementById("optionColour").innerHTML = document.productBuy.color_name.value;
	if(id==document.productBuy.color_classId.value){
		document.getElementById(type+'Swatch_' + id).className='selected';
	}else{
		document.getElementById(type+'Swatch_' + id).className='normal';
	}
}

// colour swatch select
function swatchSelect(type,id, p_id, c_id, c_name, sts_codi){
	//alert("type="+type+" id="+id+" p_id="+p_id+" c_id="+c_id+" c_name="+c_name);
	document.productBuy.color_id.value = c_id;
	document.productBuy.color_name.value = c_name;
	document.productBuy.color_classId.value = id;

    // load sizes of selected color
    toggleColours(type,id,p_id,c_id);

	// use ajax to change main flash image if a mouse-click event occurred
	if(sts_codi != ''){
	   ajax_2 = new sack();
	   ajax_2.requestFile = 'product_detail_flash_image.php?product_id=' + p_id + '&sts_codi=' + sts_codi; // file to be retrieved
	   ajax_2.runAJAX();
	   ajax_2.xmlhttp.onreadystatechange = function(){
	       if(ajax_2.xmlhttp.readyState == 4){ // OK from server
	           eval(ajax_2.xmlhttp.responseText);
	       } // end if
	   } // end inner function

	   ajax_3 = new sack();
	   ajax_3.requestFile = 'ajax_update_related.php?product_id=' + p_id + '&sts_codi=' + sts_codi; // file to be retrieved
	   ajax_3.runAJAX();
	   ajax_3.xmlhttp.onreadystatechange = function(){
	       if(ajax_3.xmlhttp.readyState == 4){ // OK from server
	           eval(ajax_3.xmlhttp.responseText);
	       } // end if
	   } // end inner function

	} // end if
}

function toggleColours(type,id,p_id,c_id){
	for(x = 0; x< document.productBuy.colors_total.value; x++){
       document.getElementById(type+'Swatch_' + x).className='normal';
     }
    document.getElementById(type+'Swatch_' + id).className='selected';
    document.getElementById('size_id').options.length = 0;	// Empty sizes select box
    document.getElementById('quantity').options.length = 0;	// Empty quantity select box
    getSizesList(p_id,c_id);
}

function getQuantity(p_id){
	var c_id = document.productBuy.color_id.value;
	var s_id = document.getElementById('size_id').value;
	document.getElementById('quantity').options.length = 0;	// Empty quantity select box

	if(s_id.length>0){
		getQuantityList(p_id, c_id, s_id);
	}
}

