//Create an option to request 
function createRequestObject() {
var req = false;
	if(window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = false;
		}
	} else if (window.ActiveXObject) { 
		try {
			req =  new ActiveXObject("Msxml2.XMLHTTP");
	    }catch(e){
			try {
		   		req = new ActiveXObject("Microsoft.XMLHTTP");
    		}catch(e){
				req = false;
			}
		}	
	}

	return req;
}
var http = createRequestObject();

function callInProgress(http) {
    switch ( http.readyState ) {
        case 1, 2, 3:
            return true;
        break;
        // Case 4 and 0
        default:
            return false;
        break;
    }
	return false;
}

function RefreshFiles(id) {
	http.open('GET', 'ajax.php?action=GetFiles&id='+id,true);
	http.onreadystatechange = parseResults;
	if (!callInProgress(http) ) {
		http.send(null);
	}
}

function parseResults() {
    if (http.readyState == 4) {
        var response = http.responseText;
		var responseData;
		var models = new Array();
		var id_value = new Array();
		var option;
		if (response.length > 0) {
			responseData = response.split('|||')
			if (responseData[0] == 'photolist') {
				document.getElementById('files').innerHTML = responseData[1];
			} else if (responseData[0] == 'modellist') {
				clearList(document.getElementById('ModelID'));
				if (responseData[1].indexOf('|' != -1)){
					option = new Option(' - Select a value - ', '0');
					document.getElementById('ModelID').add(option, document.getElementById('ModelID').length);
					models = responseData[1].split('||');
					for(i=0; i<models.length; i++) {
						id_value = models[i].split('|');
						option = new Option(id_value[0], id_value[1]);
						document.getElementById('ModelID').add(option, document.getElementById('ModelID').length);
					}
					document.inventform.ModelID.disabled = false;
				} else {
					option = new Option(' - Select a Manufacturer - ', '0');
					document.getElementById('ModelID').add(option, document.getElementById('ModelID').length);
					document.getElementById('ModelID').disabled = true;
				}
			}
		}
	}
}

function DelFile(id) {
	if (confirm("Are you sure you want to delete this photo?")) {
		http.open('GET', 'ajax.php?action=DeleteFile&id='+id,true);
		http.onreadystatechange = parseResults;
		if (!callInProgress(http) ) {
			http.send(null);
		}
	}
}

function SaveCaption(id) {
	var caption = document.getElementById('caption_'+id).value;
	var url = 'ajax.php?action=SaveCaption&id='+id+'&caption='+escape(caption);
	http.open('GET', url, true);
	document.getElementById('capdiv_'+id).innerHTML = caption;
	http.onreadystatechange = parseResults;
	if (!callInProgress(http) ) {
		http.send(null);
	}
}

function EditCaption(id) {
	var caption = document.getElementById('capdiv_'+id).innerHTML;
	if (document.getElementById('caption_'+id) == null) {
		var editBox = '<input type="text" id="caption_'+id+'" value="'+caption+'" size="40" maxchars="100" onBlur="SaveCaption(\''+id+'\');">';
		document.getElementById('capdiv_'+id).innerHTML = editBox;
		document.getElementById('caption_'+id).focus();
	}
}

function clearList(list){
	while (list.length > 0){
		list.remove(0);
	}
}

function addFileWindow(id) {
	var width='300';
	var height='135';
	var location = "addfile.php";
	if (id > 0 || id != '') {
		location += '?id='+id;	
	}
	var left = Math.floor((screen.width - width) / 2);
	var top = Math.floor((screen.height - height) / 2);
	var options='width='+width+',height='+height+',left='+left+',top='+top+',status=no';	
	var popupwin = window.open(location,'popup',options);
}

function RetrieveModels() {
	var ManufacturerID = document.getElementById('ManufacturerID').options[document.getElementById('ManufacturerID').selectedIndex].value;
	http.open('GET', 'ajax.php?action=GetModels&ManufacturerID='+ManufacturerID,true);
	http.onreadystatechange = parseResults;
	if ( !callInProgress(http) ) {
		http.send(null);
	}
}

function swapImages(src, width, height, caption) {
	document.getElementById('picture').src = src;
	document.getElementById('picture').width = width;
	document.getElementById('picture').height = height;
	document.getElementById('picture').alt = caption;
	document.getElementById('caption').innerHTML = caption;
}