//tex22=""
//************************************************************************
function code_check(p)      // 
//************************************************************************
{// 
tex22=p.toLowerCase()                           // 
tex22=tex22.replace(/> </g,"><")                // 
mb=tex22.split("<")                             // 
me=tex22.split(">")
if  (mb.length!=me.length)                      // 
 {alert("Quantity <not equal to quantity>! Saving is not possible!")
return
}	  
for(var j = 1; j <= mb.length-1; j++)            //
{if (mb[j].indexOf(">")<0)
{alert("Invalid tag - there is '>' inside the head! \r\n Fix it and repeat!")
return false}
}
if (mb[0]!="" || me[me.length-1]!="")
{alert("Text outside the tag! Saving is not possible!")
return false
}
//
//----------------------------------------------------------
var arr = new Object()                         // 
arr["a"]=0;arr["address"]=0;arr["area"]=1;arr["b"]=0;arr["bgsound"]=1;arr["big"]=0;arr["blockquote"]=0;arr["br"]=1;
arr["button"]=0;arr["caption"]=0;arr["script"]=0;arr["center"]=0;arr["cite"]=0;arr["code"]=0;arr["col"]=1;
arr["colgroup"]=0;arr["comment"]=0;arr["dd"]=1;arr["dfn"]=0;arr["dir"]=0;arr["div"]=0;arr["dl"]=0;arr["dt"]=0;
arr["em"]=0;arr["embed"]=1;arr["fieldset"]=0;arr["font"]=0;arr["form"]=0;arr["h1"]=0;arr["h2"]=0;arr["h3"]=0;
arr["h4"]=0;arr["h5"]=0;arr["h6"]=0;arr["i"]=0;arr["iframe"]=0;arr["img"]=1;arr["input"]=1;arr["kbd"]=0;
arr["label"]=0;arr["legend"]=0;arr["link"]=1;arr["listing"]=0;arr["map"]=0;arr["marquee"]=0;arr["menu"]=0;
arr["nobr"]=0;arr["ol"]=0;arr["option"]=1;arr["p"]=0;arr["plaintext"]=0;arr["pre"]=0;arr["s"]=0;arr["samp"]=0;
arr["select"]=0;arr["small"]=0;arr["span"]=0;arr["strike"]=0;arr["strong"]=0;arr["style"]=0;arr["sub"]=0;
arr["sup"]=0;arr["table"]=0;arr["tbody"]=0;arr["td"]=0;arr["textarea"]=0;arr["tfoot"]=0;arr["th"]=0;arr["thead"]=0;
arr["tr"]=0;arr["tt"]=0;arr["u"]=0;arr["ul"]=0;arr["var"]=0;arr["wbr"]=0;arr["xmp"]=0;arr["hr"]=1;arr["li"]=1;
arr["!-ss-"]=1;arr["!"]=1;
for(var j = 1; j <mb.length ; j++)         
{if (mb[j].substr(0,1)!="/" && mb[j].substr(0,1)!="?")   //
{poz1=mb[j].indexOf(">") 
poz2=mb[j].indexOf(" ") 
if (poz1<0 && poz2<0)
{alert("Error in HTML code! Saving is not possible!")
return false
}
else if  (poz1>0 && poz2<0)
{poz=poz1
raz=">"
}
else if  (poz1<0 && poz2>0)
{poz=poz2
raz=" "
}
else if  (poz1>0 && poz2>0)
{if (poz1<poz2)
{poz=poz1
raz=">"
}
else
{poz=poz2
raz=" "
}}
tagn=mb[j].substr(0,poz)
tagn=tagn.toLowerCase()
tagz=true
if (arr[tagn]!=undefined)                      // 
{
ztag=arr[tagn]
if (ztag==0)                                   // 
{mb2=tex22.split("<"+tagn+">")
mb22=tex22.split("<"+tagn+" ")
me2=tex22.split("</"+tagn+">")
if (tagn!=="dt")                                //
{if  ((mb2.length+mb22.length-2)!=me2.length-1) //
{alert("ERROR IN CODE: \r\n number of heads and tails of "+ tagn +" tag do not match \r\n or invalid \r\n tag or wrong location! \r\n CANNOT SAVE! "+mb2.length+"<>"+me2.length )
return false
}}
arr[tagn]=arr[tagn]+10                         // 
}
else if (ztag>10)                              // 
{continue}
else if (ztag==1)                              // 
{arr[tagn]=arr[tagn]+10                        // 
}}                                             // 
else                                           // 
{alert("Invalid tag "+tagn)	
return	false
}}}
for(var j2 = 1; j2 <mb.length ; j2++)         
{if (mb[j2].substr(0,1)=="/")                   //
{mbx=mb[j2].replace("/","")                     //
poz=mbx.indexOf(" ") 
if (poz>0)                                      //
{tagn=mbx.substr(0,poz)}   
else                                            //
{poz=mbx.indexOf(">") 
if (poz>0)
{tagn=mbx.substr(0,poz)}
else
{alert("Error in HTML code! Saving is not possible!")
return false
}	   
tagn=tagn.toLowerCase()
if (arr[tagn]!=undefined) 
{if (arr[tagn]<10)                              // 
{alert("The tail of the "+ tagn +" tag is headless! \r\n Saving is not possible!"+arr[tagn])
return
}}
else
{alert("ERROR IN CODE: Invalid tag </"+tagn+">")
return false
}
}}}
html_code2=tex22                                        //
html_code2=html_code2.replace(/\[/g,"(")   ///
html_code2=html_code2.replace(/\]/g,")")   ///
ot_arr = new Array();                                   //
iii=0 
if (html_code2.indexOf(">")>=0)
{while (html_code2.indexOf(">")>=0)	
{poz=html_code2.indexOf(">")
ot_arr[iii]=poz
iii++
html_code2=html_code2.replace(">","]")                                  // 
}}
do_arr = new Array();                                                    //
iii=0 
if (html_code2.indexOf("<")>=0)
{while (html_code2.indexOf("<")>=0)	
{poz=html_code2.indexOf("<")
do_arr[iii]=poz
iii++
html_code2=html_code2.replace("<","[")
}}

html_code22=""                                                             //
for(var j2 = 0; j2 <ot_arr.length ; j2++)                                  //
{www5=tex22.substr(do_arr[j2],ot_arr[j2]-do_arr[j2]+1)
poz=www5.indexOf(" ")
if (poz>0)
{www5=www5.substr(0,poz)+">"}
if (www5!="")
{html_code22=html_code22+www5
}}
// 
html_code22=html_code22.replace(/<img>/g,"<img></img>")
html_code22=html_code22.replace(/<hr>/g,"<hr></hr>")
html_code22=html_code22.replace(/<br>/g,"<br></br>")
html_code22=html_code22.replace(/<area>/g,"<area></area>")
html_code22=html_code22.replace(/<param>/g,"<param></param>")
html_code22=html_code22.replace(/<col>/g,"<col></col>")

if (html_code22.indexOf("<li")>=0)                  //
{html_code22=html_code22.replace(/<\/li>/g,"")       
html_code22=html_code22.replace(/<li>/g,"<li></li>")
}
if (html_code22.indexOf("<dt")>=0)
{html_code22=html_code22.replace(/<dt>/g,"<dt></dt>")
html_code22=html_code22.replace(/<\/dt><\/dt>/g,"</dt>")
}
if (html_code22.indexOf("<dd")>=0)
{html_code22=html_code22.replace(/<dd>/g,"<dd></dd>")
html_code22=html_code22.replace(/<\/dd><\/dd>/g,"</dd>")
}
if (html_code22.indexOf("<option")>=0)
{html_code22=html_code22.replace(/<option>/g,"<option></option>")
html_code22=html_code22.replace(/<\/option><\/option>/g,"</option>")
}
if (html_code22.indexOf("<ul")>=0)
{html_code22=html_code22.replace(/<ul>/g,"<ol>")
html_code22=html_code22.replace(/<ul /g,"<ol ")
html_code22=html_code22.replace(/<\/ul>/g,"</ol>")
}
if (html_code22.indexOf("<menu")>=0)
{html_code22=html_code22.replace(/<menu>/g,"<ol>")
html_code22=html_code22.replace(/<menu /g,"<ol ")
html_code22=html_code22.replace(/<\/menu>/g,"</ol>")
}
html_code22=html_code22.replace(/></g,">\r\n<")     //
html_code22_arr=html_code22.split("\r\n")           //

//-----------------------------
//
//-----------------------------

//-----------------------------------------------------------
// 
//-----------------------------------------------------------
//
//
// 
for(var jj = 0; jj <html_code22_arr.length-1 ; jj++)                //
{if (html_code22_arr[jj]==html_code22_arr[jj+1] && html_code22_arr[jj].indexOf("</")<0 && html_code22_arr[jj].indexOf("<div")<0)
// 
{alert("ERROR IN CODE: \r\n repeating the tag "+ html_code22_arr[jj] +"! \r\n Correct and repeat!")
return false			
}}
t_list=",<tr>,<td>,<th>,<thead>,<tbody>,<tfoot>,<caption>,<table>,"     //
tag_list="<caption>,<th>,<td>"                                 // 
tag_list_arr=tag_list.split(",")                               // 
tabl_html=html_code22
if (tabl_html.indexOf("</table>")>=0)                          // 
{while (tabl_html.indexOf("</table>")>=0)                      // 
{poz_end2=tabl_html.indexOf("</table>")                        // 
_www=tabl_html.substr(0,poz_end2+8)                               
poz_beg2=_www.lastIndexOf("<table")
_www2=_www.substr(poz_beg2)                                     //
html_code221_arr=_www2.split("\r\n")                            //
for(var j = 0; j <html_code221_arr.length; j++)                // 
{
for(var i = 0; i <tag_list_arr.length ; i++)                    //
{if (html_code221_arr[j]==tag_list_arr[i])                      //
{tag_e=tag_list_arr[i].replace("<","</")	                    //
for(var j2 = j+1; j2 <html_code221_arr.length ; j2++)           //
{if (html_code221_arr[j2]==tag_e)                               //
{break}
else if (t_list.indexOf(","+html_code221_arr[j2]+",")>=0)       //
{alert("ERROR IN CODE: \r\n inside the tag "+ tag_list_arr [i] +" invalid tag "+ html_code221_arr [j2] +"! \r\n Correct and repeat!")
return false				
}}}}}
tabl_html=tabl_html.substr(0,poz_beg2)+tabl_html.substr(poz_end2+8)
}
}





if (html_code22_arr[0].indexOf("</")==0)               // 
{alert("ERROR IN CODE: \r\n tag "+ html_code22_arr [0] +" wrong location! \r\n Correct and repeat!")
return false	
}	
obj_end_d=html_code22_arr[0].replace("<","</")         // 
obj_end_e=html_code22_arr[html_code22_arr.length-1]    // 
if (obj_end_e!=obj_end_d)                              // 
{alert("ERROR IN CODE: \r\n at the end must be "+ obj_end_d +" is "+ obj_end_e +"! \r\n Correct and repeat!")
return false		
}	                                                      //
else
{amount=0                                                 //
for(var j3 = 1; j3 <=html_code22_arr.length-2 ; j3++)     // 
{if (html_code22_arr[j3]==obj_end_d)                       //
{if (amount==0)                                           // 
{alert("ERROR IN CODE: \r\n at the end must be \r\n "+ obj_end_d +" is "+ obj_end_e +" \r\n but not the start tag! \r\n Correct and repeat!")
return false		
}
else
{amount--}}
else if (html_code22_arr[j3]==html_code22_arr[0])                        //
{amount++
}}}

for(var j = 0; j <1000 ; j++)                                             //
{ud=false
for(var j2 = 1; j2 <html_code22_arr.length ; j2++)                        //
{if (html_code22_arr[j2].indexOf("</")==0)                                //
{if	(html_code22_arr[j2].replace("/","")==html_code22_arr[j2-1])          //
{html_code22_arr[j2]=""                                                   //
html_code22_arr[j2-1]=""
ud=true                                                                   //
}}}
//
if (!ud)                                                                  //
{for(var j2 = 1; j2 <html_code22_arr.length ; j2++)                       //
{if (html_code22_arr[j2].indexOf("</")==0)                                //
{alert("ERROR IN CODE: \r\n tag "+ html_code22_arr[j2] +" wrong location! \r\n Correct and repeat!")
return false	
}}}
ost_arr = new Array();                                                    //
ost_arr_i=0
for(var j2 = 0; j2 <html_code22_arr.length ; j2++)                        //
{if (html_code22_arr[j2]!="")                                             //
{ost_arr[ost_arr_i++]=html_code22_arr[j2]}	
}
if (ost_arr_i==0) break                                                   //
html_code22_arr=ost_arr                                                   //
}
//--------------------------------
// 
//---------------------------------
tex22=tex22.replace(/<p /g,"<p>")	                                    //
html_code=tex22
poz_p=tex22.indexOf("<p>")                                              //
if (poz_p>=0)                                                           //
{in_p_no="</h1>,</h2>,</h3>,</h4>,</h5>,</h6>,</div>,</p>,</ol>,</ul>,</menu>,</table></option></select><li"       //
in_p_arr=in_p_no.split(",")
if (poz_p==0)                                                          //
{tex22_2=tex22.substr(0,tex22.length-4)                                //
for(var j = 0; j <in_p_arr.length ; j++) 
{in_p_arr[j]
if (tex22_2.indexOf(in_p_arr[j])>0)
{alert("Invalid tag inside 'P' tag "+in_p_arr[j] +" !")
return false
}}}
else                                                  //
{html_code=tex22                                                  
tex_to_del=""                                         //
poz_beg_ol2=0
for(var i = 0; i <= 1000; i++)                 
{if (tex_to_del!="")
{html_code=html_code.replace(tex_to_del,"")
}
poz_end_ol=html_code.indexOf("</p>")	             // 
if (poz_end_ol<0)                                    // 
{break}	                    
html_list=html_code.substr(0,poz_end_ol)             // 
for(var j = 0; j <= 1000; j++)
{
poz_beg_ol=html_list.indexOf("<p>")	                 // 
if (poz_beg_ol<0)                                   // 
{break}	
poz_beg_ol2=poz_beg_ol                              // 
html_list=html_list.substr(poz_beg_ol2+3)           // 
}
tex_to_del="<p>"+html_list+"</p>"	                 //
for(var j = 0; j <in_p_arr.length ; j++) 
{if (html_list.indexOf(in_p_arr[j])>0)
{alert("Invalid tag inside 'P' tag "+in_p_arr[j] +" !")
return false
}}}}}

//------------------------------------------
//
//------------------------------------------

html_code=html_code22                      //
if (!check("dl","dt")) {return}            //

html_code= html_code22                     //
if (!check("select","option")) {return}    //

html_code=html_code22
if (!check("dl","dd")) return

html_code=html_code22
if (!check("ol","li")) return


tabl_html=tex22
tabl_html=tabl_html.replace(/\r/g,"")
tabl_html=tabl_html.replace(/\n/g,"")
tabl_html=tabl_html.replace(/<thead/g,"<tbody")
tabl_html=tabl_html.replace(/<\/thead>/g,"</tbody>")
tabl_html=tabl_html.replace(/<tfoot/g,"<tbody")
tabl_html=tabl_html.replace(/<\/tfoot>/g,"</tbody>")



if (tabl_html.indexOf("</table>")>=0)                                     //
{while (tabl_html.indexOf("</table>")>=0)                                  //
{poz_end22=tabl_html.indexOf("</table>")                                   //
_www=tabl_html.substr(0,poz_end22+8)                               
poz_beg22=_www.lastIndexOf("<table")
_www2=_www.substr(poz_beg22)

html_code=_www2
if (!check("tbody","tr")) return

html_code=_www2
if (!check("tr","td")) return

html_code=_www2
if (!check("tr","th")) {return}

html_code=_www2
if (!check("table","caption")) return

html_code=_www2
if (!check("table","tbody")) return

html_code=_www2
if (!check("table","thead")) return


tabl_html=tabl_html.substr(0,poz_beg22)+tabl_html.substr(poz_end22+8)



}
if (tabl_html.indexOf("<tbody")>=0 || tabl_html.indexOf("<td")>=0 || tabl_html.indexOf("<tfoot")>=0 || tabl_html.indexOf("<th")>=0 ||
tabl_html.indexOf("<tr")>=0 )       // 
{alert("Table tags outside the table! \r\n Fix and repeat!")
return false			
}}
else                                  //
{if (tabl_html.indexOf("<tbody")>=0 || tabl_html.indexOf("<td")>=0 || tabl_html.indexOf("<tfoot")>=0 || tabl_html.indexOf("<th")>=0 || tabl_html.indexOf("<tr")>=0 )
{alert("Table tags outside the table! \r\n Fix and repeat!")
return	false				
}}	

//-----------------------------------------------------------
//
//-----------------------------------------------------------
tabl_html=tex22
tabl_html=tabl_html.replace(/\r/g,"")
tabl_html=tabl_html.replace(/\n/g,"")
if (tabl_html.indexOf("</table>")>=0)                                     //
{
while (tabl_html.indexOf("</table>")>=0)                                  //
{poz_end2=tabl_html.indexOf("</table>")                                   //
_www=tabl_html.substr(0,poz_end2+8)                               
poz_beg2=_www.lastIndexOf("<table")
_www2=_www.substr(poz_beg2)

if (_www2.indexOf("<td")>=0 || _www2.indexOf("<th")>=0 )                  //
{_www2=_www2.replace(/<thead/g,"<t*head")
_www2=_www2.replace(/<\/thead>/g,"</t*head>")

//
if (_www2.indexOf("<caption")>=0)
{while (_www2.indexOf("<caption")>=0)
{poz_beg=_www2.indexOf("<caption")
poz_end=_www2.indexOf("</caption>")
_www2=_www2.substr(0,poz_beg)+_www2.substr(poz_end+10)
}}
if (_www2.indexOf("<th")>=0)
{while (_www2.indexOf("<th")>=0)
{poz_beg=_www2.indexOf("<th")
poz_end=_www2.indexOf("</th>")
_www2=_www2.substr(0,poz_beg)+_www2.substr(poz_end+5)
}}
if (_www2.indexOf("<td")>=0)
{while (_www2.indexOf("<td")>=0)
{poz_beg=_www2.indexOf("<td")
poz_end=_www2.indexOf("</td>")
_www2=_www2.substr(0,poz_beg)+_www2.substr(poz_end+5)
}}

if (_www2.indexOf("<colgroup")>=0)
{while (_www2.indexOf("<colgroup")>=0)
{poz_beg=_www2.indexOf("<colgroup")
poz_end=_www2.indexOf("</colgroup>")
_www2=_www2.substr(0,poz_beg)+_www2.substr(poz_end+11)
}}
_www2=_www2.replace(/></g,">\r\n<")                                        //
_www2_arr=_www2.split("\r\n")   

for(var j = 0; j <_www2_arr.length ; j++) 
{if (_www2_arr[j]!="")                                                    // 
{if (_www2_arr[j].substr(0,2)=="<t" || _www2_arr[j].substr(0,3)=="</t" )  // 
{}
else
{
alert("TAG AND/OR TEXT \r\n '"+ _www2_arr[j] +"' \r\n OUTSIDE TABLE CELL! \r\n FIX AND REPEAT!")
return	false		
}
if (_www2_arr[j].substr(_www2_arr[j].length-1)!=">")                      // 
{alert("TAG AND/OR TEXT OUTSIDE TABLE CELL "+ _www2_arr[j] +"! \r\n FIX AND REPEAT N 2!")
return	false		
}
count=_www2_arr[j].split("<")                                             // 
//alert(_www2_arr[j])

if (count.length>2)
{alert("TAG AND/OR TEXT OUTSIDE TABLE CELL "+ _www2_arr[j] +"! \r\n FIX AND REPEAT N 3!")
return	false		
}}}}
tabl_html=tabl_html.substr(0,poz_beg2)+tabl_html.substr(poz_end2+8)
}}
return true
}

//**********************************************************************************************************
function check(con,tag)   // 
//**********************************************************************************************************
{tag_end="</"+tag+">"                                                  
tag_begin="<"+tag
con_end="</"+con+">"
con_begin="<"+con
tag_html=html_code

if (tag_html.indexOf(tag_begin)>=0)                                    // 
{if (tag_html.indexOf(con_begin)>=0)                                   // 
{while (tag_html.indexOf(con_end)>=0)                                  // 
{poz_end2=tag_html.indexOf(con_end)                                    // 
_www=tag_html.substr(0,poz_end2+con_end.length)
poz_beg2=_www.lastIndexOf(con_begin)
_www2=_www.substr(poz_beg2)                                            // 
tag_html=tag_html.substr(0,poz_beg2)+tag_html.substr(poz_end2+con_end.length)      // 
}
if (tag_html.indexOf(tag_begin)>=0)                                    //
{alert("The "+ tag +" tag is outside the "+ con +" tag! \r\n Correct and repeat!")
return	false	
}}
else          //
{alert("The "+ tag +" tag is outside the "+ con +" tag! \r\n Correct and repeat!")
return	false		
}}
return true
}


