// tracker variables
var pp,p,p_num,t,tn;

function getHTTPObject() {
	var http = false;
	//Use IE's ActiveX items to load the file.
	if(typeof ActiveXObject != 'undefined') {
		try {http = new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e) {
			try {http = new ActiveXObject("Microsoft.XMLHTTP");}
			catch (E) {http = false;}
		}
	//If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.
	} else if (XMLHttpRequest) {
		try {http = new XMLHttpRequest();}
		catch (e) {http = false;}
	}
	return http;
}

// change trim sets
function changeTrimSet(group){
	var http = getHTTPObject();
	var params = 'ajax=true&action=trim&group='+group;
	http.open("POST", "/includes/ajax.php", true);
	
	//Send the proper header infomation along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	http.onreadystatechange = function(){
		switch(http.readyState){
			case 4:
				if(http.status == 200){
					document.getElementById('trimSet').innerHTML = http.responseText;
					// change highlighted link
					var binding_links_div = document.getElementById('binding_links');
					var links = binding_links_div.getElementsByTagName('a');
					for(i=0; i<links.length; i++){
						if(i == group){
							links[i].setAttribute('id', 'current');
						}else{
							links[i].setAttribute('id', '');
						}
						links[i].blur();
					}
				}else{
					document.getElementById('trimSet').innerHTML = 'An error occurred.';
				}
			break;
			case 3:
				document.getElementById('trimSet').innerHTML = 'Loading...';
			break;
		}
		document.getElementById('trimSet').innerHTML += '<div class="clear"></div>';
	}
	http.send(params);
}

// image reloader
function reloadImage(pArray, tArray){
	var image_container = document.getElementById('app_image');
	var image = image_container.getElementsByTagName('img');
	var src = image[0].getAttribute('src');
	
	var vars = src.split("?")[1];
	
	var pp_temp = vars.split("pp=")[1];
	pp = pp_temp.split("&")[0];
	
	var p_temp = pp_temp.split("p=")[1];
	p = p_temp.split("&")[0];
	
	var pNum_temp = p_temp.split("pn=")[1];
	p_num = pNum_temp.split("&")[0];
	
	var t_temp = pNum_temp.split("t=")[1];
	t = t_temp.split("&tn=")[0];
	tn = t_temp.split("&tn=")[1];
	
	if(pArray != null){
		pp = pArray[0];
		p = pArray[1];
		p_num = pArray[2];
		
		updateApplication(pArray,null);
	}else{
		t = tArray[0];
		tn = tArray[1];
		
		updateApplication(null,tArray);
	}
	// reload new image
	image[0].src = "/image.php?pp="+pp+"&p="+p+"&pn="+p_num+"&t="+t+"&tn="+tn;
}

// correlate up-close textures with current carpet/trim
function updateApplication(pArray,tArray){
	var binding_div = document.getElementById('binding_color');
	var carpet_div = document.getElementById('carpet_color');
	
	var div = document.getElementById('large_textures');
	var tags = div.getElementsByTagName('div');
	
	if(pArray != null){
		// rug
		tags[1].style.background = "#fff url(/images/"+pArray[0]+"/"+pArray[1]+"/134x55/"+pArray[1]+"_"+pArray[2]+".jpg) top left no-repeat";
		
		if(pArray[1].indexOf("_") != -1){
			temp_split1 = pArray[1].split("_")[0];
			temp_split2 = pArray[1].split("_")[1];
			pArray[1] = temp_split1 + " " + temp_split2;
		}
		tags[1].innerHTML = pArray[1] + " " + pArray[2];
		carpet_div.innerHTML = pArray[1] + " " + pArray[2];
	}else{
		// trim
		tags[0].style.background = "#fff url(/images/Bindings/"+tArray[0]+"/134x55/"+tArray[0]+"_"+tArray[1]+".jpg) top left no-repeat";
		if(tArray[1].indexOf("_") != -1){
			temp_split1 = tArray[1].split("_")[0];
			temp_split2 = tArray[1].split("_")[1];
			tArray[1] = temp_split1 + " " + temp_split2;
		}
		if(tArray[0] == 'Kal'){
			tags[0].innerHTML = "Cotton " + tArray[1];
			binding_div.innerHTML = "Cotton " + tArray[1];
		}else if(tArray[0] == 'Suede'){
			tags[0].innerHTML = "Faux Suede " + tArray[1];
			binding_div.innerHTML = "Faux Suede " + tArray[1];
		}else{
			tags[0].innerHTML = tArray[0] + " " + tArray[1];
			binding_div.innerHTML = tArray[0] + " " + tArray[1];
		}
	}
}

// email current application data
function sendAppData(){
	// send application data to FORM page
	if(pp.indexOf("_") != -1){
		temp_split1 = pp.split("_")[0];
		temp_split2 = pp.split("_")[1];
		pp = temp_split1 + " " + temp_split2;
	}
	if(p.indexOf("_") != -1){
		temp_split1 = p.split("_")[0];
		temp_split2 = p.split("_")[1];
		p = temp_split1 + " " + temp_split2;
	}
	if(t.indexOf("_") != -1){
		temp_split1 = t.split("_")[0];
		temp_split2 = t.split("_")[1];
		t = temp_split1 + " " + temp_split2;
	}
	if(tn.indexOf("_") != -1){
		temp_split1 = tn.split("_")[0];
		temp_split2 = tn.split("_")[1];
		tn = temp_split1 + " " + temp_split2;
	}
	switch(t){
		case 'Kal':
			t = 'Cotton';
		break;
	}
	
	document.forms['appData'].vars.value = pp+","+p+","+p_num+","+t+","+tn;
	document.forms['appData'].submit();
}
