﻿function OpenNewWindow(url, target, width, height){
  window.open(url,target,"toobar=0, menubar=0, resizable=1, location=0, width=" + width + ", height=" + height + ", scrollbars=1"); 
}


function OpenNewWindowResize(url, target, width, height){
  window.open(url,target,"toobar=0, menubar=0, resizable=1, location=0, width=" + width + ", height=" + height + ", scrollbars=0"); 
}


function ShowSecurityCodeWindow(){
  window.open('Images/Security_Code.jpg','_blank','toolbar=0, menubar=0, resizable=0, scrollbar=0, width=540, height=140');
}



function disable(control){
  control.disabled = true;
}



function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}



function SwitchImageButton(ctrl, src){
  ctrl.src = src;
}



// ---------------------------------------------------------
// Phone number formatter, created for Foodry
// For more info, visit:
// http://www.foodry.com/blog
// ---------------------------------------------------------
function formatPhone(elm, e) {
    var keychar;

    // If used in onkeypress, pass in the event and this will
    // grab the character and do the right thing. This allows
    // for a smoother user experience than if the chars are
    // being visibly deleted.
    if (e) {
        var keynum;
        if (window.event) {
            keynum = e.keyCode
        }
        else if (e.which) {
            keynum = e.which
        }

        keychar = String.fromCharCode(keynum)
    }   

    // Allow a backspace to go through, so the user
    // can correct any typos.
    if (/[\b]/.exec(keychar)) {
        return true;
    } else {
        var p = elm.value + keychar;

        // Don't allow a leading 1 or 0. We also strip out all
        // non-numeric characters here to make the formatting
        // easier later on. This could be modified to allow
        // letters if you consider them valid.
        p = p.replace(/^[01]/,"");
        p = p.replace(/\D+/g, "");

        // You can easily change the formatting of the phone
        // number by editing the conditionals below.
        if (p.length > 0 && p.length < 3) {
            p = "("+p;
        }
        else if (p.length >= 3 && p.length < 7) {
            p = "("+p.substring(0,3)+") "+p.substring(3);
        }
        else if (p.length >= 7 && p.length < 10) {
            p = "("+p.substring(0,3)+") "+p.substring(3,6)+"-"+p.substring(6);
        }
        else if (p.length) {
            p = "("+p.substring(0,3)+") "+p.substring(3,6)+"-"+p.substring(6,10);
        }
        elm.value = p;

        return false;
    }
}



function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}



// Removes leading whitespaces
function LTrim( value ) {
  var re = /\s*((\S+\s*)*)/;
  return value.replace(re, "$1");
}



// Removes ending whitespaces
function RTrim( value ) {
  var re = /((\s*\S+)*)\s*/;
  return value.replace(re, "$1");	
}



// Removes leading and ending whitespaces
function trim( value ) {
  return LTrim(RTrim(value));	
}



function showDivOption(ctrl, divCtrl, optionField)
{
 for (i= 0 ; i < ctrl.options.length; i++)
 {      
   if (ctrl.options[i].selected)
   {             
     if (ctrl.options[i].value == optionField)
     {
       divCtrl.style.display = 'block';                  
     }
     else
     {
       divCtrl.style.display = 'none';
     }           
   }
 }
}



function showHideDiv(item)
{
  var state = '';
  
  var dvCtrl = document.getElementById(item);
  
  if(dvCtrl.style.display == null)
  {
    state = 'none';
  }
  else
  {
    state = dvCtrl.style.display;
  }
  
  if(state == 'block')
  {
    dvCtrl.style.display = 'none';                  
  }
  else
  {
    dvCtrl.style.display = 'block';                  
  } 
}




function GetWordCount(value)
{  
  try
  {  
    var content = value;
    var words = 0;
    var chars = 0;
    if (content)
    {
      punctRegX = /[!\.?;,:&_\-\-\{\}\[\]\(\)~#'"]/g;
      content = content.replace(punctRegX, "");
      trimRegX = /(^\s+)|(\s+$)/g;
      content = content.replace(trimRegX, "");
      if (content)
      {
        splitRegX = /\s+/;
        var array = content.split(splitRegX);
        words = array.length;
        chars = content.length;
      }
    }
    return words;
 }
 catch(err)
 {
   return 0;
 }
}