﻿function addEvent(_elem, _evtName, _fn, _useCapture)
{
    if (_elem == null) return;
   if (typeof _elem.addEventListener != 'undefined')
   {
      if (_evtName === 'mouseenter')
         { _elem.addEventListener('mouseover', mouseEnter(_fn), _useCapture); }
      else if (_evtName === 'mouseleave')
         { _elem.addEventListener('mouseout', mouseEnter(_fn), _useCapture); }
      else
         { _elem.addEventListener(_evtName, _fn, _useCapture); }
   }
   else if (typeof _elem.attachEvent != 'undefined')
   {
      _elem.attachEvent('on' + _evtName, _fn);
   }
   else
   {
      _elem['on' + _evtName] = _fn;
   }
}

var EnterFlag;
function PreventEnter(e)
{
    EnterFlag = false;
    if (e.which == 13)
    {
        // Netscape/Firefox/Opera
        EnterFlag = true;
        return false;
    }
    else if (window.event && e.keyCode == 13)
    {
        // IE
        event.returnValue = false;
        EnterFlag = true;
        return false;
    }
}

function roundNumber(num, dec)
{
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function radToDeg(r)
{
    return r * 180 / Math.PI;
}

function degToRad(d)
{
    return d * Math.PI / 180;
}

function mouseEnter(_fn)
{
   return function(_evt)
   {
      var relTarget = _evt.relatedTarget;
      if (this === relTarget || isAChildOf(this, relTarget))
         { return; }

      _fn.call(this, _evt);
   }
};

function removeChildNodes(ctrl)
{
    while (ctrl.childNodes.length > 0)
    {
        ctrl.removeChild(ctrl.firstChild);
    }
}

function isAChildOf(_parent, _child)
{
   if (_parent === _child) { return false; }
      while (_child && _child !== _parent)
   { _child = _child.parentNode; }

   return _child === _parent;
}

function ismaxlength(obj)
{
    var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
    if (obj.getAttribute && obj.value.length>mlength)
        obj.value=obj.value.substring(0,mlength)
}

function HideModal(behaviorID)
{
    var modal = $find(behaviorID);
    if (modal) modal.hide();
}

function ShowModal(behaviorID)
{
    var modal = $find(behaviorID);
    if (modal) modal.show();
}

function formatPrice(orgPrice)
{
    if (orgPrice == null || typeof(orgPrice) == 'undefined') return "N/A";
    if (isNaN(orgPrice)) return "N/A";
    var orgPriceSplit = orgPrice.toString();
    var numString = orgPriceSplit[0];
    var dec;
    if (orgPriceSplit.indexOf(".") > -1)
    {
        orgPriceSplit = orgPrice.split(".");
        numString = orgPriceSplit[0];
        dec = orgPriceSplit[1];
    }
    else
    {
        numString = orgPriceSplit;
        dec = "00";
    }
    var newString = "";
    var index = 1;
    var trip = 0;
    while(numString.length >= index)
    {
        if (trip!=3)
        {
            //haven’t reached 3 chars yet
            newString = numString.charAt(numString.length - index) + newString;
            //add char
            index++;
            trip++;
        }
        else
        {
            //reached 3 chars, add comma
            newString = "," + newString;
            trip = 0;
        }
    }
    if (dec.length < 2)
    {
        dec = dec + "0";
    }
    else if (dec.length > 2)
    {
        var arr = dec.split('');
        dec = arr[0] + arr[1];
    }
    return ("$" + newString + "." + dec);
}

/// Formats Seconds to Minutes and Seconds
function toMinuteAndSecond(sec)
{
    return Math.floor(sec / 60) + ":" + ((sec % 60) < 10 ? "0":"") + (sec % 60);
}

function shutdownRedirect()
{
    window.location = "http://www.qcindustries.com/";
}

function numbersonly(myfield, e, dec)
{
    var key;
    var keychar;
    
    // ie vs. other browsers
    if (window.event)
       key = window.event.keyCode;
    else if (e)
       key = e.which;
    else
       return true;
    keychar = String.fromCharCode(key);

    
    if ((key==null) || (key==0) || (key==8) || 
        (key==9) || (key==13) || (key==27) ) // control keys
    {
       return true;
    }
    else if ((("0123456789").indexOf(keychar) > -1)) // numbers
    {
       return true;
    }
    else if (dec && (keychar == ".")) // decimal, skip point
    {
       myfield.form.elements[dec].focus();
       return false;
    }
    else // not allowed
    {
       return false;
    }
}

/* Padding Functions */

function PadNum(num, length)
{
    return PadLeft(num.toString(), length, '0');
}

function PadLeft(str, length, paddingChar)
{
    if (!isUndefined(str) && !isUndefined(length))
    {
        if (isUndefined(paddingChar)) paddingChar = ' ';
        while (str.length < length)
        {
            str = paddingChar + str;
        }
    }
    return str;
}

function PadRight(str, length, paddingChar)
{
    if (!isUndefined(str) && !isUndefined(length))
    {
        if (isUndefined(paddingChar)) paddingChar = ' ';
        while (str.length < length)
        {
            str = str + paddingChar;
        }
    }
    return str;
}

function getScrollTop()
{
    if(typeof pageYOffset!= 'undefined')
    {
        //most browsers
        return pageYOffset;
    }
    else
    {
        var B = document.body; //IE 'quirks'
        var D = document.documentElement; //IE with doctype
        D = (D.clientHeight)? D: B;
        return D.scrollTop;
    }
}

function crossBrowserInnerText(element, text)
{
    if (moz) { element.textContent = text; } else { element.innerText = text; }
}

function get_CrossBrowserInnerText(element)
{
    if (moz) { return element.textContent; } else { return element.innerText; }
}

function getControlValue(id)
{
    return ControlValue[ControlID.indexOf(id)];
}

/* True, if the object is not part of a function. */
function isAlien(o) {return isObject(o) && !isFunction(o.constructor);}

/* True, if the object is constructed of the native Array Object */
function isArray(o) {return isObject(o) && o.constructor == Array;}

/* True, if the object is of the meta-type boolean */
function isBoolean(o) {return 'boolean' == typeof o;}

/* True, if the object is an Object with the getMonth method */
function isDate(o) {return isObject(o) && o.getMonth;}

/* True, if the object is set and has children nodes or a node type */
function isDomElement(o) {return o && ('undefined' !== typeof o.childNodes || o.nodeType);}

/* True, if the object is set, the native Event Object is defined, and the object has an event phase */
function isEvent(o){return o && 'undefined' != typeof Event && o.eventPhase;}

/* True, if the object is null */
function isNull(o) {return null === o;}

/* True, if the object is of the meta-type 'function' */
function isFunction(o) {return 'function' == typeof o;}

/* True, if the object is of the meta-type number and is finite as determined by the native function */
function isNumber(o) {return 'number' == typeof o && isFinite(o);}

/* True, if the object is of the meta-type object or function */
function isObject(o) {return (o && 'object' == typeof o) || isFunction(o);}

/* True, if the object has any value, including 0 and false, both of which are normally falsy */
function isSet(o) {return ((o || false === o || 0 === o) && ! isNull(o) && '' !== o && ! isUndefined(o));}

/* True, if the object is of the meta-type 'string' */
function isString(o) {return 'string' == typeof o;}

/* True, if the object is of the meta-type 'undefined' */
function isUndefined(o) {return 'undefined' == typeof o;}

/* True, if the object does not have any members that are not functions */
function isEmpty(o)
{
    var i, v;
    if (isObject(o))
    {
        for (i in o)
        {
            v = o[i];
            var t = isUndefined(v);
            var t1 = isFunction(v);
            if (!t && !t1)
            {
                return false;
            }
        }
    }
    return true;
}

function ArrayWedge(strArray,arrValue,arrValueON)
{ 
    var curArray = strArray.split(',');
    var newArray = new Array(curArray);

    //Remove arrValue if Found
    for (arrIdx=0; arrIdx < curArray.length; arrIdx++)
    {
        if (curArray[arrIdx]==arrValue)
        {
            newArray = curArray.slice(0,arrIdx);
            newArray = newArray.concat(curArray.slice(parseInt(arrIdx,10)+1));
            break;
        }
    }
    //Add arrValue if Value ON
    if (arrValueON)
    {
        if (newArray=='')
        {newArray = arrValue;}
        else
        {newArray.push(arrValue);}
    }
    return newArray;
}

function iniStdDisclaimer()
{
    $('ctl00_hidTempDisclaimerIDs').value=$('ctl00_hidDisclaimerIDs').value;
    var strArray = $('ctl00_hidDisclaimerIDs').value;
    var curArray = strArray.split(',');
    var ctlID;
    
    for (arrIdx=0; arrIdx < curArray.length; arrIdx++)
    { //ctl00_repeatStandardDisclaimer_ctl00_chkDisclaimer_1
        ctlID = 'ctl00_repeatStandardDisclaimer_ctl' + PadNum((parseInt(curArray[arrIdx]) - 1),2) + '_chkDisclaimer_' + (parseInt(curArray[arrIdx]));
        if ($(ctlID))
        {
            $(ctlID).checked=true;
        }
    }
}

function copyToClipboard(text2copy) {
  if (window.clipboardData) {
    window.clipboardData.setData("Text",text2copy);
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
}

// 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 loadXMLDoc(dname)
//{
//    if (window.XMLHttpRequest)
//    {
//        xhttp = new XMLHttpRequest();
//    }
//    else
//    {
//        xhttp = new ActiveXObject("Microsoft.XMLHTTP"); // IE
//    }
//    xhttp.open("GET", dname, false);
//    xhttp.send("");
//    return xhttp.responseXML;
//}

//function displayResult(xmlFile, xsltFile)
//{
//    xml = loadXMLDoc(xmlFile);
//    xsl = loadXMLDoc(xsltFile);
//    if (window.ActiveXObject)
//    {
//        // code for IE
//        return xml; //.transformNode(stylesheet);
//    }
//    else if (document.implementation && document.implementation.createDocument)
//    {
//        // code for Mozilla, Firefox, Opera, etc.
//        xsltProcessor = new XSLTProcessor();
//        xsltProcessor.importStylesheet(xsl);
//        resultDocument = xsltProcessor.transformToFragment(xml, document);
//        return resultDocument;
//    }
//}

//function ShowHelp(helpXML)
//{
//    // todo: read array dumped with appdata attribute names/values for on the fly replace!
//    var xdoc = displayResult(WebRoot + 'Help/HelpXML/' + helpXML, WebRoot + 'Help/HelpXML/Help.xslt');
//    ShowModal('mpeHelp');
//    var importedNode = document._importNode(xdoc, true);
//    if (!document.importNode)
//    {
//        $(strPHID + 'divHelpContent').innerHTML = importedNode.innerHTML;
//    }
//    else
//    {
//        $(strPHID + 'divHelpContent').appendChild(importedNode);
//    }
//}

//if (!document.ELEMENT_NODE) {
//  document.ELEMENT_NODE = 1;
//  document.ATTRIBUTE_NODE = 2;
//  document.TEXT_NODE = 3;
//  document.CDATA_SECTION_NODE = 4;
//  document.ENTITY_REFERENCE_NODE = 5;
//  document.ENTITY_NODE = 6;
//  document.PROCESSING_INSTRUCTION_NODE = 7;
//  document.COMMENT_NODE = 8;
//  document.DOCUMENT_NODE = 9;
//  document.DOCUMENT_TYPE_NODE = 10;
//  document.DOCUMENT_FRAGMENT_NODE = 11;
//  document.NOTATION_NODE = 12;
//}

//document._importNode = function(node, allChildren) {
//  switch (node.nodeType) {
//    case document.ELEMENT_NODE:
//      var newNode = document.createElement(node.nodeName);
//      /* does the node have any attributes to add? */
//      if (node.attributes && node.attributes.length > 0)
//        for (var i = 0; il = node.attributes.length; i < il)
//          newNode.setAttribute(node.attributes[i].nodeName,
//          node.getAttribute(node.attributes[i++].nodeName));
//      /* are we going after children too, and does the node have any? */
//      if (allChildren && node.childNodes && node.childNodes.length > 0)
//        for (var i = 0; il = node.childNodes.length; i < il)
//          newNode.appendChild(document._importNode (node.childNodes[i++], allChildren));
//        return newNode;
//        break;
//    case document.TEXT_NODE:
//    case document.CDATA_SECTION_NODE:
//    case document.COMMENT_NODE:
//        return document.createTextNode(node.nodeValue);
//        break;
//  }
//};

if(typeof(Sys) != 'undefined') Sys.Application.notifyScriptLoaded();