// Copyright (c) 2000-2009 SureCode Technologies. All Rights Resvered.


// Globals for setting the target fields...
var LocalFieldChecks = new Array();
var hLastWinTarget;

function scutGetParam(nOffset, sData, sSep) { var tArray  = sData.split(sSep); return tArray[nOffset]; }
function scutFixIt(data) { data = data.replace(/<br>/g,"\n"); data = data.replace(/%22/g,"\""); return data.replace(/%2e/g,"\'");}
function scutCheckPageInits() { if (window.onScPageLoad) { onScPageLoad(); } }
function scutIfIE() { if (document.all) { return true; } else { return false; } }
function scutAsciiCheck(testdata) { asciRE = new RegExp("^[x20-x7f]") ; if (asciRE.test(testdata)) { return true; } else { return false; } }

function scMouseOut(thisImg, orgSRC) { thisImg.src=orgSRC; }
function scMouseOver(thisImg, OVRtag) { 
  var lSrc =  thisImg.src.substring(0, thisImg.src.length-4); 
  var rSrc =  thisImg.src.substring(thisImg.src.length-4, thisImg.src.length); 
  thisImg.src = lSrc + OVRtag + rSrc;
} // end

function scutshowDiv(showIndex, maxIndex) {
	for (i=1; i <= maxIndex; i++) { document.getElementById('sc_div'+i).className = 'sc_hideit'; }
	if (showIndex != '0') { document.getElementById('sc_div'+showIndex).className = 'sc_showit'; }
} // scutshowDiv


function scutsPopUpDivInCenter(divID) {
	
	// Center it
	var scCal2Top = (document.body.clientHeight - document.getElementById(divID).offsetHeight) / 2;
	var scCal2Left = (document.body.clientWidth - document.getElementById(divID).offsetWidth) / 2;

  // Set position
	document.getElementById(divID).style.top = scCal2Top;
	document.getElementById(divID).style.left = scCal2Left;

	// Show it
	document.getElementById(divID).style.visibility = "visible";

} // scutshowDiv

function scutLocalFieldChecks() {

	for (var i=0; i<LocalFieldChecks.length; i++) { 

		// Format is FieldName|Check|Failed Prompt|
		var paramArray  = LocalFieldChecks[i].split("|");

		// Parse the parms..
    var FieldNames = paramArray[0];
		var CheckType = paramArray[1];
		var ErrorPrompt = paramArray[2];
		var Param1 = paramArray[3];
		var Param2 = paramArray[4];

		var FieldNamesArray  = FieldNames.split(",");
		for (var j=0; j<FieldNamesArray.length; j++) { 

		var FieldName = FieldNamesArray[j];		

		// If its not there - skip it...
		if (typeof(document.doit[FieldName]) != "object") { continue; }

		// Get the data value..
		var TheInputData = document.doit[FieldName].value;		

		// Do the correct check based on the type...
		switch (CheckType) {

			case "SIZE":
				if (TheInputData.length < Param1  || TheInputData.length > Param2 ) { alert(ErrorPrompt); return false; }
				break;

			case "SELECTED":
				var selObj = document.doit[FieldName];
				var theOffset = selObj.selectedIndex;
				TheInputData = scutFixIt(selObj.options[theOffset].value);
				if (TheInputData.length < Param1  || TheInputData.length > Param2 ) { alert(ErrorPrompt); return false; }
				break;

			case "CHECKED":
				var getObj = document.doit[FieldName];
				var isChecked = 0;

				// enum over the array looking for a checked value
			  for (ii=0; ii<getObj.length; ii++) { if ( getObj[ii].checked ) {isChecked = 1; }}

				// do check
				if (isChecked == 0) { alert(ErrorPrompt); return false; }
				break;		

			case "DATE2":
				if (TheInputData.length == 0  && Param1 == 0 ) { break; }
				var rxCheckDate = /^(01|02|03|04|05|06|07|08|09|10|11|12)\/(01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\/\d{2}$/ ;
				if (rxCheckDate.test(TheInputData) != true) { alert(ErrorPrompt); return false; }			
				break;

			case "DATE3":
				if (TheInputData.length == 0  && Param1 == 0 ) { break; }
				var rxCheckDate = /^(01|02|03|04|05|06|07|08|09|10|11|12)\/(01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\/\d{4}$/ ;
				if (rxCheckDate.test(TheInputData) != true) { alert(ErrorPrompt); return false; }			
				break;

				case "TIME2":
				if (TheInputData.length == 0  && Param1 == 0 ) { break; }
				var rxCheckDate = /^(01|02|03|04|05|06|07|08|09|10|11|12):[0-5][0-9] (AM|PM)$/ ;
				if (rxCheckDate.test(TheInputData) != true) { alert(ErrorPrompt); return false; }			
				break;

			case "TIME_POP":
				if (TheInputData.length == 0  && Param1 == 0 ) { break; }
				var rxCheckDate = /^(01|02|03|04|05|06|07|08|09|10|11|12):(00|15|30|45) (AM|PM)$/ ;
				if (rxCheckDate.test(TheInputData) != true) { alert(ErrorPrompt); return false; }			
				break;			

			case "REGEX":
				if (TheInputData.length == 0  && Param1 == 0 ) { break; }
				Param2 = Param2.replace(/SLx/g,"\\");
				var rxCheck = new RegExp(Param2);
				if (rxCheck.test(TheInputData) != true) { alert(ErrorPrompt); return false; }				
				break;

			default:
				alert(ErrorPrompt);
				return false;

		} // end switch

	} // end FieldNamesArray enum	
  } // end LocalFieldChecks enum

	return true;

} // end 

function scutSetText( oblSelList, objTestBox ) {
  var theOffset = oblSelList.selectedIndex;
  objTestBox.value = scutFixIt(oblSelList.options[theOffset].value);
} // end scutSetText

function scutGoToSelection( oblSelList ) {
  var theOffset = oblSelList.selectedIndex;
  document.location.href = oblSelList.options[theOffset].value;
} // end scutGoToSelection

function scutGetSelectValue( oblSelList ) {
  var theOffset = oblSelList.selectedIndex;
  return oblSelList.options[theOffset].value;
} // end scutGetSelectValue


// Sel the Next value on the list
function scutNextSelItem ( oblSelList ) {
  var theOffset = oblSelList.selectedIndex;

	if (theOffset == -1) { return 0; }
	if (theOffset == oblSelList.options.length - 1) { alert('No next item available.'); return 0;}
	oblSelList.selectedIndex = theOffset + 1;
	return 1;
} // end scutNextSelItem


// Sel the Prev value on the list
function scutPrevSelItem ( oblSelList ) {
  var theOffset = oblSelList.selectedIndex;
	
	if (theOffset == -1) { return 0; }
	if (theOffset == 0) { alert('No previous item available.'); return 0; }
	oblSelList.selectedIndex = theOffset - 1;
	return 1;
} // end scutPrevSelItem


// Load a select box with a list of dates around a center date
function scutDateLoadList(objListBox, MiddleDay, Range) {

 var dSelDate = new Date(scutGetParam(0,MiddleDay,'-'), scutGetParam(1,MiddleDay,'-')-1, scutGetParam(2,MiddleDay,'-'));
 dSelDate.setTime(dSelDate.getTime() - (1000*60*60*24*Range));
 for(i=0; i < 2*Range+1; i++) {

  Day = dSelDate.getDate();
  YrL = dSelDate.getFullYear();
  Mon = dSelDate.getMonth()+1;
  wDay = dSelDate.getDay();
  Sep = "-";
  DateString = YrL+Sep+fW(Mon)+Sep+fW(Day);

  objListBox[objListBox.length] = new Option(DateString, DateString, objListBox.length); dSelDate.setTime(dSelDate.getTime() + (1000*60*60*24)); }
} // end scutDateLoadList




// Set a group of checkboxes
function scutCheckAll ( oblCheckList, theValue ) {

	// If there is only one checkbox, javascript deals with it differently (???) 		
	if (oblCheckList.length != null) { for (i=0; i<oblCheckList.length; i++) { oblCheckList[i].checked = theValue; } }
	else { oblCheckList.checked = theValue; }

} // end scutCheckAll

function scutSetText2( oblSelList, sSep) {
  var theOffset = oblSelList.selectedIndex;
  for (var i=2; i<arguments.length; i++) { 
    if (arguments[i].type == "text" || arguments[i].type == "textarea" || arguments[i].type == "hidden") {
     arguments[i].value = scutFixIt(scutGetParam(i-2, oblSelList.options[theOffset].value, sSep)); 
    } else if (arguments[i].type == "radio" || arguments[i].type == "checkbox") {

    } // end of type switch
  } // end of argument enum
} // end scutSetText


function scutPopIt(theURL, theWidth, theHeight) {
  var xPos = (screen.width - theWidth)/2;
  var yPos = (screen.height - theHeight)/2;
  var Format = "menu=NO,toolbar=NO,status=NO,scrollbars=YES,resizable=YES,width="+theWidth+",innerWidth="+theWidth+",height="+theHeight+",innerHeight="+theHeight+",top="+yPos+",left="+xPos;
  if (PopGenWindow && PopGenWindow.open) { PopGenWindow.close(); }
  PopGenWindow = window.open(theURL,"TheGenWin", Format);
} // end scutPopIt


function scutAddToList(objTextBox, objListBox) {
  if (objTextBox.value != "") { objListBox[objListBox.length] = new Option(objTextBox.value, objListBox.length); objTextBox.value = ""; objListBox.focus(); }
  else { window.alert("Enter some data and try again."); objTextBox.focus(); }
} // end scutAddToList

function scutDeleteFromList( objListBox ) {
  if (objListBox.selectedIndex > -1) { nDel=objListBox.selectedIndex; objListBox.options[nDel]=null; scutSelectListItem(objListBox, nDel); }
  else { window.alert("No item selected for delete."); objListBox.focus(); }
} // end scutDeleteFromList

function scutSelectListItem( objListBox, nItem ) {
  if (nItem > -1 && objListBox.options.length > nItem) { objListBox.options[nItem].selected = true; }
	else if (nItem == -1) { objListBox.options[objListBox.length-1].selected = true; }
	else if (nItem == -2) { for (i=0; i<objListBox.length; i++) { objListBox.options[i].selected = true; } }
} // end scutSelectListItem

function scutGetSelectList( objListBox, sSep ) {
	sSelList = '';
	for (i=0; i<objListBox.length; i++) { if (objListBox.options[i].selected && String(objListBox.options[i].value).length > 3) { sSelList = sSelList + objListBox.options[i].value + sSep; } } 
	if (sSelList.length > 1) { sSelList = sSelList.substr(0, sSelList.length-1); }
	return sSelList;
} // end scutSelectListItem


function scutDoPush(TheData, targetObj, sAppend) {
  if (sAppend == "DOREPLACE") {  targetObj.value = scutFixIt(TheData); return; }
  if (targetObj.value) { targetObj.value = targetObj.value + scutFixIt(sAppend) + scutFixIt(TheData); }
  else { targetObj.value = scutFixIt(TheData); }
} // end scutDoPush

function scutGetPushData(getObj) {
  var RetString; RetString=getObj.value;

  // IE Only please
	if (document.all) {
    var range = document.selection.createRange();
    if (range.text) {RetString=range.text;}
	} // end of IE check

  return RetString;
} // end scutGetPushData


// Push an array of data from child to parent without getting a javascript error...
function scutDoPushbackArray(sErrorMsg, sOKMsg, sGetName, sTargetName, sAppend) {

	// Do the push for each field...
	var tGetNameArray  = sGetName.split(',');
	var tTargetNameArray  = sTargetName.split(',');
	for (i=0; i<tTargetNameArray.length; i++) {

		var sMsg = "DO_NOTHING";
		if (i == tTargetNameArray.length-1) {sMsg = sOKMsg;}
		scutDoPushback(sErrorMsg, sMsg, tGetNameArray[i], tTargetNameArray[i], sAppend)

	} // end of array enum
} // end scutDoPushbackArray

// Push data from child to parent without getting a javascript error...
function scutDoPushback(sErrorMsg, sOKMsg, sGetName, sTargetName, sAppend) {

 // Baseline checks
 if (opener == null) { alert( scutFixIt(sErrorMsg) ); return; }
 if (typeof(opener.document.doit) != "object") { alert( scutFixIt(sErrorMsg) ); return; }

 // Convert input names to target OBJs if necessary
 var targetObj;
 if (typeof(sTargetName) != "object") { 

   if ( typeof(opener.document.doit[sTargetName] ) == "object" ) { targetObj = opener.document.doit[sTargetName]; } // User pushed object name
   else { alert( scutFixIt(sErrorMsg) ); return; } // Error

 } // end of get target code

 // User pushed actual target OBJ through...
 else { targetObj = sTargetName; }

 // Convert name to source OBJ if necessary
 var getObj;
 if (typeof(sGetName) != "object") { 

   if ( typeof(document.pushback[sGetName] ) == "object" ) { getObj = document.pushback[sGetName]; } // User pushed object name
   else { alert( scutFixIt(sErrorMsg) ); return; } // Error

 } // end of get target code

 // User pushed actual target OBJ through...
 else { getObj = sGetName; }

 // Check them one last time
 if (getObj == null) { alert( scutFixIt(sErrorMsg) ); return; }
 if (targetObj == null) { alert( scutFixIt(sErrorMsg) ); return; }

 // Ready to do the push
 var pushData; pushData=scutGetPushData(getObj); scutDoPush(pushData, targetObj, sAppend);

 // If confirm ask, else just close...
 if ( sOKMsg == "DO_NOTHING") { return; }

 if ( sOKMsg.length > 0 ) {
   if (confirm( scutFixIt(sOKMsg)) ) { window.close(); }
 } else {  window.close(); }

} // end scutDoPushback


// Used the chech for input data fields
function scutCheckIt(sGetName, sErrorMsg) {

 var getObj;

 if (typeof(sGetName) != "object") { 

   if ( typeof(document.doit[sGetName] ) == "object" ) { getObj = document.doit[sGetName]; } // User pushed object name
   else { alert( scutFixIt(sErrorMsg) ); return false; } // Error

 } // end of get target code

 // User pushed actual target OBJ through...
 else { getObj = sGetName; }

 // Check the input one last time
 if (getObj == null) { alert( scutFixIt(sErrorMsg) ); return fasle; }

 // Its a text input style
 if ( getObj.type == "textarea" || getObj.type == "text") {
   // Make sure We have some data 
   if (String(getObj.value).length < 1) { alert( scutFixIt(sErrorMsg) ); return false; }
 } // end of type switch

 // Its a checkbox or radio style
 else if ( getObj[0].type == "radio" || getObj[0].type == "checkbox") {

  // enum over the array looking for a checked value
  for (i=0; i<getObj.length; i++) { if ( getObj[i].checked ) { return true; }}

  // If we get this far its an error
  alert( scutFixIt(sErrorMsg) ); return false; 

 } // end of type switch

 return true;

} // end scutCheckIt


// Show the date and time
function scutShowLocalTime( sFormat ) {

  var curDateTime = new Date();
  var theTag = " a.m.";
  var theDay = curDateTime.getDate();
  if (theDay < 10) { theDay = "0"+theDay.toString(); }
  else { theDay = theDay.toString(); }
  var theMin = curDateTime.getMinutes();
  if (theMin < 10) { theMin = "0"+theMin.toString(); }
  else { theMin = theMin.toString(); } var theHour = curDateTime.getHours();
  if (theHour > 12) { theHour-=12; theTag = " p.m.";}
  else if (theHour == 12) {theTag = " p.m.";}
  else if (theHour == 0) {theHour = 12;}
  var DatePart = (curDateTime.getMonth()+1).toString()+"-"+theDay+"-"+(curDateTime.getFullYear()).toString();
  var TimePart = (theHour).toString()+":"+theMin+theTag;

	// Show it based on the format
	if (sFormat == "DateTime") { document.write(DatePart+" "+TimePart); }
	else if (sFormat == "Date") { document.write(DatePart); }
	else if (sFormat == "Time") { document.write(TimePart); }

} // end scutShowLocalTime



//put all args in call in single quotes UNLESS it is a number
//set theWidth and theHeight as a number
//if theWidth is 'full' the popup will be as wide as the whole screen
//if theHeight is 'full' the popup will be as tall as the whole screen
//if horizAlign is 'left' the popup will align flush left (centered by default)
//if horizAlign is 'right' the popup will align flush right
//if horizAlign is '' or not even in call, the popup will align centered horizontally by default
//if vertPos is '' or not even in call, the popup will align centered vertically by default
function scutPopItControl(theURL, theWidth, theHeight, horizAlign, vertPos) {

  // Setup defauts
  var xPos = (screen.width - theWidth)/2;
  var yPos = (screen.height - theHeight)/2;
  var newAlign = horizAlign;
  var newVertical = vertPos;
  var newWidth = theWidth;
  var newHeight = theHeight;
  var sTargetName = "scGenPopWin";
  var sDisplayStyle = "standard";

  // Do overrides
  if (newAlign == 'left') {var xPos = 0;}
  if (newAlign == 'right') {var xPos = (screen.width - theWidth - 10);}
  if (newVertical > 0) {var yPos = vertPos;}
  if (newHeight == 'full') {var newHeight = (screen.height - 55);}
  if (newWidth == 'full') {var newWidth = (screen.width - 10);}

  // Provide support for option params, sDisplayStyle, target name 
	if (arguments.length > 4) {  sDisplayStyle = arguments[5]; } 
  if (arguments.length > 5) { sTargetName = arguments[6]; } 

	var Format = "menubar=yes,toolbar=yes,status=no,scrollbars=YES,resizable=YES,width="+newWidth+",innerWidth="+theWidth+",height="+newHeight+",  innerHeight="+newHeight+",top="+yPos+",left="+xPos;

  // We are using the help stye...
  if ( sDisplayStyle == 'help' ) { Format = "menubar=NO,toolbar=NO,status=NO,scrollbars=YES,resizable=NO,width="+newWidth+",innerWidth="+theWidth+",height="+newHeight+",  innerHeight="+newHeight+",top="+yPos+",left="+xPos; }
  if ( sDisplayStyle == 'full' ) { Format = "menubar=YES,toolbar=no,status=YES,scrollbars=YES,resizable=YES,width="+newWidth+",innerWidth="+theWidth+",height="+newHeight+",  innerHeight="+newHeight+",top="+yPos+",left="+xPos; }
  
  // If there is already such a window open, close it for force our new one to be on top...
	// var thisPopWindow;
  // if (PopCalWindow && PopCalWindow.open) { PopCalWindow.close(); } 

  // Do it...
  var hWinTarget = window.open(theURL, sTargetName, Format);
  hLastWinTarget = hWinTarget;
  hWinTarget.focus();

} // end scutPopItControl

