//===============================================================
// WebTechSolutions 2002
//===============================================================

//===============================================================
// Funzione di utilita' lato client
//===============================================================

var sClass;

//===============================================================
// Gestione evento form change
//===============================================================

function ssa1FormChangeEvent(pobjElement) {
	try {
		ssa1FormChange(pobjElement);
	}
	catch(e) {}
}

//===============================================================
// Gestione evento form check
//===============================================================

function mCheckPost(psString) {
	var bCheck = true;
	try {
		bCheck = ssa1FormCheck();
	}
	catch(e) {}

	if (bCheck) mPost(psString);
}

//===============================================================
// setFocus: assegna il focus nelle form al primo campo
//===============================================================

function setFocus() {
	if (document.forms[0]!=null) {
		for (var i = 0; i < document.forms[0].elements.length; i++) {
				if (document.forms[0].elements[i].type != "hidden") {
					try {
					document.forms[0].elements[i].focus();
					break;
					}
					catch(e) {}
				}

		}
	}
} 

//===============================================================
// mPost: effettua i post nelle form
//===============================================================

function mPost(sPostString) {
document.ssa1Form.ssa1Action.value = sPostString;
document.ssa1Form.submit();
}

//===============================================================
// mOvr: funzione utilizzata (mouse over) per modificare la classe nei tag <td>
//===============================================================

function mOvr(src,sClassOver){ 
		sClass = src.className;
		src.className = sClassOver;
}

//===============================================================
// mOut: funzione utilizzata (mouse out) per modificare la classe nei tag <td>
//===============================================================

function mOut(src){ 
		src.className = sClass; 
}

//===============================================================
// mClk: funzione utilizzata (mouse click) gestire il click sui tag <td>
//===============================================================

function mClk(src){

var browserType = navigator.userAgent.toLowerCase();

if (browserType.indexOf('msie') > 0) {
	if(event.srcElement.tagName=='TD')
		src.children.tags('A')[0].click();
}
else {
	for (var i = 0; i < src.childNodes.length; i++) {
//		alert(event.srcElement.tagName);
		if (src.childNodes[i].href != null) {
			try {
				window.location.href = src.childNodes[i].href;
				break;
			}
			catch(e) {}
		}
	}
}
}

//===============================================================
// Funzioni utilizzate per gestire le finestre di popup
//===============================================================

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function


function mOpenPopup(root, mtid, params) {
	var url = root + "ssa1/ssa1DataLookup.asp?MTID=" + mtid + "&ShowHeader=FALSE";
	
	if(params.length > 0) {		
		params = replaceSubstring(params, '\"', '\'');
		url += "&" + params;
	}
	
	var hWnd = window.open(url,"","width=1000,height=680,resizable=yes,status=yes,scrollbars=yes");

	if ((document.window != null) && (!hWnd.opener))
		hWnd.opener = document.window;

}

function mSelectItem(id_fld, id_value, descr_fld, descr_value) {
	var f1 = "ssa1db" + id_fld;
	var f2 = "ssa1db" + descr_fld;
	var v1 = id_value;
	var v2 = descr_value;

	if (window.opener != null) {
		try
		{
			window.opener.document.ssa1Form.elements(f1).value
			window.opener.document.ssa1Form.elements(f1).value = v1;
			window.opener.document.ssa1Form.elements(f2).value = v2;
		}
		catch(e)
		{
			// ignore
		} 			
	}
	
	window.close();
}
