var ie = /MSIE/.test(navigator.userAgent);
var moz = !ie && navigator.product == "Gecko";

var TabOrder = new Array();

function in_array(arr,item) {
 var i;
 for (i=0; i<arr.length; i++) {
  if (arr[i] == item) {
   return true;
//   break;
  }
 }
 return false;
}

function in_array_index(arr,item) {
 var i,idx;
 for (i=0; i<arr.length; i++) {
  if (arr[i] == item) {
   idx = i;
   break;
  }
 }
 return idx;
}

function fnRound(value, precision) {
 precision = Math.pow(10,precision);
 value = Math.round(value * precision) / precision;
 return value;
}

function SetKeyFocus(evt) {
 var e = (evt) ? evt : (window.event)?window.event:null;
 var src = (e.target) ? e.target : ((e.srcElement)?e.srcElement:null);
 charCode = 0;
 if (e) {
  var charCode = (e.charCode) ? e.charCode : ( (e.keyCode) ? e.keyCode : ((e.which)?e.which:0) );
 }
 if (charCode == 13) {
  if (in_array(TabOrder,src.id)) {
   idx = in_array_index(TabOrder,src.id);
   if (idx == TabOrder.length-1) { idx = TabOrder.length-2; }
   do {
    idx++
    obj = document.getElementById(TabOrder[idx]);
    objParent = document.getElementById(obj.name);
    if (!objParent) { objParent = obj }
   } while ((obj.disabled == 'disabled' || objParent.style.visibility == 'hidden' || objParent.style.display == 'none') && idx < TabOrder.length-1)
  } else {
   obj = document.getElementById(TabOrder[0]);
  }
  if (obj && obj.disabled == '') {
   if (obj.type == 'submit') { obj.click(); }
   else if (obj.tagName.toUpperCase() == 'input'.toUpperCase() && obj.type == 'text') { obj.focus(); obj.select(); }
   else { obj.focus(); }
  }
  return false;
 }
}

function NegateInput(inp,evt) {
 var e = (evt) ? evt : (window.event)?window.event:null;
 var src = (e.target) ? e.target : ((e.srcElement)?e.srcElement:null);
 myChar = 0;
 if (e) {
  var myChar = (e.charCode) ? e.charCode : ( (e.keyCode) ? e.keyCode : ((e.which)?e.which:0) );
 }
 myChar = String.fromCharCode(myChar);
 if (myChar == '-') {
  if (parseFloat(inp.value)) {
   inp.value = -inp.value;
  }
  return false;
 }
 if (myChar == '+') {
  if (parseFloat(inp.value) && inp.value < 0) {
   inp.value = -inp.value;
  }
  return false;
 }
}

function getCookie(name) {
 var oCookies = document.cookie;
 var index = oCookies.indexOf(name + "=");
 if (index == -1) return null;
 index = oCookies.indexOf("=", index) + 1;
 var endstr = oCookies.indexOf(";", index);
 if (endstr == -1) endstr = oCookies.length;
 return unescape(oCookies.substring(index, endstr));
}

function fnChangeCSS(theClass,element,value) {
//documentation for this script at http://www.shawnolson.net/a/503/
 var cssRules;
 if (document.all) {
  cssRules = 'rules';
 } else if (document.getElementById) {
  cssRules = 'cssRules';
 }
 for (var S = 0; S < document.styleSheets.length; S++) {
  for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
   if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
    document.styleSheets[S][cssRules][R].style[element] = value;
   }
  }
 }
}

var newWindow
function popWindow(TheURL, newWidth, newHeight, ShowMenu) {
 var options = 'width=' + newWidth + ',height=' + newHeight
 if (ShowMenu == 1) {
  ShowMenu = 'yes';
  newHeight = newHeight + 60
 };
 if (ShowMenu == 0) {
  ShowMenu = 'no';
  newHeight = newHeight + 30
 };
 options = options + ',menubar='+ShowMenu+',toolbar=no,scrollbars=yes,resizable=yes,status=no'

 if (newWindow && newWindow.focus && !newWindow.closed) {
  newWindow.focus();
  newWindow.resizeTo(newWidth,newHeight);
 } else {
  newWindow = window.open('', 'wPortVN', options);
  if (!newWindow) { alert ('Please disable your pop-up killer!'); return; }
  newWindow.resizeTo(newWidth,newHeight);
 }
 newWindow.moveTo((screen.width-newWidth-10)/2,(screen.height-newHeight-100)/2);
 newWindow.document.location = TheURL;
 newWindow.focus();
}

var popImg
function imgWindow(imgURL, imgWidth, imgHeight) {
 var options;
 if (imgWidth && imgHeight) {
  options = 'width=' + imgWidth + ', height=' + imgHeight + ', scrollbars=no, resizable=no';
 } else {
  options = 'width=500, height=500, scrollbars=no, resizable=no';
 }
  var output = "";
  output += "<html>\n";
  output += "<head>\n";
  output += "<title>image</title>\n";
  output += "</head>\n";
  output += "<script type='text/javascript' language='JavaScript'>\n";

  output += "function setSize() {\n";
  output += " var pic = document.getElementById('pic');\n";
  output += " var loading = document.getElementById('loading');\n";
  output += " window.resizeTo(500, 500);\n";
  output += " var lostWidth = 500 - document.body.clientWidth;\n";
  output += " var lostHeight = 500 - document.body.clientHeight;\n";
  output += " var wndWidth = pic.width + lostWidth;\n";
  output += " var wndHeight = pic.height + lostHeight;\n";
  output += " window.resizeTo(wndWidth,wndHeight);\n";
//  output += " loading.style.display = 'none';\n";
//  output += " pic.style.display = '';\n";
  output += " window.moveTo((screen.width-wndWidth)/2,(screen.height-wndHeight)/2);";
  output += "}\n";

  output += "</script>\n";
  output += "<body style='overflow: hidden;' scrolling='no' onload='javascript: setSize();' marginwidth=0 marginheight=0 topmargin=0 leftmargin=0>\n";
  output += "<a href='javascript: self.close();'>\n";
//  output += "<img style='position: absolute; display: none;' id='pic' src='"+imgURL+"'";
  output += "<img id='pic' src='"+imgURL+"'";
  if (imgWidth && imgHeight) {
   output += "width="+imgWidth+" height="+imgHeight
  }
  output += " border=0 alt='close'></a>\n";
//  output += "<span style='position: absolute;' id='loading'><h1>Loading image...</h1></span>\n";
  output += "</body>\n";
  output += "</html>\n";

  if (popImg && popImg.focus && !popImg.closed) {
   popImg.focus();
  } else {
   popImg = window.open('', 'iPortVN', options);
   if (!popImg) { alert ('Please disable your pop-up killer!'); return; }
   popImg.focus();
  }
  popImg.document.write(output)
  popImg.document.close()
}

// SendMailTo procedure (avoid SPAM) (c) 2004 Boian Georgiev
function ePismo(suf, adr, dom, name) {
 if (name) {
 	window.location = "mail"+"to:"+name+"<"+adr+"@"+dom+"."+suf+">";
 } else {
 	window.location = "mail"+"to:"+adr+"@"+dom+"."+suf;
 }
}

// 10x to GwynforWeb (http://www.experts-exchange.com/M_890682.html)
// http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20917957.html
function properResize(wnd, imageId) {
 if (!wnd) { return; }
 var img = wnd.document.getElementById(imageId);
// if (document.all) {
  wnd.resizeTo(500, 500);
  lostWidth = 500 - wnd.document.body.clientWidth;
  lostHeight = 500 - wnd.document.body.clientHeight;
  wndWidth = img.width + lostWidth;
  wndHeight = img.height + lostHeight;
  wnd.resizeTo(wndWidth,wndHeight);
// } else {
//  wnd.innerWidth = img.width;
//  wnd.innerHeight = img.height;
//  wnd.resizeTo(img.width, img.height);
// }
}

