// Global Variables
var Timer;
var oAjaxObj_top;
var oAjaxObj_middle;
var oAjaxObj_bottom;

// Mozilla/Safari
if (window.XMLHttpRequest) {
  oAjaxObj_top = new XMLHttpRequest();
	oAjaxObj_middle = new XMLHttpRequest();
	oAjaxObj_bottom = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
  oAjaxObj_top = new ActiveXObject("Microsoft.XMLHTTP");
	oAjaxObj_middle = new ActiveXObject("Microsoft.XMLHTTP");
	oAjaxObj_bottom = new ActiveXObject("Microsoft.XMLHTTP");
}


// AJAX functions
function sas_edit_xmlhttpPost(strURL, product_id) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            sas_edit_updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send("product_id="+product_id);
}

function sas_post(strURL, product_style, container, color, trim, prod_id, ImageName) {
    //alert(container);
		if(container=='top'){
			oAjaxObj_top.open('POST', strURL, true);
      oAjaxObj_top.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      oAjaxObj_top.onreadystatechange = function() {
        if (oAjaxObj_top.readyState == 4) {
          updatepage(oAjaxObj_top.responseText, container);
			    return 1;
        }
		  }
      oAjaxObj_top.send("color="+color+"&product_style="+product_style+"&trim="+trim+"&prod_id="+prod_id+"&ImgName="+ImageName);
		}
		if(container=='bottom'){
			oAjaxObj_bottom.open('POST', strURL, true);
      oAjaxObj_bottom.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      oAjaxObj_bottom.onreadystatechange = function() {
        if (oAjaxObj_bottom.readyState == 4) {
          updatepage(oAjaxObj_bottom.responseText, container);
			    return 1;
        }
		  }
      oAjaxObj_bottom.send("color="+color+"&product_style="+product_style+"&trim="+trim+"&prod_id="+prod_id+"&ImgName="+ImageName);
		}
				
		return 1;
}



function sas_xmlhttpPost(strURL, product_style, container, color, trim, prod_id, ImageName) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
		//alert(container);
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText, container);
						return 1;
        }
				return -1;
      }
		
    var s_res = self.xmlHttpReq.send("color="+color+"&product_style="+product_style+"&trim="+trim+"&prod_id="+prod_id+"&ImgName="+ImageName);
		//alert(s_res);
		return 1;
}

function vest_xmlhttpPost(strURL, product_style, color, prod_id, ImageName) {
    //alert("strURL: " + strURL + "\n product_style: " + product_style + "\n color: " + color + "\n prod_id: " + prod_id + "\n ImageName: " + ImageName);
		var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
		//alert(container);
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatevest(self.xmlHttpReq.responseText);
						return 1;
        }
				return -1;
      }
		
    var s_res = self.xmlHttpReq.send("color="+color+"&product_style="+product_style+"&prod_id="+prod_id+"&ImgName="+ImageName);
		//alert(s_res);
		return 1;
}

function updatepage(str, container){
  ires = new Array();
	var ires = str.split("&");
	var first_return = ires[0].split("-");
			
		if (container === "top"){
		  document["model_a"].src = ires[1];
    	document["model_b"].src = ires[2];
    	document["model_d"].src = ires[4];
    	document["model_a"].alt = first_return[0]+"-"+first_return[1]+"-"+first_return[4];
    	document["model_b"].alt = first_return[0]+"-"+first_return[1]+"-"+first_return[4];
    	document["model_d"].alt = first_return[0]+"-"+first_return[1]+"-"+first_return[4];
    	
    	document.forms['top_sas_vars'].elements["top_style"].value = first_return[0];
    	document.forms['top_sas_vars'].elements["top_color"].value = first_return[2];
					
			// this controls the choice with the cart; remove before going live	
			document.forms['top_sas_vars'].elements["top_product_number"].value = first_return[0];
			document.getElementById('top_color_code').value = first_return[3];
			document.getElementById('top_product_id').value = first_return[4];
			document.getElementById('w_top_product_id').value = first_return[4];
		
			//this is for the wishlist
			document.getElementById('w_top_product_number').value = first_return[0]+"-"+first_return[3];
			document.getElementById('w_top_size').value = document.forms['top_sas_vars'].elements["top_size"].selectedIndex;
				
    	if(ires[6]>0 ){
			  document.forms['top_sas_vars'].elements["top_price"].value = ires[6];
			}
    	for(xx=7; xx<=ires.length-1; xx++){
			  document.forms['top_sas_vars'].elements["top_size"].options[xx-7] = new Option(ires[xx], ires[xx]);
				//this is to load up the first option by default
			  if(xx==7){
			    document.getElementById('w_top_size').value = ires[xx];
			  }
			}
		}//if top
		else {
			document["model_c"].src = ires[3];
			document["model_e"].src = ires[5];
			document["model_c"].alt = first_return[0]+"-0"+"-"+first_return[3];
			document["model_e"].alt = first_return[0]+"-0"+"-"+first_return[3];
			
			document.forms['bottom_sas_vars'].elements["bottom_style"].value = first_return[0];
			document.forms['bottom_sas_vars'].elements["bottom_color"].value = first_return[2];
			
			// this controls the choice with the cart; 
			document.forms['bottom_sas_vars'].elements["bottom_product_number"].value = first_return[0];
		
			document.getElementById('w_bottom_product_number').value = first_return[0]+"-"+first_return[3];
			document.getElementById('bottom_color_code').value = first_return[3];
			document.getElementById('bottom_product_id').value = first_return[4];
			//wishlist
			document.getElementById('w_bottom_product_id').value = first_return[4];
			document.getElementById('w_bottom_size').value = document.forms['bottom_sas_vars'].elements["bottom_size"].selectedIndex;
						
			if(ires[6]>0 ){
			  document.forms['bottom_sas_vars'].elements["bottom_price"].value = ires[6];
			}
			for(xx=7; xx<=ires.length-1; xx++){
			  document.forms['bottom_sas_vars'].elements["bottom_size"].options[xx-7] = new Option(ires[xx], ires[xx]);
			  //this is to load up the first option by default
			  if(xx==7){
			    document.getElementById('w_bottom_size').value = ires[xx];
			  }
			}
		}//else bottom
		
  return;
}

function updatevest(str){
  ires = new Array();
	var arr_chunks = str.split("|");
	var arr_prod_info = arr_chunks[0].split("&");
	var arr_size_info = arr_chunks[2].split("&");
	var dbl_price = arr_chunks[3];
	var arr_colors = arr_chunks[4].split("&");		
		
		  document["model_a"].src = arr_chunks[1];
    	
    	document["model_a"].alt = arr_prod_info[0]+"-"+arr_prod_info[2]+"-"+arr_prod_info[3];
    	    	
    	document.forms['top_sas_vars'].elements["top_style"].value = arr_prod_info[0];
    	document.forms['top_sas_vars'].elements["top_color"].value = arr_prod_info[1];
					
			// this controls the choice with the cart; remove before going live	
			document.forms['top_sas_vars'].elements["top_product_number"].value = arr_prod_info[0];
			document.getElementById('top_color_code').value = arr_prod_info[2];
			
			document.getElementById('top_product_id').value = arr_prod_info[3];
			//document.getElementById('w_top_product_id').value = arr_prod_info[4];
		
			//this is for the wishlist
			//document.getElementById('w_top_product_number').value = arr_prod_info[0]+"-"+arr_prod_info[3];
			//document.getElementById('w_top_size').value = document.forms['top_sas_vars'].elements["top_size"].selectedIndex;
				
    	document.forms['top_sas_vars'].elements["top_price"].value = dbl_price;
			
    	for(xx=0; xx<=arr_size_info.length-1; xx++){
			  document.forms['top_sas_vars'].elements["top_size"].options[xx] = new Option(arr_size_info[xx], arr_size_info[xx]);
				//this is to load up the first option by default
			  //if(xx==0){
			  //  document.getElementById('w_top_size').value = arr_size_info[xx];
			 // }
			}
			//now knock out the colors
			var color_box_html = "<table cellpadding=1 cellspacing=1>\n<tr>\n";
			var ctr = 1;
			for(c=0; c<=arr_colors.length-1;c++){
			  if(ctr % 5 == 0){
				  color_box_html += "<tr>";
				}
				var code_val = arr_colors[c].split(":");
				//0 is the code. 1 is the name
				color_box_html += "<td><a href='#'	onclick=\"javascript: getVestImages(document['model_a'].alt,'" + code_val[1].toLowerCase() + "','');\"><IMG	SRC=\"/imgs/color_" + code_val[0] + ".gif\" WIDTH=\"15\" HEIGHT=\"15\" ALT=\""+code_val[1].toLowerCase()+"\" BORDER=\"0\"></a></td>\n";
			  ++ctr;
				if(ctr % 5 == 0){
				  color_box_html+="</tr>";
				}
			}
		  while(ctr % 5 !=0){
			  color_box_html += "<td></td>\n";
				++ctr;
			}
			color_box_html + "</tr>\n</table>";
			document.getElementById('color_box').innerHTML = color_box_html;
		
		
  return;
}

function getTopImages(top_product_id, color, ImgName){
	if (top_product_id !=""){
		//product = new Array();
		var top_product = top_product_id.split("-");
		var p_res = sas_xmlhttpPost('getproductimage.php', top_product[0], 'top', color, top_product[1], top_product[2], ImgName);
		//alert(p_res);
		return 1;
	}
}

function getMiddleImages(product_id, color, ImgName){
	if (product_id !=""){
		product = new Array();
		var product = product_id.split("-");
		sas_xmlhttpPost('getproductimage.php', product[0], 'middle', color, product[1],product[2], ImgName);
	}
}



function getBottomImages(product_id, color, ImgName){
	if (product_id !=""){
		//product = new Array();
		var product = product_id.split("-");
		if(true == isNaN(product[2])){
		  product[2] = document.getElementById('bottom_product_id').value;
		}
		sas_xmlhttpPost('getproductimage.php', product[0], 'bottom', color, product[1], product[2], ImgName);
		return 1;
		//(strURL, product_style, container, color, trim, prod_id)
		//alert(product_id);
	}
}

function getVestImages(top_product_id, color, ImgName){
	if (top_product_id !=""){
		//product = new Array();
		var top_product = top_product_id.split("-");
		var p_res = vest_xmlhttpPost('getproductimage.php', top_product[0], color, top_product[2], ImgName);
		//alert(p_res);
		return 1;
	}
}

function get_top_default(product_id, color){
	if (product_id !=""){
		//product = new Array();
		var product = product_id.split("-");
		if(true == isNaN(product[2])){
		  product[2] = document.getElementById('top_product_id').value;
		}
		sas_post('getproductimage.php', product[0], 'top', color, product[1], product[2], product[3]);
	}
}
function get_bottom_default(product_id, color){
	if (product_id !=""){
		//product = new Array();
		var product = product_id.split("-");
		if(true == isNaN(product[2])){
		  product[2] = document.getElementById('top_product_id').value;
		}
		sas_post('getproductimage.php', product[0], 'bottom', color, product[1], product[2], product[3]);
	}
}
//for testing*******************************************
function load_default(product_id_top, color_top, product_id_bottom, color_bottom){
	//getTopImages(product_id_top, color_top);
	get_top_default(product_id_top, color_top);
	get_bottom_default(product_id_bottom, color_bottom);
	
	//  getBottomImages(product_id_bottom, color_bottom);
	// alert('good');
  
	return;
}


// sas edit options
function getSasOptions(product_id){
	if (product_id !=""){
		sas_edit_xmlhttpPost('getsasoptions.php', product_id);
	}
}

function sas_edit_updatepage(str){
	sasires = new Array();
	var sasires = str.split("&");
	
	// update 'is_top' pulldown menu selection
	document.forms['sas_edit'].elements["is_top_choice"].selectedIndex = sasires[1];
	
	// update image
	for(yy=3; yy<=sasires.length-1; yy++){
		document.getElementById("container_images").options[yy-3] = new Option(sasires[yy], sasires[yy]);
	}
	
	
	
	
	
	
	
}


function removeOption(cont){
  	var x=document.getElementById(cont);
  	x.remove(x.selectedIndex);
}

function checkMenu(menu, proddefualt){
	var menulen = document.forms['sas_edit'].elements[menu].options[length];
	
	for(count=0; count <= menulen; count++){
	
	
	}


}


// Image container functions
function ScrollLeft1(){
  Timer = setInterval("document.getElementById('top').scrollLeft -= 2", 15);
}
function ScrollRight1(){
  Timer = setInterval("document.getElementById('top').scrollLeft += 2", 15);
}

function ScrollLeft2(){
  Timer = setInterval("document.getElementById('bottom').scrollLeft -= 2", 15);
}
function ScrollRight2(){
  Timer = setInterval("document.getElementById('bottom').scrollLeft += 2", 15);
}

function ScrollLeftMiddle(){
  Timer = setInterval("document.getElementById('middle').scrollLeft -= 2", 15);
}
function ScrollRightMiddle(){
  Timer = setInterval("document.getElementById('middle').scrollLeft += 2", 15);
}

function showImage(path_name, iname){
	document.getElementById('product_image').src = path_name;
	document.getElementById('cont_name').value = iname;
	
}