﻿var dialog = null;
function showDialogDiv(title, url, dwidth, dheight){ 
	if(!dwidth) {
		dwidth = 800;
	}
	if(!dheight) {
		dheight = 600;
	}
	document.getElementById("dialogDivTitle").innerHTML = title;
	if(!dialog){ // lazy initialize the dialog and only create it once
		dialog = new Ext.LayoutDialog("dialogDiv", { 
		 	modal:true,
               width:dwidth,
               height:dheight,
               shadow:false,
               resizable:true,
               proxyDrag: true,
               center: {
                   autoScroll:false,
                   tabPosition: 'top'
               }
		}); 
	}

	var layout = dialog.getLayout();
    layout.beginUpdate();
    layout.add('center', new Ext.ContentPanel('ot_center',
				{
					fitToFrame: true,
					fitToContainer: true,
					autoScroll: true,
					resizeEl: 'ot-iframe'
				}));
    layout.endUpdate();
	Ext.get('ot-iframe').dom.src = url;

	reLocationDialog();		
	//ie有效
	document.getElementById("ot-iframe").onreadystatechange = function(){
		if(this.readyState=='complete'){				
			dialog.show();	
		}
	};

	//ff有效
	document.getElementById("ot-iframe").onload = function(){			
		dialog.show();	
	};
} 

function reLocationDialog() {
	if(dialog){ 
		var dWidth = (document.body.clientWidth-dialog.width)/2 + top.document.documentElement.scrollLeft;
		var dHeight = top.document.documentElement.scrollTop;

		dialog.moveTo(dWidth,dHeight+140);
	}
}

if(document.addEventListener) {
	window.addEventListener("resize", reLocationDialog, false); //FF
} else if(document.attachEvent) {
	window.attachEvent("onresize", reLocationDialog); //IE
}

function getMoneyConvert(moneyValue){
	if(moneyValue == 0) {
		return "0.00";
	}
	var result = "";
	var moneyValue = parseFloat(moneyValue);  
	if(moneyValue >= 100000000){
		var wPart = moneyValue % 100000000;
		result = (moneyValue - wPart)/100000000  + "亿";
		if(moneyValue/10000 > 10000){
			result += getMoneyConvert(wPart);
		}
	}
	else if(moneyValue >= 10000){
		var qPart = moneyValue % 10000;
		result = (moneyValue - qPart)/10000  + "万";
		if(moneyValue/10000 > 0){
			result += getMoneyConvert(qPart);
		}
	} 
	else if(moneyValue > 0){	//保留小数点后两位
		result = parseFloat(moneyValue).toFixed(2);
	} 
	return result;
} 

var preChoiceDivName;
function isChoiceDivDisplay(choiceDivName){
	if(choiceDivName && preChoiceDivName != choiceDivName){	
		preChoiceDivName = choiceDivName;	
		return false;
	}

	if('' !=  $("choiceDiv").style.display && $("choiceDiv").visible()){
		$("choiceDiv").hide();
		return true;
	}
	return false;
}

function writeCookie(name, value, expire) {
	if (!expire) {
		expire = 3600000;
	}

	var date = new Date();
	date.setTime(date.getTime()+expire);
	var expires = date.toGMTString();

	document.cookie = name + "=" + escape(value) + "; expires=" + expires + "; path=/";
}


function readCookie(Name) { 
	var search = Name + "="; 
	if(document.cookie.length > 0){ 
		offset = document.cookie.indexOf(search); 
		if(offset != -1) {  
			offset += search.length; 
			end = document.cookie.indexOf(";", offset); 
			if(end == -1) end = document.cookie.length; 
			return unescape(document.cookie.substring(offset, end)); 
		} 
		else {
			return "" 
		}
    } 
} 

function deleteCookie(name) { 
	var expire = new Date(); 
	expire.setTime(expire.getTime() - (86400 * 1000 * 1)); 
	writeCookie(name, "", expire); 
} 

Date.prototype.format = function(format){
	/*
  	 * eg:format="yyyy-MM-dd hh:mm:ss";
  	 */
	var o = {
  		"M+" :  this.getMonth()+1,  //month
  		"d+" :  this.getDate(),     //day
  		"h+" :  this.getHours(),    //hour
      	"m+" :  this.getMinutes(),  //minute
      	"s+" :  this.getSeconds(), //second
      	"q+" :  Math.floor((this.getMonth()+3)/3),  //quarter
     	"S"  :  this.getMilliseconds() //millisecond
   	}
  
   	if(/(y+)/.test(format)) {
    	format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
   	}
 
   	for(var k in o) {
    	if(new RegExp("("+ k +")").test(format)) {
      		format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    	}
   	}
 	return format;
}

function toUTF8(szInput){ 
  var wch,x,uch="",szRet="";
  for (x=0; x<szInput.length; x++){
    wch=szInput.charCodeAt(x);
    if (!(wch & 0xFF80)){
      szRet += szInput.charAt(x);
    }
    else if (!(wch & 0xF000)){
     uch = "%" + (wch>>6 | 0xC0).toString(16) + "%" + (wch & 0x3F | 0x80).toString(16);
     szRet += uch; 
    }
    else{
     uch = "%" + (wch >> 12 | 0xE0).toString(16) + 
      "%" + (((wch >> 6) & 0x3F) | 0x80).toString(16) +
      "%" + (wch & 0x3F | 0x80).toString(16);
   szRet += uch; 
  }
 }
 return(szRet);
} 

function fGetXY(a,offset) {
  var p=offset?offset.slice(0):[0,0],tn;
  while(a) {
    tn=a.tagName.toUpperCase();
    p[0]+=a.offsetLeft-(tn=="DIV"&&a.scrollLeft?a.scrollLeft:0);
    p[1]+=a.offsetTop-(tn=="DIV"&&a.scrollTop?a.scrollTop:0);
    if (tn=="BODY") break;
    a=a.offsetParent;
  }
  return p;
}

function getRadioValue(field) { 
  if (field.length) {
    for (var i = 0; i < field.length; i++) {
      if (field[i].checked) { 
        return field[i].value;
      }
    }
  }
  else {
    if (field.checked) {
      return field.value;
    }
  }
  return null;
}

function parseDate(str) {
	try {
		var arys= new Array();
		arys=str.split('-'); 

		for(var i = 0; i < arys.length; i++){
			if(arys[i] == '08'){
				arys[i] = '8';
			}
		}
		
		var newDate=new Date(parseInt(arys[0]),parseInt(arys[1]) - 1, parseInt(arys[2]));
		if(isNaN(newDate)){
			return null;
		} 		 
		return newDate;
	} catch (e) {
		return null;
	}
}

function getCheckboxValues(field, uncheckedValue) {
  var valueArray = new Array();
  if (field.length) {
    for (var i = 0; i < field.length; i++) {
      if (field[i].checked) {
        valueArray[i] = field[i].value;
      }
      else {
        valueArray[i] = uncheckedValue;
      }
    }
  }
  else {
    if (field.checked) {
       valueArray[0] = field.value;
    }
    else {
       valueArray[0] = uncheckedValue;
    }
  }
  return valueArray;
}

function getCheckboxCheckedValues(field) {
  var valueArray = new Array();
  if (field.length) {
    for (var i = 0; i < field.length; i++) {
      if (field[i].checked && field[i].value != "") {
         valueArray.push(field[i].value);
      }
    }
  }
  else {
    if (field.checked && field.value != "") {
       valueArray.push(field.value);
    }     
  }
  return valueArray;
}

function getValueArray(fields) {
  var valueArray = new Array();
	
  if (!fields) {
    return valueArray;
  }

  if (!fields.length) {
    if (fields.value) {
      valueArray[0] = fields.value;
    }
      return valueArray;
  }

  if ("checkbox" == fields[0].type || "radio" == fields[0].type) {
    var index = 0;
    for (var i = 0; i < fields.length; i++) {
      if (fields[i].checked) {
        valueArray[index++] = fields[i].value;
      }
    }
  }
  else {
    for (var i = 0; i < fields.length; i++) {
      valueArray[i] = fields[i].value;
    }
  }
  return valueArray;
}


var clientErrors = new Array();
var clientErrorCount = 0;

function addClientError(anErrorMessage) {
  clientErrors[clientErrorCount++] = anErrorMessage;
}

function hasClientErrors() {
    return clientErrors.length > 0;
}

function drawClientMessages() {
  var allMessages = "";
  if (clientErrors.length > 0) {
    allMessages += "<ul>";
    for (var i = 0; i < clientErrors.length; i++) {
      allMessages += "<li>" + clientErrors[i] + "</li>";
    }
    allMessages += "</ul>";
    document.getElementById("message").innerHTML = allMessages;
  }
}
var imagePath = "../images/icon/"; 
var feildErrorStyle = "float:left;margin-left:0px;padding:2px;padding-right:10px;border:solid 1px #FF6600;background-color:#FFF2E9;color: black;";
var errorStyle = "float:left;margin-left:0px;padding:3px;border:solid 1px #FF6600;background-color:#FFF2E9;color: black;";
var errorImage = " <img src=\"../images/icon/icon_red.gif\" width=\"14\" height=\"14\" align=\"absmiddle\" /> "; 
var messageStyle = "float:left;margin-left:0px;padding:3px;border:solid 1px #00BE00;background-color:#E6FFE6;color: black;";
var messageImage = " <img src=\"../images/icon/icon_green.gif\" width=\"14\" height=\"14\" align=\"absmiddle\" /> ";
var promptStyle = "float:left;margin-left:0px;padding:3px;border:solid 1px #00A8FF;background-color:#E2F5FF;color: black;";
var promptImage = " <img src=\"" + imagePath + "icon_blue.gif\" width=\"14\" height=\"14\" align=\"absmiddle\" /> ";

var actionErrorBoxStyle = "border:2px #FF6600 solid;background:#FFEACC url('../images/main/action_error_icon.jpg') no-repeat 8px 8px;float:none;margin-top:15px;width:450px;height:76px;text-align:left;";
var actionErrorTextStyle = "margin-left:75px;margin-top:20px;margin-bottom:10px;margin-right:10px;font-size:16px;font-weight:bold;";
var actionMessageBoxStyle = "border:2px #00BE00 solid;background:#E6FFE6 url('../images/main/action_message_icon.gif') no-repeat 8px 8px;float:none;margin-top:15px;width:450px;height:76px;text-align:left;";
var actionMessageTextStyle = "margin-left:75px;margin-top:20px;margin-bottom:10px;margin-right:10px;font-size:16px;font-weight:bold;";

function initImagePath(imagePath){
	if(!imagePath){
		return;
	}
	this.errorImage = " <img src=\"" + imagePath + "icon_red.gif\" width=\"14\" height=\"14\" align=\"absmiddle\" /> ";
	this.messageImage = " <img src=\"" + imagePath + "icon_green.gif\" width=\"14\" height=\"14\" align=\"absmiddle\" /> ";
	this.promptImage = " <img src=\"" + imagePath + "icon_blue.gif\" width=\"14\" height=\"14\" align=\"absmiddle\" /> ";
}

function clearMessages(divId) {
  var div = document.getElementById(divId);
  div.innerHTML = "";
  div.style.cssText = "";
}


function drawClientMessage(divId, message) {
  var div = document.getElementById(divId);
  div.style.cssText = messageStyle;
  div.innerHTML = messageImage + message;
  //window.scrollTo(0, 0);
}


function drawClientError(divId, error) {
  var div = document.getElementById(divId);
  div.style.cssText = errorStyle;
  div.innerHTML = errorImage + error;
  //window.scrollTo(0, 0);
}


function drawMessages(reply, actionErrorDivName) {
  if (!actionErrorDivName) {
    actionErrorDivName = "actionError";
  }

  var result = reply.getResult();
  var allMessages = "";
  
  if (reply.isFault()) { 
    allMessages += "code: " + result.code + "<br />";
    allMessages += "message: " + result.message + "";
    var errorDiv = document.getElementById(actionErrorDivName);
   	var errorBoxDiv = document.getElementById("actionErrorBox");
	if (errorBoxDiv) {
		errorBoxDiv.style.cssText = actionErrorBoxStyle;
		errorDiv.style.cssText = actionErrorTextStyle;
		errorDiv.innerHTML = allMessages;
	}
	else {
		errorDiv.style.cssText = errorStyle;
		errorDiv.innerHTML = errorImage + allMessages;
	}
    return;
  }

  // If return a script, execute it
  if (result.script) {
    eval(result.script);
  }

  if (result.fieldErrors) {
    for (var i in result.fieldErrors) {
      if (i == Buffalo.BOCLASS) {
        continue;
      }
      //alert(i + ":" + result.fieldErrors[i]);
      var errorDiv = document.getElementById(i + "Error");
      //alert(errorDiv.style.cssText);
      if (errorDiv) {
        errorDiv.style.cssText = feildErrorStyle;
        errorDiv.innerHTML = errorImage + result.fieldErrors[i];
      }
    }
  }

  if (result.actionMessages && result.actionMessages.length > 0) {
    var errorDiv = document.getElementById(actionErrorDivName);
    if (errorDiv) {
	  var errorBoxDiv = document.getElementById("actionErrorBox");
	  if (errorBoxDiv) {
		errorBoxDiv.style.cssText = actionMessageBoxStyle;
		errorDiv.style.cssText = actionMessageTextStyle;
		errorDiv.innerHTML = result.actionMessages;
	  }
	  else {
		errorDiv.style.cssText = messageStyle;
		errorDiv.innerHTML = messageImage + result.actionMessages;
	  }
    }
  }
  else if (result.actionErrors && result.actionErrors.length > 0) {
    var errorDiv = document.getElementById(actionErrorDivName);
    if (errorDiv) {
	  var errorBoxDiv = document.getElementById("actionErrorBox");
	  if (errorBoxDiv) {
		errorBoxDiv.style.cssText = actionErrorBoxStyle;
		errorDiv.style.cssText = actionErrorTextStyle;
		errorDiv.innerHTML = result.actionErrors;
	  }
	  else {
		errorDiv.style.cssText = errorStyle;
		errorDiv.innerHTML = errorImage + result.actionErrors;
	  }
    }
  }
}

function drawSuccessMessage(successMessage) {
  var errorDiv = document.getElementById("actionError");
  errorDiv.style.cssText = messageStyle;
  errorDiv.innerHTML = messageImage + successMessage;
}

function alertMessages(reply) {
  var result = reply.getResult();
  var allMessages = "";
 
  if (reply.isFault()) {
    allMessages += "code: " + result.code + "\n";
    allMessages += "message: " + result.message + "\n";
    alert(allMessages);
    return;
  }

  // If return a script, execute it
  if (result.script) {
    eval(result.script);
  }

  if (result.actionMessages && result.actionMessages.length > 0) {
    for (var i = 0; i < result.actionMessages.length; i++) {
      allMessages += result.actionMessages[i] + "\n";
    }
    alert(allMessages);
  }
  else if (result.actionErrors && result.actionErrors.length > 0) {
    for (var i = 0; i < result.actionErrors.length; i++) {
      allMessages += result.actionErrors[i] + "\n";
    }
    alert(allMessages);
  }
}

function hasErrors(reply) {
  return reply.isFault() || hasActionErrors(reply) > 0 || hasFieldErrors(reply) > 0;
}

function hasActionErrors(reply) {
  var result = reply.getResult();
  return result && result.actionErrors && result.actionErrors.length > 0;
}

function hasActionMessages(reply) {
  var result = reply.getResult();
  return result && result.actionMessages && result.actionMessages.length > 0;
}

function hasFieldErrors(reply) {
  var result = reply.getResult();
  if (!result || !result.fieldErrors) {
    return false;
  }

  var errors = result.fieldErrors;
  for (i in errors) {
    return true;
  }

  return false;
}

var buffalo;

if(Buffalo){
	 buffalo = new Buffalo("");
}

/*
buffalo.events.onLoading = function(state) {
    if (state) {  
        window.status = "加载中...";
    }
    else { 
        window.status = "加载完成";
    }
}
*/

function openPrompt(field) {
    var promptMessage = promptMessages[field.name];
    if ("undefined" == promptMessage) {
        return;
    }
    var errorDiv = document.getElementById(field.name + "Error");
    errorDiv.style.cssText = promptStyle;
    errorDiv.innerHTML = promptImage + promptMessage;
}

function closePrompt(field) {
    var errorDiv = document.getElementById(field.name + "Error");
    errorDiv.style.cssText = "";
     
    var promptMessage = promptMessages[field.name];

    if (null == promptMessage) {
        errorDiv.innerHTML = "&nbsp;";
    }
    else{
    	errorDiv.innerHTML = promptMessage;
    }
}
function openActionPrompt(promptContent, actionErrorDivName) {
  if (!actionErrorDivName) {
    actionErrorDivName = "actionError";
  }

  var errorDiv = document.getElementById("actionError");
  errorDiv.style.cssText = promptStyle;
  errorDiv.innerHTML = promptImage + promptContent;
}

function closeActionPrompt(promptMessages, actionErrorDivName) {
  if (!actionErrorDivName) {
    actionErrorDivName = "actionError";
  }

  var actionErrorDiv = document.getElementById(actionErrorDivName);
  actionErrorDiv.innerHTML = "";
  actionErrorDiv.style.cssText = "";
  actionErrorDiv.style.display = "none";
  
  //add by chenzy
  var actionErrorBoxDiv = document.getElementById("actionErrorBox");  
  if(actionErrorBoxDiv){
  	actionErrorBoxDiv.style.cssText = "";
  	actionErrorBoxDiv.style.display = "none";
  }

  if (promptMessages == null) {
    return;
  }

  for (var fieldName in promptMessages) {
    var fieldErrorDiv = document.getElementById(fieldName + "Error");
    if (fieldErrorDiv) {
      fieldErrorDiv.style.cssText = "";
      fieldErrorDiv.innerHTML = "";
    }
  }  
}

function isDate(dateStr, dilimeter) {
  if (dateStr == null) {
    return false;
  }

  if (dilimeter == "" || dilimeter == null) {
    dilimeter = "-";
  }

  var tempy = "";
  var tempm = "";
  var tempd = "";
  var tempArray;
  if (dateStr.length < 8 && dateStr.length > 10) {
    return false;
  }

  tempArray = dateStr.split(dilimeter);
  if (tempArray.length != 3) {
    return false;
  }

  if (tempArray[0].length == 4) {
    tempy = tempArray[0];
    tempd = tempArray[2];
  } else {
    tempy = tempArray[2];
    tempd = tempArray[1];
  }
  tempm = tempArray[1];
  var tDateString = tempy + "/" + tempm + "/" + tempd;
  var tempDate = new Date(tDateString);
  if (isNaN(tempDate)) {
    return false;
  }

  if (((tempDate.getUTCFullYear()).toString() == tempy)
      && (tempDate.getMonth() == parseInt(tempm) - 1)
      && (tempDate.getDate() == parseInt(tempd))) {
    return true;
  } else {
    return false;
  }
}
