
function isIE55() {
	//Detect IE5.5+
	var version=0;
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		var temp=navigator.appVersion.split("MSIE");
		version=parseFloat(temp[1]);
	}

	if (version>=5.5) //NON IE browser will return 0
		return true;
	return false;
}

function trycursor(cur_url,restrict) {

                document.body.style.cursor = "url(" + cur_url + ")";
                //alert( 'Sorry! It looks like you are not using IE5.5 or greater.\nYou must be using IE5.5 or greater to use this feature. \nPlease see our help area if you need assistance.\n\nNote: Your MySpace friends will still be able to see this cursor\non your page as long as they are using IE5.5.' );
}



function debugFailFunc( responseText, localArgs ) {
	alert( responseText );
}

function debugSuccess( responseText, localArgs ) {
	alert( responseText );
}
 
function silentSuccess() {
	return true;
}

function ratedSuccess( responseText, localArgs ) {
	var elem = document.getElementById(localArgs+"_status");
	elem.innerHTML = 'Thanks!';
	elem.style.display = 'block';
	/*elem.style.backgroundColor = '#f9f9f9';
	elem.style.border = '1px solid black';
	var span = document.createElement('span');
	
	var item = document.createTextNode('Thanks!');
	span.appendChild(item);
	span.style.fontSize = '12px';
	span.style.fontWeight = 'bold';
	elem.appendChild( span ); */
}

var star_main_src = "/images/bigstar-dark.gif";
var star_mo_src = "/images/bigstar-bright.gif";	
var star_half_src = "/images/bigstar-both-half.gif";
/*var stat_button_src = "/images/ibutton2.gif"; */
var mouse_over_star = false;
var timer_started = false;
var g_a = new Array('x','y','z'); // dummy values shouldn't affect anything
var affil_done = false;

document.grabbed_items = g_a;

Array.find = function(ary, element){
    for(var i=0; i<ary.length; i++){
        if(ary[i] == element){
            return i;
        }
    }
    return -1;
}

function checkAffilURL( site_elm_id ) {
	
	var site_url = document.getElementById( site_elm_id ).value.toLowerCase();

	var valid_url = (site_url.indexOf(".com")!=-1) || 
			(site_url.indexOf(".net")!=-1) || 
			(site_url.indexOf(".org")!=-1) ||
			(site_url.indexOf(".biz")!=-1) ||
			(site_url.indexOf(".us")!=-1);

	if (site_url == '' || !valid_url) {
		alert( 'Please enter a valid site URL from which you will distribute this toy.' );
		return false;
	} else {
		return true;
	}	
}

function ajaxNewAffil( aff_id, site_elm_id  ) {
	if (!affil_done) {
		var site_url = document.getElementById( site_elm_id ).value.toLowerCase();
		var args = 'id='+aff_id;
		ajaxCall( escape(site_url), 'newaffil', args, silentSuccess, silentSuccess, false );
	} else {
		// already rgistered.
	}
}

function pc( p_id ) {
	var args = 'id='+p_id;
	ajaxCall( 'pc', 'pc', args, silentSuccess, silentSuccess, false );	
}

function ajaxGrab( table, content_id ) {
	var arr = document.grabbed_items;
	// if hte user hasn't already grabbed it
	if (Array.find(arr,content_id+'_grabbed') == -1) {
		var args = 'id='+content_id;
		
		ajaxCall( table, 'grab', args, silentSuccess, silentSuccess, false );
		
		// don't let people send multiple grab records (at least prevent most of them);
		document.grabbed_items[document.grabbed_items.length] = content_id + "_grabbed";
	}
}

function ajaxUpdateRating( table, rating, content_id, the_container_id ) {
	
	var args = "id="+content_id+"&"+"p1="+rating;
	
	// disable the spring-back thingy.s
	var statuselem = document.getElementById(the_container_id+"_status");
	statuselem.style.display = "block";
	
	var elem = document.getElementById(the_container_id);
	elem.onmouseout = function () { return false; };
	
	
	//	ajaxCall( 'layouts', 'rate', args, debugSuccess, debugFailFunc, args );	
	/* disable any further mouseovers and clicks */
	for(var i=1;i<=5;i++) {
		var the_id = the_container_id+'_star_'+i;
		document.getElementById(the_id).onmouseover = function() { return false; };
		document.getElementById(the_id).onmouseout = function() { return false; };
		document.getElementById(the_id).onclick = function() { return false; };
		if (i<=rating)
			document.getElementById(the_id).src=star_mo_src; 
		else
			document.getElementById(the_id).src=star_main_src; 
	}
	//alert("I will set a rating of "+rating+" on layout id " + content_id + ' by using ajax.');
	ajaxCall( table, 'rate', args, ratedSuccess, silentSuccess, the_container_id );
}

function springBack( the_container ) {
	var the_container_elem = document.getElementById(the_container);
	//alert(mouse_over_star?'true':'false');
	if (!mouse_over_star) {
		setStar(the_container, the_container_elem.actual_rating);
		//console.info('set it back');
	}
	timer_started = false;		
}

function setStar( the_container_id, rating ) {
	for(var i=1;i<=5;i++) {
		var the_id = the_container_id+'_star_'+i;

		if (rating >= i ) {
			document.getElementById(the_id).src=star_mo_src;
		} else {
			if (rating >= (i-0.750)) {
				document.getElementById(the_id).src=star_half_src;
			} else {
				document.getElementById(the_id).src=star_main_src;
			}
		}
	}	
}

function setupStars( table, the_container_id, the_item_id, stat_fn, actual_rating ) {
	var use_stat_button = true;
	// this is so individual pages can override the default behavior.
	if (document.use_stat_button && document.use_stat_button == 'no')
		use_stat_button = false;
		
	var cont = document.getElementById( the_container_id );
	cont.style.display="block";
	cont.actual_rating = actual_rating;

	for(var i=0;i<5;i++) {
		var img = document.createElement('img');
		var rating = i+1;
		img.id = the_container_id+"_star_"+rating;
		img.src = star_main_src;
		img.width = 30;
		img.height = 30;
		img.rating = rating;
		
		img.onmouseover = function(e) { 
			mouse_over_star = true;
			if (!e) var e = window.event;
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
			
			setStar(the_container_id, this.rating);
		};
		img.onmouseout = function(e) {
			mouse_over_star = false;
			
			if (!timer_started)
				setTimeout("springBack('"+the_container_id+"')",300);
			timer_started = true;
			
			if (!e) var e = window.event;
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
			
			setStar(the_container_id, this.rating);
		};
		
		img.onclick = function(e) { ajaxUpdateRating( table, this.rating, the_item_id, the_container_id); };
		cont.appendChild( img );
	}
	
	
/*	var status = document.createElement( 'span' );
	status.style.fontSize = "11px";
	status.style.display = 'none';
	status.id = the_container_id + '_status';
	status.appendChild( document.createTextNode('Working!'));
	cont.appendChild( status ); */
	
}
