function autoUpdatesp(obj) {
	if (obj.checked) {
		updateSomeSpFunction(allspurl, newspurl, updatesptimes, transferurl,
				true);
	}
}

function updateSomeSpFunction(allspurl, newspurl, updatesptime, sptransferurl,
		isLoop) {

	if (isLoop) {
		var autoupdateobj = $('autoupdatespid');
		if (autoupdateobj == null) {
			return;
		}
		var autoupdatespchecked = autoupdateobj.checked;
		if (!autoupdatespchecked) {
			return;
		}
	}
	try {

		var req = new Request( {
			url : sptransferurl,
			method : 'get',
			onSuccess : function(responseText) {
				if (responseText == null||responseText.trim()=='') {
					if (isLoop) {
						window.setTimeout( function() {
							updateSomeSpFunction(allspurl, newspurl,
									updatesptime, sptransferurl, isLoop);
						}, updatesptime);
					}
				} 

				var sphash = JSON.decode(responseText);
				var minVersion = sphash.minVersion;
				if (minVersion == null) {

					if (isLoop) {
						window.setTimeout( function() {
							updateSomeSpFunction(allspurl, newspurl,
									updatesptime, sptransferurl, isLoop);
						}, updatesptime);
					}

					return;
				}
				var maxVersion = sphash.maxVersion;
				var oldversion = $('spversionid').value;

				if (maxVersion == oldversion) {

					if (isLoop) {
						window.setTimeout( function() {
							updateSomeSpFunction(allspurl, newspurl,
									updatesptime, sptransferurl, isLoop);
						}, updatesptime);
					}
					return;
				}
				if (minVersion <= oldversion + 1 && oldversion < maxVersion) {
					var matchs = sphash.matchs;
					setTimeUpdateSomeSpFunction(matchs, oldversion);
					oldversion = maxVersion;
					$('spversionid').value = oldversion;
					if (isLoop) {
						window.setTimeout( function() {
							updateSomeSpFunction(allspurl, newspurl,
									updatesptime, sptransferurl, isLoop);
						}, updatesptime);
					}
				} else {
					//alert('oldversion:'+oldversion+'minVersion:'+minVersion+'maxVersion:'+maxVersion+'time:'+new Date());
					updateAllSpFunction(allspurl, newspurl, updatesptime,
							sptransferurl, isLoop);
				}
			},
			onFailure : function(instance) {
				if (isLoop) {
					window.setTimeout( function() {
						updateSomeSpFunction(allspurl, newspurl, updatesptime,
								sptransferurl, isLoop);
					}, updatesptime);
				}
			}
		}).send('url=' + newspurl + '?noCache=' + new Date().getTime());
	} catch (ex) {
		alert(ex);
	}
}

function updateAllSpFunction(allspurl, newspurl, updatesptime, sptransferurl,
		isLoop) {
	var req = new Request( {
		url : sptransferurl,
		method : 'get',
		onSuccess : function(responseText) {
			if (responseText == null) {
				return;
			} else if (responseText.trim() == '') {
				return;
			}

			var sphash = JSON.decode(responseText);

			var version = sphash.version;
			if (version == null) {
				return;
			}
			var matchs = sphash.matchs;
			setTimeUpdateAllSpFunction(matchs);
			$('spversionid').value = version;

			if (isLoop) {
				window.setTimeout( function() {
					updateSomeSpFunction(allspurl, newspurl, updatesptime,
							sptransferurl, isLoop);
				}, updatesptime);
			}
		},
		onFailure : function(instance) {
			if (isLoop) {
				window.setTimeout( function() {
					updateSomeSpFunction(allspurl, newspurl, updatesptime,
							sptransferurl, isLoop);
				}, updatesptime);
			}
		}
	}).send('url=' + allspurl + '?noCache=' + new Date().getTime());
}

function setTimeUpdateSomeSpFunction(matchs, oldversion) {
	if (matchs == null || matchs.length == 0) {
		return;
	}
	var match = matchs[0];
	if (match.version > oldversion) {
		var spspan = $('spspan_' + match.lineId + '_' + match.index);
		if (spspan != null) {
			var isupdate = spspan.getProperty('_isupdate');
			if (isupdate == 'true') {
				var tempsp = match.sp;
				spspan.setProperty('text', tempsp.toFixed(2));
			}
		}
	}
	matchs.erase(match);
	if (matchs.length % 10 == 0) {
		window.setTimeout( function() {
			setTimeUpdateSomeSpFunction(matchs, oldversion);
		}, 200);
	} else {
		setTimeUpdateSomeSpFunction(matchs, oldversion);
	}
}

function setTimeUpdateAllSpFunction(matchs) {
	if (matchs == null || matchs.length == 0) {
		return;
	}
	var match = matchs[0];
	var sps = match.sp;
	sps.each( function(sp, index) {
		var spspan = $('spspan_' + match.lineId + '_' + index);
		if (spspan == null) {
			return;
		}
		var isupdate = spspan.getProperty('_isupdate');
		if (isupdate == 'true') {
			var tempsp = sp;
			spspan.setProperty('text', tempsp.toFixed(2));
		}
	});
	matchs.erase(match);
	if (matchs.length % 2 == 0) {
		window.setTimeout( function() {
			setTimeUpdateAllSpFunction(matchs);
		}, 50);
	} else {
		setTimeUpdateAllSpFunction(matchs);
	}

}
