var viewPopupWinHeight = 600;

var viewPopupWinWidth = 800;





/*

    Emulates insertAdjacentHTML in all browsers

*/

if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){

	HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode) {

		switch (where){

		case 'beforeBegin':

			this.parentNode.insertBefore(parsedNode,this)

			break;

		case 'afterBegin':

			this.insertBefore(parsedNode,this.firstChild);

			break;

		case 'beforeEnd':

			this.appendChild(parsedNode);

			break;

		case 'afterEnd':

			if (this.nextSibling) 

                this.parentNode.insertBefore(parsedNode,this.nextSibling);

			else

			    this.parentNode.appendChild(parsedNode);

			break;

		}

	}

	HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr) {

		var r = this.ownerDocument.createRange();

		r.setStartBefore(this);

		var parsedHTML = r.createContextualFragment(htmlStr);

		this.insertAdjacentElement(where,parsedHTML)

	}

	HTMLElement.prototype.insertAdjacentText = function(where,txtStr) {

		var parsedText = document.createTextNode(txtStr)

		this.insertAdjacentElement(where,parsedText)

	}

}



function findElement(item) {

/*

    Finds one element in the document by ID

    Returns [false, single element]

*/

    return document.getElementById ? document.getElementById(item) : document.all[item];

}



function findElementInTags(thetag,boolstr,coll) {

/*

    Finds a single (the first) element in the document based on a custom boolean condition

        thetag: name of a tag, eg: "DIV"

        boolstr: what should be evaluated on each tag, eg: "className.indexOf('off') > -1"

        coll: collection to start with instead of document root, eg: findElement("startingdiv");

    Returns [false, single element]

*/

    if(coll)

        var tagColl = coll.getElementsByTagName(thetag);

    else

        var tagColl = document.getElementsByTagName(thetag);

    var theEl = false;

    for(i=0;i<tagColl.length;i++) {;

        if(eval("tagColl[i]." + boolstr)) {

            theEl = tagColl[i];

            break;

        }

    }

    return theEl;

}



function findElementsInTags(thetag,boolstr,coll) {

/*

    Finds all elements in the document based on a custom boolean condition

        thetag: name of a tag, eg: "DIV"

        boolstr: what should be evaluated on each tag, eg: "className.indexOf('off') > -1"

        coll: collection to start with instead of document root, eg: findElement("startingdiv");

    Returns [false, array of elements]

*/

    if(coll)

        var tagColl = coll.getElementsByTagName(thetag);

    else

        var tagColl = document.getElementsByTagName(thetag);

    var theEls = new Array();

    for(i=0;i<tagColl.length;i++)

        if(eval("tagColl[i]." + boolstr))

            theEls[theEls.length] = tagColl[i];

    if(theEls.length > 0)

        return theEls;

    else

        return false;

}



function alternateOneSet(theID) {

/*

    Starting with target object(s), alternates rows of one collection starting at theID

        theID: id of element containing elements to be alternated

*/

    var theDiv = findElement(theID);

    var allLines = findElementsInTags("DIV","className.indexOf('line') > -1",theDiv);

    for(var i=0; i<allLines.length; i++) {

        if(i % 2 != 0) {

            var theString = allLines[i].className;

            if(theString == "line") 

                allLines[i].className = "line colored";

            if(theString == "line last") 

                allLines[i].className = "line colored last";

        }

    }   

}



function alternateRows(stringArg, argIsArray) {

/*

    Starting with target object(s), alternates rows of one or more collections

        stringArg: id or ids of starting object, eg: "startingdiv" or "sdiv1/sdiv2/sdiv3"

        argIsArray: whether stringArg is multiple ids or not, eg: [true, false]

*/

    if(argIsArray && stringArg.indexOf("/") > -1) {

        var idArray = stringArg.split("/");

        for(var i=0; i<idArray.length; i++)

            alternateOneSet(idArray[i]);

    } else

        alternateOneSet(stringArg)

}



function popWindow(thePage) {

/*

    Opens a popup window to a specific height and width

*/

    ww = viewPopupWinWidth;

    wh = viewPopupWinHeight;

    w1 = window.open(thePage,'w1','width='+ww+',height='+wh+',status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');

    if(window.focus) w1.window.focus();

}



function validateSelects(form){

/*

    Validates ALL select boxes in a form.

*/

    for(var i=0;i<form.elements.length;i++)

        if((form.elements[i].selectedIndex>-1))

            if((form.elements[i].selectedIndex==0) && !(form.elements[i].disabled == true)){

                alert('Please select a '+form.elements[i].name.toLowerCase());

                return false;

            }

    return true;

}



function validateQuantities(form) {

/*

    Validates AT LEAST one vwquantity boxes in a form.

*/

    var numValid = 0;

    for(var i=0;i<form.elements.length;i++)

        if((form.elements[i].name.indexOf("vwquantity")>-1))

            if(parseInt(form.elements[i].value) > 0)

                numValid++;

    if(numValid > 0)

        return true;

    else {

        alert('Please select at least one quantity');

        return false;

    }

}