var ns4 = (document.layers) ? true : false;
var ie4 = (document.all && !document.getElementById) ? true : false;
var ie5 = (document.all && document.getElementById) ? true : false;
var ns6 = (!document.all && document.getElementById) ? true : false;

// Increments persistent cart values
function cart_add(count, subtotal) {
	var divCART_COUNT = document.getElementById('divcart_count');
	var divCART_SUBTOTAL = document.getElementById('divcart_total');
	var CART_COUNT = 0;
	var CART_SUBTOTAL = 0;
	
	if (divCART_COUNT && divCART_SUBTOTAL) {
		CART_COUNT=parseInt(divCART_COUNT.innerHTML);
		CART_SUBTOTAL=parseFloat(divCART_SUBTOTAL.innerHTML.replace('$',''));
		if (!isNaN(CART_COUNT) && !isNaN(CART_SUBTOTAL)) {
			CART_COUNT += parseInt(count);
			CART_SUBTOTAL += parseFloat(subtotal)*parseInt(count);
			divCART_COUNT.innerHTML = CART_COUNT;
			divCART_SUBTOTAL.innerHTML = "$" + CART_SUBTOTAL.toFixed(2);
		}
	}
}

function cart_update(count, subtotal) {
	var divCART_COUNT = document.getElementById('divcart_count');
	var divCART_SUBTOTAL = document.getElementById('divcart_total');
	var CART_COUNT = 0;
	var CART_SUBTOTAL = 0;
	
	if (divCART_COUNT) {
		divCART_COUNT.innerHTML = count;
	}

	if (divCART_SUBTOTAL) {
		divCART_SUBTOTAL.innerHTML = subtotal;
	}
}

function htmlCheckAll(chkID, chkAll, chkValue) {
    var chkState = chkValue;
	if (chkID.length) {
		for(i=chkID.length-1; i >= 0; i--) {
			chkID[i].checked = chkValue;
		}
	} else {
		chkID.checked=chkValue;
	}

	if (chkAll.length) {
		for (i = 0; i < chkAll.length; i++) {
			chkAll[i].checked = chkState;
		}
	} else {
		chkAll.checked = chkState;
	}
}

function htmlCheckState(chkID, chkAll) {
	var chkState = true;
	if (chkID.length) {
		for(i=chkID.length-1; i >= 0; i--) {
			chkState = chkState && chkID[i].checked;
		}
	} else {
		chkState = chkState && chkID.checked;
	}

	if (chkAll.length) {
		for (i = 0; i < chkAll.length; i++) {
			chkAll[i].checked = chkState;
		}
	} else {
		chkAll.checked = chkState;
	}
}

function PageSubmit(frm, page) {
	if (page == '')
		{frm.elements['PageNow'].value='1';}
	else
		{frm.elements['PageNow'].value=page;}
	frm.submit();
}

function HiddenSubmit(frm, field, value) {
	frm.elements[field].value=value;
	frm.submit();
}

function onKeyPress (elem, btnSubmit, e, suffix) {

	//elem is the object where the key press occurred; usually a text input (e.g. ctl00_cphBody_txtKeyword)
	//btnSubmit is the id of the button to click WITHOUT the .net ClientID prefix (e.g. 'btnSubmit')
	//e is event data from FF
	
	//Routine parses elem to determine the ClientID prefix that .net added to elem.
	//The function assumes the button to be clicked is in the same context where the keypress occurred
	//The button id is determined as the ClientID prefix of elem combined with btnID
	//(e.g. elem.id=ctl00_cphBody_txtKeyword and btnSubmit=btnExample then the function finds
	//ctl00_cphBody_btnExample and clicks the button

	//Determine which key was pressed	
	var keycode;
	if (window.event) {keycode = window.event.keyCode;} //IE
	else if (e) {keycode = e.which;} //FF
	else {return true;} // Unknown...return without submit

	//If the key pressed was "enter", parse the inputs and submit the form by clicking the appropriate button
	if (keycode == 13) {
		if (suffix) {
			re = new RegExp('(.*_)(' + suffix +')'); //$1 is the prefix
		} else {
			re = new RegExp('(.*_)(.*)'); //$1 is the prefix
		}
		document.getElementById(elem.id.replace(re,'$1')+btnSubmit).click();
		event.cancelBubble=true;
		event.returnValue=false;
		return false;
	}
	else {return true;}
}

function showDiv(IsVisible, ctrl) {
	if (IsVisible && (ie4 || ie5) && document.all[ctrl]) { document.all[ctrl].style.display = ''; document.all[ctrl].style.visibility = 'visible';}
	if (!IsVisible && (ie4 || ie5) && document.all[ctrl]) { document.all[ctrl].style.display = 'none'; document.all[ctrl].style.visibility = 'hidden'; }
	if (IsVisible && (ns6) && document.getElementById(ctrl)) { document.getElementById(ctrl).style.visibility = 'visible'; document.getElementById(ctrl).style.display = 'block';}
	if (!IsVisible && (ns6) && document.getElementById(ctrl)) { document.getElementById(ctrl).style.visibility = 'hidden'; document.getElementById(ctrl).style.display = 'none';}
	if (IsVisible && ns4 && document.layers[ctrl]) {  document.layers[ctrl].visibility = 'visible'; }
	if (!IsVisible && ns4 && document.layers[ctrl]) { document.layers[ctrl].visibility = 'hidden'; }
	//if (IsVisible && ns4) { document.layers[ctrl].visibility = 'visible'; }
	//if (!IsVisible && ns4) { document.layers[ctrl].visibility = 'hidden'; }
}

function setcookie(name, value, expires)
{
	// expires is an integer representing the number of 
	// milliseconds into the future the cookie should expire
	// oneYear = 1000*60*60*24*365
	// very important to include the path otherwise the cookie scope is a sub-directory
	var today = new Date();
	var expiredate = new Date(today.getTime()+expires);
  if (expires > 0)
		{document.cookie = name + '=' + escape(value) + '; path=/; expires=' + expiredate.toUTCString();}
	else
		{document.cookie = name + '=' + escape(value) + '; path=/';}
}

function txtInputHint_blur(txtInput, hint) {
  var t = document.getElementById(txtInput.id);
  if (t.value == "" || t.value == hint) {
    t.style.color = "#aaa";
    t.value = hint;
  }
}

function txtInputHint_focus(txtInput, hint) {
  var t = document.getElementById(txtInput.id);
  if (t.value == hint) t.value = "";
  t.style.color = "";
}

/* used by admin tools for dumb behavior of the radEditor */
function OnRadEditorLoad(editor)
{
   var style = editor.GetContentArea().style;
   style.backgroundImage = "none";
   style.backgroundColor = "white";
   style.color= "black";
}

//This code is used to provide a reference to the radwindow "wrapper"
function GetRadWindow()
{
	var oWindow = null;
	if (window.radWindow) oWindow = window.radWindow;
	else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
	return oWindow;
}		

function ShowPRODUCT_ADD(rwid, EntityKey, Quantity, URL )
{                    
	var txtQuantity=document.getElementById(Quantity);
	if (txtQuantity) {
		Quantity=txtQuantity.value
	} else {
		Quantity='1'
	}
	var oWnd = window.radopen(URL+'?EntityKey='+EntityKey+'&Quantity='+Quantity, rwid);
}

function ShowPRODUCT_ADD_SELECTED(rwid, chkID, URL )
{       
	var EntityGUID='';
	var delimiter='';
	if (chkID.length) {
		for(i=chkID.length-1; i >= 0; i--) {
			if (chkID[i].checked) {
				EntityGUID=EntityGUID+delimiter+chkID[i].value;
				delimiter=',';
			}
		}
	} else {
		if (chkID.checked) {
			EntityGUID=chkID.value;
		}
	}
	if (EntityGUID) {
		var oWnd = window.radopen(URL+'?EntityGUID='+EntityGUID, rwid);
	} else {
		alert('Please select one or more items to add');
	}
	
}

function ShowPopupWindow(rwid, URL, width, height, padwidth, padheight )
{                    
	var oWnd = window.radopen(URL,rwid);

	if (!width | width==0) {width=630;}
	if (!height | height==0) {height=430;}

	if (padwidth > 0) {width=width+padwidth;}
	if (padheight > 0) {height=height+padheight;}

	oWnd.SetSize(width,height);
	oWnd.Center();
}

function EmailPagePopupWindow(rwid, URL, targetURL, width, height )
{                    
	var oWnd = window.radopen(URL + "&PageURL=" + targetURL, rwid);

	oWnd.SetSize(width,height);
	oWnd.Center();
}

// Called when a window is being shown. Good for setting an argument to the window 
function OnClientShow(radWindow)
{                    
	 // Set the argument object to the radWindow        
  //radWindow.Argument = arg;

  // var oPos=getWindowPosition();
	radWindow.Center();
	
}

// Called when a window is being closed.
function OnClientClose(radWindow)
{ 
	// Destination=CANCEL; Do Nothing
	// Destination=CONTINUE; Try to update persistent cart based on Price and Quantity
	// Destination=CART; Redirect to cart
  var oArg = radWindow.Argument;
  if (oArg && oArg.Destination) {
		if (oArg.Destination == 'CONTINUE' && oArg.Quantity && oArg.Price) {
			cart_update(oArg.Quantity,oArg.Price);
		} else if (oArg.Destination == 'CART') {
			document.location.href = oArg.URL;
		} else if (oArg.Destination == 'REFRESH') {
			Refresh_Parent();
		} else if (oArg.Destination == 'FILE_SEARCH') {
			var src=oArg.Src;
			var frm = document.forms["aspnetForm"];
			var re = new RegExp('^(.*)' + oArg.txtFileSearch +'$'); // Extract "Prefix" from Prefix_txtFileSearch
			var Prefix;
			for (i = frm.elements.length-1; i>=0; i--) {
				elm = frm.elements[i];
				if (re.test(elm.id)) {elm.value=src;}
			}
		}
	}
}

function OpenWindow(name, url, width, height)
{
    var wnd = window.radopen(url, name);          
    wnd.SetSize(width, height);
		wnd.Center();
    return false;
}

function CloseWindow()
{
		var oWindow = GetRadWindow();
		oWindow.Close();
		return false;
}

function Refresh_Parent()
{
//	document.location.reload();
document.getElementById('aspnetForm').submit();
}

function email_validate(btn) {
	var re = new RegExp('^(.*)btn.*$'); // Extract "Prefix" from Prefix_btnWhatever
	if (re.test(btn.id)) {
		var Prefix=RegExp.$1;
		
		var email=document.getElementById(Prefix+"txtEmail");
		if (email) {
			if ( !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value))) {
				alert("Invalid E-mail Address! Please re-enter.");
				return (false);
			}
		}
	}
	return (true);
}

function AssignImage(img, imgURL) {
	document.getElementById(img).src=imgURL;
}

function isDate(source, arguments) {

	var dateStr = arguments.Value;
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2,4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	var baseyear;
	var d;
	
	d = new Date();
	baseyear = d.getFullYear();
	arguments.IsValid=true;

	if (matchArray == null) {
		arguments.IsValid=false;
		return arguments.IsValid;
	}

	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[5];

	if ((year > 99 && year < 1000) || year < 0) { // check year range
		arguments.IsValid=false;
	}
	
	if (year < 50) { //check for short year
		year = parseInt(year)+Math.floor(baseyear / 100.0)*100;
	} else {
		if (year < 100) {
			year = parseInt(year)+Math.floor(baseyear / 100.0)*100-100;
		}
	}

	if (month < 1 || month > 12) { // check month range
		arguments.IsValid=false;
	}

	if (day < 1 || day > 31) { //check day range
		arguments.IsValid=false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) { //check short months
		arguments.IsValid=false;
	}

	if (month == 2) { // check feb allowing for leap year
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			arguments.IsValid=false;
		}
	}
	return arguments.IsValid;
}

function qs() {
	var a = new Array();
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			a[key.toUpperCase()] = val;
		}
	}
	return a;
} 

function form_clear(frm) {
    
	var elements = frm.elements; 
	frm.reset();

	for(i=0; i<elements.length; i++) {
		field_type = elements[i].type.toLowerCase();
		switch(field_type) {
			case "text": 
			case "password": 
			case "textarea":
				elements[i].value = ""; 
				break;
//		case "hidden":	
			case "radio":
			case "checkbox":
				if (elements[i].checked) {
					elements[i].checked = false; 
				}
				break;
			case "select-one":
			case "select-multi":
				elements[i].selectedIndex = -1;
				break;
			default: 
				break;
		}
	}
}

function form_isempty(frm) 
{
	var elements = frm.elements; 

	for(i = 0; i < elements.length; i++) 
	{
		field_type = elements[i].type.toLowerCase();
		switch(field_type) 
		{
			case "text": 
			case "password": 
			case "textarea":			    
				if(elements[i].value != "")
				    return false; 
				break;
			case "radio":
			case "checkbox":
				if (elements[i].checked)
				    return false;
				break;
			case "select-one":
			case "select-multi":
				if (elements[i].selectedIndex > -1 && elements[i].value != "")
				    return false;
				break;
			default: 
				break;
		}
	}
	
	return true;
}

function form_validate(frm) 
{
    if(form_isempty(frm)) 
    {
        alert("All fields cannot be blank.");
        return false;
    }
    
    return true;
}

function check_byprefix(frm,prefix,value) {
	var elements = frm.elements; 
	var re = new RegExp('^(.*)' + prefix +'(.*)$');

	for(i=0; i<elements.length; i++) {
		if (re.test(elements[i].name)) {
			field_type = elements[i].type.toLowerCase();
			switch(field_type) {
				case "radio":
				case "checkbox":
					if (elements[i].value==value) {
						elements[i].checked = true; 
					}
					break;
				default: 
					break;
			}
		}
	}
}

function marcAnalysis_showDiv(expand)
{
    showDiv(expand,'btnCollapseAll');
    showDiv(!expand,'btnExpandAll');
    
    showDiv(expand,'divExpand0000');
    showDiv(expand,'divExpand0100');
    showDiv(expand,'divExpand0200');
    showDiv(expand,'divExpand0300');
    showDiv(expand,'divExpand0400');
    showDiv(expand,'divExpand0500');
    showDiv(expand,'divExpand0600');
    showDiv(expand,'divExpand0700');
    showDiv(expand,'divExpand0800');
    showDiv(expand,'divExpand0900');
    
    showDiv(expand,'btnCollapse0000');
    showDiv(expand,'btnCollapse0100');
    showDiv(expand,'btnCollapse0200');
    showDiv(expand,'btnCollapse0300');
    showDiv(expand,'btnCollapse0400');
    showDiv(expand,'btnCollapse0500');
    showDiv(expand,'btnCollapse0600');
    showDiv(expand,'btnCollapse0700');
    showDiv(expand,'btnCollapse0800');
    showDiv(expand,'btnCollapse0900');

    showDiv(!expand,'btnExpand0000');
    showDiv(!expand,'btnExpand0100');
    showDiv(!expand,'btnExpand0200');
    showDiv(!expand,'btnExpand0300');
    showDiv(!expand,'btnExpand0400');
    showDiv(!expand,'btnExpand0500');
    showDiv(!expand,'btnExpand0600');
    showDiv(!expand,'btnExpand0700');
    showDiv(!expand,'btnExpand0800');
    showDiv(!expand,'btnExpand0900');
}

// set a validation cookie, this runs on page load because it's outside of any function
setcookie('validate','js',1000*60*60*24*365);
 