function stringTrim(str) {
	return (str + "").replace(/^\s+|\s+$/g,"");
}

function addText(textEl, text, lPadding, rPadding) {
    var currentText = textEl.value;
    text = (lPadding ? lPadding : "") + text + (rPadding ? rPadding : "");
	textEl.value = currentText + text;
}

function display_send_to_friend(){

	if( document.getElementById('send_to_friend').style.display == 'none' ){
		display('send_to_friend','');
		
		document.getElementById('send_to_friend').style.top = display_get_absolute_offset_top(document.getElementById('td_send_to_friend'),25);
		document.getElementById('send_to_friend').style.left = display_get_absolute_offset_left(document.getElementById('td_send_to_friend'),-400);
		
		$('td_send_to_friend').className = 'display_send_to_friend';
		$('div_send_to_friend').style.color = '#FFFFFF';
	}
	else
		close_send_to_friend();
		
}
function close_send_to_friend(){
	display('send_to_friend','none');
	
	$('td_send_to_friend').className = 'close_send_to_friend';
	$('div_send_to_friend').style.color = '#003366';
	
}
function submit_send_to_friend(){
	//alert(window.location);
	targetlink = "send_to_friend.php?yourname="+$('yourname').value+"&youremail="+$('youremail').value+"&friendname="+$('friendname').value+"&friendemail="+$('friendemail').value+"&subject="+encodeURIComponent($('subject').innerHTML)+"&message="+encodeURIComponent($('message').value)+"&link="+window.location;
	ajax_request_html(targetlink,'send_to_friend');
}

function isEmailValid(email){
	return email.match(/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/) != null;
}

function print_r(theObj) {
	if (theObj.constructor == Array || theObj.constructor == Object) {
		document.write("<ul>")
		for ( var p in theObj) {
			if (theObj[p].constructor == Array
					|| theObj[p].constructor == Object) {
				document.write("<li>[" + p + "] => " + typeof (theObj)
						+ "</li>");
				document.write("<ul>")
				print_r(theObj[p]);
				document.write("</ul>")
			} else {
				document.write("<li>[" + p + "] => " + theObj[p] + "</li>");
			}
		}
		document.write("</ul>")
	}
}

function isDigit(limitField, evt, limitNum) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
    var ss = String.fromCharCode(charCode);
    if(ss == null) return false;
    //acceptable characters (- . _ 0-9 a-z)
    if ((charCode == 8)||(charCode == 9)||(charCode == 16)||(charCode == 37)||(charCode == 39)||(charCode == 45) || (charCode == 46) ||((charCode >= 48)&& (charCode <= 57))) {
    	
    	if (limitField.value.length >= limitNum) {
    		if(window.event) {//IE
				evt.returnValue = false;   //event.returnValue=false 效果相同.
			}
			else {//Firefox
				evt.preventDefault();
			}
    	} else
			return true;
	} else {
		if(window.event) {//IE
			evt.returnValue = false;   //event.returnValue=false 效果相同.
		}
		else {//Firefox
			evt.preventDefault();
		}
	}
}

function format(input){
	var num = input.value.replace(/\,/g,'');
	if(!isNaN(num)){
		if(num.indexOf('.') > -1){
			num = num.split('.');
			num[0] = num[0].toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'');
			if(num[1].length > 2){
//				alert('You may only enter two decimals!');
				num[1] = num[1].substring(0,num[1].length-1);
			} input.value = num[0]+'.'+num[1];
		} else {
			input.value = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'') };
	} else {
//		alert('You may enter only numbers in this field!');
		input.value = input.value.substring(0,input.value.length-1);
	}
}

function formatInt(input){
	var num = input.value.replace(/\,/g,'');
	if(!isNaN(num)){
		if(num.indexOf('.') > -1) {
			input.value = input.value.substring(0,input.value.length-1);
		}
	} else {
		input.value = input.value.substring(0,input.value.length-1);
	}
}

function limitText(limitField, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	}
}

function set_homepage() {
 	if (document.all) {
        document.body.style.behavior='url(#default#homepage)';
  		document.body.setHomePage('http://www.exmoo.com');
    }
    else if (window.sidebar) {
		if(window.netscape) {
			try {  
            	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
				var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
		    	prefs.setCharPref('browser.startup.homepage','http://www.exmoo.com');
         	}  
         	catch(e) {  
				home_tips();
			}
		} 
	}
}

function home_tips(){
	var set_home = document.getElementById('set_home');
	if (set_home) {
	 	if (set_home.style.display == '' )
	 		set_home.style.display = 'none';
		else
			set_home.style.display = '';
	}
}


/*
 * for header search
 */
function click_search(){
	document.getElementById('search_categories').style.left=display_get_absolute_offset_left(document.getElementById('choose_categories'), 0);
	document.getElementById('search_categories').style.top=display_get_absolute_offset_top(document.getElementById('choose_categories'), 30);//26
	
	if ( document.getElementById('search_categories').style.display == 'none'){
		open_search();
	}else{
		hide_search();
	}

	if (document.getElementById('search_type').value == "exmoo"){
		document.getElementById('category4').style.display = 'none';
	}else{
		document.getElementById('category4').style.display = '';
	}
	if (document.getElementById('search_type').value == "google"){
		document.getElementById('category1').style.display = 'none';
	}else{
		document.getElementById('category1').style.display = '';
	}
	if (document.getElementById('search_type').value == "yahoo"){
		document.getElementById('category3').style.display = 'none';
	}else{
		document.getElementById('category3').style.display = '';
	}
	if (document.getElementById('search_type').value == "baidu"){
		document.getElementById('category5').style.display = 'none';
	}else{
		document.getElementById('category5').style.display = '';
	}
}

function hide_search(){
	document.getElementById('search_categories').style.left=display_get_absolute_offset_left(document.getElementById('choose_categories'), -4);
	document.getElementById('search_categories').style.top=display_get_absolute_offset_top(document.getElementById('choose_categories'), -3);//26
	document.getElementById('search_categories').style.display = 'none';
	//document.getElementById('search_table').style.cssText = 'height:22px; border-left:#FFF 2px solid;border-top:#FFF 2px solid;border-right:#FFF 2px solid; ';
}

function oepn_search(){
	document.getElementById('search_categories').style.left=display_get_absolute_offset_left(document.getElementById('choose_categories'), -4);
	document.getElementById('search_categories').style.top=display_get_absolute_offset_top(document.getElementById('choose_categories'), -3);//26
	document.getElementById('search_categories').style.display = '';
	//document.getElementById('search_table').style.cssText = 'height:22px; border-left:#CCC 2px solid;border-top:#CCC 2px solid;border-right:#CCC 2px solid; ';
}

function click_categories(serial, search_type){
	document.getElementById('choose_categories').innerHTML = document.getElementById('category'+serial).innerHTML;
	document.getElementById('search_type').value = search_type;
	click_search();
}

function exmoo_search(){
	if (document.getElementById('search_type').value == "exmoo"){
		document.search_form.q.value = document.search_form.keywords.value;
	}
	document.search_form.submit();
}

function checkEnter(e) { //e is event object passed from function invocation
	var characterCode; // literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		exmoo_search();
		return false;
	} else{
		return true;
	}
}


function post_to_url(path, params, method, target) {
    method = method || "post"; // Set method to post by default, if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
	form.setAttribute("target", target);
    form.setAttribute("action", path);

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);    // Not entirely sure if this is necessary
    form.submit();
}
