/* 
 * This fuction gets the pid and cgid in order to fire the Ajax the ajax requests
 * to support to swap the variants without a full page reload.
 */
function swapVariantByAjax() {

	// initialize pif and cgid
	var pid					= null;
	var cgid				= null;
	
	// get cart add product form
	var cartAddProductForm	= document.forms["cartaddproduct"];
	// get the form elements
	var allFormElements		= cartAddProductForm.elements;
	
	// loop over form elements
	for (var k = 0; k < allFormElements.length; k++) {
	
		var element = allFormElements[k];
		// get pid and cgid by element name
		if (element.name == "pid") {
		
			pid = element.value;
		} else if (element.name == "cgid") {
		
			cgid = element.value;
		}
	}
	
	// get query string
	var queryString	= getQueryString(pid, cgid);
	
	// update page components by Ajax
	getProductMainDescription(queryString);
	getProductMoreDetails(queryString);
	getProductHeader(queryString);
	getSelProductInfo(queryString);
}

/* 
 * Helper method to get the query string
 * 
 * @input: pid - The product ID
 * @return: pid - The product ID
 */
function getQueryString(pid, cgid) {

    return "pid=" + encodeURIComponent(pid) + "&cgid=" + encodeURIComponent(cgid);
}

/* 
 * Requests the product main description component by Ajax
 * 
 * @input queryString The query string for the Ajax request
 * @return The Ajax response object
 */
function getProductMainDescription(queryString) {

    var url		= document.forms.cartaddproduct.update_url_mdescr.value;
	var ajax	= new Request.HTML({url: url, update: "mainDescription"}).send(queryString);

	return ajax;
}

/* 
 * Requests the product more details component by Ajax
 * 
 * @input queryString The query string for the Ajax request
 * @return The Ajax response object
 */
function getProductMoreDetails(queryString) {

    var url		= document.forms.cartaddproduct.update_url_moreinfo.value;
	var ajax	= new Request.HTML({url: url, update: "productInformationCol1"}).send(queryString);
	
	return ajax;
}

/* 
 * Requests the product header component by Ajax
 * 
 * @input queryString The query string for the Ajax request
 * @return The Ajax response object
 */
function getProductHeader(queryString) {

    var url		= document.forms.cartaddproduct.update_url_productheader.value;
	var ajax	= new Request.HTML({url: url, update: "productHeader"}).send(queryString);
	
	return ajax;
}

/* 
 * Requests the product name component by Ajax
 * 
 * @input queryString The query string for the Ajax request
 * @return The Ajax response object
 */
function getSelProductInfo (queryString) {

    var url		= document.forms.cartaddproduct.update_url_selproductinfo.value;
	var ajax	= new Request.HTML({url: url ,update: "productInfo"}).send(queryString);
	
	return ajax;
}

