// JavaScript Document 

function Transpose_Email(userid,domain,subject)
{
	var email = domain +'@' + userid;
	var TempBefore = '';
	var TempAfter = '';
	var NewString = '';
	var Start = 0;
	
	var WhereHash = email.indexOf ('@');
	for (Count = 1; Count <= WhereHash; Count ++)
	{
		TempBefore += email.substring (Start, Count);
		Start++
	}
	Start = WhereHash;
	Start ++
	WhereHash +=2;
	for (Count = WhereHash; Count <=email.length; Count++)
	{
		TempAfter +=email.substring (Start, Count)
		Start++
	}
	NewString = TempAfter +'@' + TempBefore;
	parent.location = 'mailto:'+NewString+'?subject='+subject;
}

//********** Loads Brands xml page ***********
//var brandArray=new Array();
var brandsXmlDoc;

function loadXML() {
		
	brandsXmlDoc = false;
	//branch for native 	XMLHttpRequest Object (firefox, safari)
	if (window.XMLHttpRequest && !(window.ActiveXObject)) {
		
		try {
			brandsXmlDoc = new XMLHttpRequest();
		} catch (e) {
			brandsXmlDoc = false;
		}
		
	} else if (window.ActiveXObject) {
		try {
			brandsXmlDoc = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			
			try {
				brandsXmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				brandsXmlDoc = false;
			}
			
		}
	}
	
	if (brandsXmlDoc!=null) {
		brandsXmlDoc.onreadystatechange = state_Change;
		brandsXmlDoc.open ("GET", 'xml/brands.xml', true);
		brandsXmlDoc.send(null);
	}	
}
	
	function state_Change() {
		if (brandsXmlDoc.readyState==4) {//4 = "Loaded"
			if (brandsXmlDoc.status == 200) {
				//alert (brandsXmlDoc.responseXML.getElementsByTagName('name')[0].firstChild.nodeValue)
				displayTable();
				// 200 = OK (whatever that means)
			} else {
				alert ('problem retrieving XML Data');
			}
		}
	}
	
								 

//***** Set to display table list Downward instead of across
function displayTable() {

	var brandsQuery = brandsXmlDoc.responseXML.getElementsByTagName('name');
	//alert (brandsQuery.length); //currently losing value of xml doc.
	if (window.ActiveXObject) {
	var bInterval=Math.ceil(brandsQuery.length/3) //Defines row count over 3 columns
	} else if (document.implementation.createDocument) {
		var bInterval=12;
	}
	var newRow=document.getElementById('brandsTable').insertRow(-1);
	//var newRow=document.getElementById('brandsTable').insertRow(-1);
	var newCell=newRow.insertCell(-1);
	newCell.colSpan=3;
	
	newCell.innerHTML="We pride ourselves in offering the finest products in Audio and Video. Click on the list below to visit our Vendors' Websites";
	var newRow=document.getElementById('brandsTable').insertRow(-1);
	var newRow=document.getElementById('brandsTable').insertRow(-1);
//alert(brandsXmlDoc.getElementsByTagName('name')[13].firstChild.nodeValue);
	for (x=0;x<bInterval;x++) {
	
		var newRow=document.getElementById('brandsTable').insertRow(-1);
		
		
		for (xCell=0;xCell<3;xCell++) {	  
			var newCell=newRow.insertCell(-1);
			var nodeAdj=x+(xCell*bInterval); //so that row/cells will read r1:1,11,21 then r2:2,12,22, etc.
			
			if (brandsQuery[nodeAdj]!= null) {
				var cellBrand=brandsXmlDoc.responseXML.getElementsByTagName('name')[nodeAdj].firstChild.nodeValue;
				var cellURL=brandsXmlDoc.responseXML.getElementsByTagName('url')[nodeAdj].firstChild.nodeValue;
				newCell.innerHTML="<a href="+cellURL+" target='blank'>"+cellBrand+"</a>";
			}		
		}
	}	
}


//**********Calls XML from php scripts
var phpXMLObj;
function getRequestData(fileLoc, target, parameterString) {

	//first create an xmlhttpcontainer - see function createHttpVar();
	var XmlHttp = createHttpVar();

	//validate ajax support;
		if (XmlHttp==null) {
		alert("Your browser does not support AJAX!");
		return;
	}
	
	//retreive data info
	XmlHttp.open("POST", fileLoc, true);
	XmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

					//alert(parameterString +":1");
	var displayObj = document.getElementById(target);
	XmlHttp.onreadystatechange = function() {
		if (XmlHttp.readyState==4){
			if(XmlHttp.status==200) {
				phpXMLObj = XmlHttp.responseXML; //retrieved xml data
				//defines proper panel function based on target;
				
				switch(target) {
					case 'coTableDiv':
					formatCoTable(phpXMLObj,0);
					break;
					case 'addList':
					//alert(parameterString);
					//alert(phpXMLObj.getElementsByTagName('createStatus').length);
					//alert(phpXMLObj.getElementsByTagName('createStatus')[0].firstChild.nodeValue);
					showQuickList(phpXMLObj,0);
					//
					//formatResultsTable(phpXMLObj);
					break;				
					case 'edQuickList':
					//alert(phpXMLObj.getElementsByTagName('createStatus')[0].firstChild.nodeValue);
					showQuickList(phpXMLObj,1);
					break;				
					case 'deleteSelectTable':
					
					formatCoTable(phpXMLObj,1);
					break;				
					case 'delComplete':			
					//alert(phpXMLObj.getElementsByTagName('delStatus')[0].firstChild.nodeValue);
					//alert(phpXMLObj.getElementsByTagName('delStatus').length);
					getRequestData('../php/get_co_list.php', 'deleteSelectTable');
					
					break;

				};
			}
		}
	};
	
	XmlHttp.send(parameterString);

	
};/*getRequestData*/
	
//creates an xmlhttpObject
function createHttpVar(){
var XmlHttpObject=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  XmlHttpObject=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    XmlHttpObject=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    XmlHttpObject=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return XmlHttpObject;
}// End xmlhttpObject;	

var coXML = new Array();
function formatCoTable(phpXMLObj,opt) {
	colName = new Array('vend_name','item_name','item_type','item_cond','item_msrp','item_sp'); //xml ref for table layout;
	
	coXML = phpXMLObj.getElementsByTagName("co_item");
	//alert("test"+coXML[1].childNodes.length);
	for (x=0 ;x < coXML.length; x++) {
		var newRow = document.getElementById("closeOutsTable").insertRow(-1);
		
		if(opt==1){			//item Idx reference to delete
		
			itemIdx=String(x);//
			
			//newRow.setAttribute("Id", itemIdx+'rowId');				//creates attributes for 'row' HTML tag - Mozilla/FF only
			newRow.id = itemIdx +'rowId';
			newRow.className='normal';
			
			var newElem2 = phpXMLObj.createElement("delBool"); 		//creates boolean delete reference in xmlObject;
			var txt = phpXMLObj.createTextNode('');
			newElem2.appendChild(txt); 
			coXML[x].appendChild(newElem2);
			
			var newCell = newRow.insertCell(-1);
			newCell.innerHTML = '<input type="checkbox" id="'+ itemIdx +'" onClick="setDelList(this)"/>';
			//newCell.onclick = function() {setDelList(this)};
		}
		
		for (colN=0;colN <colName.length;colN++){
		var newCell = newRow.insertCell(-1);
		newCellVal = coXML[x].getElementsByTagName(colName[colN])[0].firstChild;
		if (colN==4 || colN==5) {
			newCell.style.textAlign = 'right';
		}
		//alert(newCellVal);
		if (!newCellVal) {		//if null-valued;
			newCell.innerHTML = '';
			} else {	
				newCell.innerHTML = newCellVal.nodeValue;
			};
		}
	}
};

/***** Delete items funtions *****/
function setDelList(listRef) {
	// toggles highlights and boolean delete('delBool') parameter
	var rowSel = listRef.id+'rowId';	
	if (document.getElementById(listRef.id).checked){
		document.getElementById(rowSel).className = 'selectRow';	
		coXML[listRef.id].getElementsByTagName('delBool')[0].firstChild.nodeValue =1;
	} else {
		document.getElementById(rowSel).className = 'normal';
		phpXMLObj.getElementsByTagName('delBool')[listRef.id].firstChild.nodeValue = 0;
	}
};

function delItems() {
	clearTable('closeOutsTable');
	var delArray = new Array()
	for (x=0; x<coXML.length; x++) {
		if(phpXMLObj.getElementsByTagName('delBool')[x].firstChild.nodeValue == 1) {
			delArray.push(String(phpXMLObj.getElementsByTagName('item_idx')[x].firstChild.nodeValue));
		}
	}
	
		//alert(delArray);
		var queryStr = 'del_idx=';
		for(d=0;d<delArray.length; d++)
			{
				if (d!=0) {
					queryStr = queryStr +',';
				}
				queryStr = queryStr +  delArray[d];
			};//Del Co Item
		//alert(queryStr);
		getRequestData('../php/delete_co_item.php', 'delComplete', queryStr);
}


/***** post new form table *****/

function addCoItem(idx) {
	var formNames=new Array('vend_name','item_name','item_type','item_desc','item_cond','item_msrp','item_sp','item_sc','item_notes');
	var formIds=new Array('vend','item','p-type','desc','cond','msrp','saleprice','margin','notes_field');

	if (idx==2){
		formNames.push('item_idx');
		formIds.push('idx_fld');
	};
	
	var queryStr = '';
	//alert(document.getElementById('vend').value);
	for(fe=0;fe<formNames.length; fe++)
	{
		if (fe!=0) {
			queryStr = queryStr +'&';
		}
		queryStr = queryStr +formNames[fe] + '=' + encodeURI(document.getElementById(formIds[fe]).value) ;

	};//Add Co Item
	//alert(queryStr);
	if (idx==1){
	getRequestData('../php/add_co_item.php', 'addList', queryStr);
	};
	if (idx==2){
	//alert(queryStr);
	getRequestData('../php/update_co_item.php', 'edQuickList', queryStr);
	};
	showQuickList(phpXMLObj);
}

/* Display updated add list*/
function showQuickList(phpXMLObj, pageIdx) {
	clearTable('edQuickList');
	//alert('quicklistcalled');
		colName = new Array('vend_name','item_name','item_type','item_cond'); //xml array string;
	
	coXML = phpXMLObj.getElementsByTagName("co_item");
	for (x=0 ;x < coXML.length; x++) {
		var newRow = document.getElementById("edQuickList").insertRow(-1);
		itemIdx=String(x);//
		if (pageIdx==1) {	
			//newRow.setAttribute("CoId",x );//coXML[x].getElementsByTagName("item_idx")[0].firstChild.nodeValue
			newRow.CoId = x;
			newRow.onclick = function () {rowHighlight(this)};
		//newRow.setAttribute("onClick","rowHighlight(this)");
		};
		
		for (colN=0;colN <colName.length;colN++){
		var newCell = newRow.insertCell(-1);
		
		newCellVal = coXML[x].getElementsByTagName(colName[colN])[0].firstChild;
		if (colN==4 || colN==5) {
			newCell.style.textAlign = 'right';
		}
		//alert(newCellVal);
		if (!newCellVal) {		//if null-valued;
			newCell.innerHTML = '';
			} else {	
				newCell.innerHTML = newCellVal.nodeValue;
			};
		}
	}

}

function rowHighlight(listObj) {
	var listItem = listObj.CoId;
	
	var formNames=new Array('vend_name','item_name','item_type','item_desc','item_cond','item_msrp','item_sp','item_sc','item_notes','item_idx');
var formIds=new Array('vend','item','p-type','desc','cond','msrp','saleprice','margin','notes_field','idx_fld');
	for(fId=0;fId<formNames.length; fId++)
	
	{
		if (coXML[listItem].getElementsByTagName(formNames[fId])[0].firstChild == null) 
		{
		 document.getElementById(formIds[fId]).value = '';
		} else {
			document.getElementById(formIds[fId]).value = coXML[listItem].getElementsByTagName(formNames[fId])[0].firstChild.nodeValue;
		};
	};	
}


//********   empties and clears all table data
function clearTable(tableId) {
	if(document.getElementById(tableId).rows.length != 0) {
	while (document.getElementById(tableId).rows.length!=0){
	document.getElementById(tableId).deleteRow(0);
	};
	}
}
