/* common js functions */

/**
 * opens a new window and loads the specified url
 * @return reference to the new window
*/
function openInNew(strUrl, strTitle, intWidth, intHeight, blnFocus) {
    if (intWidth==null) {intWidth="300";}
    if (intHeight==null) {intHeight="200";}

	//center the window
    var intLeft = (screen.availWidth-intWidth)/2;
    var intTop = (screen.availHeight-intHeight)/2;

	//replace all spaces
	strTitle=strTitle.replace(" ","_");

    var newWin=window.open(strUrl,strTitle,"width="+intWidth+",height="+intHeight+",left="+intLeft+",top="+intTop+",dependent=yes,scrollbars=yes,resizable=yes,directories=no,location=no,menubar=no,titlebar=no,toolbar=no,status=no");
    if (newWin) {
	    if (blnFocus!=false) {  //not the same as (!blnFocus) - handles null better
	        newWin.focus();
	    } 
    } else {
    	alert("This action opens a new window. Please add www.kitchentuneup.com to your list of sites allowed to open popups.");
    }
    return newWin;
}

/**
 * submits a form to a new window
 * @return reference to the new window
*/
function submitToNew(frm, intWidth, intHeight, blnFocus) {
	var newWin=openInNew("about:blank",intWidth,intHeight,blnFocus);
	frm.target=newWin.name;
	frm.submit();
}

/**
 * simple json template substitution. required jquery. 
 * @param template - an element to be used as the template. any element will work
 * @param data - an array of data to substituted into the template
 */
function processTemplate(template,data) {
	for (i in data) {
		var instance=template.clone();
		var instanceText=instance.html();
		var row=data[i];
		for (key in row) {
			instanceText=instanceText.replace("${"+key+"}",row[key]);
		}
		instance.html(instanceText);
		template.parent().append(instance);
	}
	template.hide();
}
