var show = false;
   //show = true;
var i,j,n,m,x,y,z;
var send_data = "";
var req = "";                                                       // object for XMLHttpRequest
var url = 'http://' + window.location.hostname + '/xserv.aspx';     // url for this website at web hoster
var maxv = 0;                   // POST required: number of query variables
var op = "";				    // POST required: servicing page
var v = new Array();   		    // POST required if maxv > 0; v1=xxx&v2=..etc.
var na = new Array();
var response = "";              // received from POST servicer:
var div = "";                   // received from POST servicer: innerHTML div id
var div_id = "";			    // received from POST servicer: innerHTML div content replacement
var jsStr;             			// received from POST servicer: javascript received

var vMax = 200;	                // used to set max size of the v array for clearing with clearv()

var lock = false;               // used when doGet() is called
var uploader;
var nextpage;
var lastab = "";
var lasted = "";
var ablock = "";
var lastres = "";
var mapitloc = "";
var mapwin;
var locateOnClose = "";
var last_mnx = "";

var assoc_id = "";
var prop_id = "";
var pm_id = "";

var assoc_bag = new bag();
var last_fun = 0;
var nextop;
var nextopvar = false;
var isnew = false;

clearv();
clearna();
function clearv(){for (i = 0; i < vMax; i++){v[i] = "";}}
function clearna(){for (i = 0; i < vMax; i++){na[i] = "";}}

function putinwin(s)
{
    win = window.open ("", "new","location=1,status=1,scrollbars=1,width=1000,height=500,toolbar=no,menubar=no");
    win.document.write("<html><head></head><body>");
    win.document.write(s);
    win.document.write("</body></html>");
}
function getV(idin) // extracts the value attribute of the element with this id
{
    var v = document.getElementById(idin).value;
    return v;
}

function edmain(funct)
{
    //if (lasted != ""){edoffx(lasted);}
    //edon(funct);
    //lasted = funct;
    op = "_pmedit";
    maxv = 1;
    v[1] = funct;
    doGet();
}
function edoff(idin)
{
    if (idin == lasted){return;}
    var x = document.getElementById(idin);
    x.style.backgroundColor = "#888888";
    x.style.color = "#ffffff";
    x.style.borderColor = "#ffffff";
    x.style.borderStyle = "outset";
}
function edoffx(idin)
{
    var x = document.getElementById(idin);
    x.style.backgroundColor = "#888888";
    x.style.color = "#ffffff";
    x.style.borderColor = "#ffffff";
    x.style.borderStyle = "outset";
}
function edon(idin)
{
    var x = document.getElementById(idin);
    x.style.backgroundColor = "#ffffff";
    x.style.color = "#888888";
    x.style.borderColor = "#888888";
    x.style.borderStyle = "inset";
}

function goedit(funct)
{
    maxv = 0;
    op = funct + ".aspx";
    doGet();
}
function sender(dest)
{
   window.location = dest + ".aspx";
}

function make_dat()
{
	 var d = "op=" + op;
     d += "&maxv=" + maxv;
     for(n=1;n<=maxv;n++)
     {
		if(v[n] != "")
          {
            //v[n] = trim(v[n]);
            v[n] = encode(v[n]);
			d += "&v" + n+ "=" + v[n];
          }
     }
     //alert("maxv="+maxv+"\nd="+d+"\n");
    return d;
}

function getReq()
{
	if (window.XMLHttpRequest){req=new XMLHttpRequest();} 	                        // code for non-IE browsers
	else if (window.ActiveXObject){req=new ActiveXObject("Microsoft.XMLHTTP");} 	// code for IE browsers
	if (req) 
	{
	    try
	    {
		req.open("POST",url,false);
		req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		send_data = make_dat();
	    req.send(send_data);
		}
		catch (err) 
		{
		    alert("HttpRequest Error\n" + err.description);
		}
		if (req.readyState==4)
		{
			if (req.status==200){response=req.responseText;}                // if OK
			else{alert("Problem retrieving data:" + req.statusText);}       // if not OK
		}
	}
}

function doGet()
{
    //alert("Starting doGet()");
    if (lock){return;}
    lock = true;
	response = null;
	sAlert = null;
	getReq();
	clearv();
	if (response)
	{
        if(show){putinwin(response);}
	    response = decode(response);
	    jsStr = "";
		split(response);
		jsStr = trim(jsStr);
        if(jsStr != ""){eval(jsStr);}
     	if (sAlert){alert(sAlert);}
	}
	else{alert("NOTHING WAS RETURNED FROM THE SERVER");}
	lock = false;
}

function lbOver(i)
{
    document.getElementById(i).style.backgroundColor = liteColor;
    document.getElementById(i).style.color = darkColor;
}

function lbOut(i)
{
	document.getElementById(i).style.backgroundColor = darkColor;
    document.getElementById(i).style.color = liteColor;
}

function putVars()
{
	unescape();
	var vn;
	for (n=1;n<=maxv;n++)
	{
		vn = "v" + n;
        if (document.getElementById(vn))
        {
		    document.getElementById(vn).value = v[n];
        }
	}
}

function loadVar()
{
     clearv();
     var n, x;
	 for(n=1;n<=maxv;n++)
     {
		x = document.getElementById(n).value;
        x = trim(x);
        v[n]= x;
     }
}

function trim(str)
{
    if (str == null){return;}
	var z = str;
	if (z == ""){return z;}
	while (z.charAt(0) == ' '){z = z.substring(1);}
	while (z.charAt(z.length - 1) == ' '){z = z.substring(0, z.length - 1);}
	return z;
}
function parseOnSlashOrDash(d)
{
	 var e = new Array();
     var n = 0;
     var x = 0;
     var c = "";
     var l = d.length;
     for (n=0;n<l;n++)
     {
		if (d.charAt(n) == "/" ||d.charAt(n) == "-")
          {
			e[x] = c;
               c = "";
               x++;
          }
          else
          {
          	c += d.charAt(n);
          }
     }
     e[x] = c;
     return e;
}
function split(s)
{
	jsStr = "";
	div = "";
	L = s.length;
	var sw = true;
	var n,b,e;
	var c;
	var jsbegin = "http://jsbegin//";
	var jsend = "http://jsend//";
    var divbegin = "http://divbegin//";
    var divend = "http://divend//";

    // extract divs
	for(n=0;n<L;n++)
	{
	    b = s.indexOf(divbegin,n);
	    if (b >= 0)
	    {
	        b += divbegin.length;
	        e = s.indexOf("$$",b);
	        if ( e > 0 )
	        {
	            div_id = s.substring(b,e);
	            b = e +2;
	            e = s.indexOf(divend,b);
	            if (e > 0)
	            {
	                div = s.substring(b,e);
	                n = e + divend.length;
	                document.getElementById(div_id).innerHTML = div;
	            }
	        }
	    }
	}
	// extract js
	for(n=0;n<L;n++)
	{
	    b = s.indexOf(jsbegin,n);
	    if (b >= 0)
	    {
	        b += jsbegin.length;
            e = s.indexOf(jsend,b);
	        if (e > 0)
	        {
	            jsStr += s.substring(b,e);
	            n = e + jsend.length;
	        }
	    }
	}
}
function getJs(fn)
{
    var head_elem = document.getElementsByTagName('head').item(0);
	var js_elem = document.createElement('script');
	js_elem.src = fn;
    js_elem.type = 'text/javascript';
    js_elem.id = fn;
    js_elem.defer = true;
    head_elem.appendChild(js_elem);
    return false;
}
function sendpsw()
{
    v[1] = document.getElementById("1").value;
    maxv = 1;
    op = "_sendpsw";
    //alert("OMV      "+op+"     "+maxv+"     "+v[1]);
    doGet();
}
function logcheck()
{
    v[1] = document.getElementById("1").value;
    v[2] = document.getElementById("2").value;
    //v[1] = "builder";
    //v[2] = "!Q@W3e4r";
    maxv = 2;
    op = "_logcheck";
    //alert("v[1]="+v[1]+"\nv[2]="+v[2]);
    doGet();
}
function forgot()
{
    v[1] = document.getElementById("1").value;
}

function viewmod()
{
    var txtin = document.getElementById("txtarea").value;
    var funct = document.getElementById("whichid").value;
    var txt = cnv2h(txtin,funct);
    switch (funct)
    {
        case "serving":     document.getElementById("head_mid_top").innerHTML = "Serving:&nbsp;" + txt;    break;
        case "tag":         document.getElementById("head_mid_bot").innerHTML = txt;      break;
        case "intro_head":  document.getElementById("intro_right_mid").innerHTML = txt;   break;
        case "intro_body":  document.getElementById("intro_right_bot").innerHTML = txt;   break;
        case "bio":         document.getElementById("bio_txt").innerHTML = txt;           break;
        case "about_us":    document.getElementById("about_us_txt").innerHTML = txt;      break;
        case "uptntinfo":   document.getElementById("uptntinfo_txt").innerHTML = txt;     break;
        case "maintreq":    document.getElementById("maintreq_txt").innerHTML = txt;      break;
        case "mgt_serve":   document.getElementById("mgt_serve_txt").innerHTML = txt;     break;
    }
}

function cnv2h(s,func)
{
    var maxl = 0;
    var maxc = 0;
    switch (func)
    {
        case "serving":     maxl = 1; maxc = 30;    break;
        case "tag":         maxl = 1; maxc = 30;    break;
        case "intro_head":  maxl = 1; maxc = 30;    break;
        case "intro_body":  maxl = 12; maxc = 400;  break;
        case "bio":         maxl = 40; maxc = 2000; break;
        case "about_us":    maxl = 40; maxc = 2000; break;
        case "uptntinfo":   maxl = 40; maxc = 2000; break;
        case "maintreq":    maxl = 40; maxc = 2000; break;
        case "mgt_serve":   maxl = 40; maxc = 2000; break;
  }
    var ret = "";
    var line_cnt = 1;
    var t;
    var j = 0;
    for (j = 0; j < s.length; j++)
    {
        t = s.charCodeAt(j);
        switch (t)
        {
            case 10:
                ret += "<br />";
                //line_cnt++;
                //if (line_cnt > maxl){alert("Too many lines in the text. Please reduce the number of lines to " + maxl);return;}
                break;
            default:
                ret += s.charAt(j);
                //if (ret.length > maxc){alert("Too many characters in the text. Please reduce the number of characters to " + maxc);return;}
                break;
        }
    }
    return ret;
}
function abutts(idin)
{
    if (lastab != "") {aboffx(lastab);}
    abonx(idin);
    ablock = idin;
    lastab = idin;
    lasted = "";
    switch (idin)
    {
        case "ab1":
            op = "_editmain";
            break;
        case "ab2":
            op = "_editslide";
            break;
        case "ab3":
            op = "_editsubs";
            break;
        case "ab4":
            op = "_editsubp";
            break;
        case "ab5":
            op = "_propmain";
            break;
        case "ab6":
            op = "_editprof";
            break;
    }
    maxv = 0;
    doGet();
}
function aboffx(idin)
{
    var x = document.getElementById(idin);
    x.style.backgroundColor = "#888888";
    x.style.color = "#ffffff";
    x.style.borderColor = "#ffffff";
    x.style.borderStyle = "outset";
}
function aboff(idin)
{
    if (ablock == idin){return;}
    var x = document.getElementById(idin);
    x.style.backgroundColor = "#888888";
    x.style.color = "#ffffff";
    x.style.borderColor = "#ffffff";
    x.style.borderStyle = "outset";
}
function abonx(idin)
{
    var x = document.getElementById(idin);
    x.style.backgroundColor = "#ffffff";
    x.style.color = "#888888";
    x.style.borderColor = "#888888";
    x.style.borderStyle = "inset";
}
function abon(idin)
{
    var x = document.getElementById(idin);
    x.style.backgroundColor = "#ffffff";
    x.style.color = "#888888";
    x.style.borderColor = "#888888";
    x.style.borderStyle = "inset";
}

function c2code(s)
{
    var ret = "";
    var j = 0;
    var c = "";
    var cnt = 1;
    for (j = 0; j < s.length;j++)
    {
        c = s.charCodeAt(j);
        ret += c + " ";
        cnt++;
        if ( cnt > 20) {cnt = 1; ret += "<br />";}
    }
    return ret;
}

function savemod()
{
    var txt = document.getElementById("txtarea").value;
    var func = document.getElementById("whichid").value;
    txt = cnv2h(txt,func);
    viewmod();
    op = "_pmupdate";
    maxv = 2;
    v[1] = func;
    v[2] = txt;
    doGet();
}
function encode(s)
{
    var e = "";
    var v = "";
    var x = "^";
    var n = s.length;
    var i = 0;
    if (n == 0){return e;}
    for (i = 0;i < n; i++)
    {
        v = s.charCodeAt(i);
        if ( v <= 47 || (v >= 58 && v <= 64) || (v >= 91 && v <= 96) )
        { 
            e += x + v;
        }
        else 
        { 
            e += s.substr(i, 1);
        }
    }
    return e;
}
function decode(e)
{
    var s = "";
    var x = "^";
    var n = e.length;
    var i = 0;
    for (i = 0;i < n;i++)
    {
        c = e.substr(i,1);
        if (c == x){ s += String.fromCharCode(e.substr(i + 1,2));i += 2; }
        else { s += c;}
    }
    return s;
}

function goto(idin)
{
    window.location = idin + ".aspx";
}
function showait()
{
    document.getElementById("uploadwait").style.visibility = "visible";
}
function propchange()
{
    var x = document.getElementById("selprop");
    var ndx = x.selectedIndex;
    var v = x.options[ndx].value;
    //alert("value of selected option:" + v);
}
function propedit(direct)
{
    //alert("propedit entered with " + direct);
    var x = document.getElementById("selprop");
    var ndx = x.selectedIndex;
    if (ndx == 0){alert("PLEASE SELECT A PROPERTY"); return false;}
    v[1] = x.options[ndx].value;
    op = "_proppix";
    if (direct == "det"){op="_selprop";}
    maxv = 1;
    doGet();
}
function selprop(idin) 
{
    if (idin == "") { return; }
    if (idin == "Add New Property") {v[1] = "0"; }
    else { v[1] = idin; }
    prop_id = v[1];
    prop('det');
}
function proppix()
{
    var x = document.getElementById("selprop");
    var ndx = x.selectedIndex;
    if (ndx == 0){alert("PLEASE SELECT A PROPERTY"); return false;}
    v[1] = x.options[ndx].value;
    maxv = 1;
    op = "_proppix";
    doGet();
}
function delpropix(idin) 
{
    v[1] = idin;
    nextop = "delpropix2();";
    nextopvar = false;
    openDelBox("Do you want to delete this picture?");
}
function delpropix2() 
{
    if (!nextopvar) {return;}
    maxv = 1;
    op = "_delpropix";
    doGet();
}
function savprop()
{
    op = "_savprop";
    maxv = 55;
    v[1] = document.getElementById("1").value;
    doGet();
}
function prop(func)
{
    switch (func) 
    {
        case "sel":
            op = "_listprop";
            tabclick(1);
            maxv = 0;
            doGet();
            return;
            break;
        case "img":
            op = "_proppix";
            tabclick(3)
            break;
        case "det":
            op = "_selprop";
            tabclick(2);
            break;
        case "notes":
            op = "_propnotes";
            tabclick(4);
            break;
        case "comm":
            op = "_propcomm";
            tabclick(5);
            break;
        case "bank":
            op = "_bankedit";
            tabclick(6);
            break;
        case "owner":
            op = "_ownedit";
            tabclick(7);
            break;
    }
    if (prop_id == "") { alert("Please select a property"); return; }
    v[1] = prop_id;
    maxv = 1;
    //putinwin(v[1]);
    doGet();
}
function addcomm()
{
    var x = document.getElementById("notebox");
    v[2] = x.value;
    v[1] = prop_id;
    maxv = 2;
    op = "_addcomm";
    doGet(); 
}
function addnotes()
{
    var x = document.getElementById("notebox");
    v[2] = x.value;
    v[1] = prop_id;
    maxv = 2;
    op = "_addnotes";
    doGet(); 
}function delprop()
{
    v[1] = document.getElementById("1").value;
    var x = document.getElementById("delprop");
    x.style.visibility = "visible";
}
function delprop2(v)
{
    var x = document.getElementById("delprop");
    x.style.visibility = "hidden";
    if ( v == 0 ) {return;}
    op = "_delprop";
    doGet();
}
function savproppix(rowcnt)
{        
    if (rowcnt > 0)
    {
        var vid = 1;
        var j;
        for (j = 0; j < rowcnt; j++ )
        {
            if (!document.getElementById("v" + j)){continue;}
            v[vid] = document.getElementById("v" + j).value;
            vid++;
            v[vid] = document.getElementById("cap" + j).value; //caption
            vid++;
            var sel = "false";
            if (document.getElementById("def" + j).checked){sel = "true";} // default pix           
            v[vid] = sel;
            vid++;
        } 
        maxv = vid -1;
        op = "_savpixinfo";
        doGet(); 
    }
}
function closeme() {window.close();}

function pixupload()
{
    var prop_id = document.getElementById("prop_id").value;
    var pm_id = document.getElementById("pm_id").value;
    if (prop_id != "") 
    {
        var loca = "upload.aspx?req=pix&prop_id="+prop_id+"&pm_id="+pm_id;
        uploader = window.open(loca,'uploader','width=1000,height=300');
        nextpage = "_proppix";
        setTimeout("testwin()",200);
    }
    else {alert("You must select a property");}
}

function savnewprop()
{
    op = "_savnewprop";
    maxv = 55;
    v[1] = document.getElementById("1").value;
    // state (v[6] should be set as well as property type v[2]
    if ( v[2].length != 3){v[2] = "sfh";}
    if ( v[6].length != 2){ v[6] = "CA";}
    doGet();
}
function newprop()
{
    op = "_newprop";
    maxv = 0;
    doGet();
}
function serv(service)
{
    op = service;
    maxv = 0;
    doGet();
}
function savslides(rowcnt)
{
    if (rowcnt > 0)
    {
        var sel = false;
        var vid = 1;
        var j;

        for (j = 0; j < rowcnt; j++ )
        {
            v[vid] = document.getElementById("v" + j).value;
            vid++;
            sel = "false";
            if (document.getElementById("active" + j).checked){sel = "true";} // default pix           
            v[vid] = sel;
            vid++;
        } 
        //alert (v);
        maxv = vid -1;
        op = "_savslideinfo";
        doGet(); 
    }
}
function uploadslide()
{
    var pm_id = document.getElementById("pm_id").value;
    if (pm_id != "")
    {
        var loca = "upload.aspx?req=slides&pm_id="+pm_id;
        uploader = window.open(loca,'uploader','width=1000,height=400');
        nextpage = "_editslide";
        setTimeout("testwin()",200);
    }
    else {alert("PM ID MISSING");}
}
function delslide(idin)
{
    var x = document.getElementById("deleteslide");
    x.style.visibility="visible";
    v[1] = idin;
}
function delslide2(v)
{
    var x = document.getElementById("deleteslide");
    x.style.visibility="hidden";
    if (v == 0){return;}
    if (v == 1)
    {
        maxv = 1;
        op = "_delslide";
        doGet();
    }
}
function revert(idin)
{
    v[1] = idin;
    maxv = 1;
    op = "_revert";
    doGet();
}
function replogo(col)
{    
    var domain = document.getElementById("domain").value;
    if (domain != "")
    {
        var loca = "upload.aspx?req=logos&domain=" + domain + "&col=" + col;
        uploader = window.open(loca,'uploader','width=1000,height=300');
        nextpage = "_editsubp";
        setTimeout("testwin()",200);
    }
    else {alert("DOMAIN MISSING");}
}
function updatepm()
{
    v[1] = document.getElementById("domain").value;
    maxv = document.getElementById("maxv").value;
    var j;
    var k = 2;
    for (j = 2; j <= maxv; j++)
    {
        v[k] = document.getElementById(j).name;
        k++;
        v[k] = document.getElementById(j).value;
        if (v[k] == ""){v[k] = " ";}
        k++;
    }
    maxv = k - 1;
    op = "_updatepm";
    doGet()
}

function resview()
{
    var x = document.getElementById("resel");
    var ndx = x.selectedIndex;
    var jx = "jx" + ndx;
    //alert("ndx="+ndx);
    if (lastres != "")
    { 
        x = document.getElementById(lastres);    
        x.style.visibility = "hidden";
    }
    lastres = jx;
    x = document.getElementById(jx);
    x.style.visibility = "visible";
}
function savres()
{
    if (lastres == ""){return;}
    var x;
    var j = 0;
    for (j = 1; j <= 6; j++)
    {
        x = document.getElementById(lastres+j);
        v[j] = x.value;
    }
    maxv = 6;
    op = "_savres";
    doGet();
}
function delres()
{
    if (lastres == ""){return;}
    v[1] = document.getElementById(lastres + "2").value;    
    var x = document.getElementById("delres");
    x.style.visibility = "visible";
    //alert( "res_id=" + v[1] );
}
function delres2(v)
{
    var x = document.getElementById("delres");
    x.style.visibility = "hidden";  
    if (v == 0) {return;}
    maxv = 1;
    op = "_delres";
    doGet();
}
function assocview(idin)
{
    var ndx = idin.substr(5);
    ndx = ndx/1;
    if (ndx == 0){v[1] = "0";}
    else {var x = document.getElementById(idin);v[1] = assoc_list[ndx];}
    maxv = 1;
    op = "_getassoc";
    doGet();
}
function saveassoc_bak()
{
        // Assoc columns, assoc_bag contains all of the original values including pm_id and assoc_id
        var x;
        assoc_bag = null;
        assoc_bag = new bag();
        x = document.getElementById("last_name");
        assoc_bag.put("last_name",x.value);
        x = document.getElementById("first_name");
        assoc_bag.put("first_name",x.value);
        x = document.getElementById("login_id");
        assoc_bag.put("login_id",x.value);
        x = document.getElementById("password");
        assoc_bag.put("password",x.value);
        x = document.getElementById("email");
        assoc_bag.put("email",x.value);
        x = document.getElementById("pm_id");
        assoc_bag.put("pm_id",x.value);
        x = document.getElementById("assoc_id");
        assoc_bag.put("assoc_id",x.value);

        // Functions Allowed
        // use the assoc_bag to send the info back to the server
        
        // fun_len tells how many functions exist
        var n = 0;
        var fid = "";
        for(n = 1; n <= last_fun; n++)
        {
            fid = "fun" + n;
            if ( document.getElementById(fid) )
            {
                x = document.getElementById(fid);
                if (x.checked == true){assoc_bag.put(fid,"true");}
            }
        }
        v[1] = assoc_bag.makeOut("true");
        //putinwin(v[1]);
        maxv = 1;
        op = "_savassoc";
        doGet();
}
function saveassoc()
{
}
function delassoc(s)
{    
    op = "_delassoc";
    maxv = 1;
    v[1] = val_bag.get("assoc_id");
    doGet(); 
}
function testwin()
{
    if(uploader.closed)
    {
        serv(nextpage);
        return;
    }
    setTimeout("testwin()",200);
}
function tnt(idin)
{
    var x = document.getElementById(idin);
    var ndx = idin/1;
    type = x.type;
    na[ndx] = x.name;
    switch (type)
    {
        case ("text"):
            v[ndx] = trim(x.value);
            break;
        case ("checkbox"):
            if (x.checked == true) {v[ndx] = "true";} else {v[ndx] = "false";}
            break;
        case ("radio"):
            alert("DON'T USE RADIO BUTTONS. USE SELECT/OPTION INSTEAD");
            break;
        case ("select-one"):
            idx = x.selectedIndex;
            v[ndx] = x.options[idx].value;
            break;
        case ("textarea"):
            v[ndx] = trim(x.value);
            break;
    }
    if(v[ndx] == ""){v[ndx] = " ";}
    
   // alert("v["+ndx+"]="+v[ndx]);
}
function checkvalues()
{
    var x;
    var y;
    var v;
    var min_names = new Array("bed_min","bath_min","area_min","price_min","oarea_min","oprice_min" );
    var max_names = new Array("bed_max","bath_max","area_max","price_max","oarea_max","oprice_max" );
    var j = 0;
    for (j = 0;j < 6;j++)
    {
        y = document.getElementsByName(min_names[j]);
        x = y[0];
        v = x.value;
        x.value = numonly(v);  // will return 0 if blank or no numbers
    }
    for (j = 0;j < 6;j++)
    {
        y = document.getElementsByName(max_names[j]);
        x = y[0];
        v = x.value;
        v = numonly(v);
        if (v == 0){v = 99999;}
        x.value = v;
    }
}
function numonly(v)
{
    v = trim(v);
    var len = v.length;
    if (len == 0){return 0;}
    var j = 0;
    var r = "";
    var d = 0;
    var len = v.length;
    for (j = 0; j < len; j++)
    {
        d = v.charCodeAt(j);
        if (d >= 48 && d <=57){r += v.substr(j,1);}
        else { r += 0;}
    }
    return r;
}

function emailer() 
{
    var x = document.getElementById("lastfield");
    maxv = x.value;
    x = document.getElementById("subject");
    var qstr = "subject=" + encode(x.value);   
    x = document.getElementById("to");
    qstr += "&to=" + encode(x.value);    
    x = document.getElementById("from");
    qstr += "&from=" + encode(x.value);    
    var j = 0;
    for (j = 1; j <= maxv; j++)
    {
        if (na[j] == "")   // possibly nothing entered, check if it is a question
        {
            x = document.getElementById(j);
            if (x)
            {
                na[j] = x.name;                
                if (v[j] == "")
                {
                    v[j] = "_____________";
                }
                qstr += "&n" + j + "=" + encode(na[j]);
                qstr += "&v" + j + "=" + encode(v[j]); 
            }         
        }
        else
        {
        
            qstr += "&n" + j + "=" + encode(na[j]);
            qstr += "&v" + j + "=" + encode(v[j]);
        }
    }
    qstr += "&maxv=" + maxv;
    var eshow = false;
    //eshow = true;
    if (eshow)
        {
        //putinwin(qstr);
        //alert("OK");
        }
    
    // SEND to emailer
	sendToLocalEmailer(qstr);       //when local smtp server
	//sendToRemoteEmailer(qstr);        //when local doesn't work
	
	function sendToLocalEmailer(email_msg)
	{
        sAlert = null;
	    response = null;
	    var eurl = 'http://' + window.location.hostname + '/_emailer.aspx';                 // eurl for this website at web hoster
        if (eurl.substr(7,5) == "local"){ eurl = 'http://localhost:12345/_emailer.aspx';}   // eurl on local dev machine
        //alert("eurl:"+eurl);

	    if (window.XMLHttpRequest){req=new XMLHttpRequest();} 	                        // code for non-IE browsers
	    else if (window.ActiveXObject){req=new ActiveXObject("Microsoft.XMLHTTP");} 	// code for IE browsers
	    if (req)
	    {
		    req.open("POST",eurl,false);
		    req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		    req.send(email_msg);
		    if (req.readyState==4)
		    {
			    if (req.status==200){response=req.responseText;}                // if OK
			    else{alert("PROBLEM WITH EMAILER:" + req.statusText);return;}   // if not OK
		    }
	    }
	    if (response)
	    {
            if(eshow){putinwin(response);}
		    split(response);
		    jsStr = trim(jsStr);
            if(jsStr != ""){ eval(jsStr);}
     	    if (sAlert){alert(sAlert);}
	    }
	}
}
function sendToRemoteEmailer(email_msg)
{
    //put up a panel that asks for user patience
    // create a dummy script tag where the src is the url for the emailer
	// and the data to be emailed are in the query string of the url
	// the emailer returns an "OK" when the mail is sent
	var headTag = document.getElementsByTagName("head").item(0);
	var elem = document.createElement("script");
	var rurl = "www.thenorthpole.com/_remailer.aspx";
    elem.setAttribute("type", "text/javascript");
    elem.setAttribute("src", rurl + "?" + email_msg);
    elem.setAttribute("id", "getscript");
	response = "";
	headTag.appendChild(elem);
	window.setTimeout("remoteEmailerCheck()",200);
}
function remoteEmailerCheck()
{
    // the emailer will send an "OK" when it completed the sending of the email
	if (response != "") {sendIsDone();}
	window.setTimeout("check()",200);
}

function puthand(idin)
{
    var x = document.getElementById(idin);
    x.style.cursor = "pointer";
}
function mapit(val)
{
    mapitloc = val;
    mapwin = window.open ("mapit.aspx?v='" + val + "'", "mapwin","location=1,status=1,scrollbars=1,width=740,height=580,toolbar=no,menubar=no");
}
function openMsgBox(text)
{
    var oBox = document.getElementById("MsgBox");
    var sBox = oBox.style;
    var oLine = document.getElementById("MsgLine");
    oLine.innerHTML = text;
    cntrDiv(300,200,"MsgBox")
    sBox.visibility = "visible";
}
function closeMsgBox()
{
    var oBox = document.getElementById("MsgBox");
    var sBox = oBox.style;
    sBox.visibility = "hidden";
}
function openDelBox(text)
{
    var oBox = document.getElementById("DelBox");
    var sBox = oBox.style;
    var oLine = document.getElementById("DelLine");
    oLine.innerHTML = text;
    cntrDiv(300,200,"DelBox")
    sBox.visibility = "visible";
}
function closeDelBox(resp)
{
    var oBox = document.getElementById("DelBox");
    var sBox = oBox.style;
    sBox.visibility = "hidden";
    if (resp == "YES") {nextopvar = true;}
    if (resp == "NO") {nextopvar = false;}
    eval(nextop);
}
function cntrDiv(Xwidth,Yheight,divid) 
{ 
	// First, determine how much the visitor has scrolled 
	var scrolledX, scrolledY; 
	if( self.pageYOffset ) 
	{ 
		scrolledX = self.pageXOffset; 
		scrolledY = self.pageYOffset; 
	}
	else if( document.documentElement && document.documentElement.scrollTop ) 
	{ 
		scrolledX = document.documentElement.scrollLeft; 
		scrolledY = document.documentElement.scrollTop; 
	}
	else if( document.body ) 
	{ 
		scrolledX = document.body.scrollLeft; 
		scrolledY = document.body.scrollTop; 
	}
		
	// Next, determine the coordinates of the center of browser's window 
	var centerX, centerY; 
	if( self.innerHeight ) 
	{ 
		centerX = self.innerWidth; 
		centerY = self.innerHeight; 
	} 
	else if( document.documentElement && document.documentElement.clientHeight ) 
	{ 
		centerX = document.documentElement.clientWidth; 
		centerY = document.documentElement.clientHeight; 
	} 
	else if( document.body ) 
	{ 
		centerX = document.body.clientWidth; 
		centerY = document.body.clientHeight; 
	} 

	//	Xwidth is the width of the div, 
	//	Yheight is the height of the div passed as arguments to the function: 
	var leftOffset = scrolledX + (centerX - Xwidth) / 2; 
	var topOffset = scrolledY + (centerY - Yheight) / 2; 
	// The initial width and height of the div can be set in the style sheet with display:none;
	// 		divid is passed as an argument to the function 
	var o=document.getElementById(divid); 
	var r=o.style; 
	r.position='absolute'; 
	r.top = topOffset + 'px'; 
	r.left = leftOffset + 'px';
}
function permis(txt)
{
    var x = document.getElementById("permis");
    var y = x.style;
    var s = "You do not have permission to perform the function of " + txt + "<br /><br />";
    s += "<center><button type=\"button\" onclick=\"permisoff();\" >OK</button></center>";
    x.innerHTML = s;
    y.visibility = "visible";
}
function permisoff()
{
    var x = document.getElementById("permis");
    var y = x.style;
    y.visibility = "hidden";
}
 
function checkcr(e)
{
    var keynum;
    if(window.event){keynum = e.keyCode;} // check if IE
    else{ if(e.which) {keynum = e.which;}}  // check if Netscape/Firefox/Opera
    if (keynum == 13 ) { logcheck();}
    return false;
}

// the next 2 functions add an assignment  

function asn_get_apm(idin)
{
    prop_id = idin;
    //put up apm box
    var x = document.getElementById("apmselect");
    x.style.visibility = "visible";
    cntrDiv(200,600,"apmselect");
}
function asn_apm_can()
{
    //hide apm box
    var x = document.getElementById("apmselect");
    x.style.visibility = "hidden";
}
function asn_apm_sel(idin)
{
    var x = document.getElementById("apmselect");
    x.style.visibility = "hidden";  
    apm_id = idin;
    var xx = new bag();
    xx.put("pm_id",pm_id);
    xx.put("assoc_id",apm_id);
    xx.put("prop_id", prop_id);
    op = "_addassign";
    maxv = 1;
    v[1] = xx.makeOut("true");
    doGet();
}
function asn_del(idin)
{
    var j = 0;
    var apm_id = "";    
    var rb = document.getElementsByName(idin);
    var rbs = rb.length;
    for (j = 0; j < rbs;j++){if (rb[j].checked){ apm_id = rb[j].value; break; }}
    var bb = new bag();
    bb.put("pm_id",pm_id);
    bb.put("assoc_id",apm_id);
    bb.put("prop_id",idin);
    op = "_delassign";
    maxv = 1;
    v[1] = bb.makeOut("true");
    doGet();
}
function turn_on(idin) {
    if (document.getElementById(idin) != null)
    {
        var x = document.getElementById(idin);
        x.style.visibility = "visible";
        setTimeout("turn_off('" + idin + "')", 5000);
    }
}
function turn_off(idin) {

    if (document.getElementById(idin) == null){return;}
    var x = document.getElementById(idin);
    x.style.visibility = "hidden";
}
function logout() 
{
    assoc_id = "";
    prop_id = "";
    window.close();
}
function modbodybg() 
{
    var x = document.getElementById("mainbody").style;
    x.backgroundImage ="url(images/whitebox.gif)"
}
function getassign2() 
{
    op = "_assignedit";
    maxv = 0;
    doGet();
}
function getassign() 
{
    setTimeout('getassign2()',900);
}
function ownsel(idin) 
{
    var own_id = idin.substr(5);
    val_bag.put("own_id", own_id);
    v[1] = val_bag.makeOut("true");
    maxv = 1
    ignore = false;
    op = "_ownsel";
    //putinwin(v[1]);
    doGet()
}
function ressel(idin) {
    var res_id = idin.substr(5);
    val_bag.put("res_id", res_id);
    v[1] = val_bag.makeOut("true");
    maxv = 1
    op = "_ressel";
    //putinwin(v[1]);
    doGet()
}