﻿
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

if (checkIt('konqueror')) {
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible')) {
	browser = "Netscape Navigator"
	version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS) {
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac"
	else if (checkIt('win')) OS = "Windows"
	else OS = "an unknown operating system";
}

/* Popup window */
/*-----------------------------------------------*/
var win = null;
function NewWindow(mypage,myname,w,h,scroll){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings =
	'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable=yes'
	win = window.open(mypage,myname,settings)
}
function SetParamNewWindow(param, val, mypage,myname,w,h,features){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	var h2 = h==0? 800 : h;
	TopPosition = (screen.height) ? (screen.height-h2)/2 : 0;
	settings =
	'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',resizable';
	if(features != null) {
		settings += features;
	}
	if(param != null && val != null) {
		var pair = param + "=" + val;
		mypage += mypage.indexOf('?') > 0 ? pair : "?" + pair;
	}
	win = window.open(mypage,myname,settings)
}
function openHelp(whichPage) {
	NewWindow('/template/help.php?page=' + whichPage,'Help',350,350,"yes");
}
function openHelp2(whichPage, width, height) {
	NewWindow('/template/help.php?page=' + whichPage,'Help',width, height,"no");
}
function set_text_focus(frm) {
	for (j = 0; j < frm.elements.length; j++){
		if (frm.elements[j].type == "text" || frm.elements[j].type == "textarea" 
		|| frm.elements[j].type == "password" || frm.elements[j].type == "select-one") // Drop down should be included
		{
			frm.elements[j].focus();
			break;
		} 
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function setValueSubmit(form) {
	var deleted = "";
	for(var i=1; i<=4; i++) {
		if(document.getElementById('span' + i).innerHTML == "image deleted") {
			deleted += "," + i;
		}
	}
	
	if(deleted.length > 0) {
		deleted = deleted.substring(1);
	}
	
	form.delfiles.value = deleted;
	
	//return form.submit();
	
	return true;
}
function isDigit(s) {
	var strValidChars = "0123456789";
	for (i = 0; i < s.length; i++){
      sChar = s.charAt(i);
      if (strValidChars.indexOf(sChar) == -1){
         return false;
      }
	}
	return true;
}
function validate_search(form, target) {
	validate(form);
	if(document.formSubmit == false) {
		return;
	}
	var error = "";
	if(form.stid.value=="id") {
		form.action = target; 
		var arr = form.keyword.value.split('-');
		if(arr.length != 2 || arr[0].length != 4 || !isDigit(arr[0])) {
			alert("广告ID无效 (例：3000-10265678)");
			document.formSubmit = false;
			return;
		}
	} 
	document.formSubmit = true;
}
function validate(form){
	
	var error = "";
	//for each form element
	for(var i=0; i<form.length; i++){
		var element = form[i];
		//if required
		if(element.getAttribute("required") == "yes"){
			//if form element if empty
			if(!valid(element.value,element.getAttribute("validate"),element))
				error += (element.getAttribute("message")=="" ? "无效: " + element.value: element.getAttribute("message")) + "\r\n";	
		}
		else if(element.getAttribute("validate") != ""){
			//if validation is need by not required
			if(element.value != ""){
				if(!valid(element.value,element.getAttribute("validate"),element))
					error += (element.getAttribute("message")=="" ? "无效: " + element.value: element.getAttribute("message")) + "\r\n";	
			}
		}
	}
	if(error != ""){
		alert(error);
		document.formSubmit = false;
		return false;
	}
	else {
		document.formSubmit = true;
		return true;
	}
}	

function valid(value,type,element){
	if(value == "")
		return false;
		
	switch(type){
		case "int":
			if(isNaN(parseInt(value)))
				return false;
			break;
		case "float":
			if(isNaN(parseFloat(value)))
				return false;
			break;
		case "email":
			var p = value.indexOf('@');
			if(p1 || p==(value.length-1))
				return false;
			break;
		case "checked":
			if(!element.checked)
				return false;
			break;
		default://string
			break;
	}
	return true;
}	

function check_length(maxchars,message,div_id)
{
	var len = message.value.length;

	if(len > maxchars){
		message.value = message.value.substr(0,maxchars);
		len = maxchars;
	}

	document.getElementById(div_id).innerHTML = maxchars - len;
}

function hide(id) {
	var field = document.getElementById(id);
	if (field.style.display=="none") {
    	field.style.display="block";
	} else {
		field.style.display="none"
	}
}


var imgsrc = new Array;

imgsrc[1] = "/img/bl/no_img.gif";
imgsrc[2] = "/img/bl/no_img.gif";
imgsrc[3] = "/img/bl/no_img.gif";
imgsrc[4] = "/img/bl/no_img.gif";

var extensions = new Array ("gif", "jpg", "jpeg", "pjpeg", "png");

function notSupported(ext) {
	ext = ext.toLowerCase();
	for(i=0; i<extensions.length; i++) {
		if(ext==extensions[i]) return false;
	}
	return true;
}
function imgadd(id,value) {
	
	if(value == null || value == 0) return;
	
	if(!validate_image(value, false)) return;
	
	((value == null || value == 0)) ? document.getElementById('img' + id).src = imgsrc[id] : imgswap(id,value);
	
	document.getElementById('imgdel' + id).style.display="inline";
}

function imgurladd(id, value) {
	if(value == null || value == 0) {
		document.getElementById('img' + id).src = "/img/bl/no_img.gif";	
		return;
	}
	if(!validate_image(value, true)) {	
		document.getElementById('imgurl' + id).value = "";
		return;
	} else {
		imgshow(id,value);	
	}
}

function validate_image(value, url) {
	
	var ext = value.substring(value.lastIndexOf(".")+1);
	if(notSupported(ext)) {
		alert("图片文件类型(" + ext + ")不支持");
		return false;
	}
	if(url) {
		if(value.length > 100) {
			alert("网址不得超过100字节。");
			return false;	
		}
		
		if(value.substring(0, 7) != 'http://' && value.substring(0, 7) !='https://') {
			alert("网址不正确(必须以http://开头)。");
			return false;		
		}
	}
	return true;
}

function imgdel(id) {
	//document.getElementById('imgfile' + id).disabled = true;
	var attr = document.getElementById('imgfile' + id).getAttribute("value");
	attr.value = "";
	document.getElementById('img' + id).src = "/img/bl/no_img.gif";
	document.getElementById('span' + id).innerHTML = "image deleted";
	document.getElementById('imgdel' + id).style.display="none";
}

function imgswap(id,filepath) {
	var imgpath = "/img/bl/img.gif";
	var epath = filepath.replace(/\\/g, "\/");
	var p = epath.split("/");
	document.getElementById('img' + id).src = imgpath; 
	document.getElementById('span' + id).innerHTML = p.pop();
}

function imgshow(id,img) {
	document.getElementById('img' + id).src = img; 	
}

function hov(loc,cls){
   if(loc.className)
      loc.className=cls;
}

