/***********************************************************************************
 * Class: CCommon
 * Description:
 *   Common class
 *
 ***********************************************************************************/

	function CCommon() {}
	
	// Pre-Model list
	CCommon.modelPreList = [['','Choose Car Model'],['','--------------------------']];
	CCommon.modelPostListOther = [['','--------------------------'],['__other','Other']];
	
	// Add list item
	CCommon.listAddItem = function(list, text, value) {
		// Add item
		var item = document.createElement("option");
		item.text = text;
		item.value = value;
		list.options[list.options.length] = item;
	}
	
	// Win popup
	CCommon.updateModels = function(sourceList, selectId, postList) {
		var form = sourceList.form;
		var type = form.type ? form.type.value : ''; //(form.name == "fUsedCars") ? "used" : "new";
		if(type != "new")
			type = "used";
		var destList = form.model;
		
		// Get source id and list
		var sourceId = sourceList.options[sourceList.selectedIndex].value;

		if(sourceId == '__other') {
			destList.options.length = 0;
			CCommon.listAddItem(destList, 'Other', '__other');
			
			// Set new model value if available
			if(selectId != null && selectId != '' && form.new_model)
				form.new_model.value = selectId;

			return;
		}

		// Model list is either new, used or just single array
		var list = (CCommon.modelList[type] && CCommon.modelList[type][sourceId]) ?
						CCommon.modelList[type][sourceId] : CCommon.modelList[sourceId];
		var preList = CCommon.modelPreList;
		var preLength = 0;
		
		// Clear current dest list
		destList.options.length = 0;

		// Add pre contents
		if(preList) {
			preLength = preList.length;
			for(var i = 0; i < preLength; i++) {
				var item = preList[i];
				CCommon.listAddItem(destList, item[1], item[0]);
			}
		}
		
		// If invalid value selected, return with empty list
		if(sourceId == "")
			return;
		
		if(!list) {
			CCommon.listAddItem(destList, 'None Available', '');
			//destList.selectedIndex = preLength;
		}
		else {
			if(selectId == null)
				destList.selectedIndex = 0;
			
			// Fill list with items
			for(var i = 0; i < list.length; i++) {
				var item = list[i];
				CCommon.listAddItem(destList, item[1], item[0]);
				if(selectId == item[0])
					destList.selectedIndex = preLength + i;
			}
		}
		
		if(postList	!= null) {
			if(postList == "other")
				postList = CCommon.modelPostListOther;
			preLength = destList.options.length;
			for(var i = 0; i < postList.length; i++) {
				var item = postList[i];
				CCommon.listAddItem(destList, item[1], item[0]);
				if(selectId != '' && selectId == item[0])
					destList.selectedIndex = preLength + i;
			}
		}
	}
	
	// Popup window
	CCommon.popup = function(name, url, width, height, offsetX, offsetY) {
		var x = (self.screenLeft ? self.screenLeft : self.screenX) + offsetX;
		var y = (self.screenTop ? self.screenTop + offsetY : self.screenY + offsetY + 100);
		
		window.open(url,'cc_'+name,'height='+height+',width='+width+',left='+x+',top='+y);
	}
