
function DemoJpg(picName,picUrl){
	this.picName = picName;
	this.picUrl = picUrl;
	
	function getPicName(){
		return this.picName;
	}
	
	function getPicNameTime(){
		// cut the name fot the time
		var temp = this.picName;
		
		if(temp != null && temp != "" && temp.length >= 6 ){
			var hour = temp.substring(0,2);
			var min = temp.substring(2,4);
			var sec = temp.substring(4,6);
		}
		var str = "";
		
		str = hour + ":" + min + ":" + sec;
		return str;
	}
	
	function getPicUrl(){
		return this.picUrl;
	}
	
	this.getPicName = getPicName;
	this.getPicUrl = getPicUrl;
	this.getPicNameTime = getPicNameTime;
}

//get cookies
function getCookies(cookieName,dfltValue) { 
             var lowerCookieName = cookieName.toLowerCase();
             var cookieStr = document.cookie;
 
             if (cookieStr == "")
             {
              return dfltValue;
             }
    
             var cookieArr = cookieStr.split("; ");
             var pos = -1;
             for (var i=0; i<cookieArr.length; i++)
             {
               pos = cookieArr[i].indexOf("=");
               if (pos > 0)
               {
                 if (cookieArr[i].substring(0, pos).toLowerCase() == lowerCookieName)
                 {
                   return unescape(cookieArr[i].substring(pos+1, cookieArr[i].length));
                 }
               }
              }
    
              return dfltValue;
             }


// camera demo object
function DemoCamera(vicId, sourceAddr) {
	this.vicId = vicId;
	this.sourceAddr = "<SourceAddr>" + BASE64.decode(sourceAddr) + "</SourceAddr>";
	function play(webPlayerName) {
		var webPlayer = document.getElementById(webPlayerName);
		if (webPlayer.State != "Ready") {
			webPlayer.Action = "Stop";
		}
		if (webPlayer.State == "Ready") {
			webPlayer.SourceAddr = this.sourceAddr;
		}
	}
	this.play = play;
}

// camera object 
function Camera(accessServerIP, naming, sessionID) {
	this.accessServerIP = accessServerIP;
	this.accessServerPort = 6001;
	this.naming = naming;
	this.sessionID = sessionID;
	this.timeBegin = null;
	this.timeEnd = null;
 	function getSourceAddr(history, tempDir) {
		var strBuf = "";
		strBuf += "<SourceAddr>";
		strBuf += "<IP>" + BASE64.decode(this.accessServerIP) + "</IP>";
		strBuf += "<Port>" + this.accessServerPort + "</Port>";
		strBuf += "<CameraID>" + BASE64.decode(this.naming) + "</CameraID>";
		if (history) {
			if (this.timeBegin != null) {
				this.timeBegin = fixTime(this.timeBegin);
				//alert(this.timeBegin);
				strBuf += "<TimeBegin>" + this.timeBegin + "</TimeBegin>";
			}
			if (this.timeEnd != null) {
				this.timeEnd = fixTime(this.timeEnd);
				//alert(this.timeEnd);
				strBuf += "<TimeEnd>" + this.timeEnd + "</TimeEnd>";
			}
		}
		strBuf += "<SessionID>" + this.sessionID + "</SessionID>";
	  	if (history) {
	  	    if(tempDir==""){
	  	      strBuf += "<TempDir>" + "C:\\" + "</TempDir>";
	  	    } else {
	  	      strBuf += "<TempDir>" + tempDir + "</TempDir>";
	  	    }
			
		}
		strBuf += "</SourceAddr>";
		return strBuf;
	}
	function play(webPlayer) {
		if (webPlayer.State != "Ready") {
			webPlayer.Action = "Stop";
		}
		if (webPlayer.State == "Ready") {
			webPlayer.SourceAddr = this.getSourceAddr(false, "");
		}
	}
	function playHistory(webPlayer, timeBegin, timeEnd, tempDir) {
		this.timeBegin = timeBegin;
		this.timeEnd = timeEnd;
		if (webPlayer.State != "Ready") {
			webPlayer.Action = "Stop";
		}
		if (webPlayer.State == "Ready") {
			webPlayer.SourceAddr = this.getSourceAddr(true, tempDir);
			//alert(this.getSourceAddr(true));
		}
	}
	function stop(webPlayer) {
		webPlayer.Action = "Stop";
	}
	
	function fixTime(s) {
		if (s.substring(s.lastIndexOf(":")).lastIndexOf(":") == 0) {
			s += ":00";
		}
		return s;
	}
	
	this.getSourceAddr = getSourceAddr;
	this.play = play;
	this.playHistory = playHistory;
	this.stop = stop;
}

// get the checked radio's value in a same-name-radio-group 
function getCheckedRadioValue(radioName) {
	var temp = document.getElementsByName(radioName);
	var result = "";
	for (i = 0; i < temp.length; i++) {
		if (temp[i].checked) {
			result = temp[i].value;
		}
	}
	return result;
}

//parse datetime string look like '2007-11-14 18:37' into date object
function parseDatetime(sDatetime) {
	tempArr = sDatetime.split(" ");
	sDate = tempArr[0];
	sTime = tempArr[1];
	dateArr = sDate.split("-");
	timeArr = sTime.split(":");
	iYear = dateArr[0];
	iMonth = dateArr[1] - 1;
	iDay = dateArr[2];
	iHour = timeArr[0];
	iMin = timeArr[1];
	d = new Date(iYear, iMonth, iDay, iHour, iMin);
	return d;
}
function cutDate(sDatetime) {
	tempArr = sDatetime.split(" ");
	sDate = tempArr[0];
	return sDate;
}
function formatDate(d) {
	var result = "";
	var year = d.getFullYear();
	var month = d.getMonth() + 1;
	var day = d.getDate();
	result = year + "-" + month + "-" + day;
	return result;
}
function formatDatetime(dt) {
	var result = "";
	var hour = dt.getHours();
	var minute = dt.getMinutes();
	result = formatDate(dt) + " " + hour + ":" + minute;
	return result;
}
function isMail(mail) {
	return (new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(mail));
}
function isMobilePhone(oNum) {
	if ("" == oNum) {
		return false;
	}
	var reg = /\D/;
	var flag = (oNum.match(reg) == null);
	if (oNum.length > 1 && oNum.substring(0, 1) == "0") {
		flag = false;
	}
	if (oNum < 13000000000 || (oNum > 14000000000&& oNum < 15000000000) || oNum > 16000000000) {
		flag = false;
	}
	return flag;
}
function isNumber(str) {
	if ("" == str) {
		return false;
	}
	var reg = /\D/;
	var flag = (str.match(reg) == null);
	if (str.length > 1 && str.substring(0, 1) == "0") {
		flag = false;
	}
	return flag;
}
/**
* 取得字符串的字节长度
*/
function strlen(str) {
	var i;
	var len;
	len = 0;
	for (i = 0; i < str.length; i++) {
		if (str.charCodeAt(i) > 255) {
			len += 2;
		} else {
			len++;
		}
	}
	return len;
}
function areYouSure(confirmInfo, gotoUrl) {
	if (confirm(confirmInfo)) {
		window.location = gotoUrl;
	}
}
function f_check_userID(obj) {
	var userID = obj.value;
	if (userID.length > 20 || userID.length < 5) {
		return false;
	}
	if (!isNaN(userID.charAt(0))) {
		return false;
	}
	if (!/^\w{1,20}$/.test(userID)) {
		return false;
	}
	return true;
}
function f_check_passwd(obj) {
	var passwd = obj.value;
	if (passwd.length > 16 || passwd.length < 6) {
		return false;
	}
	//去掉了密码首字母不能为数字，不能含有特殊字符的校验，密码改为只校验长度
	// 6 --- 16
	//if (!isNaN(passwd.charAt(0))) {
	//	return false;
	//}
	//if (!/^\w{1,20}$/.test(passwd)) {
	//	return false;
	//}
	return true;
}
function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1) {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}
function setCookie(name, value) {
	var expdate = new Date();
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	if (expires != null) {
		expdate.setTime(expdate.getTime() + (expires * 1000));
	}
	document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expdate.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
}
function delCookie(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = GetCookie(name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
function getCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieVal(j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) {
			break;
		}
	}
	return null;
}
function trim(str) {
	return str.replace(/(^\s*)|(\s*$)/g, "");
}
function ltrim(str) {
	return str.replace(/(^\s*)/g, "");
}
function rtrim(str) {
	return str.replace(/(\s*$)/g, "");
}
function goHome() {
	window.location = "/";
}

function getOs(){
	var OsObject = ""; 
	if(navigator.userAgent.indexOf("MSIE")>0) { 
		return "MSIE"; 
	} 
	if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){ 
		return "Firefox"; 
	} 
	if(isSafari=navigator.userAgent.indexOf("Safari")>0) { 
		return "Safari"; 
	} 
	if(isCamino=navigator.userAgent.indexOf("Camino")>0){ 
		return "Camino"; 
	} 
	if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){ 
		return "Gecko"; 
	} 
}

var BASE64={
    /**
     * 此变量为编码的key，每个字符的下标相对应于它所代表的编码。
     */
    enKey: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
    /**
     * 此变量为解码的key，是一个数组，BASE64的字符的ASCII值做下标，所对应的就是该字符所代表的编码值。
     */
    deKey: new Array(
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
        -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
        -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
    ),
    /**
     * 编码
     */
    encode: function(src){
        //用一个数组来存放编码后的字符，效率比用字符串相加高很多。
        var str=new Array();
        var ch1, ch2, ch3;
        var pos=0;
       //每三个字符进行编码。
        while(pos+3<=src.length){
            ch1=src.charCodeAt(pos++);
            ch2=src.charCodeAt(pos++);
            ch3=src.charCodeAt(pos++);
            str.push(this.enKey.charAt(ch1>>2), this.enKey.charAt(((ch1<<4)+(ch2>>4))&0x3f));
            str.push(this.enKey.charAt(((ch2<<2)+(ch3>>6))&0x3f), this.enKey.charAt(ch3&0x3f));
        }
        //给剩下的字符进行编码。
        if(pos<src.length){
            ch1=src.charCodeAt(pos++);
            str.push(this.enKey.charAt(ch1>>2));
            if(pos<src.length){
                ch2=src.charCodeAt(pos);
                str.push(this.enKey.charAt(((ch1<<4)+(ch2>>4))&0x3f));
                str.push(this.enKey.charAt(ch2<<2&0x3f), '=');
            }else{
                str.push(this.enKey.charAt(ch1<<4&0x3f), '==');
            }
        }
       //组合各编码后的字符，连成一个字符串。
        return str.join('');
    },
    /**
     * 解码。
     */
    decode: function(src){
        //用一个数组来存放解码后的字符。
        var str=new Array();
        var ch1, ch2, ch3, ch4;
        var pos=0;
       //过滤非法字符，并去掉'='。
        src=src.replace(/[^A-Za-z0-9\+\/]/g, '');
        //decode the source string in partition of per four characters.
        while(pos+4<=src.length){
            ch1=this.deKey[src.charCodeAt(pos++)];
            ch2=this.deKey[src.charCodeAt(pos++)];
            ch3=this.deKey[src.charCodeAt(pos++)];
            ch4=this.deKey[src.charCodeAt(pos++)];
            str.push(String.fromCharCode(
                (ch1<<2&0xff)+(ch2>>4), (ch2<<4&0xff)+(ch3>>2), (ch3<<6&0xff)+ch4));
        }
        //给剩下的字符进行解码。
        if(pos+1<src.length){
            ch1=this.deKey[src.charCodeAt(pos++)];
            ch2=this.deKey[src.charCodeAt(pos++)];
            if(pos<src.length){
                ch3=this.deKey[src.charCodeAt(pos)];
                str.push(String.fromCharCode((ch1<<2&0xff)+(ch2>>4), (ch2<<4&0xff)+(ch3>>2)));
            }else{
                str.push(String.fromCharCode((ch1<<2&0xff)+(ch2>>4)));
            }
        }
       //组合各解码后的字符，连成一个字符串。
        return str.join('');
    }
};


