var oldIndex=0;
var selectedIndex=0;
function selectSample(index,product_id){
	document.getElementById('sample').value=product_id;
	highlightSample(index);
	selectedIndex=index;
	if (oldIndex!=0){
		unhighlightSample(oldIndex);
	}
	oldIndex=index;
}

function highlightSample(index){
	var currentIndex=document.getElementById('sample').value;
	document.getElementById('sample'+index).src="images/product_"+index+"_on.png";
}

var unhighlightSample = function(index){
	if (index==selectedIndex)
		return;
	document.getElementById('sample'+index).src = "images/product_"+index+"_off.png";
}

function clickOther() {

	var isChecked = document.getElementById('otherCheckBox').checked;
	
	var label = document.getElementById('otherLabel');
	var input = document.getElementById('otherInput');

	if (isChecked) {
		label.style.display = 'none';
		input.style.display = 'block';
		input.value = "other";
	}
	else {
		label.style.display = 'block';
		input.style.display = 'none';
	}
}
var t = new Image();
t.src="images/product_1_off.png";
t.src="images/product_2_off.png";
t.src="images/product_3_off.png";
t.src="images/product_4_off.png";
t.src="images/product_5_off.png";

var http_request = false;

/****
	create XMLHttpRequest object
****/
function makeRequest(url, parameters) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
     	// set type accordingly to anticipated content type
        //http_request.overrideMimeType('text/xml');
        http_request.overrideMimeType('text/html');
     }
  } else if (window.ActiveXObject) { // IE
     try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
  }
  if (!http_request) {
     alert('Cannot create XMLHTTP instance');
     return false;
  }
  http_request.onreadystatechange = processFormAComplete;
  http_request.open('GET', url + parameters, true);
  http_request.send(null);
}

function processFormAComplete() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var results=http_request.responseText.split("&");
			var result = results[1].split("=")[1];
			if (result=="success")
			{
				//copy info for 2nd submit from 1st form
				document.getElementById('f2_firstname').value=document.getElementById('first_name').value;
				document.getElementById('f2_lastname').value=document.getElementById('last_name').value;
				document.getElementById('f2_email').value=document.getElementById('email_address').value;
				document.getElementById('f2_sample').value=document.getElementById('sample').value;
				
				document.getElementById('pageA').style.display = "none";
				document.getElementById('pageB').style.display = "block";
				pageTracker._trackPageview("Sample_affinity");
			}
			else {
				var msg = results[2].split("=")[1];
				switch (msg){
					case 'duplicate order':
						document.getElementById('errMsgTxt3').style.display="block";
						break;
					default:
						break;
					
				}
			}
				
		}
	}
}

function itemSanitize(val)
{
	var ret = val.replace(/#/gi,'%23');
	return ret;
}
   
   
/****
	Create GET string from form variables
****/
function get(obj,url) {
		var getstr = "?";
		getstr+="source="+document.getElementById('source').value;
		getstr+="&sample="+document.getElementById('sample').value;
		if (document.getElementById('g_female').checked)
			getstr+="&gender="+document.getElementById('g_female').value;
		else if (document.getElementById('g_male').checked)
			getstr+="&gender="+document.getElementById('g_male').value;
		getstr+="&birthyear="+obj.birthyear.value;
		getstr+="&birthday="+obj.birthday.value;
		getstr+="&birthmonth="+obj.birthmonth.value;
		getstr+="&optin="+obj.optin.value;
		getstr+="&optin2="+obj.optin2.value;
		getstr+="&optin3="+obj.optin3.value;
		getstr+="&zip="+obj.zip.value;
		getstr+="&state="+ itemSanitize(obj.state.value);
		getstr+="&city="+ itemSanitize(obj.city.value);
		getstr+="&apt="+ itemSanitize(obj.apt.value);
		getstr+="&street="+ itemSanitize(obj.street.value);
		getstr+="&lastname="+ itemSanitize(obj.lastname.value);
		getstr+="&firstname="+ itemSanitize(obj.firstname.value);
		getstr+="&email="+ itemSanitize(obj.email.value);
		return getstr;
  }

/****
	submit sample information via AJAX
****/
function submitSample(){
	
	//getstr = get(document.getElementById('hairapySamplesForm'),'samplesProxy.php');
	//makeRequest("samplesProxy.php", getstr);
	
	getstr = get(document.getElementById('hairapySamplesForm'),'samplesProxySunsik.php');
	makeRequest("samplesProxySunsilk.php", getstr);
}

var oldIndex=0;
var selectedIndex=0;
function selectSample(index,product_id){
	document.getElementById('sample').value=product_id;
	highlightSample(index);
	selectedIndex=index;
	if (oldIndex!=0){
		unhighlightSample(oldIndex);
	}
	oldIndex=index;
}

function highlightSample(index){
	var currentIndex=document.getElementById('sample').value;
	document.getElementById('sample'+index).src="images/product_"+index+"_on.png";
}

var unhighlightSample = function(index){
	if (index==selectedIndex)
		return;
	document.getElementById('sample'+index).src = "images/product_"+index+"_off.png";
}

function clickOther() {

	var isChecked = document.getElementById('otherCheckBox').checked;
	
	var label = document.getElementById('otherLabel');
	var input = document.getElementById('otherInput');

	if (isChecked) {
		label.style.display = 'none';
		input.style.display = 'block';
		input.value = "other";
	}
	else {
		label.style.display = 'block';
		input.style.display = 'none';
	}
	
	for (x = 0; x < 7; x++) {
		if (eval("document.extraInfo.q2[" + x + "].checked") == true) {
			document.extraInfo.q2[x].checked = false;
      	}
   	}
}

function passedSelection() {

	if (GETVars.GET["id"]) {
	
		var id = GETVars.GET["id"];
		
		switch(id) {
			
			case "caida": { break; }
			case "curls": { selectSample(1,100160); break; }
			case "volume": { selectSample(4,100158); break; }
			case "esponja": { break; }
			case "hydra": { selectSample(5,100161); break; }
			case "poof": { break; }
			case "straight": { selectSample(3,100162); break; }
			case "therma": { break; }
			case "waves": { selectSample(2,100159); break; }
			default: { break; }
		};
	
	}

}