function rewriteURL(init1, init2, init3) {
	var searchForm = document.getElementById('searchForm');
	var searchField = document.getElementById('searchField');
	var make = document.getElementById('make');
	var style = document.getElementById('style');
	
	if (searchField.value!=init1 && searchField.value!="") searchForm.action +='search:' +urlencode(searchField.value.replace(/\./g," "));
	if (make.value!=init2) searchForm.action +='.make:' +urlencode(make.value.replace(/\./g,"~"));
	if (style.value!=init3) searchForm.action +='.style:' +urlencode(style.value);
	window.location=searchForm.action;
}

function removeVal(field, name) {
	var field = document.getElementById(field);
	if(field.value == name) {
		field.value = '';
		field.style.color='#353535';
	}
}
function restoreVal(field, name) {
	var field = document.getElementById(field);
	if(field.value == '') {
		field.value = name;
		field.style.color='#A8A8A8';
	}
}


function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                                     
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}

function draw_video_player(youtube_id) {
	if (navigator.appVersion.indexOf("Mac")!=-1) {
		var v1 = "";
		var v2 = "";
	} else {
		var v1 = "<param name=\"wmode\" value=\"transparent\"></param>";
		var v2 = "wmode=\"transparent\" ";

	}

	var video = "<object width=\"425\" height=\"344\">"+v1+"<param name=\"movie\" value=\"http://www.youtube.com/v/"+youtube_id+"&autoplay=1&hl=en&rel=0\"></param><embed src=\"http://www.youtube.com/v/"+youtube_id+"&autoplay=1&hl=en&rel=0\" type=\"application/x-shockwave-flash\" "+v2+"width=\"425\" height=\"344\"></embed></object>";
	return video;
}

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

// Detail page voting functions:

function flasher(){
  $('.ratingImgs').EffectChain({duration:0.01, effect:"fadeTo",fadeToOpacity:1,onStep:function(){
	  $('.rate').find("img").each(
		function(i) {
		   $(this).animate
			({opacity: "0.3"}, { queue:false, duration:2000} );
		});			 
	} 
  });
  t = window.setTimeout(flasher, 3000);
}

function stopFlasher(){
	clearTimeout(t);
}

function startFlasher() {
	$('.rate').find("img").each(
	function(i) {
	   $(this).css("opacity","0.3");
	});	
	//t = window.setTimeout(flasher, 3000);
}

function overFlash(id, count, flasher_id) {
	//stopFlasher();
	$('#'+flasher_id).find("img").each(
	function(i) {
	   $(this).stop();
	   $(this).css("opacity","1");
	   return (this.id != id.id);
	});		  
}

function outFlash(id, count, flasher_id) {
	if (document.getElementById('f').value==0) {
	startFlasher();
	} else {
		$('#'+flasher_id).find("img").each(
		function(i) {
		   $(this).css("opacity","0.3");
		});	
		if (document.getElementById(flasher_id+'_h').value>0) {
			$('#'+flasher_id).find("img").each(
			function(i) {
			   $(this).stop();
			   $(this).css("opacity","1");
			   return (this.id != document.getElementById(flasher_id+'_h').value);
			});
		}		  
	}
}

function clickFlash(id, count, flasher_id, qcount, lh) {
	stopFlasher();
	$(id).animate ({opacity: "0"}, { duration:50} );
	$(id).animate ({opacity: "1"}, { duration:50} );
	$(id).animate ({opacity: "0"}, { duration:50} );
	$(id).animate ({opacity: "1"}, { duration:50} );
	lightUp(qcount, lh);
	document.getElementById('f').value=1;
	document.getElementById(flasher_id+'_h').value=count;
}

function verifySelect(id, count, init, lh) {
	var id=document.getElementById(id);
	if (id.value==init) {
		$('#check_'+count).attr({src: lh+'/images/check_nok.png'});
		$('#check_'+count).css("display","block");
		return false;
	} else {
		$('#check_'+count).attr({src: lh+'/images/check_ok.png'});
		$('#check_'+count).css("display","block");
		return true;
	}
}

function verifyMSelect(id, count, init, lh) {
	
	var id1=document.getElementById('s1');
	var id2=document.getElementById('s2');
	var id3=document.getElementById('s3');
	
	if (id1.value==init && id2.value==init && id3.value==init) {
		$('#check_'+count).attr({src: lh+'/images/check_nok.png'});
		$('#check_'+count).css("display","block");
		return false;
	} else {
		$('#check_'+count).attr({src: lh+'/images/check_ok.png'});
		$('#check_'+count).css("display","block");
		return true;
	}
}

function verifyInput(id, count, init, lh) {
	var mailcheck = "ok";
	var robotcheck = "ok";
	if (id=="email") { 
		var id=document.getElementById(id);
		var mailcheck = checkValidation(id);
	} else if (id=="robot") {
		var id=document.getElementById(id);
		var n1=document.getElementById('o');
		var n2=document.getElementById('t');
		var rsum = eval(n1.value) + eval(n2.value);
		if (rsum!=eval(id.value)) robotcheck = "nok";
	} else {
		var id=document.getElementById(id);
	}
	if (id.value=="" || mailcheck!="ok" || robotcheck!="ok") {
		if (mailcheck!="ok") {
			$('#emailmsg').html(mailcheck);
			$('#emailmsg').css("display","block");
		}
		$('#check_'+count).attr({src: lh+'/images/check_nok.png'});
		$('#check_'+count).css("display","block");
		return false;
	} else {
		$('#emailmsg').css("display","none");
		$('#check_'+count).attr({src: lh+'/images/check_ok.png'});
		$('#check_'+count).css("display","block");
		return true;
	}
}

function lightUp(count, lh) {
	$('#check_'+count).attr({src: lh+'/images/check_ok.png'});
	$('#check_'+count).css("display","block");
}
function darken(count, lh) {
	$('#check_'+count).attr({src: lh+'/images/check_nok.png'});
	$('#check_'+count).css("display","block");
}
function checkAll(lh) {
	var experience = verifySelect('experience', 1, "Select an option:", lh);
	var title = verifyInput('title', 2, '', lh);
	var f1_h = document.getElementById('f1_h').value; var f1;
	if (f1_h>0) { lightUp(3, lh); f1 = true; } else { darken(3, lh); f1 = false; }
	var f2_h = document.getElementById('f2_h').value; var f2;
	if (f2_h>0) { lightUp(4, lh); f2 = true; } else { darken(4, lh); f2 = false; }
	var f3_h = document.getElementById('f3_h').value; var f3;
	if (f3_h>0) { lightUp(5, lh); f3 = true; } else { darken(5, lh); f3 = false; }
	var f4_h = document.getElementById('f4_h').value; var f4;
	if (f4_h>0) { lightUp(6, lh); f4 = true; } else { darken(6, lh); f4 = false; }
	var nick = verifyInput('nick', 7, '', lh);
	//var email = verifyInput('email', 8, '', lh);
	var robot = verifyInput('robot', 11, '', lh);
	var mexperience = verifySelect('mexperience', 9, 'Select an option:', lh);
	var style = verifyMSelect('', 10, "Select Playing Style:", lh);

	if (experience && title && f1 && f2 && f3 && f4 && nick && mexperience && style && robot) {
		$("#reviewform").submit();
		// jQuery.facebox('Would submit review');
	} else {
		jQuery.facebox('<span style="font-weight:bold; color:red; font-size:16px;">Errors detected.</span> <br/><br/>Please verify fields marked with <img src="'+lh+'/images/check_nok.png" width=40 height=41 /> sign. Thank you.');
	}
}
function checkValidation(formInput) {

    if (typeof(formInput) != "object") {
        alert("Validation not supported on this browser.");
        return(false);
    }

    var message;

    if (stringEmpty(formInput.value)) {
        message = "Error! There is no input value entered.";
    } else if (noAtSign( formInput.value )) {
        message = "Error! The address \"" + formInput.value + "\" does not contain an '@' character.";
    } else if (nothingBeforeAt(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" must contain at least one character before the '@' character";
    } else if (noLeftBracket(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" contains a right square bracket ']',\nbut no corresponding left square bracket '['.";
    } else if (noRightBracket(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" contains a left square bracket '[',\nbut no corresponding right square bracket ']'.";
    } else if (noValidPeriod(formInput.value)) {
        message = "Error! The address \"" + formInput.value + "\" must contain a period ('.') character.";
    } else if (noValidSuffix(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" must contain a two, three or four character suffix.";
    } else {
		message = "";	
	}

    var objType = typeof(formInput.focus);
    if (objType == "object" || objType == "function") {
         formInput.focus();
    }
	if (message!='') {
    	return(message);
    } else {
	    return ("ok");
	}
}

function stringEmpty (formField) {
    // CHECK THAT THE STRING IS NOT EMPTY
    if ( formField.length < 1 ) {
        return ( true );
    } else {
        return ( false );
    }
}

function noAtSign (formField) {
    // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
    if (formField.indexOf ('@', 0) == -1) {
        return ( true )
    } else {
        return ( false );
    }
}

function nothingBeforeAt (formField) {
    // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
    if ( formField.indexOf ( '@', 0 ) < 1 ) {
        return ( true )
    } else {
        return ( false );
    }
}

function noLeftBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
    if ( formField.indexOf ( '[', 0 ) == -1 && formField.charAt (formField.length - 1) == ']') {
        return ( true )
    } else {
        return ( false );
    }
}

function noRightBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
    if (formField.indexOf ( '[', 0 ) > -1 && formField.charAt (formField.length - 1) != ']') {
        return ( true );
    } else {
        return ( false );
    }
}

function noValidPeriod (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf ( '@', 0 ) > 1 && formField.charAt (formField.length - 1 ) == ']')
        return ( false );

    // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
    if (formField.indexOf ( '.', 0 ) == -1)
        return ( true );

    return ( false );
}

function noValidSuffix(formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf('@', 0) > 1 && formField.charAt(formField.length - 1) == ']') {
        return ( false );
    }

    // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
    var len = formField.length;
    var pos = formField.lastIndexOf ( '.', len - 1 ) + 1;
    if ( ( len - pos ) < 2 || ( len - pos ) > 4 ) {
        return ( true );
    } else {
        return ( false );
    }
}

function clearTArea(id, init) {
	if ($('#'+id).val() == init) $('#'+id).val('');
}

function toggleReviewForm() {
	$('#writeareview').toggleClass('writereviewb');
    $('#writeareview').toggleClass('writereviewbgrey');
	$('#submitreview').slideToggle('slow');
	$('#noreviews').slideToggle('fast');
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function disBar() {
	var name = 'disablebar';
	var value = 'true';
	var days = '1';
	
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	
	$('#backBar').css("display","none");
	$('#masterwrapper').css("position","relative");
	$('#masterwrapper').css("top","0");
	$('#masterwrapper').css("display","block");
}
