/*
SMT_RENDERING.js
© Nate Grover (www.nategrover.com)
This software is not submitted to the public domain. 
You are free to use it provided:

1) this notice remains intact.
2) you have read and agreed with the usage terms

For usage terms view http://www.nategrover.com/terms.html

Within those restrictions, you may use, incorporate or
extend this software. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/


/**************************************
The Default Renderers

***************************************/

/*
function useDefaultRenderer
@param columnNumber:  the column that will render the data according to the renderertype
@param rendererType:  a SmartTable property indiation which default rendere to apply in the column
There are five default rendererTypes:

LINK_TYPE
EMAIL_LINK_TYPE
TEXT_FIELD_TYPE
RADIO_BUTTON_TYPE
CHECKBOX_TYPE

Each rendering function expects to find certain associative values in the array of row data.
See below for more specifics on each renderer.
*/
function useDefaultRenderer(columnNumber,rendererType){
	switch(rendererType){
		case this.LINK_TYPE:
		   this.p13[columnNumber] = this.LINK_RENDERER;
		   break;
		case this.EMAIL_LINK_TYPE:
		   this.p13[columnNumber] = this.EMAIL_LINK_RENDERER;
		   break;
		case this.TEXT_FIELD_TYPE:
		   this.p13[columnNumber] = this.TEXT_FIELD_RENDERER;
		   break;
		case this.RADIO_BUTTON_TYPE:
		   this.p13[columnNumber] = this.RADIO_RENDERER;
		   break;
		case this.CHECKBOX_TYPE:
		   this.p13[columnNumber] = this.CHECKBOX_RENDERER;
		   break;
	    default:break;
	}
}


/*
function makeLink
@param currentRow:  'currentRow' is a reference available to the method that calls makeLink.
A reference to the array of row data being generated
@param colNum:  'colNum' is a reference available to the method that calls makeLink.
a reference to the current column being generated in the current row
@param theCell:  'theCell' is a reference available to the method that calls makeLink.
a reference to the DomElement of type TD that is going to be added to the table.
*/

/*
  this default renderer expects the row array to have a named value of "href" which 
  is where the link will point to;
*/
function makeLink(currentRow,colNum,theCell){
	var newLink = c1["j"].cloneNode(false);
	newLink.className = this.p35;
	newLink.innerHTML = currentRow[colNum];
	newLink.href = currentRow["href"];
	theCell.appendChild(newLink);
}

/*
  this default renderer expects the row array to have a named value "email" which 
  is where the email link will point to;
*/

function makeEmailLink(currentRow,colNum,theCell){
	var newLink = c1["j"].cloneNode(false);
	newLink.className = this.p35;
	newLink.innerHTML = currentRow[colNum];
	newLink.href = "mailto:" + currentRow["email"];
	theCell.appendChild(newLink);
}
/*
  this default renderer expects the row array to have a named value of "textId" which 
  is the id param of the HTML text element being generated.  This will allow the developer
  of the page to get a reference to any existing text fields for manipulation, and will 
  allow forms to submit these values
*/

function makeTextField(currentRow,colNum,theCell){
    // netscape can't render the text-field properly
	// no value appears in the field if the field is appended dynamically,
	// whether it's a DOM element appended or an HTML string set as .innerHTML, no diff
	var t = c1["h"].cloneNode(false);
	t.type = "text";
	t.value = currentRow[colNum];
    t.id = currentRow["textId"];
	theCell.appendChild(t);
}

/*
  this default renderer expects the row array to have named values: 
        radioValue
        radioName
*/

function makeRadio(currentRow,colNum,theCell){
    // explorer won't render the radio properly as a DOM element, 
	// so we just insert the HTML directly
	// Netscape doesn't recognize radio sets with same name if they are
	// generated dynamically, and there's nothing I know of to fix that.
	if(navigator.userAgent.indexOf("MSIE") == -1){
		var r = c1["h"].cloneNode(false);
		r.type = "radio";
		r.value = currentRow["radioValue"];
		r.name = currentRow["radioName"];
		theCell.innerHTML = currentRow[colNum] + ":";
		theCell.appendChild(r);
	}
	else{
		theCell.innerHTML = currentRow[colNum] + 
			":" + 
			"<INPUT TYPE=RADIO NAME=" + 
			currentRow["radioName"] + 
			" VALUE="  + 
			currentRow["radioValue"] + ">";
	}
}

/*
  this default renderer expects the row array to have named values:
        checkBoxValue
        checkBoxName
*/
function makeCheckbox(currentRow,colNum,theCell){
	var c = c1["h"].cloneNode(false);
	c.type = "checkbox";
	c.value = currentRow["checkboxValue"];
	c.id = currentRow["checkboxId"];
	theCell.innerHTML = currentRow[colNum] + ":";
	theCell.appendChild(c);
}
sp.useDefaultRenderer = useDefaultRenderer;
sp.makeLink = makeLink;
sp.makeEmailLink = makeEmailLink;
sp.makeTextField = makeTextField;
sp.makeRadio = makeRadio;
sp.makeCheckbox = makeCheckbox;
sp.LINK_TYPE = 0;
sp.EMAIL_LINK_TYPE = 1;
sp.TEXT_FIELD_TYPE = 2;
sp.RADIO_BUTTON_TYPE = 3;
sp.CHECKBOX_TYPE = 4;
sp.LINK_RENDERER = "this.makeLink(currentRow,colNum,theCell)";
sp.EMAIL_LINK_RENDERER = "this.makeEmailLink(currentRow,colNum,theCell)";
sp.TEXT_FIELD_RENDERER = "this.makeTextField(currentRow,colNum,theCell)";
sp.RADIO_RENDERER = "this.makeRadio(currentRow,colNum,theCell)";
sp.CHECKBOX_RENDERER = "this.makeCheckbox(currentRow,colNum,theCell)";
