/*
 * Gets a element by id
 */
function $(id) {
	return document.getElementById(id);
}

/*
 * Gets a element by id
 */
function $v(id) {
	return document.getElementById(id).value;
}
 
/*
 * Gets a element by id
 */
function $s(id, str) {
	document.getElementById(id).value = str;
}
 
/*
 * Sets a value to element as inner HTML
 */
function $h(id, str) {
	document.getElementById(id).innerHTML = str;
}

/*
 * Clears value in text field 
 */
function clsValue(obj) {
	obj.value = "";
}

/*
 * Trim string
 */
 function trim (str) {
	return(str.replace(/^\s+|\s+$/g, ""));
}

/* 
 * Count the text length 
 */
function txCount(field, cntfield, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	} else {
		document.getElementById(cntfield).value = maxlimit - field.value.length;
	}
}

/*
 * display loading
 */
function displayloading(action) {
	if (action == 1) {
		var width = document.documentElement.scrollWidth;
		
		var layer = document.createElement("div");
		layer.style.zIndex = 100;
		layer.id = "layer";
		layer.style.position = "absolute";
		layer.style.top = "0px";
		layer.style.left = "0px";
		layer.style.height = document.documentElement.scrollHeight + "px";
		layer.style.width = width + "px";
		layer.style.backgroundColor = "white";
		layer.style.opacity = "0.6";
		layer.style.filter += ("progid:DXImageTransform.Microsoft.Alpha(opacity=60)");
		document.body.appendChild(layer);
		
		var div = document.createElement("div");
		div.style.zIndex = 101;
		div.id = "loader";
		div.style.position = (navigator.userAgent.indexOf("MSIE 6") > -1) ? "absolute" : "fixed";
		div.style.top = (document.documentElement.scrollHeight / 2) + "px";
		div.style.left = (width / 2) + "px";
		document.body.appendChild(div);
		
		var img = document.createElement("img");
		img.src = "/images/loading.gif";
		div.appendChild(img);
		
	} else {
		document.body.removeChild(document.getElementById('layer'));
		document.body.removeChild(document.getElementById('loader'));
	}
}

/*
 * wrap text
 */
function wrapText(mystring, desiredlength) {
	var result = "";
	
	var tokens = mystring.split("<br/>");
	for (var i = 0 ; i < tokens.length ; i++) {
		if (trim(tokens[i]).length < 1) {
			result += "<br/>";
			
		} else {
			var token = tokens[i].split(" ");
			var rs = "";
			
			for (var j = 0 ; j < token.length ; j++) {
				if (token[j].length > desiredlength) {
					var count = 0;
					
					while (count < token[j].length) {
						if ((count + desiredlength) < token[j].length) rs += token[j].substr(count, desiredlength) + "<br/>";
						else rs += token[j].substr(count, desiredlength);
						count += desiredlength;
					}
				} else {
					rs += token[j] + " ";
				}
			}
			result += trim(rs) + "<br/>";
		}
	}
	
	return result;
}

/*
 * cast month to full name
 */
function castMonth(month) {
	var m = "";
	
	switch(month) {
		case 0: m = "Jan"; break;
		case 1: m = "Febr"; break;
		case 2: m = "Mar"; break;
		case 3: m = "Apr"; break;
		case 4: m = "May"; break;
		case 5: m = "Jun"; break;
		case 6: m = "Jul"; break;
		case 7: m = "Aug"; break;
		case 8: m = "Sep"; break;
		case 9: m = "Oct"; break;
		case 10: m = "Nov"; break;
		case 11: m = "Dec"; break;
	}
	return m;
}

function castValue(value) {
	if (value < 10) return "0" + value;
	else return value;
}

/****************************************************************/

/*
 * check foul language
 */

function checkfoul(obj) {
	var str = obj.value;
	var str2 = str.toLowerCase();
	var add = "";
	
	Restaurant.getFoul(str,function(rts) {
		var len = rts.length;  
		
		if (str != "") {
			for (var i=0 ; i<len ; i++) {
				var rs = rts[i].name.toLowerCase();    
			 
				if (str2.length >= 3) {
					if (rs!=null) {
						add += rts[i].name;
					}
				}
					
				var rd = "";
				if (add==""||add.length==0) {
					document.getElementById("verified").value = "0";
					rd += "";
				} else {
					document.getElementById("verified").value = "1";
					rd += "<br />Comments contain bad word(s) :" + add;
				}
				$h("foul", rd);
			}
		}
	});
}

function clearFoul() {
	document.getElementById("verified").value = "";
	$h("foul","");
}
 
/*
 * only allow ascii
 */
function checkascii(obj){
	var len = obj.value.length
	var w = "";
	
	for (var i=0; i < len ; i++) {
		var x = obj.value.charAt(i);
		var code = x.charCodeAt(0);
		
		if (code <= 8436){
			w += x;
		} else {
			obj.value = obj.value.substring(0, obj.value.indexOf(x));
			alert("Only allow ASCII, please enter valid characters.");
		}
	}
}

/*
 *to remove single quote from a string
 */
function htmlEncode(str){
	var len = str.value.length
	var w = "";
	
	for (var i=0; i < len ; i++) {
		var x = str.value.charAt(i);
		var code = x.charCodeAt(0);
		
		if (code != 39){
			w += x;
		} else {
			str.value = str.value.substring(0, str.value.indexOf(x));
		}
	}
}
/*
 * check user name - only allow Alphanumeric
 */
function checkuname(key, which) {
	if (key == 0) { // if there is mozila firefox
		if (which >= 65 && which <= 90) return true; // A - Z
		else if (which >= 97 && which <= 122) return true; // a - z
		else if (which >= 48 && which <= 57) return true; // 0 - 9
		else if (which == null || which == 8 || which == 9 || which == 13 || which == 27 || which == 32 || which == 64 || which == 95) return true;
		else return false;
	
	} else {
		if (key >= 65 && key <= 90) return true;
		else if (key >= 97 && key <= 122) return true;
		else if (key >= 48 && key <= 57) return true;
		else if (key == null || key == 8 || key == 9 || key == 13 || key == 27 || key == 32 || which == 64 || key == 95) return true;
		else return false;
	}
}

/*
 *	check keyword
 */
function checkkeyword(key, which) {
	if (key == 0) { // if there is mozila firefox
		if (which >= 65 && which <= 90) return true; // A - Z
		else if (which >= 97 && which <= 122) return true; // a - z
		else if (which >= 48 && which <= 57) return true; // 0 - 9
		else if (which == null || which == 8 || which == 9 || which == 13 || which == 27 || which == 32 || which == 64) return true;
		else return false;
	
	} else {
		if (key >= 65 && key <= 90) return true;
		else if (key >= 97 && key <= 122) return true;
		else if (key >= 48 && key <= 57) return true;
		else if (key == null || key == 8 || key == 9 || key == 13 || key == 27 || key == 32 || key == 64) return true;
		else return false;
	}
}

/*
 * check mobile number - only allow Alphanumeric
 */
function checkmsid(key, which) {
	if (key == 0) { // if there is mozila firefox
		if (which >= 48 && which <= 57) return true; // 0 - 9
		else if (which == null || which == 8 || which == 9 || which == 13 || which == 27) return true;
		else return false;
	
	} else {
		if (key >= 48 && key <= 57) return true;
		else if (key == null || key == 8 || key == 9 || key == 13 || key == 27) return true;
		else return false;
	}
}


function clearExample(obj) {
	obj.value = "";
}

function fillExample(obj, example) {
	if (trim(obj.value) == "") obj.value = example;
}

/********************************************************************/
/****************************** rating ******************************/
/********************************************************************/

var isSetRate_ = false;
var onClickUp_ = 0;
var onClickDown_ = 0;

/*
 * change the feature when mouse over into thumbs icon
 */
function thumbsover(id, rate) {
	if (isSetRate_ == false) {
		if (rate == 1) $(id).src="/images/tup.jpg";
		else $(id).src = "/images/tdown.jpg";
	}
}

/*
 * change the feature when mouse out from thumbs icon 
 */
function thumbsout(id, rate) {
	if (isSetRate_== false)  {
		if (rate == 1) $(id).src="/images/tupdis.jpg";
		else $(id).src = "/images/tdowndis.jpg";
	}
}

/*
 * set rate 
 */
function setRate(id, rate) {
	if (isSetRate_== false) {
		$("thumbsup").src="/images/tupdis.jpg";
		$("thumbsdown").src="/images/tdowndis.jpg";
		isSetRate_ = true;
		
		if (rate == 1) {
			$(id).src="/images/tup.jpg";
			onClickUp_ = 1;
			
		} else {
			$(id).src = "/images/tdown.jpg";
			onClickDown_ = 1;
		}

		$("rating").value = rate;
	} else {
		if (onClickUp_ == 1) {
			if (id == "thumbsdown") {
				$(id).src = "/images/tdown.jpg";
				$("thumbsup").src = "/images/tupdis.jpg";
				onClickUp_ = 0;
				onClickDown_ = 1;
			}
		} else if (onClickDown_ == 1) {
			if (id == "thumbsup") {
				$(id).src = "/images/tup.jpg";
				$("thumbsdown").src = "/images/tdowndis.jpg";
				onClickDown_ = 0;
				onClickUp_ = 1;
			}
		}
		$("rating").value = rate;
	}
}