function startSpinner(page,line){
	if(page=='index-add'){item='addLine';}
	if(page=='index-remove'){item='p-edit-'+line;}
	if(page=='search'){item='findLi';}
	$(item).innerHTML='';
		newIMG = Item.createElement('img',{'src':'images/spinner.gif','width':'14px','height':'14px','alt':'Uploading your data...','title':'Uploading your data...'});
	$(item).appendChild(newIMG);
}

function stopSpinner(page){
	if(page=='index'){
		$('addLine').innerHTML='';
		newA = Item.createElement('a',{'onclick':'return validateForm()','href':'#addLine'});
				newIMG = Item.createElement('img',{'src':'images/add.png','title':'Add to Request','alt':'Add to Request','width':'12px','height':'12px'});
			newA.appendChild(newIMG);
		$('addLine').appendChild(newA);
	}
	if(page=='search'){
		$('findLi').innerHTML='';
			newIn = Item.createElement('input',{'type':'button','value':'Find',"onclick":"validateForm('search');",'name':'find','id':'find'});
		$('findLi').appendChild(newIn);
	}
}

function updateClosed(){
	if(xml_req.readyState == 4){
		if (xml_req.responseText == 'Your search returned no results.'){
			$('results').innerHTML='';
				newTD = Item.createElement('td',{'colspan':'3',"text":xml_req.responseText});
			$('results').appendChild(newTD);
		}
		else {
			var response = eval("(" + xml_req.responseText + ")");
			$('results').innerHTML='';
			for(i=0;i < response.data.info.length; i++) {
				line = i+1;
				newTR = Item.createElement('tr',{"id":'line'+response.data.info[i].id});
						lineTD = Item.createElement('td',{"text":line+')'});
					newTR.appendChild(lineTD);
						repairTD = document.createElement('td');
						if(response.data.info[i].repair!=''){
							repairA = Item.createElement('a',{'style':'padding-right: 5px;','target':'_blank','href':'http://salsa.corp.apple.com/psp/gcrmp_4/EMPLOYEE/CRM/c/APC_DISPATCH.APC_REPAIR.GBL?Page=APC_REPAIR&BUSINESS_UNIT=AC001&AP_DISPATCH_ID='+response.data.info[i].repair,'title':'Launch Repair'});
								repairIMG = Item.createElement('img',{'src':'images/rLaunch.gif','width':'16px','height':'16px','alt':'Launch Repair'});
								repairA.appendChild(repairIMG);
							repairTD.appendChild(repairA);
						}
						repairTD.innerHTML+=response.data.info[i].repair;
					newTR.appendChild(repairTD);
						date = response.data.info[i].date;
						dateA = date.split(' ');
						big = dateA[0].split('-');
						small = dateA[1].split(':');
						hour = small[0];
						if(hour>12){
							hour = hour-12;
							end='pm';
						}
						else{
							end='am';
						}
						dateTD = Item.createElement('td',{"class":"centerText","text":big[1]+'/'+big[2]+'/'+big[0]+' '+hour+':'+small[1]+' '+end});
					newTR.appendChild(dateTD);
				$('results').appendChild(newTR);	
			}
			staticData.parse("contentTable", 1);
			sortTable("contentTable");
		}
	stopSpinner('search');
	}
}

function updateSearch(){
	if(xml_req.readyState == 4){
		if (xml_req.responseText == 'Your search returned no results.'){
			$('results').innerHTML='';
				newTD = Item.createElement('td',{"colspan":"6","text":xml_req.responseText});
			$('results').appendChild(newTD);
		}
		else if (xml_req.responseText == 'Your search returned an error.'){
			$('results').innerHTML='';
				newTD = Item.createElement('td',{"colspan":"6","text":xml_req.responseText});
			$('results').appendChild(newTD);
		}
		else {
			var response = eval("(" + xml_req.responseText + ")");
			$('results').innerHTML='';
			for(i=0;i < response.data.info.length; i++) {
				line = i+1;
					newTR = Item.createElement('tr',{"id":"line"+response.data.info[i].id});
						lineTD = document.createElement('td',{"text":line+")"});
					newTR.appendChild(lineTD);
						partTD = Item.createElement('td',{"text":response.data.info[i].part});
					newTR.appendChild(partTD);
						repairTD = document.createElement('td');
						if(response.data.info[i].repair!=''){
							repairA = Item.createElement('a',{"style":"padding-right: 5px;","target":"_blank","href":"http://salsa.corp.apple.com/psp/gcrmp_4/EMPLOYEE/CRM/c/APC_DISPATCH.APC_REPAIR.GBL?Page=APC_REPAIR&BUSINESS_UNIT=AC001&AP_DISPATCH_ID="+response.data.info[i].repair,"title":"Launch Repair"});
								repairIMG = Item.createElement('img',{"src":"images/rLaunch.gif","width":"16px","height":"16px","alt":"Launch Repair"});
								repairA.appendChild(repairIMG);
							repairTD.appendChild(repairA);
						}
						repairTD.innerHTML+=response.data.info[i].repair;
					newTR.appendChild(repairTD);
						batchTD = Item.createElement('td',{"class":"centerText","text":response.data.info[i].batch});
					newTR.appendChild(batchTD);
						boxTD = document.createElement('td',{"class":"centerText","text":response.data.info[i].box});
					newTR.appendChild(boxTD);
					temp1 = response.data.info[i].added.split(' ');
					temp2 = temp1[0].split('-');
					year = temp2[0];
					month = temp2[1];
					day = temp2[2];
						addedTD = Item.createElement('td',{"class":"centerText","text":month+'-'+day+'-'+year});
					newTR.appendChild(addedTD);
				$('results').appendChild(newTR);
			}
			staticData.parse("contentTable", 0);
			sortTable("contentTable");
		}
		stopSpinner('search');
	}
}

function clearForm(){
	$('part').value='';
	$('repair').value='';
	for (i=0;i<$('form').keep.length;i++){
		if($('form').keep[i].checked){	
			if($('form').keep[1].checked){
				$('box').value='';
				$('batch').selectedIndex=0;
			}
		}
	}
}

function addRow(){
	if (xml_req.readyState == 4) {
		if (xml_req.responseText!=''){
			newTR = Item.createElement('tr',{"id":"line"+xml_req.responseText});
				lineTD = Item.createElement('td',{"text":lineCount+")"});
					newTR.appendChild(lineTD);
				partsTD = Item.createElement('td',{"id":"p"+xml_req.responseText,"text":$('part').value});
					newTR.appendChild(partsTD);
				repairTD = Item.createElement('td',{"id":"r"+xml_req.responseText});
					if($('repair').value!=''){
						repairA = Item.createElement('a',{"style":"padding-right: 5px;","target":"_blank","href":"http://salsa.corp.apple.com/psp/gcrmp_4/EMPLOYEE/CRM/c/APC_DISPATCH.APC_REPAIR.GBL?Page=APC_REPAIR&BUSINESS_UNIT=AC001&AP_DISPATCH_ID="+$('repair').value,"title":"Launch Repair"});
							repairIMG = Item.createElement('img',{"src":"images/rLaunch.gif","width":"16px","height":"16px","alt":"Launch Repair"});
							repairA.appendChild(repairIMG);
						repairTD.appendChild(repairA);
					}
					repairTD.innerHTML += $('repair').value;
					newTR.appendChild(repairTD);
				batchTD = Item.createElement('td',{"id":"ba"+xml_req.responseText,"class":"centerText","text":$('batch').value});
					newTR.appendChild(batchTD);
				boxTD = Item.createElement('td',{"id":"b"+xml_req.responseText,"class":"centerText","text":$('box').value});
					newTR.appendChild(boxTD);
				editTD = Item.createElement('td',{"class":"centerText"});
					editP = Item.createElement('p',{"id":"p-edit-"+xml_req.responseText});
						editTD.appendChild(editP);
					editA = Item.createElement('a',{"href":"#line"+xml_req.responseText,"title":"Remove Line","onclick":"return edit('line-"+xml_req.responseText+"')"});
							editIMG = Item.createElement('img',{"src":"images/remove.png","width":"12px","height":"12px","alt":"Remove Entry","title":"Remove Entry"});
							editA.appendChild(editIMG);
						editP.appendChild(editA);
					newTR.appendChild(editTD);
			$('results').appendChild(newTR);
			clearForm();
			lineCount++;
			staticData.parse("contentTable", 0);
			sortTable("contentTable");
			$('part').focus();
			stopSpinner('index');
		}
		else {
			alert('Ajax not supported/working. Your item was not added, please contact Calvin K. Cox @ ccox@apple.com');
		}
	}
}

function removeRow(){
	if (xml_req.readyState == 4){
		if (xml_req.responseText==''){
			parent = $('line'+removeID).parentNode;
			parent.removeChild($('line'+removeID));
			lineCount = lineCount-1;
			staticData.parse("contentTable", 0);
			sortTable();
		}
		else{
			alert('Ajax not supported/working. Your item was not removed, please contact Calvin K. Cox @ ccox@apple.com');
		}
	}
}

function edit(line){
	splitLine = line.split("-");
	line = splitLine[1];
	part = $('p'+line).innerHTML;
	box = $('b'+line).innerHTML;
	batch = $('ba'+line).innerHTML;
	$('p-edit-'+line).innerHTML='';
		newA = Item.createElement("a",{"href":"#line"+line,"title":"Remove Entry","onclick":"return remove('"+line+"','"+part+"','"+box+"','"+batch+"'); startSpinner('index-remove','"+line+"')","text":"remove"});
		$('p-edit-'+line).appendChild(newA);
		$('p-edit-'+line).innerHTML+=" | ";
		newA2 = Item.createElement("a",{"href":"#line"+line,"title":"Cancel","onclick":"return cancel('"+line+"')","text":"cancel"});
		$('p-edit-'+line).appendChild(newA2);
	return false;
}

function cancel(line){
	$('p-edit-'+line).innerHTML='';
	editA = Item.createElement('a',{"href":"#line"+line,"title":"Remove Line","onclick":"return edit('line-"+line+"')"});
		editIMG = Item.createElement('img',{"src":"images/remove.png","width":"12px","height":"12px","alt":"Remove Entry","title":"Remove Entry"});
		editA.appendChild(editIMG);
	$('p-edit-'+line).appendChild(editA);
	return false;
}

function closed(){
	if (xml_req.readyState == 4){
		if (xml_req.responseText==''){
			$('repair').value='';
			$('content').innerHTML='';
			div = Item.createElement('div',{"style":"font-size: 20px;text-align:center;position:relative;top:45px;","text":"Thanks for Submitting!"});
			$('content').appendChild(div);
		}
		else{
			alert('Ajax not supported/working. Your item was not removed, please contact Calvin K. Cox @ ccox@apple.com');
		}
	}
}