function getxmlhttp() {

var xmlhttp=false;
try {
	// if Javascript version>5
	xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
	//if not use the older ActiveX object
	try {
	//if using IE
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
	xmlhttp=false;
	}
}
// if not using IE, create a javascript instance of the object
if(!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	xmlhttp=new XMLHttpRequest();
}

return xmlhttp;

}

function getCat(myForm, categ_form, categ_next_form, id) {
var selected_index = categ_form.selectedIndex;
var selected_val = categ_form.options[selected_index].value;
myForm.type.selectedIndex=0;

url_str="get_info.php?type=cat&id="+selected_val;
var xmlhttp=getxmlhttp();
xmlhttp.open("GET",url_str,true);
xmlhttp.send(null);
xmlhttp.onreadystatechange = function () {
	if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var cats_str=xmlhttp.responseText;//response=paid:no_words:no_pics:div_str:opt_str:cat_id[0],cat_name[0]:cat_id[1],cat_name[1] .....
		var split_cats=cats_str.split(":");
		var no = split_cats.length;//if no = 5 -> no subcat
		paid=split_cats[0];
		no_words=split_cats[1];
		no_pics=split_cats[2];
		div_str=split_cats[3];
		opt_str=split_cats[4];
		if(no==5) { // do not open new select box, set selected category
			upper=id-1;
			myForm.categ.value=selected_val;
			// if not free activate type list	
			if(paid==1) document.getElementById("div_type").style.display='block';
			else  document.getElementById("div_type").style.display='none';
		}
		else { 
			myForm.temp_categ.value=selected_val;
			myForm.categ.value="0";
			upper=id; // open new select box
			var no_subcats=no-5;
			document.getElementById("div_type").style.display='none';
		}

		if(paid==0) {
		myForm.no_words.value=no_words;
		myForm.count.value = no_words;
		if(no_words==0)
			document.getElementById('counter').style.display="none";
		else	
			document.getElementById('counter').style.display="block";
		}
		// display image boxes
		if(no_pics>0) document.getElementById('div_image').style.display="block";
		var div_image;
		for(k=0;k<no_pics;k++) {
			div_image='div_image'+k;
			document.getElementById('div_image'+k).style.display="block";
		}
		for(k=no_pics;k<8;k++)
		{
			div_image='div_image'+k;
			document.getElementById(div_image).style.display="none";
		}

		//display all boxes until that level
		for(i=1; i<= upper; i++) {
			div_id='div_categ'+(i+1);
			document.getElementById(div_id).style.display='block';
		}
		//hide all boxes above that level
		for( i=(upper+1); i< 6; i++ ) {
			div_id='div_categ'+(i+1);
			document.getElementById(div_id).style.display='none';
		}

		// div str
		if(div_str!='0') {
			var split_div=div_str.split(",");
			var no = split_div.length;
			for(f=0; f< no; f++) {
				div_spec=split_div[f];
				var split_spec=div_spec.split("=");
				var div_name=split_spec[0];
				if(split_spec[1]==1)
					document.getElementById(div_name).style.display='block';
				else document.getElementById(div_name).style.display='none';
			}
		}

		// opt str
		if(opt_str!='0') {
			var split_opt=opt_str.split(",");
			var no = split_opt.length;
			if(no>1) {
				var div_opt_name=split_opt[0];
 				document.getElementById('div_options_title').style.display='block';
				// write the set name to the div_options_title
				document.getElementById('div_options_title').innerHTML='<table cellpadding=0 cellspacing=0 width=100%><tr><td class="gray">'+div_opt_name+'</td></tr></table>';
				document.getElementById('div_options').style.display='block';
				// write the table and the checkboxes to the div_options div
				var inner_opt='';
				inner_opt='<table cellpadding=0 cellspacing=0 width=100%><tr><td colspan=2 style="padding-left: 50px;"><table cellpadding=0 cellspacing=0 width=100%><tr>';
				for(f=1; f< no; f++) {
					div_spec=split_opt[f];
					var split_spec=div_spec.split("=");
					var opt_name=split_spec[0];
					var opt_id=split_spec[1];
					inner_opt+='<td class="form_right" height=25><input type="checkbox" name="opt_'+opt_id+'">'+opt_name+'</td>';
					if((f-1)%3==2) inner_opt+='</tr><tr>';
				} 
				inner_opt+='</tr></table></td></tr></table>';
				document.getElementById('div_options').innerHTML=inner_opt;
			}
			else { 
				document.getElementById('div_options_title').style.display='none';
				document.getElementById('div_options').style.display='none';
			}
		} else { 
				document.getElementById('div_options_title').style.display='none';
				document.getElementById('div_options').style.display='none';
			}

		if(no==5) return; //if no subcategories return
		categ_next_form.options.length = 0;
		var index = 0;
		// set the options for the next select case
		for (var j=0;j<no_subcats;j++) {
			split_cat=split_cats[(j+5)].split(',');
			var cat_id=split_cat[0];
			var cat_name=split_cat[1];
			categ_next_form.options[index] = new  Option(cat_name, cat_id);
			index++;
		}
	}
}
}

function getType (myForm,myDoc, old_type) {

if(!old_type) {
var selected_index = myForm.type.selectedIndex;
var id = myForm.type.options[selected_index].value;
} else  var id=old_type;

if(id) {
url_str="get_info.php?type=type&id="+id;
var xmlhttp=getxmlhttp();
xmlhttp.open("GET",url_str);
xmlhttp.onreadystatechange = function () {

	if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {

		var type_str=xmlhttp.responseText;//amount, no_words, no_pictures, is_admin
		var split_type=type_str.split(":");

		var amt=split_type[0];
		var no_words=split_type[1];
		var apic=split_type[2];
		var isAdmin=split_type[3];
		myDoc.count.value = no_words;
		myForm.no_words.value=no_words;
		if(apic>0) document.getElementById('div_image').style.display="block";
		var div_image;
		for(k=0;k<apic;k++) {
			div_image='div_image'+k;
			document.getElementById('div_image'+k).style.display="block";
		}
		for(k=apic;k<8;k++)
		{
			div_image='div_image'+k;
			document.getElementById(div_image).style.display="none";
		}
		if(amt>0) {
		if(isAdmin==0)
			document.getElementById('payment').style.display="block";
		}
		else 
			document.getElementById('payment').style.display="none";

		if(no_words==0)
			document.getElementById('counter').style.display="none";
		else	
			document.getElementById('counter').style.display="block";

	}
}
xmlhttp.send(null);
}
}

function getStates (myForm, old_country, old_state) {
if(old_country!=0) {
var selected_index = myForm.country.selectedIndex;
var id = myForm.country.options[selected_index].value;
} else id=old_country;

url_str="get_info.php?type=state&id="+id;
var xmlhttp=getxmlhttp();
xmlhttp.open("GET",url_str);
xmlhttp.onreadystatechange = function () {

	if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {

		var states_str=xmlhttp.responseText;//SELECT_STATE:id, name:
		var split_states=states_str.split(":");
		var no = split_states.length;
	
		myForm.state.options.length = 0;
		var stateIndex = 0;
		myForm.state.options[stateIndex] = new  Option(split_states[0], "");
		stateIndex++;

		for (var j=1;j<no;j++) {
			split_state=split_states[j].split(',');
			var state_id=split_state[0];
			var state_name=split_state[1];
			myForm.state.options[stateIndex] = new  Option(state_name, state_id);
			if(old_state==state_id) myForm.state.selectedIndex=stateIndex;
			stateIndex++;
		}
		
	}
}

xmlhttp.send(null);
	
}

function CountTitleLeft(field, max) {
	if (field.value.length > max)
		field.value = field.value.substring(0, max);
}

function CountTextLeft(myForm, field, count) {
	var corrector=0
	var text=field.value + " ";
	var netscapecorrector;
	var content, words;
	var diff;
	if (document.layers) {netscapecorrector=1;maximalwords+=1}
	if (document.all) {netscapecorrector=0}
	var no_words=myForm.no_words.value;
	if(no_words>0)
	{
		var iwhitespace = /^[^A-Za-z0-9]+/gi; // initial whitespace
		var left_trimmedStr = text.replace(iwhitespace, "");
		var na = rExp = /[^A-Za-z0-9]+/gi; // non alphanumeric
		var cleanedStr = left_trimmedStr.replace(na, " ");
		var splitString = cleanedStr.split(" ");
		var word_count = splitString.length -1;
		count.value=no_words-word_count+corrector;
	}
}

function submitForm(myForm)
{
	var dv;

	var ns4=document.layers
	var ns6=document.getElementById&&!document.all
	var ie4=document.all

	if (ns4) dv=document.loading;
	else if (ns6) dv=document.getElementById("loading").style;
	else if (ie4) dv=document.all.loading.style;

	if(stripHTML==1)
	{
		var re= /<\S[^><]*>/g;
		myForm.title.value=myForm.title.value.replace(re, "");
		myForm.desc.value=myForm.desc.value.replace(re, "");
	}
	if(ns4) dv.visibility="block";
	else if (ns6||ie4) dv.display="block";
	myForm.submit();
}

function checkOld(myForm) {
	if(myForm.categ.value!=0) old_cat = myForm.categ.value;
	else old_cat = myForm.temp_categ.value;
	if(old_cat>0) {

		url_str="get_info.php?type=cat_parent&id="+old_cat;
		var xmlhttp=getxmlhttp();
		xmlhttp.open("GET",url_str);
		xmlhttp.onreadystatechange = function () {

		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			// paid:no_words:no_pics:is_parent:div_str:parent,categories that have same parent:parent2, .......
			var cats_str=xmlhttp.responseText;
			//alert("response="+cats_str);
			var split_cats=cats_str.split(":");
			var j=1;
			var paid=split_cats[0];
			var no_words=split_cats[1];
			var no_pics=split_cats[2];
			var is_parent=split_cats[3];
			var div_str=split_cats[4];
			var opt_str=split_cats[5];
			var no=split_cats.length;
			var var_subcats=0;
			for (i=(split_cats.length-1); i>=6; i--) {
				var string=split_cats[i]; //parent,cat1,cat2 .....
				var str_split=string.split(",");
				var parent=str_split[0];
				
				//mark parent from options
				var len=myForm.elements[(j-1)].options.length; // 

				found=0;
				for(k=0; k<len && !found; k++) {

					if(myForm.elements[(j-1)].options[k].value==parent) {
						//alert("I select="+k+" on "+myForm.elements[(j-1)].name);
						myForm.elements[(j-1)].selectedIndex=k;
						found=1;	
					}
				}
				if(str_split.length>1) {// it has subcats
					div_id='div_categ'+(j+1);
					document.getElementById(div_id).style.display='block';
					var_subcats=1;
				} else var_subcats=0;

				// populate options
				myForm.elements[j].options.length=0;

				var index = 0;
				// set the options for the next select case
				for (var k=1;k<str_split.length;k++) {
					split_i=str_split[k].split('~');
					var cat_id=split_i[0];
					var cat_name=split_i[1];
					myForm.elements[j].options[index] = new  Option(cat_name, cat_id);
					index++;
				}

				j++;
			}
			if(paid==1) document.getElementById("div_type").style.display='block';
			else  document.getElementById("div_type").style.display='none';

			if(paid==0) {
			myForm.no_words.value=no_words;
			myForm.count.value = no_words;
			if(no_words==0)
				document.getElementById('counter').style.display="none";
			else	
				document.getElementById('counter').style.display="block";
			}

			// display image boxes
			if(paid==0) {
				if(no_pics>0) document.getElementById('div_image').style.display="block";
				var div_image;
				for(k=0;k<no_pics;k++) {
					div_image='div_image'+k;
					document.getElementById('div_image'+k).style.display="block";
				}
				for(k=no_pics;k<8;k++)
				{
					div_image='div_image'+k;
					document.getElementById(div_image).style.display="none";
				}
			}

		// div str
		if(div_str!='0') {
			var split_div=div_str.split(",");
			var no = split_div.length;
			for(f=0; f< no; f++) {
				div_spec=split_div[f];
				var split_spec=div_spec.split("=");
				var div_name=split_spec[0];
				if(split_spec[1]==1)
					document.getElementById(div_name).style.display='block';
				else document.getElementById(div_name).style.display='none';
			}
		}

		// opt str
		if(opt_str!='0') {
			var split_opt=opt_str.split(",");
			var no = split_opt.length;
			if(no>1) {
				var div_opt_name=split_opt[0];
 				document.getElementById('div_options_title').style.display='block';
				// write the set name to the div_options_title
				document.getElementById('div_options_title').innerHTML='<table cellpadding=0 cellspacing=0 width=100%><tr><td class="gray">'+div_opt_name+'</td></tr></table>';
				document.getElementById('div_options').style.display='block';
				// write the table and the checkboxes to the div_options div
				var inner_opt='';
				inner_opt='<table cellpadding=0 cellspacing=0 width=100%><tr><td colspan=2 style="padding-left: 50px;"><table cellpadding=0 cellspacing=0 width=100%><tr>';
				for(f=1; f< no; f++) {
					div_spec=split_opt[f];
					var split_spec=div_spec.split("=");
					var opt_name=split_spec[0];
					var opt_id=split_spec[1];
					inner_opt+='<td class="form_right" height=25><input type="checkbox" name="opt_'+opt_id+'">'+opt_name+'</td>';
					if((f-1)%3==2) inner_opt+='</tr><tr>';
				} 
				inner_opt+='</tr></table></td></tr></table>';
				document.getElementById('div_options').innerHTML=inner_opt;
			}
			else { 
				document.getElementById('div_options_title').style.display='none';
				document.getElementById('div_options').style.display='none';
			}
		} else { 
				document.getElementById('div_options_title').style.display='none';
				document.getElementById('div_options').style.display='none';
			}


		}
		}
		xmlhttp.send(null);
	}
}

function addHit(id) {
url_str="add_hit.php?id="+id;
var xmlhttp=getxmlhttp();
xmlhttp.open("GET",url_str);
xmlhttp.onreadystatechange = function () {

    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	}
	}
	
	xmlhttp.send(null);
	
}

function myConfirm(msg)
{
    if ( ! window.showModalDialog || window.external.IYBrowserExtend) {
    if(!confirm(msg)) return false;
    } else {
    var oArguments = {
	Message : msg
    };
    var oAction = window.showModalDialog("confirmation.html",oArguments,"dialogHeight:150px;dialogWidth:300px;status:no;help:no;resizable:yes;scroll:no;");
			
    if(oAction)
    {
    if(!oAction.confirmed) return false;
    }
} 
    return true;
}
