var fo_Items = Array();
var itemcnt = 0;

function loadFastOrd(){

	itemcnt=0+eval(document.form1.prevcnt.value)
	document.form1.pageqty.value=eval(document.form1.prevqty.value)
	document.form1.totqty.value=eval(document.form1.cartqty.value)+eval(document.form1.pageqty.value)

	getData()

	loadStyles()

	document.form1.style.disabled=false
	document.form1.add.disabled=false

	//Select style from details page
	if(document.form1.detitem.value!=""){
		for (i = 0; i < document.form1.style.length; i++){
			if(document.form1.style.options[i].value.toUpperCase()==document.form1.detitem.value.toUpperCase()){
				document.form1.style.selectedIndex=[i]
			}
		}
		styleChanged()
	}

	//Select color from details page
	if(document.form1.detcolor.value!=""){
		styleChanged()
		for (i = 0; i < document.form1.colors.length; i++){
			if(document.form1.colors.options[i].value.toUpperCase()==document.form1.detcolor.value.toUpperCase()){
				document.form1.colors.selectedIndex=[i]
			}
		}
	}

	fo_UpdateItems();
}



 
function getData(){
	//Assign item data to data variable
	var oXML = getXMLHttpObj();
	oXML.open('GET', "fastorddata.asp", false);
	oXML.send('');
	data=(oXML.responseText);
}
 
function getXMLHttpObj(){
	if(typeof(XMLHttpRequest)!='undefined')
 		return new XMLHttpRequest();
 
 	var axO=['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'], i;
 	for(i=0;i<axO.length;i++)
 		try{
 			return new ActiveXObject(axO[i]);
 		}catch(e){}
 	return null;
}
 
function fo_AddItem() {
	if (document.pressed=="Add to Page") {
		if (validate()) {
			var selIndex	= document.form1.style.selectedIndex
			var styleValue	= document.form1.style.options[selIndex].value
			var selIndex	= document.form1.colors.selectedIndex
			var colorValue	= document.form1.colors.options[selIndex].value
			var selIndex	= document.form1.sizes.selectedIndex
			var sizeValue	= document.form1.sizes.options[selIndex].value
			itemcnt++;

			// Add item to new object then push object into global item array
			var myItem		= {};
			myItem.style	= styleValue;
			myItem.color	= colorValue;
			myItem.size		= sizeValue;
			myItem.qty		= document.form1.qty.value;
			myItem.id		= itemcnt;
			myItem.price	= '<a href="#" onclick="fo_CheckPricing()">check</a>';
			myItem.uid		= document.form1.jsuid.value;
			fo_Items.push(myItem);

			fo_UpdateItems();
		}
		return false;
	} else {
		return true;
	}
}

function fo_RemoveItem(x) {
	itemcnt--;

	for (var i=0 ; i<fo_Items.length ; i++ ) {
		var myItem = fo_Items[i];
		if (myItem.id == x) fo_Items.splice(i,1);
	}

	fo_UpdateItems();
}

function fo_UpdateItems() {
	var myDiv = document.getElementById("fastord_cart");

	var myOutput  = '<table border="0" cellspacing="2" cellpadding="3">';
	myOutput += '<tr class="tbl_header">';
	myOutput += '<td>Item</td>';
	myOutput += '<td>Color</td>';
	myOutput += '<td>Size</td>';
	myOutput += '<td>Qty.</td>';
	myOutput += '<td>Price</td>';
	myOutput += '<td>Remove</td>';
	myOutput += '</td>';

	var myFormItems = '';

	document.form1.pageqty.value = 0;

	for (var i=0 ; i<fo_Items.length ; i++) {
		var myItem = fo_Items[i];
		var tblClass = (i%2==0) ? "tbl_data_even" : "tbl_data_odd";
		myOutput += '<tr>';
		myOutput += '<td align="left" class="tbl_data ' + tblClass + '"><a href="item.asp?item=' + myItem.style + '&uid=' + myItem.uid + '&color=' + myItem.color + '">' + myItem.style + '</a></td>';
		myOutput += '<td align="left" class="tbl_data ' + tblClass + '">' + myItem.color + '</td>';
		myOutput += '<td align="left" class="tbl_data ' + tblClass + '">' + myItem.size + '</td>';
		myOutput += '<td class="tbl_data ' + tblClass + '">' + myItem.qty + '</td>';
		myOutput += '<td align="right" class="tbl_data ' + tblClass + '">' + myItem.price + '</td>';
		myOutput += '<td class="tbl_data ' + tblClass + '"><a href="#" onclick="fo_RemoveItem(' + myItem.id + ')">remove</a></td>';
		myOutput += '</tr>';

		myFormItems += myItem.style + ',';
		myFormItems += myItem.color + ',';
		myFormItems += myItem.size + ',';
		myFormItems += myItem.qty + '|';

		document.form1.pageqty.value = eval(document.form1.pageqty.value) + eval(myItem.qty);
	}

	document.form1.totqty.value	= eval(document.form1.cartqty.value) + eval(document.form1.pageqty.value)
	document.form1.prevqty.value = eval(document.form1.pageqty.value)
	document.form1.items.value = myFormItems;
	myOutput += '</table>';

	if (fo_Items.length == 0) {
		myOutput = '<h4>Please select an item above...</h4>';
	}

	myDiv.innerHTML = myOutput;
}

function fo_CheckPricing() {
	document.pressed = 'Check Pricing';
	document.form1.submit();
}

function validate() {
	var selIndex = document.form1.style.selectedIndex;
	var styleValue = document.form1.style.options[selIndex].value;
	var qtyValue = document.form1.qty.value;
	var valFlag = 0;
	var itxt = "";
	var qtxt = "";
	if (styleValue==null||styleValue=="") {
		itxt="Select Item ";
		valFlag = 1;
	}
	if (qtyValue==null||qtyValue==""||qtyValue=="0"||IsNumeric(qtyValue)==false) {
		qtxt="Check Quantity";
		document.form1.qty.focus();
		valFlag = 1;
	}
	if (valFlag==0) {
		return true;
	} else {
		alert(itxt+qtxt);
		return false;
	}
}

 function IsNumeric(sText){
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
 
function cleardiv(){
	document.form1.items.value=""
	document.form1.pageqty.value=0
	document.form1.totqty.value=document.form1.cartqty.value
	document.form1.prevqty.value=0
	fo_Items = Array();
	fo_UpdateItems();
}

 function loadStyles(){
	 //Load styles combo box
	var arrLines = data.split("\n")
	stylesCombo=new Array()
	stylesCombo[0]=new Option("","")
	for (i=0;i<arrLines.length;i++) {
		var ln=arrLines[i]
		var epos = ln.indexOf("|")
		var style = ln.substring(0, epos)
		if(style!="") {
			stylesCombo[i+1]=new Option(style,style)
		}
	}
	populateStyles(stylesCombo)
}

function populateStyles(x){
	var cacheobj=document.form1.style
	for (m=cacheobj.options.length-1;m>0;m--)
		cacheobj.options[m]=null
	selectedarray=eval(x)	  
	for (i=0;i<selectedarray.length;i++)
		 if (selectedarray[i] != undefined)
			cacheobj.options[i]=new Option(selectedarray[i].text,selectedarray[i].value)
		
	cacheobj.options[0].selected=true  
}
 
 function populateColors(x){
 var cacheobj=document.form1.colors
 for (m=cacheobj.options.length-1;m>0;m--)
 cacheobj.options[m]=null
 selectedarray=eval(x)
 for (i=0;i<selectedarray.length;i++)
 cacheobj.options[i]=new Option(selectedarray[i].text,selectedarray[i].value)
 cacheobj.options[0].selected=true
}

 function populateSizes(x){
 var cacheobj=document.form1.sizes
 for (m=cacheobj.options.length-1;m>0;m--)
 cacheobj.options[m]=null
 selectedarray=eval(x)
 for (i=0;i<selectedarray.length;i++)
 cacheobj.options[i]=new Option(selectedarray[i].text,selectedarray[i].value)
 cacheobj.options[0].selected=true
}
 


function styleChanged(){
var selIndex = document.form1.style.selectedIndex
var styleValue = document.form1.style.options[selIndex].value

if (styleValue.length>0) {
	//Update colors combo box
	var spos = (data.indexOf('\n'+styleValue+'|') + styleValue.length + 2)
	var epos = data.indexOf("|", spos)
	var colors = data.substring(spos, epos)
	var arrColors = colors.split(",")
	colorsCombo=new Array()
	for (i=0;i<arrColors.length;i++){
	colorsCombo[i]=new Option(arrColors[i],arrColors[i])
	}
	populateColors(colorsCombo)

	//Update sizes combo box
	spos = epos + 1
	epos = data.indexOf('\n', spos)
	var sizes = data.substring(spos, epos - 1)
	var arrSizes = sizes.split(",")
	sizesCombo=new Array()
	for (i=0;i<arrSizes.length;i++){
	sizesCombo[i]=new Option(arrSizes[i],arrSizes[i])
	}
	populateSizes(sizesCombo)
}
}


function noenter(){
return !(window.event && window.event.keyCode == 13);
}




	

