var winW;
var winH;
function getSize() {
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    winW = window.innerWidth + window.pageXOffset;
    winH = window.innerHeight + window.pageYOffset;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    winW = document.documentElement.clientWidth;
    winH = document.documentElement.clientHeight + document.documentElement.scrollTop;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    winW = document.body.clientWidth;
    winH = document.body.clientHeight;
  }
}
function showLoading() {
  getSize();
  document.getElementById('vblock').style.width = (parseFloat(winW))+"px";
  document.getElementById('vblock').style.height=(parseFloat(winH)+5)+"px";
  document.getElementById('vblock').style.left=0;
  document.getElementById('vblock').style.top =0;
  document.getElementById('vblock').style.display = 'block';
  document.getElementById('loading').style.left = winW/2+"px";
  document.getElementById('loading').style.top= "250px";
  document.getElementById('loading').style.display='block';
}
function hideBack(){
  getSize();
  var tail = document.getElementById("tail");
  //document.getElementById('vblock').style.width = (parseFloat(winW)+20)+"px";
  if ( tail != null ) {
     document.getElementById('vblock').style.height=(getElementTop(tail)+75)+"px";
  } else {
     document.getElementById('vblock').style.height=(parseFloat(winH)+150)+"px";
  }
  document.getElementById('vblock').style.width = "100%";
  document.getElementById('vblock').style.left=0;
  document.getElementById('vblock').style.top =0;
  document.getElementById('vblock').style.display = 'block';
}

function createUrl (form, action) {
  var dt = new Date();
  var url = action+"?refresh="+dt.getFullYear()+dt.getMonth()+dt.getDate()+dt.getHours()+dt.getMinutes()+dt.getMilliseconds();
  for (var ix = 0; ix < form.elements.length; ix ++) {
    if ( form.elements[ix].type == 'radio' || form.elements[ix].type == 'checkbox' ) {
        if ( form.elements[ix].checked ) {
            url += "&"+form.elements[ix].name+"="+form.elements[ix].value;
        }
    } else {
      url += "&"+form.elements[ix].name+"="+form.elements[ix].value;
    }
  }
  return url;
}
var popUp = null;
function HideRedPopUp() {
    if ( popUp != null ) {
//alert(1);
        popUp.style.display='none';
//alert(2);
        document.body.removeChild(popUp);
//alert(3);
    }
}
function ShowRedPopUp (obj, image, width, height, head, body) {
    var left = getElementLeft(obj);
    var top  = getElementTop(obj);
    popUp = document.createElement("div");
    if ( navigator.appName == "Microsoft Internet Explorer" ) {
        popUp.style.background = "background-image: url('"+image+"') no-repeat";
    } else {
        popUp.style.backgroundImage = "url('"+image+"')";
        popUp.style.backgroundRepeat = "no-repeat";
    }
    popUp.style.width = width+"px";
    popUp.style.height = height+"px";
    popUp.style.position = 'absolute';
    popUp.style.top = (top-100)+"px";
    popUp.style.left = (left-275)+"px";
    popUp.className = "popup-red";
    popUp.style.display = "block";
    //
    var divHead = document.createElement("div");
    divHead.className = "head";
    divHead.appendChild( document.createTextNode(head) );
    //
    var divBody = document.createElement("div");
    divBody.className = "body";
    divBody.appendChild( document.createTextNode(body) );
    //
    popUp.appendChild(divHead);
    popUp.appendChild(divBody);
    //
    document.body.appendChild(popUp);
    popUp.style.display = 'block';
}

function ShowAddOnPopUp (obj, image, width, height, head, body) {
    var left = getElementLeft(obj);
    var top  = getElementTop(obj);
    popUp = document.createElement("div");
    if ( navigator.appName == "Microsoft Internet Explorer" ) {
        popUp.style.background = "background-image: url('"+image+"') no-repeat";
    } else {
        popUp.style.backgroundImage = "url('"+image+"')";
        popUp.style.backgroundRepeat = "no-repeat";
    }
    popUp.style.width = width+"px";
    popUp.style.height = height+"px";
    popUp.style.position = 'absolute';
    popUp.style.top = (top-28)+"px";
    popUp.style.left = (left+22)+"px";
    popUp.className = "popup-yellow";
    popUp.style.display = "block";
    //
    var divHead = document.createElement("div");
    divHead.className = "head";
    divHead.appendChild( document.createTextNode(head) );
    //
    var divBody = document.createElement("div");
    divBody.className = "body";
    divBody.appendChild( document.createTextNode(body) );
    //
    popUp.appendChild(divHead);
    popUp.appendChild(divBody);
    //
    document.body.appendChild(popUp);
}
function validateEmail(email) {
   if (!email.value) {
      return true;
   }
   var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
   if (!re_mail.test(email.value)) {
      alert('An Invalid E-mail Address');
      email.focus();
      email.select();
      return false;
   }
   return true;
}

function chkNum(obj, nobj, max) {
   if ( obj.value != '' && isNaN(obj.value) ) {
       alert('Not a valid number.');
       obj.select();
       obj.focus();
   }
   if (obj.value.length >= parseFloat(max) && nobj != null) {
        nobj.select();
        nobj.focus();
   }
   return true;
}

function dividePh (phone, ph1, ph2, ph3 ) {
       if ( phone.length ==13) {
          ph1.value = phone.substring(1,4);
          ph2.value = phone.substring(5,8);
          ph3.value = phone.substring(9,13);
       }
       if ( phone.length == 10 ) {
          ph1.value = phone.substring(0,3);
          ph2.value = phone.substring(3,6);
          ph3.value = phone.substring(6,10);
       }
       if ( phone.length ==8) {
          ph1.value = "";
          ph2.value = phone.substring(0,3);
          ph3.value = phone.substring(4,8);

       }
}
function divideDate(date, month, day, year) {
   if ( date != '' ) {
      var dt = date.split("/");
      if ( dt.length == 3 ) {
           month.value = dt[0];
           day.value = dt[1];
           year.value = dt[2];
      }
   }
}

function sumPh (phone, ph1, ph2, ph3 ) {
    phone.value = '';
    if ( ph1.value != null && ph1.value != '' ) {
          phone.value += ph1.value;
    }
    if ( ph2.value != null && ph2.value != '' ) {
          phone.value += ph2.value;
    }
    if ( ph3.value != null && ph3.value != '' ) {
          phone.value += ph3.value;
    }
}
function sumDate(date, month, day, year) {
    date.value = '';
    if ( year.value.length != 4 ) {
         alert('Year should be 4 digits.');
         year.select();
         year.focus();
         return false;
    }
    if ( month.value != null && month.value != '' ) {
         if ( month.value.length < 2 ) {
               month.value = '0'+month.value;
         }
         date.value += month.value;
    }
    if ( day.value != null && day.value != '' ) {
         if ( day.value.length < 2 ) {
               day.value = '0'+day.value;
         }
         date.value += "/"+day.value;
    }
    if ( year.value != null && year.value != '' ) {
         date.value += "/"+year.value;
    }
    return true;
}
function getElementLeft (elem) {
   var xpos;
   xpos = elem.offsetLeft;
   var temp = elem.offsetParent;
   while (temp != null) {
      xpos += temp.offsetLeft;
      temp  = temp.offsetParent;
   }
   return xpos;
}

function getElementTop (elem) {
   var ypos;
   ypos = elem.offsetTop;
   var temp = elem.offsetParent;
   while (temp != null) {
      ypos += temp.offsetTop;
      temp  = temp.offsetParent;
   }
   return ypos;
}

/*
function showPopUp(obj, popId) {
    if (document.layers) {
          document.layers[popId].top = ( getElementTop(obj) + 5 );
          document.layers[popId].top = ( getElementTop(obj) + 5 );
          document.layers[popId].left = ( getElementLeft(obj) + 5 );
          document.layers[popId].display = 'block';
    } else {
          document.getElementById(popId).style.top = ( getElementTop(obj) -300 )+"px";
          document.getElementById(popId).style.left = ( getElementLeft(obj) -600 )+"px";
          document.getElementById(popId).style.display = 'block';
    }
    return false;
}
*/

function closePopUp(popId) {
   if ( document.getElementById(popId) != null ) {
      document.getElementById(popId).style.display = 'none';
   }
   return true;
}
function currencyNumberOnly (n) {
    var inx = 0;
    var num = '';
    var isNegative = 'N';
    if ( n.indexOf('(') > -1 || n.indexOf(')') > -1 ) {
        isNegative = 'Y';
    }
   while ( inx <= n.length ) {
      if ( n.charAt(inx) >= 0 && n.charAt(inx) <= 9 || n.charAt(inx) == '.' || n.charAt(inx) == '-' ) {
         num = num + n.charAt(inx);
      }
      inx ++;
   }
    if ( isNegative == 'Y' ) {
        if ( parseFloat(num) > 0 ) {
             num = (parseFloat(num) * -1);
        }
    }
    return num;
}
function phoneNumberOnly (p) {
   var inx = 0;
   var phone = '';

   while ( inx <= p.length ) {
      if ( p.charCodeAt(inx) >= 48 && p.charCodeAt(inx) <= 57 ) {
         phone = phone + p.charAt(inx);
      }
      inx ++;
   }
   return phone;
}
function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals);
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()

    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {

        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0

        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }

    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length

    if (pad_total > 0) {

        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++)
            value_string += "0"
        }
    return value_string
}
function getDate (dt) {
	 var idx = dt.indexOf("/");
	 var month = dt.substring(0,idx);
	 idx += 1;
	 var idx2 = dt.indexOf("/",idx);
	 var day = dt.substring(idx, idx2);
	 idx2 += 1;
	 var year = dt.substring(idx2, 255);
     var dat = new Date(year, month, day);
	 return dat;
}
function checkDate (month, day, year) {
  var tmp = (parseFloat(month.value)-1)+"/"+day.value+"/"+year.value;
  var date = getDate(tmp);
  if ( parseFloat(month.value) != (parseFloat(date.getMonth())+1) ||
       parseFloat(day.value) != parseFloat(date.getDate()) ||
       parseFloat(year.value) != parseFloat(date.getFullYear()) ||
       parseFloat(date.getFullYear()) < 1900 
     )
  {
     alert('Invalid Date.');
     month.select();
     month.focus();
     return false;
  }
  return true;
}
function checkFutureDate(month, day, year) {
  var tmp = (parseFloat(month.value)-1)+"/"+day.value+"/"+year.value;
  var date = getDate(tmp);
  var curr = new Date();
  if ( date > curr ) {
    alert('Date cannot be in future.');
    month.select();
    month.focus();
    return false;
  }
  return true;
}
function formatCurrency2(num) {
   num = num.toString().replace(/\$|\,/g,'');
   if(isNaN(num)) {
      alert("Not a valid Currency");
      document.forms[form][obj.name].value = 0;
      return false;
   }
   sign = (num == (num = Math.abs(num)));
   num = Math.floor(num*100+0.50000000001);
   cents = num%100;
   num = Math.floor(num/100).toString();
   if(cents<10)
      cents = "0" + cents;
   for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
   num = num.substring(0,num.length-(4*i+3))+','+
   num.substring(num.length-(4*i+3));
   return (((sign)?'':'-') + '' + num + '.' + cents);
}

