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;

//checks all DataGrid CheckBoxes with the given name with the given value
function dgCheckAll(chkForm, chkID, chkValue) {
	re = new RegExp('.*:' + chkID + '$');  //generated control name starts with a colon
	for(i = chkForm.elements.length-1; i>=0 ; i--) {
//	for(i = 0; i < chkForm.elements.length; i++) {
		elm = chkForm.elements[i];
		if (elm.type == 'checkbox') {
			//elm.checked = chkValue;
			if (re.test(elm.name)) {
				elm.checked = chkValue;
			}
		}
	}
}

function dgCheckState(chkForm, chkID, chkAll) {
	var chkState = true;
	re = new RegExp('.*:' + chkID + '$');  //generated control name starts with a colon

	for(i = chkForm.elements.length-1; i>=0 ; i--) {
		elm = chkForm.elements[i];
		if (elm.type == 'checkbox') {
			if (re.test(elm.name)) {
				chkState = chkState && elm.checked;
			}
		}
	}
	chkForm.elements[chkAll].checked = chkState;
}

function htmlCheckAll(chkID, chkAll, chkValue) {
//	for(i = 0; i < chkID.length; i++) {
	for(i=chkID.length-1; i >= 0; i--) {
		chkID[i].checked = chkValue;
	}

	for(i = 0; i < chkAll.length; i++) {
		chkAll[i].checked = chkValue;
	}
}

function htmlCheckState(chkID, chkAll) {
	var chkState = true;
//	for(i = 0; i < chkID.length; i++) {
	for(i=chkID.length-1; i >= 0; i--) {
		chkState = chkState && chkID[i].checked;
	}

	for (i = 0; i < chkAll.length; i++) {
		chkAll[i].checked = chkState;
	}
}

function SortSubmit(frm, field, datatype, direction, page, pagesize) {
	// frm.elements['Mode'].value='SORT';
	frm.elements['SortField'].value=field;
	frm.elements['SortDataType'].value=datatype;
	frm.elements['SortOrder'].value=direction;
	if (page == '')
		{frm.elements['Page'].value='0';}
	else
		{frm.elements['Page'].value=page;}
	if (page != '')
		{frm.elements['PageSize'].value=pagesize;}
	frm.submit();
}

function onKeyPress (btnSubmit) {
	var keycode;
	if (window.event) {keycode = window.event.keyCode;}
	else if (e) {keycode = e.which;}
	else {return true;}
	if (keycode == 13) {
		//document.frmLogin.btnForgotPassword.focus();
		//document.frmLogin.btnForgotPassword.click();
		document.getElementById(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'; }
	if (!IsVisible && (ns6) && document.getElementById(ctrl)) { document.getElementById(ctrl).style.visibility = 'hidden'; }
	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 textboxRemainingChars_Display(textbox, divID)
{
	var div = document.getElementById(divID);
	if (div)
	{
		if (div.style.display == "none")
			div.style.display = "";
			
		if (textbox.value.length > 0)
		{
			var r = textbox.maxLength - textbox.value.length;
			div.innerHTML = r + " characters left";
		}
		else
			div.innerHTML = "Up to " + textbox.maxLength + " chars.";
		
	}

}

function printpage() {
window.print();  
}

function isinteger() {
	var keycode;
	if (window.event) {keycode = window.event.keyCode;}
	else if (e) {keycode = e.which;}
	else {return true;}
	
	if (keycode < 48 || keycode > 57) {
		event.cancelBubble=true;
		event.returnValue=false;
		return false;}
	else {return true;}
}

/* used by admin tools for dumb behavior of the radEditor */
function OnClientLoad(editor)
{
   var style = editor.GetContentArea().style;
   style.backgroundColor = "white";
   style.color= "black";
}

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] = val;
		}
	}
	return a;
} 

function deliverydate_submit(msg)
{
	if(document.frm.ddlDeliveryState.selectedIndex==0)
	{
		document.frm.ddlDeliveryState.focus();
		return false;
	} else {
		document.getElementById('divDeliveryWait').style.display="block";
		if (document.getElementById('trDeliveryDates')) document.getElementById('trDeliveryDates').style.display="none";
		return true;
	}
}

function product_personal_minimum_confirm(txtQtyID, product_personal_minimum, product_personal_minimum_alert ) {
	txtQty = document.getElementById(txtQtyID);
	if (txtQty) {
		if (txtQty.value < product_personal_minimum) {
			return confirm(product_personal_minimum_alert);
		}
	}
	return true;
}

function site_split_personal(rwid, URL, quantity, width, height, padwidth, padheight) {
	if (quantity > 1) {
		URL = URL + '&Quantity=' + quantity;
		ShowPopupWindow(rwid, URL, width, height, padwidth, padheight);
	}
	return true;
}

/*
function site_split_personal(txtQtyID, rwid, URL, width, height, padwidth, padheight) {
	rv = true;
	txtQty = document.getElementById(txtQtyID);
	if (txtQty) {
		if (txtQty.value > 1) {
			URL = URL + '&Quantity=' + txtQty.value;
			ShowPopupWindow(rwid, URL, width, height, padwidth, padheight);
			rv = true;
		} else {
			var frm = document.forms["frm"];
			var destination=document.getElementById('Destination');
			destination.value='CARTITEM_PERSONALIZE_ALL';
			frm.submit();
		}
	} else if (txtQtyID > 1) {
			URL = URL + '&Quantity=' + txtQtyID;
			ShowPopupWindow(rwid, URL, width, height, padwidth, padheight);
			rv = true;
}
	return rv;
}
*/
function GetRadWindow()
{
	var oWindow = null;
	if (window.radWindow) oWindow = window.radWindow;
	else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
	return oWindow;
}		

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 CloseWindow()
{
		var oWindow = GetRadWindow();
		oWindow.Close();
		return false;
}

// Called when a window is being shown. Good for setting an argument to the window 
function OnPopupShow(radWindow)
{                    
	 // Set the argument object to the radWindow        
  //radWindow.Argument = arg;

  // var oPos=getWindowPosition();
	radWindow.Center();
	
}

// Called when a window is being closed.
function OnPopupClose(radWindow)
{ 
  var oArg = radWindow.Argument;
  
  if (oArg && oArg.Destination) {
		var frm = document.forms["frm"];
		var destination=document.getElementById('Destination');
		destination.value=oArg.Destination;
		frm.submit();
/*
		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;}
			}
		}
*/		
	}
}
// set a validation cookie, this runs on page load because it's outside of any function
setcookie('validate','js',1000*60*60*24*365);

