var GB_ROOT_DIR = "/Tools/Controls/GreyBox_v5_53/greybox/";
document.write("<script type='text/javascript' src='/Tools/Controls/GreyBox_v5_53/greybox/AJS.js'></script>");
document.write("<script type='text/javascript' src='/Tools/Controls/GreyBox_v5_53/greybox/AJS_fx.js'></script>");
document.write("<script type='text/javascript' src='/Tools/Controls/GreyBox_v5_53/greybox/gb_scripts.js'></script>");
document.write("<link href='/Tools/Controls/GreyBox_v5_53/greybox/gb_styles.css' rel='stylesheet' type='text/css' />")
document.write("<link rel='stylesheet' href='/Portals/BackOffice/Styles/ADVISO-Portal-Calendar.css' />");
document.write("<script type='text/javascript' src='/Tools/Functions/calendar_us.js'></script>");

function ForceLogin(tUserID){
	if (tUserID == 0) {
	GB_showCenter("Session Timeout", '/Tools/AJAX/gb_Login.asp?Mode=Portal', 220, 400);
	} else {
	alert ('You are already logged in!');	
	}
}

function openWin( windowURL, windowName, windowFeatures ) {
 window.open( windowURL, windowName, windowFeatures );
}

function CalPop(sInputName, evnt)
{
    /*var properties = "left=" + (evnt.screenX - 100);
    properties += ",top=" + (evnt.screenY + 10);
    properties += ",toolbar=0,width=186,height=167,resizable=no,scrollbars=no,menubar=no,toolbar=no,status=no,location=no,titlebar=no";
	window.open('/Tools/Controls/Calendar/Calendar.asp?tField=' + escape(sInputName) + '&tDate=' + escape(window.eval(sInputName).value), 'CalPop', properties);*/
	GB_showCenter("Calendar", '/Tools/Controls/Calendar/Calendar.asp?tField=' + sInputName + '&tDate=' + escape(document.getElementById(sInputName).value), evnt.screenY + 10, evnt.screenX - 100/*, callbackfunction()*/);
}

function CalPopDual(sInputNameFrom,sInputNameTo,evnt)
{
    var properties = "left=" + (evnt.screenX + 20);
    properties += ",top=" + (evnt.screenY + 1);
    properties += ",toolbar=0,width=186,height=167,resizable=no,scrollbars=no,menubar=no,toolbar=no,status=no,location=no,titlebar=no";
	window.open('/Tools/Controls/Calendar/Calendar.asp?tFieldFrom=' + escape(sInputNameFrom)  + '&tFieldTo=' +  escape(sInputNameTo) + '&tDate=' + escape(window.eval(sInputNameFrom).value)  + '&tDate2=' +  escape(window.eval(sInputNameTo).value), 'CalPopDual', properties);
}

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0) {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;
	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	return result;
}

function validNum(formField,fieldLabel,required)
{
	var result = true;
	var AllowNulls = true;
	if (required.value = "false")
	{
		if (formField.value == "")
		 {
		   AllowNulls = false;
		 }
	 }
	if (AllowNulls &&  !validRequired(formField,fieldLabel))
		result = false;
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	return result;
}

function validInt(formField,fieldLabel,required)
{
	var result = true;
	var AllowNulls = true;
	if (required.value = "false")
	{
		if (formField.value == "")
		 {
		   AllowNulls = false;
		 }
	 }
	if (AllowNulls &&  !validRequired(formField,fieldLabel))
		result = false;
 	if (result)
 	{
 		var num = parseInt(formField.value,10);
 		if (isNaN(num))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	return result;
}

function validDate(formField,fieldLabel,required)
{
	var result = true;
	if (formField.value == "") { // is this a blank field entry
		if (required == true) { // is this a required field entry 
 			alert('"' + fieldLabel +'" is a required field.  Please make an entry');
			formField.focus();		
			result = false;
		}
 	} else { // validate the field entry
 		var elems = formField.value.split("/");
 		result = (elems.length == 3); // should be three components
 		if (result)	{
 			var month = parseInt(elems[0],10);
  			var day = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}
  		if (!result) {
 			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();		
		}
	} 
	return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;
	if (formField.value == "") { // is this a blank field entry
		if (required == true) { // is this a required field entry 
 			alert('"' + fieldLabel + '" is a required field.  Please make an entry');
			formField.focus();		
			result = false;
		}
 	} else { // validate the field entry
		if ( ((formField.value.length < 3) || !isEmailAddr(formField.value)) ) {
			alert('"' + fieldLabel + '" requires a complete email address (ex: yourname@yourdomain.com)');
			formField.focus();
			result = false;
		}
   }
   return result;
}

function JavaReplace(argvalue, x, y) 
{
	if ((x == y) || (parseInt(y.indexOf(x)) > -1)) {
	  errmessage = "replace function error: \n";
	  errmessage += "Second argument and third argument could be the same ";
	  errmessage += "or third argument contains second argument.\n";
	  errmessage += "This will create an infinite loop as it's replaced globally.";
	  alert(errmessage);
	  return false;
	}
	while (argvalue.indexOf(x) != -1) {
	  var leading = argvalue.substring(0, argvalue.indexOf(x));
	  var trailing = argvalue.substring(argvalue.indexOf(x) + x.length, argvalue.length);
	  argvalue = leading + y + trailing;
	}
	return argvalue;
}

function textCounter(field, countfield, maxlimit) 
{
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	else 
		countfield.value = maxlimit - field.value.length;
}
	
function getIndex (e) { 
  var f = e.form.elements; 
  for (var i = 0; i < f.length; i++) 
    if (e == f[i]) 
      return i; 
  return -1; 
} 

function checkMaxLength (e, l) { 
  if (!l) 
    l = e.maxLength; 
  if (e.value.length == l) { 
    e.form.elements[getIndex(e) + 1].focus(); 
    return true; 
  } 
  else return true; 
} 

function ToggleSection(name) {
	var div = document.getElementById(name);
	if (div.style.display != 'none') {
		div.style.display = 'none';
	} else {
		div.style.display = '';
	}
}

function validPassword(formField1, formField2) 
{
    var result = false;
    if (formField1.value != formField2.value) {
        //formField1.value = '';
        formField2.value = '';
        alert('Your passwords do not match');
        formField2.focus();
    } else {     result = true;
    } return result;
}

function checkAll(chkBox, val) { 
    for (var i=0; i<chkBox.length; i++) chkBox[i].checked = val; 
} 

function DualEntry(Parent, Child1, Child2) 
{
		Child1.value = Parent.value;
		Child2.value = Parent.value;
}

function CalculatePercentage(tFieldPC,tFieldAmount,tDenominator) {
	if ( tDenominator > 0 ) { 
	 t = ( parseFloat(JavaReplace(JavaReplace(tFieldAmount.value,"$",""),",","")) / parseFloat(JavaReplace(JavaReplace(tDenominator,"$",""),",","")) ) * 100;
    tFieldPC.value = t + '%';
    } else  { 
    tFieldPC.value = '0.0%';
	}
}

function ApplyPercentage(tFieldAM,tFieldPC,tDenominator) {
	if ( tFieldPC < 1 ) { 
		 t = ( parseFloat(JavaReplace(JavaReplace(tFieldPC.value,"%",""),",","")) * parseFloat(JavaReplace(JavaReplace(tDenominator,"$",""),",","")) );
    } else  { 
		 t = ( parseFloat(JavaReplace(JavaReplace(tFieldPC.value,"%",""),",","")) * parseFloat(JavaReplace(JavaReplace(tDenominator,"$",""),",","")) )/100;
    } 
    tFieldAM.value = (t.toCurrency());
}
		
function RefreshImage(valImageId) {
	var objImage = document.images[valImageId];
	if (objImage == undefined) {
		return;
	}
	var now = new Date();
	objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}

function addelement(cbo, field){
	var newvalue = window.prompt("Please enter the new " + field + " choice:", "");         
	if (newvalue != "" && newvalue != null) {  
		cbo.options[cbo.options.length] = new Option(newvalue,"new-" + newvalue + "");                 
		cbo.selectedIndex = cbo.options.length - 1;
	 }
}

function displayEmail(eMail1,eMail2,eMail3) {  
	var a,b,c,d,e  
	a='<a href=\"mai'  
	b=eMail1  
	c='\"><font color="">'  
	a+='lto:'  
	b+='@'  
	e='</font></a>'  
	b+=eMail2  
	b+='.'  
	b+=eMail3  
	d=b  
	document.write(a+b+c+d+e)  
}  

function isset(varname)  {
  if(typeof( window[ varname ] ) != "undefined") return true;
  else return false;
}

function Mod10(In){
var CardNumber = In;
var evens = "";
var odds = "";
var evenstotal = 0;
var oddstotal = 0;
var Total, temp;
    if(CardNumber.length == 16){
            for(var x=0; x<16;x+=2){
                    odds += CardNumber.charAt(x);
                    evens += CardNumber.charAt(x+1);
            }
            for (var i=0; i < 8; i++){
                    temp = evens.charCodeAt(i)-48;
                    if(temp.length > 1){temp -= 9}
                    evenstotal += temp;
                    oddstotal += odds.charCodeAt(i)-48;
            }
            Total = (oddstotal + evenstotal);
            return (Total == 30 || Total == 40 || Total == 50 || Total == 60)? true : false;

    }else if(CardNumber.length == 13){
            for(var x=0; x<13;x+=2){
                    odds += CardNumber.charAt(x);
                    evens += CardNumber.charAt(x+1);
                    if(x == 12)
                            odds += CardNumber.charAt(x+2);
            }
            for (var i=0; i < 6; i++){
                    temp = evens.charCodeAt(i)-48;
                    if(temp.length > 1){temp -= 9}
                    evenstotal += temp;
                    oddstotal += odds.charCodeAt(i)-48;
            }
            Total = (oddstotal + evenstotal);
            return (Total == 30 || Total == 40 || Total == 50 || Total == 60)? true : false;
    }else{
            return false;
    }
}
function ElementExists(Id){
	return (document.getElementById(Id))? true : false;
}
function DestroyElementById(Id){
	document.getElementById(Id).parentNode.removeChild(document.getElementById(Id));	
}
function CreateSpan(ParentId, Id, Text){
	(ElementExists(Id))? DestroyElementById(Id) : '';
	span = document.createElement('span');
	span.setAttribute('id', Id);
	span.appendChild(document.createTextNode(Text))
	document.getElementById(ParentId).appendChild(span);
}
function SpanReplace(SpanId, NewText){
	Span = document.getElementById(SpanId);
	sp = document.createElement('span');
	sp.setAttribute('id', SpanId);
	sp.appendChild(document.createTextNode(NewText));
	Span.parentNode.replaceChild(sp, Span);
}
function CreateHiddenFormField(ParentId, Id, Value){
	(ElementExists(Id))? DestroyElementById(Id) : '';
	hidden = document.createElement('input');
	hidden.setAttribute('id', Id);
	hidden.setAttribute('name', Id);
	hidden.setAttribute('type', "hidden");
	hidden.setAttribute('maxlength', "255");
	hidden.value = Value;
	hidden.style.width = "100px";
	hidden.style.fontSize = "9pt";
	document.getElementById(ParentId).appendChild(hidden);
}
function CreateRow(ParentId, Id){
	(ElementExists(Id))? DestroyElementById(Id) : '';
	row = document.createElement('tr');
	row.setAttribute('id', Id);
	document.getElementById(ParentId).appendChild(row);
}
function CreateOptionElement(value, text){
	option = document.createElement('option');
	option.value = value;
	option.appendChild(document.createTextNode(text));
	return option;
}
function FormatCurrency(Unformatted){
	Unformatted = (Unformatted.indexOf("$") >= 0)? Unformatted.substr(0, Unformatted.indexOf("$")) + Unformatted.substr(Unformatted.indexOf("$")+1, Unformatted.length) : Unformatted;
	
	if(Unformatted.indexOf(".") >= 0){
		 Unformatted += (Unformatted.substr(Unformatted.indexOf("."), Unformatted.length).length == 1)?	"0" : "";
	} else {
		Unformatted += ".00";	
	}
	Formatted = "$"+Unformatted;
	return Formatted;
}
/*
InternationalCheck(Select Drop down Box Id this.Id for onchange event,
					  Index at which drop down is no longer Intranational,
					  Fields to Hide if International - "id3, id4",
					  Fields to Hide if Intranational - "id1, id2",
					  Function to Eval if International - "SpanReplace(SpanId,NewText), SpanReplace(SpanId2,NewText2)",
					  Funciton to Eval if Intranational - "SpanReplace(SpanId3,NewText3)");
*/
function InternationalCheck(SelectFieldId, Index, FieldSet1, FieldSet2, EvalInternational, EvalIntranational){
	var SelectedIndex = document.getElementById(SelectFieldId).selectedIndex;
	var FS1 = FieldSet1.split(", ");
	var FS2 = FieldSet2.split(", ");
	var EvalInter = EvalInternational.split(", ");
	var EvalIntra = EvalIntranational.split(", ");
	if(SelectedIndex > Index){
		for(var x=0; x<FS1.length; x++){
			document.getElementById(FS1[x]).disabled = 'disabled';
			document.getElementById(FS1[x]).style.display = 'none';
		}
		for(var y=0; y<FS2.length; y++){
			document.getElementById(FS2[y]).disabled = '';
			document.getElementById(FS2[y]).style.display = '';
		}
		for(var z=0; z<EvalInter.length; z++){
			eval(EvalInter[z]);
		}
	}else{
		for(var x=0; x<FS1.length; x++){
			document.getElementById(FS1[x]).disabled = '';
			document.getElementById(FS1[x]).style.display = '';
		}
		for(var y=0; y<FS2.length; y++){
			document.getElementById(FS2[y]).disabled = 'disabled';
			document.getElementById(FS2[y]).style.display = 'none';
		}
		for(var z=0; z<EvalIntra.length; z++){
			eval(EvalIntra[z]);
		}
	}
}

function validateQuantity(theForm, bundle)
{
	quantity = theForm.form.txtQuantity.value;
	if (quantity != '') {
	  if (bundle > 1) {
		if (quantity % bundle) {
			alert('Quantity must be in multiples of ' + bundle);
		} else {
			theForm.form.submitted = true;
		}
	  } else {
		theForm.form.submitted = true;
	  }
	} else {
		alert('Please enter a quantity.');
	}
  return true;
}

function CharacterCount(w,x){
	var y=w.value;
	var r = 0;
	r=y.length;
	x.value=r;
} 

function ImagePop(sInputName, evnt){
	GB_showCenter("Image Preview", sInputName);
}

function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}


