function goToTop()
{
	window.location = "#Top";
}

function goBack()
{
	window.history.go(-1);
}

function checkAll(chkName)
{
	var noChecked = 0;
	var noUnChecked = 0;
	
	for(var i=0; i < document.forms.length; i++)
	{
		for(var j=0; j < document.forms[i].length; j++)
		{
			var eltName = document.forms[i].elements[j].name;
			var eltType = document.forms[i].elements[j].type;
			
			if (eltName == chkName && eltType == "checkbox")
			{
				if (document.forms[i].elements[j].checked)
					noChecked += 1;
				else
					noUnChecked += 1;
			}
		}
	}
	for(var i=0; i < document.forms.length; i++)
	{
		for(var j=0; j < document.forms[i].length; j++)
		{
			var eltName = document.forms[i].elements[j].name;
			var eltType = document.forms[i].elements[j].type;
			
			if (eltName == chkName && eltType == "checkbox")
			{
				if (noChecked > noUnChecked)
					document.forms[i].elements[j].checked = false;
				else
					document.forms[i].elements[j].checked = true;					
			}
		}
	}	
}

function openInNewWindow(sUrl, sTitle)
{
	var sLeft 	= (window.screen.width - 975) / 2;
	var sTop	= (window.screen.height - 600) / 2;
	
	window.open(sUrl, sTitle, 
		"personalbar=no,toolbar=no,status=no,scrollbars=yes,location=no,resizable=yes,menubar=yes,width=975,height=600,left=" + sLeft + ",top=" + sTop);
}

function clearAll(oFrm)
{
	//This removes all the text entries
	for(var i=0; i < oFrm.length; i++)
	{
		var eltType = oFrm.elements[i].type;

		if (eltType != "button" && 
			eltType != "submit" &&
			eltType != "hidden" &&
			eltType != "reset")
		{
			switch (eltType) {
				case 'select-one':
					oFrm.elements[i].selectedIndex = 0;
					break;
				case 'checkbox':
					oFrm.elements[i].value = '0';
					oFrm.elements[i].checked = false;
					break;
				default:
					oFrm.elements[i].value = "";
			}					
		}
	}
}

function moveSelected(oSelFrom, oSelTo)
{
	for (i=oSelFrom.options.length - 1; i >= 0; i--)
	{
		if (oSelFrom.options[i].selected)
		{
			var txt = oSelFrom[i].text;
			var id = oSelFrom[i].value;				
			
			var lenSelTo = oSelTo.options.length;
			oSelTo.options[lenSelTo] = new Option(txt, id);
			oSelFrom.options[i] = null;
		}
	}

	// Sort the list the items were moved to
	var tmpList = new Array(oSelTo.values);
	var hash = new Hashtable();

	for(i = 0; i < oSelTo.length; i++)
	{
		tmpList[i] = oSelTo[i].text;
		hash.put(oSelTo[i].text, oSelTo[i].value);
	}
	
	tmpList.sort(sortHelper);

	for (i = 0; i < tmpList.length; i++)
		oSelTo.options[i] = new Option( tmpList[i], hash.get(tmpList[i]));
}

function sortHelper( x, y )
{
	return ( (x < y ) ? -1 : ( ( x > y ) ? 1 : 0 ) );
}

function checkIsInteger(s)
{   
    var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

/*
* Checks if email address is valid, returns true if valid else false
*/
function isValidEmail(emailStr) 
{
    if (emailStr.length == 0) {
        return true;
    }

    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
    var matchArray=emailStr.match(emailPat);
    if (matchArray == null) {
        return false;
    }

    var user=matchArray[1];
    var domain=matchArray[2];
    if (user.match(userPat) == null) {
        return false;
    }
    
    var IPArray = domain.match(ipDomainPat);
    if (IPArray != null) {
        for (var i = 1; i <= 4; i++) {
            if (IPArray[i] > 255) {
                return false;
            }
        }
        return true;
    }

    var domainArray=domain.match(domainPat);
    if (domainArray == null) {
        return false;
    }
    var atomPat=new RegExp(atom,"g");
    var domArr=domain.match(atomPat);
    var len=domArr.length;
    if ((domArr[domArr.length-1].length < 2) ||
        (domArr[domArr.length-1].length > 3)) {
    
        return false;
    }
    if (len < 2) {
        return false;
    }
    return true;
}


// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;
var usaPhoneCount = 10;

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function isValidInternationalPhone(strPhone)
{
    if (isEmpty(strPhone)) {
        return true;
    }
    s = stripCharsInBag(strPhone,validWorldPhoneChars);
    return (checkIsInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function isValidUSAPhone(strPhone)
{
    //alert("validating USA Phone");
    if (isEmpty(strPhone)) {
        return true;
    }
    s = stripCharsInBag(strPhone, phoneNumberDelimiters);
    return (checkIsInteger(s) && s.length == usaPhoneCount);
}

function isEmpty(strValue) 
{
    var strippedVal = strValue.replace(/^\s+/,'').replace(/\s+$/,'');
    if (strippedVal.length == 0) {
        return true;
    }
    return false;
}

function isPhoneValid(strPhone,countrySelVal) 
{
    if (!isEmpty(strPhone) && !checkIsInteger(stripCharsInBag(strPhone,validWorldPhoneChars))) {
        return " - specify only numeric values for phone:" + strPhone;
    }
    if (countrySelVal.toLowerCase() == "united states") {
        if (!isValidUSAPhone(strPhone)) {
            return " - specify a valid phone number:" + strPhone;
        }
    }
    
    return null;
}

/*
* Variables to store the user entries. 
*/
var to_find = "";             // Variable that acts as keyboard buffer
var to_find_org = "";
var selBox = null;
var onchangeTarget = "";
var timeoutID = "";           // Process id for timer (used when stopping 
                              // the timeout)
timeoutInterval = 200;        // Milliseconds. Shorten to cause keyboard 
                              // buffer to be cleared faster
var timeoutCtr = 0;           // Initialization of timer count down
var timeoutCtrLimit = 3 ;     // Number of times to allow timer to count 
                              // down
/*
* After the timeout interval clears the buffer that stores the
* user entry
*/
function idle()
{
	timeoutCtr += 1
   	if(timeoutCtr > timeoutCtrLimit){
  		to_find="";
       	to_find_org="";
      	timeoutCtr = 0;
      	selBox = onchangeTarget;
      	window.clearInterval(timeoutID);
   	}
}

// Select the item in the select box that matches the user entry
function findMatch(selectBox, objEvent) 
{
  	if (objEvent.keyCode == 16) {
    	return;
  	}
  	
  	onchangeTarget = selectBox.onchange;
  	selectBox.onchange = null;
	selBox = selectBox;

 	// Stop Timer
  	window.clearInterval(timeoutID)

  	var c = String.fromCharCode(objEvent.keyCode);
  	to_find_org += c;
  	c = c.toUpperCase(); 
  	to_find += c;
  
  	// Reset timer
  	timeoutID = window.setInterval("idle()", timeoutInterval);  

  	allWords = selectBox.options;
  	var posLow = 0;
  	var posHigh = selectBox.options.length;
  	var foundIt = false;
  	var st2 = new String(to_find);
  	s2 = st2.toLowerCase();
  	while (posLow <= posHigh) { 
    	posMid = Math.floor((posLow + posHigh) / 2); 
       	s1 = selectBox.options[posMid].text;
        s = selectBox.options[posMid].text;
       	st = new String(s);
       	s = st.toLowerCase();

       	if (s.indexOf(s2) == 0) {
           	foundIt = true;
            selectBox.selectedIndex = posMid;
	       
            break;
       	} else {
           	if (s2 < s) {
              	posHigh = posMid - 1;
            } else {
              	posLow = posMid + 1;
            }
       	}
  	}

  	if (!foundIt) {
     	selectBox.selectedIndex = 0;
  	}
}

// Function to hide or show a bLOCK or Some Tag. 
function expandDiv(blockName, strTag, strAttr){ 
//alert("JS Expand Div: " + '\n\ttag:' + strTag + '\n\tblock:' + blockName + '\n\t strAttr:' + strAttr);
    var elem = document.getElementsByTagName(strTag);
    var elemDiv = document.getElementsByTagName(strTag);

	if (document.images) {
	   var plus = new Image();
	   plus.src = "/images/exp_plus.gif";
	   var minus = new Image();
	   minus.src = "/images/exp_minus.gif";
	}

    if (document.images) {
        if (document[blockName].src == minus.src)
            document[blockName].src = plus.src;
        else
            document[blockName].src = minus.src;
    }
    
    for (var i =0;i<elem.length;i++) 
    { 
        if ((elem[i].getAttribute(strAttr)!=null)&& (elem[i].getAttribute(strAttr)=="yes" ))
        { 
            elem[i].style.display=="none"? 
            	elem[i].style.display="block":
            	elem[i].style.display="none"; 
            for (var j =0;j<elemDiv.length;j++)
            {                        
                if ((elemDiv[j].getAttribute(strAttr)!=null)&&(elemDiv[j].getAttribute(strAttr)=="yes" )&&(elem[i].style.display=="none"))
                {
                    elemDiv[j].style.height="308";
                }
                if ((elemDiv[j].getAttribute(strAttr)!=null)&&(elemDiv[j].getAttribute(strAttr)=="yes" )&&(elem[i].style.display=="block"))
                {
                    elemDiv[j].style.height="256";
                }
            }
        }
    } 
}

