// **************************************************************
// ** Returns true if the given email is a valid address, false otherwise.
// **************************************************************
function isEmailValid(email) {
  var emailRegexp=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/
  return emailRegexp.test(email)
}

// **************************************************************
// ** Functionality for the nextfield function
// **************************************************************
var lastKeyPressed;
document.onkeypress = setLastKeyPressed;

function setLastKeyPressed(e)
{
	if (e != null)
		lastKeyPressed = e.which;
	else
		lastKeyPressed = false;
}

function nextfield(fldobj,elmid,frmname,maxchar,minchar,pelmid)
{
	if ((fldobj.value.length > minchar) && (fldobj.value.length < maxchar)) {
		fldobj.focus();
	}
	else if(fldobj.value.length == maxchar)
	{
		document.getElementById(elmid).focus()
	}
	else if ((fldobj.value.length == minchar) && ((lastKeyPressed == 8) || (!lastKeyPressed))) {
			document.getElementById(pelmid).focus();
			$(pelmid).select();
	}
}

// **************************************************************
// ** Limit the characters that can be entered into a field.
// **
// ** Usage: return charFilter(event,'filtertype')
// **
// ** Available filter types:
// **   int         - returns only numbers
// **   emailUser   - returns only valid email username characters
// **************************************************************
var changedVal;

function charFilter(e,f) {
    var key;
    var keychar;
    var reg;
    changedVal = false;

    if (window.event) key = e.keyCode;
    else if (e.which) key = e.which;
    else return true;

    keychar = String.fromCharCode(key);

    switch (f) {
        case 'int':
            reg = /\x08|\d/;
            break;

        case 'emailUser':
            reg = /\x08|\.|-|\w/;
            break;

        // Use int filter by default
        default:
            reg = /\x08|\d/;
            break;
    }

    changedVal = reg.test(keychar);
    return reg.test(keychar);
}

// Disable the button to prevent multiple clicks and in turn multiple submissions.
function submitClick(btn) {
    btn.disabled = true;
    btn.className = "btn_disabled";
}



// =============================================================================
// addClassName
//
// Description : adds a class to the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement                - element to manipulate
//    strClass                  - class name to add
//    blnMayAlreadyExist        - boolean that triggers check for duplicate class
//
function addClassName(objElement, strClass, blnMayAlreadyExist) {

    // if there is a class
    if (objElement.className) {

        // the classes are just a space separated list, so first get the list
        var arrList = objElement.className.split(' ');

        // if the new class name may already exist in list
        if (blnMayAlreadyExist) {

            // get uppercase class for comparison purposes
            var strClassUpper = strClass.toUpperCase();

            // find all instances and remove them
            for (var i = 0; i < arrList.length; i++) {

                // if class found
                if (arrList[i].toUpperCase() == strClassUpper) {

                    // remove array item
                    arrList.splice(i, 1);

                    // decrement loop counter as we have adjusted the array's contents
                    i--;
                }
            }
        }

        // add the new class to end of list
        arrList[arrList.length] = strClass;

        // add the new class to beginning of list
        // arrList.splice(0, 0, strClass);

        // assign modified class name attribute
        objElement.className = arrList.join(' ');

    // if there was no class
    } else {

        // assign modified class name attribute
        objElement.className = strClass;
    }
}
//
// addClassName
// =============================================================================


// =============================================================================
// hasClassName
//
// Description : returns boolean indicating whether the object has the class name
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement                - element to manipulate
//    strClass                  - class name to add
//
function hasClassName(objElement, strClass) {

    // if there is a class
    if (objElement.className) {

        // the classes are just a space separated list, so first get the list
        var arrList = objElement.className.split(' ');

        // get uppercase class for comparison purposes
        var strClassUpper = strClass.toUpperCase();

        // find all instances and remove them
        for (var i = 0; i < arrList.length; i++) {

            // if class found
            if (arrList[i].toUpperCase() == strClassUpper) {

                // we found it
                return true;
            }
        }
    }

    // if we got here then the class name is not there
    return false;
}
//
// hasClassName
// =============================================================================


// =============================================================================
// removeClassName
//
// Description : removes a class from the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement                - element to manipulate
//    strClass                  - class name to remove
//
function removeClassName(objElement, strClass) {

    // if there is a class
    if (objElement.className) {

        // the classes are just a space separated list, so first get the list
        var arrList = objElement.className.split(' ');

        // get uppercase class for comparison purposes
        var strClassUpper = strClass.toUpperCase();

        // find all instances and remove them
        for ( var i = 0; i < arrList.length; i++ ) {

            // if class found
            if (arrList[i].toUpperCase() == strClassUpper) {

                // remove array item
                arrList.splice(i, 1);

                // decrement loop counter as we have adjusted the array's contents
                i--;
            }
        }

        // assign modified class name attribute
        objElement.className = arrList.join(' ');
    }

    // if there was no class
    // there is nothing to remove
}
//
// removeClassName
// =============================================================================










// =============================================================================
// START: selectTab() - change tab styles, update content
//

function selectTab(id, tabs_displayed, section) {
    if (typeof(ClickTaleExec) == 'function') {
        ClickTaleExec("selectTab('"+id+"', '"+tabs_displayed.id+"', '"+section+"')"); // if clicktale exists
    }
    
    var tab1 = "tab1_" + section + "_" + id;
    var tab2 = "tab2_" + section + "_" + id;
    var footerSpan = 'footer_' + section + '_' + id;
    
    for (i = 0; i < tabs_displayed; i++) {

        var current_tab1 = "tab1_" + section + "_" + i;
        var current_tab2 = "tab2_" + section + "_" + i;
        var current_content = "content_" + section + "_" + i;
        var currentFooter = "footer_" + section + "_" + i

        if (tab1 == current_tab1 && tab2 == current_tab2) {

            if ($(current_content)) { 
                $(current_content).style.display = "block";
            }

            addClassName($(current_tab1), "tab1_selected");
            addClassName($(current_tab2), "tab2_selected");

        } else {

            if ($(current_content)) {
                $(current_content).style.display = "none";
            }

            removeClassName($(current_tab1), "tab1_selected");
            removeClassName($(current_tab2), "tab2_selected");
        }
        
        if ($(currentFooter)) {
            if (footerSpan == currentFooter) { 
                $(currentFooter).style.display = "block";
            } else { 
                $(currentFooter).style.display = "none";
            }
        }
    }    
}

//
// END: selectTab()
// =============================================================================

// =============================================================================
// START: getElementsByClassName() - retrieves an array of elements using
//                                   specified class name
//

function getElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  
  objContElm = objContElm || document;
  
  var objColl = objContElm.getElementsByTagName(strTag);
  
  if (!objColl.length &&  strTag == "*" &&  objContElm.all) {
      objColl = objContElm.all;
  }
  
  var arr = new Array();
 
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
  
  var arrClass = strClass.split(delim);
  
  for (var i = 0, j = objColl.length; i < j; i++) {
    
        var arrObjClass = objColl[i].className.split(' ');
        
        if (delim == ' ' && arrClass.length > arrObjClass.length) {
            continue;
        }
        var c = 0;
        
        comparisonLoop:
        
        for (var k = 0, l = arrObjClass.length; k < l; k++) {
          
            for (var m = 0, n = arrClass.length; m < n; m++) {
            
                if (arrClass[m] == arrObjClass[k]) {
                    c++;
                }
            
                if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
              
                    arr.push(objColl[i]);
                    break comparisonLoop;
                }
            }
        }
    }
    return arr;
}

// To cover IE 5.0's lack of the push method
Array.prototype.push = function(value) {
  this[this.length] = value;
}
