﻿ //获取url参数
function request(paras)
{
	var url=location.href;
	var paraString=url.substring(url.indexOf("?")+1,url.length).split("&");
	var paraObj=
	{
	}
	for(var i=0;j=paraString[i];i++)
	{
		paraObj[j.substring(0,j.indexOf("=")).toLowerCase()]=j.substring(j.indexOf
		("=")+1,j.length);
	}
	var returnValue=paraObj[paras.toLowerCase()];
	if(typeof(returnValue)=="undefined")
	{
		return "";
	}else
	{
		return returnValue;
	}
}
//邮箱验证
function isEmail(str) {
    var reg = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    return reg.test(str);
}
//多邮箱验证
function multiEmail(str)
{
	var paraString=str.split(";");
	var chk=true;
	for(var i=0;i<paraString.length;i++)
	{
		if(!isEmail(paraString[i]))
		{
//			alert("["+paraString[i]+"] This email is incorrect!");			
			return false;
		}
		
	}
	return chk;
}
//捕捉文本框中的回车操作并触发用户指定的button点击动作
function ValPressKey(e,id)
{
    var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
    if (keyCode == 13){
        $("#"+id).click();
        return false;
    }
    else
    {
        return true;
    }
}

function IsNumeric(value) {
    var validate = /(^\d+$)/;
    if ((validate.test(value)) && (value > 0))
        return 0;
    else
        return 1;
}
function IsUSZipFormat(value) {
    var reg = /^\d{5}$/;
    return reg.test(value);
}
function IsCanadaZipFormat(value) {
    var reg = /^[a-zA-Z](\d)[a-zA-Z](\s)*(\d)[a-zA-Z](\d)$/;
    return reg.test(value);
}
function cleanSelect(id) {
    var select = document.getElementById(id);
    select.options.length = 0;
}
function fillData4States(id, states) {
    var selStates = document.getElementById(id);
    selStates.options.length = 0;
    for (var i = 0; i < states[1]; i++) {
        var sDetail = states[2 + i].split(',');
        var stateItem = new Option(sDetail[0], sDetail[1]);
        selStates.options.add(stateItem);
    }
}
function aotoComplete4City(id, sCities) {
    var cities = sCities.split(',');
    var selCities = document.getElementById(id);
    var isExist = false;
    var oldCityValue = $(selCities).next().val();
    selCities.options.length = 0;

    for (var i = 0; i < cities.length; i++) {
        var cityItem = new Option(cities[i], cities[i]);
        selCities.options.add(cityItem);

        if (oldCityValue == cities[i]) isExist = true;
    }

    if (!isExist) $(selCities).next().val(cities[0]);
}

String.prototype.trim=function ()
{
	return this.replace(/(^\s*)|(\s*$)/g,"");
}
//去除字符串两端特殊字符
String.prototype.trimChar=function ()
{
	str=this.replace(/(^[;&=?]*)|([;&=?]*$)/g,'').replace("&&","&").replace("=&","=").replace(";;",";").replace("=;","=").replace(/(^\s*)|(\s*$)/g,'');
	return str;
}
//js cookie操作 start
document.setCookie = function(name, value, exphour) {
    var str = name + "=" + escape(value);
    if (exphour != 0) {//为0时不设定过期时间，浏览器关闭时cookie自动消失
        var date = new Date();
        var ms = exphour * 3600 * 1000;
        date.setTime(date.getTime() + ms);
        str += "; expires=" + date.toGMTString();
    }
    document.cookie = str;
}; //document.setCookie("test","zhangsan",-1);
document.getCookie = function(name) {
    var arrStr = document.cookie.split("; ");
    for (var i = 0; i < arrStr.length; i++) {
        var temp = arrStr[i].split("=");
        if (temp[0] == name) return unescape(temp[1]);
    }
    return "";
}; //document.getCookie("test");
document.delCookie = function(name) {
    if (document.getCookie(name)) {
        document.setCookie(name, "", -1);
    }
}; //document.delCookie("test");
//js cookie操作 end
function getObj(ID){
    return document.getElementById(ID);
}
function getSelectedValue(id)
{
var list=getObj(id);
   var i=list.selectedIndex;	
	var value=list.options[i].value;
	return value;
}
function getRadiosValue(name)
{
	var value;
	var radios=$("input[name='"+name+"']");
	$(radios).each(function ()
	{
		if($(this).attr("checked"))
		{
			value=$(this).attr("value").trim();
		}
	})
	return value;
}
//文本框内输回车，跳到下一行
function moveNextWhenEnter(objs, obj, keycode, maxIndex) {
    if (keycode == 13) {
        var nextTabIndex = obj.tabIndex + 1;
        for (var itargetIndex = nextTabIndex; itargetIndex <= maxIndex; itargetIndex++) {
            var targetInput = $(objs).filter("[tabindex=" + itargetIndex + "]:enabled");
            if ($(targetInput).length > 0) {
                $(targetInput)[0].focus();
                return;
            }
        }
    }
}
function killErrors() {
return true;
}
window.onerror = killErrors;

//Header Search Begin
function ClickHeaderSearchBtn(e)
{
    if(e.keyCode == 13)
    {
        SubmitSearch();
        //Prevent the default event of form when you press the Enter key
        var formsCollection = document.getElementsByTagName("form");
        for(var i=0;i<formsCollection.length;i++)
        {
            formsCollection[i].onsubmit = function(){return false;};
        }
    }
}

function SubmitSearch()
{
    var txtSearchText = document.getElementById(searchBoxId);
    if(txtSearchText.value.trim() != "")
    {
        //UpdateUsedCount
        Callback_UpdateUsedCount.PerformCallback(txtSearchText.value.trim().replace(new RegExp("'", "g"), "''"));
    }
}

//Get the search result by the text that inputted
var objInterval = null;
var searchResultElement = document.createElement("div");
searchResultElement.id = "searchResult";
var searchBoxId = null;
document.body.appendChild(searchResultElement);

function GetSearchResult(searchText)
{
    Callback_GetSearchResult.PerformCallback(searchText);
}
function ShowSearchResult(result)
{
    if(result != "")
    {
        searchResultElement.innerHTML = result.split('&')[0];
        var searchBox = $("#" + searchBoxId);
        searchResultElement.style.top = searchBox.offset().top + searchBox.height() + "px";
        searchResultElement.style.left = searchBox.offset().left + "px";
        searchResultElement.style.display = "block";
    }
    else
    {
        searchResultElement.style.display = "none";
    }
}
function HideSearchResult()
{
    if(searchResultElement.style.display == "block")
        searchResultElement.style.display = "none";
}
if(document.getElementById("searchResult"))
{
    var searchResultElement = document.getElementById("searchResult");
    document.body.onclick = function(){HideSearchResult()};
}
function CheckInputTextIsEmpty(e,searchText)
{
    var regexTestKeyCode = /38|40|37|39|16|17|18|19|20|144|145|13|112|113|114|115|116|117|118|119|120|121|122|123|45|36|33|34|35/;//Buttons: Up Down Left Right Shift Control ALt Pause Caps_Lock Num_Lock Scroll_Lock Enter F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 Insert Home PageUp PageDown End
    if(!regexTestKeyCode.test(e.keyCode))
    {
        if(searchText == "")
        {   
            searchResultElement.style.display = "none";
        }
        else
        {
            clearTimeout(objInterval);
            objInterval = setTimeout(function () { GetSearchResult(searchText.replace(new RegExp("'", "g"), "''") ); }, 800);
        }
    }
}
function SetValueOfSearchBox(fullSearchText)
{
    document.getElementById(searchBoxId).value = fullSearchText.trim();
    HideSearchResult();
    SubmitSearch();
}
function ChangeSelectedItem(e, textBoxId) 
{
    searchBoxId = textBoxId;
	var container = document.getElementById("searchResultList");
	if(container != null && searchResultElement.style.display != "none")
	{
	    var items = container.getElementsByTagName("li");
	    var itemsLength = items.length;

        var inputBox = document.getElementById(searchBoxId);
        var colorToChange = "silver";
        var hasCount = 0;
        for(var i=0;i<items.length;i++)
        {
	        var thisBackgroundColor = items[i].style.backgroundColor;
	        if(thisBackgroundColor == colorToChange)
	        {
		        hasCount++;
	        }
        }
        if(hasCount == 0)
        {
            //When You Press The Up Button
	        if(e.keyCode == 38)
	        {
		        inputBox.value = items[itemsLength-1].innerHTML;
		        items[itemsLength-1].style.backgroundColor = colorToChange;
	        }
	        //Down Button
	        else if(e.keyCode == 40)
	        {
		        inputBox.value = items[0].innerHTML;
		        items[0].style.backgroundColor = colorToChange;
	        }
        }
        else
        {
	        for(var j=0;j<itemsLength;j++)
	        {
		        var currentBackgroundColor = items[j].style.backgroundColor;
		        if(currentBackgroundColor == colorToChange)
		        {
			        items[j].style.backgroundColor = "";
			        //When You Press The Up Button
			        if(e.keyCode == 38)
			        {
				        if(j == 0)
				        {
					        //set the value of input box
					        inputBox.value = items[itemsLength-1].innerHTML;
					        items[itemsLength-1].style.backgroundColor = colorToChange;
				        }
				        else
				        {
					        inputBox.value = items[j-1].innerHTML;
					        items[j-1].style.backgroundColor = colorToChange;
				        }
				        break;
			        }
			        //Down Button
			        else if(e.keyCode == 40)
			        {
				        if(j == itemsLength-1)
				        {
					        inputBox.value = items[0].innerHTML;
					        items[0].style.backgroundColor = colorToChange;
				        }
				        else
				        {
					        inputBox.value = items[j+1].innerHTML;
					        items[j+1].style.backgroundColor = colorToChange;
				        }
				        break;
			        }
		        }
	        }
        }
	}
}
//Header Search End
