function chkBrowser(){			
	//alert("hi");
	var Opera = (navigator.userAgent.indexOf("Opera")!=-1);
	var MSIE = (navigator.appVersion.indexOf("MSIE")!=-1);
	var Moz = (navigator.userAgent.indexOf("Mozilla/5")!=-1);
	var myframe = document.getElementById("panMain");
	//alert(navigator.appVersion.indexOf("Opera"));
	//alert(MSIE);
	//alert(Moz);
		
	if (Moz)
	{
		//alert("fireFox");
		myframe.style.height="100%";					
	}
	if (navigator.userAgent.indexOf("Opera")!=-1) 
	{
		//alert("IE");
		hgt='100%';
		myframe.style.height=hgt;
		myframe.style.bottom=0;
	}

}

window.onload= function(){    
   chkBrowser();
}

function verifyEmail(sEmail) {

		var at="@"
		var dot="."
		var lat=sEmail.indexOf(at)
		var lstr=sEmail.length
		var ldot=sEmail.indexOf(dot)

		
		if (sEmail.substring(ldot+1,ldot+2)=="") { return false }

		if (sEmail.indexOf(at)==-1){ return false }

		if (sEmail.indexOf(at)==-1 || sEmail.indexOf(at)==0 || sEmail.indexOf(at)==lstr){ return false }

		if (sEmail.indexOf(dot)==-1 || sEmail.indexOf(dot)==0 || sEmail.indexOf(dot)==lstr){ return false }

		if (sEmail.indexOf(at,(lat+1))!=-1){ return false }

		if (sEmail.substring(lat-1,lat)==dot || sEmail.substring(lat+1,lat+2)==dot){ return false }

		if (sEmail.indexOf(dot,(lat+2))==-1){ return false }
		
		if (sEmail.indexOf(" ")!=-1){ return false }
		
		return true					
}

function chkValid(fOK)
{
	var i;
	
	for (i=0; i<document.Form1.elements.length; i++)
	{	
		if (document.Form1.elements[i].attributes['required'] != undefined) 
		{
			var iReq=document.Form1.elements[i].attributes['required'].value;
			switch(iReq)
			{
				case "1":
				   	if (document.Form1.elements[i].type == "select-one")
					{ 	//	alert(trimString(document.Form1.elements[i].value) );
						if(document.Form1.elements[i].value == '' || document.Form1.elements[i][document.Form1.elements[i].selectedIndex].text == "Select One")
						{
							var sMsg = document.Form1.elements[i].attributes['invalidMsg'].value;
							alert (sMsg);
							fOK=0;
							document.Form1.elements[i].focus();
							break;
						}
					}
					else if (document.Form1.elements[i].type == "radio")
					{ 
					    if (validateRadioButton(Form1,document.Form1.elements[i].name))
					    {
					       	var sMsg = document.Form1.elements[i].attributes['invalidMsg'].value;
							alert (sMsg);
							fOK=0;
							document.Form1.elements[i].focus();
							break;
						}
					}
					else if (trimString(document.Form1.elements[i].value) == '')
					{
						var sMsg = document.Form1.elements[i].attributes['invalidMsg'].value;
						alert (sMsg);
						fOK=0;
						document.Form1.elements[i].focus();
						break;
					}
			}
		}
		
		if (fOK != 0)
		{
		    if (document.Form1.elements[i].attributes['chkEmail'] != undefined) 
		    {
		        var iChkEmail = document.Form1.elements[i].attributes['chkEmail'].value;	
			    if(iChkEmail == "1")
			    {
			        if(verifyEmail(document.Form1.elements[i].value) == false)
				    {
				        alert(document.Form1.elements[i].attributes['emailInvalidMsg'].value);
				        document.Form1.elements[i].focus();
				        return false;
				    }
			    }
		    }
		}
		if (!fOK){break;}	
	}
return fOK;
}
function validateRadioButton(frm, btnName)
{
	var btn = frm[btnName]
	var valid = 0
	for (var x = 0;x < btn.length; x++)
	{
		valid = btn[x].checked
		if (valid) {break}
	}
	if (valid)
	    return 0;
	else 
	    return 1;
}

function doCommand(s) {

	var el = document.getElementById('_txtCommand');
	var f = document.getElementById('Form1');
	var isSave = false;
	var isDelete = false;
	var fOK = true;
	//determine if 'save' is a part of the command...  if so, ignore isDirty variable	
	if (s.split('save').length > 1) {isSave = true}
	if (s.split('delete').length > 1) {isDelete = true}
	
	if (s == 'undo') {isDirty = false;}
	
	if (s == 'View' | s == 'view') {		
		var tab = arguments[1];		
		if ((tab.toString()).length > 0) {			
			if (tab < 10000) {
				changeView(tab,1); 
				fOK = false;
			} else {
				fOK = false;
			}
		}
	}
		
	if (isDelete) {
		fOK = window.confirm('Are you sure you wish to delete this record?');
	}
	
	if (isSave) {
		if (self.doValidate) {
			fOK = doValidate();
		} else { 
			fOK = true;
		}
		if (fOK) { isDirty = false }
	}
	
	fOK = chkValid(fOK);	
	if (fOK) {
		
		if (arguments.length > 1) {
			for (var a=1; a < arguments.length; a++) {
				s = s + ',' + arguments[a];
			}
		}
		el.value = s;
	}
    
	if (fOK) {
		
		f.submit();
	}
	
}

function setCommand(s) {

	var el = document.getElementById('_txtCommand');
	var f = document.getElementById('Form1');
	var isSave = false;
	var isDelete = false;
	var fOK = true;
	//determine if 'save' is a part of the command...  if so, ignore isDirty variable	
	if (s.split('save').length > 1) {isSave = true}
	if (s.split('delete').length > 1) {isDelete = true}
	
	if (s == 'undo') {isDirty = false;}
	
	if (s == 'View' | s == 'view') {		
		var tab = arguments[1];		
		if ((tab.toString()).length > 0) {			
			if (tab < 10000) {
				changeView(tab,1); 
				fOK = false;
			} else {
				fOK = false;
			}
		}
	}
		
	if (isDelete) {
		fOK = window.confirm('Are you sure you wish to delete this record?');
	}
	
	if (isSave) {
		if (self.doValidate) {
			fOK = doValidate();
		} else { 
			fOK = true;
		}
		if (fOK) { isDirty = false }

	}

	fOK = chkValid(fOK);
	
	if (fOK) {
		
		if (arguments.length > 1) {
			for (var a=1; a < arguments.length; a++) {
				s = s + ',' + arguments[a];
			}
		}
		el.value = s;
	}

    return fOK;
}

function logTraffic (iPid) {
	
	var el;

	var sQry = location.search;
	sQry = sQry.substr(1);
	sQry = sQry.replace(/=/g,"||");
	sQry = sQry.replace(/&/g,"|AND");
	
	var sURL = location.protocol + '//synergy.relevantweb.com/macrim/site/traffic.aspx?cid=44&pid=' + iPid + '&hip=&ha=' + navigator.userAgent + '&ref=' + document.referrer + '&qry=' + sQry ;


    if (document.getElementById && document.body.style) {
		el = document.getElementById("imgT");
	}
    else if (document.all && !document.getElementById) {
		el = document.all("imgT");
	}
    else if (document.layers) {
		el = document.layers("imgT");
    }
	
	if (el) {
		el.src = sURL;
	}
}

function trimString (str) {
  while (str.charAt(0 ) == ' ')
    str = str.substring(1);
  while (str.charAt(str.length - 1) == ' ')
    str = str.substring(0, str.length - 1);
    //	alert(str);
  return str;
}

//===========================================================================
//Packages Page Script
//===========================================================================
//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
    	window.onload = func;
	} 
	else 
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

/* ===============================================
 * ================ COMMON =======================
 * Some common functions for the basic JavaScript objects
 *
 * ===============================================
 */

String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/, "");
}

// Define methods for the Array data structure.
Array.prototype.indexOf = function(item, start) 
{ 
	for (var i = (start || 0); i < this.length; i++) 
	{ 
		if (this[i] == item) 
		{ 
			return i; 
		} 
	} 
	return -1; 
}

/* ===============================================
 * ============= GET ELEMENTS ====================
 * Methods to help retrieve elements from the DOM.
 *
 * ===============================================
 */

/*
 * Gets all the Elements by given Class Name. 
 * 
 * @param string searchClass - Name of the css class to look for.
 * @param object node - (optional) The node you want to start from. Defaults to 'document' if none is specified. 
 * @param string tagName - (optional) Limit  results by adding a tagName. Defaults to '*' if none is specified.
 * 
 * @returns array - Returns an array containing all the nodes given by the specified className.
 */ 
function getElementsByClass(searchClass, node, tagName)
{
	var	classElements =	new	Array();
	if (node == null)
		node = document;
	if (tagName ==	null)
		tagName	= '*';
	
	var	els	= node.getElementsByTagName(tagName);
	var	elsLen = els.length;
	
	for	(i = 0,	j =	0; i < elsLen; i++)
	{
		if (hasClass(els[i], searchClass))
		{
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/* ===============================================
 * ================ STYLING ======================
 * Methods for manipulating an element's styles (CSS).
 *
 * ===============================================
 */

function getClasses(element)
{
	return element.className.trim().split(/\s+/);
}

function hasClass(element, className)
{
	return getClasses(element).indexOf(className) != -1;
}

function addClass(element, className)
{
    var classes = getClasses(element);
    
    if (classes.indexOf(className) == -1)
    {
        classes.push(className);
        element.className = classes.join(' ');
    }
}

function removeClass(element, className)
{
    var classes = getClasses(element);
    var index = classes.indexOf(className);
    
    if (index != -1)
    {
        classes.splice(index, 1);
        element.className = classes.join(' ');
    }
}

/* ===============================================
 * ================ The EFFECT ======================
 * Methods for adding the Row Highlight Effect.
 *
 * ===============================================
 */

// The methods from my Redux Example
function initRowHighlighting()
{
	if (!document.getElementsByTagName)
		{ return; }

	var tables = getElementsByClass('highlightTable', document, 'table');
	
	for(var i = 0; i < tables.length; i++)
	{
		var table = tables[i];
		//Make sure to use th tags for header row.
		attachRowMouseEvents(table.getElementsByTagName('tr'));
	}
}
function attachRowMouseEvents(rows)
{
	for(var i = 0; i < rows.length; i++)
	{
		var row = rows[i];
		if(i%2 == 1)
		{
			row.onmouseover = function() { addClass(this, 'highlight'); }
			row.onmouseout = function() { removeClass(this, 'highlight'); }
		}
		else
		{
			row.onmouseover = function() { addClass(this, 'highlightAlt'); }
			row.onmouseout = function() { removeClass(this, 'highlightAlt'); }
		}
	}
}

addLoadEvent(initRowHighlighting);
