//Trims a string from both sides
function strTrim(tmpStr)
{
	tmpStr = tmpStr.replace(/^\s+/,"");//remove leading
	tmpStr = tmpStr.replace(/\s+$/,"");//remove trailing
	return tmpStr;
}
//Trims all fields in a form
function trimFields()
{
	for(var i=0; i < obj.elements.length; i++)
	{
		if(obj.elements[i].type == "text" || obj.elements[i].type == "textarea" || obj.elements[i].type == "password")
		{
			obj.elements[i].value = strTrim(obj.elements[i].value);
		}
	}
}
//Checks email against pattern
function chkEmail(tmpStr)
{
	var email_pat = /^[a-z][a-z0-9_\.\-']*[a-z0-9]@[a-z0-9]+[a-z0-9\.\-_]*\.[a-z]{2,}$/i;
	return(email_pat.test(tmpStr));
}

//Checks validity of date fields
function chkDate(tmpStr)
{
	var dt_pat = /^\d{2,2}\/\d{2,2}\/\d{4,4}$/;
	if(!dt_pat.test(tmpStr)) 
	{
		return false;
	}
	var dtGiven = new Date(tmpStr);
	var arrDt = tmpStr.split("/");
	var dtMon = parseInt(arrDt[0],10);//force decimal or else 08,09 will return 0
	var dtDay = parseInt(arrDt[1],10); //force decimal or else 08,09 will return 0
	var dtYear = parseInt(arrDt[2],10); //force decimal or else 08,09 will return 0
	if((dtGiven.getMonth() != dtMon - 1) || (dtGiven.getDate() != dtDay) || (dtGiven.getFullYear() != dtYear))
	{
		alert("date value error");
		return false;
	}
	return true;
}

//Validates text-area length
function chkLen(strField, cntlName, maxChar)
{
	if(obj.elements[cntlName].value.length > maxChar)
	{
		alert(strField+" should be within "+maxChar+" characters.");
		obj.elements[cntlName].focus();
		obj.elements[cntlName].select();
		return false;
	}
	return true;
}

//Converts RGB(r, g, b) to HEX
function toHex(col)
{
	if(col && col.indexOf("#") == -1)
	{
		col = col.split("(")[1].split(")")[0].split(", ");
		col = '#' + Number(col[0]).toString(16) + Number(col[1]).toString(16) + Number(col[2]).toString(16);
	}
	return col.toUpperCase();
}
//-------------------------------------------------------------------------
function writeFlash(swfPath, width, height)
{
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" align="middle">\n');
	document.write('<param name="allowScriptAccess" value="sameDomain" />\n');
	document.write('<param name="movie" value="'+swfPath+'" />\n');
	document.write('<param name="quality" value="high" />\n');
	document.write('<param name="wmode" value="transparent" />\n');
	document.write('<param name="bgcolor" value="#00000" />\n');
	document.write('<embed src="'+swfPath+'" quality="high" bgcolor="#000000" width="'+width+'" height="'+height+'" wmode="transparent"\n');
	document.write('align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"\n');
	document.write('pluginspage="http://www.macromedia.com/go/getflashplayer" />\n');
	document.write('</object>');
}
//-------------------------------------------------------------------------
function showImage(imagePath)
{
	var wOpt='scrollbars=no,width=100,height=100,left='+25+',top='+25;
	newWindow = window.open("","newWindow",wOpt);
	//var width,height;
	var doc = newWindow.document;
	doc.open();
	doc.write('<html>\n');
	doc.write('<head>\n');
	doc.write('<title>Actual Image</title>\n');
	doc.write('<meta http-equiv="imagetoolbar" content="no">\n');
	doc.write('<script language="javascript">\n');
	doc.write('function fitPic()\n');
	doc.write('{\n');
	doc.write('	if (window.innerWidth)\n');
	doc.write('	{\n');
	doc.write('		iWidth = window.innerWidth;\n');
	doc.write('		iHeight = window.innerHeight;\n');
	doc.write('	}\n');
	doc.write('	else\n');
	doc.write('	{\n');
	doc.write('		iWidth = document.body.clientWidth;\n');
	doc.write('		iHeight =document.body.clientHeight;\n');
	doc.write('	}\n');
	doc.write('	iWidth = document.images[0].width - iWidth;\n');
	doc.write('	iHeight = document.images[0].height - iHeight;\n');
	doc.write('	window.resizeBy(iWidth, iHeight);\n');
	doc.write('}\n');
	doc.write('</script>');
	doc.write('</head>\n');
	doc.write('<body leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" ondblclick="self.close();" onblur="self.close();" onload="fitPic();">\n');
	doc.write('<img src="'+imagePath+'" alt="Double-click to Close this window" border="0">\n');
	doc.write('</body>\n');
	doc.write('</html>\n');
	doc.close();
	newWindow.focus();
}
