/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Updated by: Mike Weiner :: http://www.wearebent.com 
Original author: Eric King (eric_andrew_king@hotmail.com)
Last Updated: May 2006

Changes: Added parameters for optional scrollbars, resizablility,
menubar, toolbar, addressbar, statusbar, fullscreen. Also tweaked the
implementation a bit - links will now give the user a popup window
even if JavaScript is disabled.

Notes: Some parameters are not cross-browser capable (e.g. fullscreen).
Browsers that do not support these abilities will ignore them.

Usage: The link is written as follows: 
onclick="newWindow(this.href, 'popup', 600, 500, 1, 1, 0, 0, 0, 1, 0);

Usage Description:
"this.href" refers to the URL given in the "a" tag; "'popup'" is the name of the popup window;
600 is the width of the popup window; 500 is the height of the popup window; the numbers that
follow designate whether a property is turned on ("1") or off ("0"), in this order:
scrollbars, resizable, menubar, toolbar, addressbar, statusbar, fullscreen
*/












<!--

function MM_swapImgRestore() { //v2.0

  if (document.MM_swapImgData != null)

    for (var i=0; i<(document.MM_swapImgData.length-1); i+=2)

      document.MM_swapImgData[i].src = document.MM_swapImgData[i+1];

}

 

function MM_preloadImages() { //v2.0

  if (document.images) {

    var imgFiles = MM_preloadImages.arguments;

    if (document.preloadArray==null) document.preloadArray = new Array();

    var i = document.preloadArray.length;

    with (document) for (var j=0; j<imgFiles.length; j++) if (imgFiles[j].charAt(0)!="#"){

      preloadArray[i] = new Image;

      preloadArray[i++].src = imgFiles[j];

  } }

}

 

function MM_swapImage() { //v2.0

  var i,j=0,objStr,obj,swapArray=new Array,oldArray=document.MM_swapImgData;

  for (i=0; i < (MM_swapImage.arguments.length-2); i+=3) {

    objStr = MM_swapImage.arguments[(navigator.appName == 'Netscape')?i:i+1];

    if ((objStr.indexOf('document.layers[')==0 && document.layers==null) ||

        (objStr.indexOf('document.all[')   ==0 && document.all   ==null))

      objStr = 'document'+objStr.substring(objStr.lastIndexOf('.'),objStr.length);

    obj = eval(objStr);

    if (obj != null) {

      swapArray[j++] = obj;

      swapArray[j++] = (oldArray==null || oldArray[j-1]!=obj)?obj.src:oldArray[j];

      obj.src = MM_swapImage.arguments[i+2];

  } }

  document.MM_swapImgData = swapArray; //used for restore

}

//-->



















function newWindow(a_str_windowURL, a_str_windowName, a_int_windowWidth, a_int_windowHeight, a_bool_scrollbars, a_bool_resizable, a_bool_menubar, a_bool_toolbar, a_bool_addressbar, a_bool_statusbar, a_bool_fullscreen) {
  var int_windowLeft = (screen.width - a_int_windowWidth) / 2;
  var int_windowTop = (screen.height - a_int_windowHeight) / 2;
  var str_windowProperties = 'height=' + a_int_windowHeight + ',width=' + a_int_windowWidth + ',top=' + int_windowTop + ',left=' + int_windowLeft + ',scrollbars=' + a_bool_scrollbars + ',resizable=' + a_bool_resizable + ',menubar=' + a_bool_menubar + ',toolbar=' + a_bool_toolbar + ',location=' + a_bool_addressbar + ',statusbar=' + a_bool_statusbar + ',fullscreen=' + a_bool_fullscreen + '';
  var obj_window = window.open(a_str_windowURL, a_str_windowName, str_windowProperties)
    if (parseInt(navigator.appVersion) >= 4) {
      obj_window.window.focus();
    }
}

/**.@author Sagri
Below is the script for redirecting the response of the submission of 
a form to the same area in context of the page where it is called from
without disturbing the other content of the page.
For instance our MagHomeDecorator page contains a page included in it 
named subscribeBox that provide users with some fields to fill the date to subscribe
Once the user subscribes the result of this subscription doesn't take a new page to 
confirm. it will confirm in the same area without even changing the main body of 
MagHomeDecorator.

Usage;
This function can be called for any action that need to be resulted in context relative area.
It will display the result of your action i that area only. What all u need to do is to have ur 
jsp/html page contain a <span> tag that will be replaced by the response data.<b> 

You can call this function using html's "onclick", "onchange" etc. properties.
You need to pass url uri of your action as well as the form name to retrieveURL method
for example  { onclick = "retrieveURL('YourPathr.do?operation=methodName', 'myForm') }
*/

/** .@param  req holds the holds the XMLHttpRequest object  
*/
var req; 
function retrieveURL(url, nameOfFormToPost){
	 url=url+getFormAsString(nameOfFormToPost);
	 //Do the AJAX call
	 req = false;
  if (window.XMLHttpRequest) { 
  try { 
		//Non-IE browsers
			req = new XMLHttpRequest();
        } catch(e) {		
			req = false;
        }
            // branch for IE/Windows ActiveX version
     }else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) { 
		//Set a function to be called after the response from the server is returned.
		req.onreadystatechange = processStateChange; 
		req.open("POST", url, true);//open the connection			 
		req.send(null); // make a call to the server
		
	}
}
    
/**.@throws Exception if server response time timeout*/
function processStateChange(){
	 if (req.readyState == 4) {// Complete 	
     var status = "";
   	try{
    status = req.status;
   	}catch(e){
   	status = "Trouble accessing it";
	}
    if (status == "ok" || status == "OK" || status == 200) { // OK response reached now we can process the response
    //Split the text response into Span elements
    spanElements = 
        splitTextIntoSpan(req.responseText);        
    //Use these span elements to update the page
    replaceExistingWithNewHtml(spanElements);   
   }
   else {
    alert("Problem with browser response:\n Response status " 
        + req.responseText.length );
      }
  }
}

/* Following function makes the request parameter available in the form of a string 
that can be used to send along with the request for the  action class to process them
*/
function getFormAsString(formName){
//Setup the return String
  
	returnString ="";
  	formElements = document.forms[formName].elements;
  	//loop through the array, building up the url in the format '/strutsaction.do&name=value'
  	for ( var i=formElements.length-1; i>=0; --i){
        //we escape (encode) each value
        returnString+="&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
       //formElements 
	 }     
 	//return the values
	return returnString; 
}//getFormAsString ended
 
function splitTextIntoSpan(textToSplit){
 
  //Split the document  
  returnElements=textToSplit. 
            split("</span>")
   //Process each of the elements        
  for(var i=returnElements.length-1;i>=0;--i){
                
    //Remove everything before the 1st span
    spanPos = returnElements[i]. 
             indexOf("<span ");               
                
    //if we find a match, take out 
    //everything before the span
    if(spanPos>0){
          subString=returnElements[i].
              substring(spanPos);
          returnElements[i]=subString;
    } 
  }
  return returnElements;
}//splitTextIntoSpan ends
 /*
  * Replaces html elements in the existing (ie viewable document)
  * with new elements (from the ajax requested document)
  * WHERE they have the same name AND are <span> elements
  * @param newTextElements (output of splitTextIntoSpan)
  *					in the format <span id=name>texttoupdate  
  */
 function replaceExistingWithNewHtml 
        (newTextElements){
 
  //loop through newTextElements
  for(var i=newTextElements.length-1;i>=0;--i){     
    //check that this begins with <span
    if(newTextElements[i]. 
        indexOf("<span ")>-1){
          //get the span name - sits
      // between the 1st and 2nd quote mark
      //Make sure your spans are in the format
      //<span id="someName">NewContent</span>
          startNamePos=newTextElements[i]. 
              indexOf('"')+1;              
      endNamePos=newTextElements[i]. 
              indexOf('"',startNamePos);
      name=newTextElements[i]. 
              substring(startNamePos,endNamePos);
      //get the content - everything 
      // after the first > mark
      startContentPos=newTextElements[i]. 
               indexOf('>')+1; 
      content=newTextElements[i].
               substring(startContentPos);                        
       
     //Now update the existing Document 
     // with this element, checking that 
     // this element exists in the document     
     if(document.getElementById(name)){
                document.getElementById(name). 
                innerHTML = content;
     } 
  }
}
}
