//清空表单
function clearForm(id) {
	var obj = document.getElementById(id);
	if (obj == undefined) {
		return;
	}
	for (var i = 0; i < obj.elements.length; i++) {
		var e = obj.elements[i];
		var type = e.type;

		if (type == "text" || type == "password" || type == "textarea") {
			e.value = "";

		} else if (type == "radio" || type == "checkbox") {
			e.checked = false;

		} else if (type == "select-one") {
			e.options[0].selected = true;

		} else if (type == "select-multiple") {
			for (var j = 0; j < e.options.length; j++) {
				e.options[j].selected = false;
			}
		}
	}
};

//处理null 字段
function handleNullFiled(item){
	for(var filed in item){
		if(item[filed] == null){
			item[filed] = '';
		}
	}
}

// 根据参数名获取上一个页面的参数值
function getParam(paramName) {
	var param = location.search.substring(1);
	var paramArray = new Array();
	paramArray = param.split("&");
	var newParamName = paramName.concat("=");
	for (var i = 0; i < paramArray.length; i++) {
		if (paramArray[i].indexOf(newParamName) > -1) {
			var paramValue = paramArray[i].replace(newParamName, '');
			return paramValue;
		}
	}
	return null;
};

// 比较日期大小
function dateCompare(startTime, endTime) {
	var startTime = new Date(Date.parse(startTime));
	var endTime = new Date(Date.parse(endTime));
	return (endTime >= startTime);
};

// 补全小数位数，value为返回值，num补全几位小数
function completionDecimal(value, num) {
	var xsd = value.split(".");
	var str1 = ".";
	for (var i = 0; i < num; i++) {
		str1 += "0";
	}
	if (xsd.length == 1) {
		value = value + str1;
		return value;
	}
	var str2 = "";
	for (var j = 0; j < num - xsd[1].length; j++) {
		str2 += "0";
	}
	if (xsd.length > 1) {
		if (xsd[1].length < num) {
			value = value + str2;
		}
		return value;
	}
};
//去2端空字符,可以去掉tab
function trimStr(str){
	return str.replace(/(^\s*)|(\s*$)/g,"");
}

//tip是提示信息，type:'success'是成功信息，'danger'是失败信息,'info'是普通信息,'warning'是警告信息
function showTip(tip, type) {
    var $tip = $('#show_mytip');
    if ($tip.length == 0) {
        $tip = $('<strong id="show_mytip" style="position:absolute;top:35%;left: 50%;z-index:9999;min-width:80%;min-height:30px;text-align:center;"></strong>');
        $('body').append($tip);
    }
    $tip.stop(true).prop('class', 'alert alert-' + type).text(tip).css('margin-left', -$tip.outerWidth() / 2).fadeIn(400).delay(2000).fadeOut(600);
}

function showMsg(msg) {
    showTip(msg, 'info');
}

function showSuccess(msg) {
    showTip(msg, 'success');
}

function showFailure(msg) {
    showTip(msg, 'danger');
}

function showWarn(msg, $focus, clear) {
    showTip(msg, 'warning');
    if ($focus) {
　　　　 $focus.focus();
    　　if (clear) $focus.val('');
　　 }
    return false;
}

function player(src){//播放音乐
    s = plus.audio.createPlayer( src );
    var num = s.getDuration();//获取音频总长度number
    setTimeout(function(){//延时获取，否则可能没有返回长度
        var num = s.getDuration();
        //alert(num);
    },100)
 
    s.play( function () {//播放完成回调
        alert( "Audio play success!" ); 
    }, function ( e ) {//失败回调
        alert( "Audio play error: " + e.message ); 
    } ); 
}

function playSound(src){
    setTimeout(function(){
    	  var borswer = window.navigator.userAgent.toLowerCase();
		  if ( borswer.indexOf( "ie" ) >= 0 ){
		    //IE内核浏览器
			$('embed').remove();
		    var strEmbed = '<embed name="embedPlay" src="'+src+'" autostart="true" hidden="true" loop="false"></embed>';
		    if ( $( "body" ).find( "embed" ).length <= 0 ){
		    	$( "body" ).append( strEmbed );
		    }
		    var embed = document.embedPlay;
		    //浏览器不支持 audion，则使用 embed 播放
		    embed.volume = 100;
		    //embed.play();这个不需要
		  } else {
		    //非IE内核浏览器
			$('audio').remove();	  
		    var strAudio = "<audio id='audioPlay' src='"+src+"' hidden='true'>";
		    if ( $( "body" ).find( "audio" ).length <= 0 ){
		    	$( "body" ).append( strAudio );
		    }
		    var audio = document.getElementById( "audioPlay" );
		    //浏览器支持 audion
		    audio.play();
		  }
    },100);
}

//设置聚焦 并且到文字最后
setFocus=function(id){  
    var t=$("#"+id).val();  
    $("#"+id).val("").focus().val(t);  
}  
//文本选中
setSelect=function(id){  
    var t=$("#"+id).val();  
    $("#"+id).val("").focus().val(t);
    $("#"+id).select();
}

function myScrollIntoView(){
	var scrollTop = document.body.scrollTop; 
	scrollTop = scrollTop - 34;
	
	$('body,html').animate({scrollTop:scrollTop},500);
}
