/**
 * File that contains all the core javascript functions used by Advizia
 * @author www.mcs-corp.com 201-541-2300
 * @version 0.1
 * @copyright www.mcs-corp.com 
 */


/**
 * function to generate HTML tags
 * @param {String} tag name of the tag
 * @param {Object} attr object containing attributes to be applied to the tag 
 * @returns {String} HTML code for the tag
 */

qt = String.fromCharCode(34)

MCS = new Object()

MCS.TagList = {AC:"action",AK:"accesskey",AL:"align",AT:"alt",BC:"bgcolor",BL:"onblur",BO:"border",CH:"onchange",CK:"checked",CL:"onclick",CN:"colspan",CO:"cols",CP:"cellpadding",CR:"clear",CS:"class",CG:"cellspacing",DA:"disabled",DC:"ondblclick",DP:"onkeypress",FO:"onfocus",HE:"height",HR:"href",HS:"hspace",ID:"id",KD:"onkeydown",KU:"onkeyup",LO:"onload",LD:"longdesc",MD:"onmousedown",ME:"method",ML:"maxlength",MM:"onmousemove",MX:"onmouseout",MT:"multiple",MU:"onmouseup",MO:"onmouseover",NA:"name",RO:"readonly",RN:"rowspan",RT:"onreset",RW:"rows",SE:"onselect",SI:"size",SR:"src",ST:"style",SU:"onsubmit",TA:"target",TI:"tabindex",TT:"title",TY:"type",VA:"value",VG:"valign",VS:"vspace",WI:"width"}


MCS.Tg = function (tag,attr) { //Creates an HTML Tag
	outStr = "<"+tag;	
	cellS = 1; cellP = 1
	$each(attr,function(item){
		if(item.indexOf('=') == -1){
			outStr += " "+ ((item.length>2)?item:MCS.TagList[item])
		} else {
			k = item.substring(0,item.indexOf('='))
			k = (k.length>2)?k:MCS.TagList[k]
			ll = item.substr(item.indexOf('=')+1)
			if(!k)alert('The attribute "' + item + '"\n in the tag "' + tag + '"\n includes an invalid abbreviation')
			kk = k.toUpperCase()
			if(kk=='CELLPADDING') cellP = 0
			if(kk=='CELLSPACING') cellS = 0
			if(kk=='SRC'){
				if(ll == 0) ll = "images/clear.gif"
				if(ll.indexOf('.') == -1) ll += ".gif"
				if(ll.indexOf('/') == -1) ll = "images/" + ll
			}
			if(kk=='STYLE') ll = MCS.S(ll,1)
			if(tag == 'SELECT' && kk == 'ONCHANGE'){
				ll = "if(this.value==''){this.selectedIndex=0}else{" + ll + "}"
			}
			outStr += " "+ kk + "=" + qt + ll + qt
		}
	})
	if(tag=='TABLE'){
		if(cellS) outStr += ' cellspacing="0"'
		if(cellP) outStr += ' cellpadding="0"'
	}	
	return outStr + ">"
}   

MCS.tg = function (tag){
	return '</'+tag+'>';
}


St = {K:"background",KA:"background-attachment",KC:"background-color",KI:"background-image",KP:"background-position",KR:"background-repeat",B:"border",BB:"border-bottom",BBC:"border-bottom-color",BBS:"border-bottom-style",BBW:"border-bottom-width",BK:"border-collapse",BC:"border-color",BL:"border-left",BLC:"border-left-color",BLS:"border-left-style",BLW:"border-left-width",BR:"border-right",BRC:"border-right-color",BRS:"border-right-style",BRW:"border-right-width",BP:"border-spacing",BS:"border-style",BT:"border-top",BTC:"border-top-color",BTS:"border-top-style",BTW:"border-top-width",BW:"border-width",G:"bottom",CS:"caption-side",X:"clear",I:"clip",C:"color",CI:"counter-increment",CR:"counter-reset",U:"cursor",A:"direction",D:"display",EC:"empty-cells",J:"float",F:"font",FF:"font-family",FS:"font-size",FT:"font-style",FV:"font-variant",FW:"font-weight",H:"height",L:"left",LS:"letter-spacing",LH:"line-height",LT:"list-style",LSI:"list-style-image",LSP:"list-style-position",LST:"list-style-type",M:"margin",MB:"margin-bottom",ML:"margin-left",MR:"margin-right",MT:"margin-top",MH:"max-height",MW:"max-width",SH:"min-height",SW:"min-width",O:"outline",OC:"outline-color",OS:"outline-style",OW:"outline-width",N:"overflow",P:"padding",PB:"padding-bottom",PL:"padding-left",PR:"padding-right",PT:"padding-top",PBA:"page-break-after",PBB:"page-break-before",PBI:"page-break-inside",Y:"position",Q:"quotes",R:"right",TL:"table-layout",TA:"text-align",TD:"text-decoration",TI:"text-indent",TT:"text-transform",T:"top",VA:"vertical-align",V:"visibility",WP:"white-space",W:"width",WS:"word-spacing",ZI:"z-index",E:"opacity"}


MCS.S = function(Sty,Tg) {
	if(Tg){
		AtIn = Sty.replace(/; /g,";").split(';')
	} else {
		Sty = Sty.replace('}','').split('{')
		AtIn = Sty[1].replace(/; /g,";").split(';')
	}
	AtOut = ""
	for (i=0;i<AtIn.length;i++) {
		sT = AtIn[i].split(':')
		if (sT[0].length<3) sT[0] = St[sT[0]]
		k = sT[1].toLowerCase().split(' ')
		sT[1] = ""
		for (j=0;j<k.length;j++) {
    		if(k[j].length == 6){
    			nC = 0
    			k[j].split("").each(function(item,index){
					if (item > "f") nC++
				})
    			if(!nC) k[j] = '#' + k[j]
			}
			if(parseInt(k[j]) == k[j] && sT[0] != "z-index"){
				k[j] += "px"
			}
			sT[1] += k[j] + ((j == k.length-1)? "":" ")
   		}
   		if(sT[1].indexOf("url(") > -1){
		   if(sT[1].indexOf(".") == -1) sT[1] = sT[1].replace(")",".gif)")
		   if(sT[1].indexOf('/') == -1) sT[1] = sT[1].replace("url(","URL(images/")
		}		
    	AtOut += sT[0] + ":" + sT[1] + "; "
    	if (sT[0] == "opacity") AtOut += "filter:alpha(opacity=" + (sT[1]*100) + "); "
  	}
  	return (Tg)? AtOut : Sty[0] + "{" + AtOut + "}"
}  

"OL,UL,LI,DIV,SPAN,A,TABLE|T,TD,dTD,TR,IMG|I,HR,INPUT|IN,FORM".split(",").each(function(item){
	eI = item.split('|')
	if (eI.length == 1) eI[1] = eI[0]
	eval("MCS." + eI[1] + " = function(){return '" + ((eI[0]=="dTD")?"</td>":"") + "' + MCS.Tg('" + ((eI[0]=="dTD")?"TD":eI[0]) + "',MCS['" + eI[1]  + "'].arguments)}; MCS." + eI[1] + ".c = '</" + eI[0] + ">'")
	
})

with(MCS){
	t = "</table>"
	D = "<td>"
	d = "</td>"
	dD = d + D
	R = "<tr>"
	r = "</tr>"
	rR = r + R
	b = "<br>"
	a = "</a>"
	s = "</span>"
	n = "&nbsp;"
	n2 = n + n
	n3 = n + n2
	n4 = n2 + n2
}


/* Creates a pulldown menu
Options are put in an array. If array item is text than that item will be used for both the value and the visible text. if the array item is a sub-array, then the first item will be the value and the second will be the visible text. An optional third sub-array item indicates any additional item to be added to the option tag (ie:selected)
For Example: 
PD("name=Test","value=99",["England",["USA","United States","selected","startwith"]]) becomes....

	<select name=test value=99>
		<option value=England>England</option>
		<option value=USA selected>United States</option>
	</select>
*/

MCS.PD = function(attr){
	NewArgs = []
	$each(MCS.PD.arguments,function(item){
		if($type(item) == "string"){
			NewArgs[NewArgs.length] = item
		} else {
			SelOpts = item
		}
	})
	c = MCS.Tg('SELECT',NewArgs)
	if(SelOpts[0][3]){
		c += ""
	}else{
		c += "<option value=''>Select</option><option value=''>-------------</option>"
	}
	$each(SelOpts,function(item){
		if($type(item) == "string") item = [item,item]
		c += "<option value='" + item[1] + "' " + ((item.length>2)?item[2]:"") + ">" + item[0] + "</option>"
	})
	c += "</select>"
	return c
}

function debug(msg){
	if(typeof console != "undefined") console.log(msg);
}