/*
  Copyright (C) 2001 - 2009 Astro Share BV, The Netherlands.
  All rights reserved.

  This file is part of the Astro-Online Software as defined in the License
  Agreement, found in file "license.txt".

  Astro-Online is distributed with NO WARRANTY OF ANY KIND.  No author
  or distributor accepts any responsibility for the consequences of using it,
  or for whether it serves any particular purpose or works at all, unless he
  or she says so in writing.

  Every copy of Astro-Online must include a copy of the License Agreement,
  normally in a plain ASCII text file named "license.txt". The License Agreement
  grants you the right to copy and modify the Astro-Online source code and
  documentation, but only under certain conditions described in the License
  Agreement. You are allowed to copy the content delivered with the source code
  and documentation. You are NOT ALLOWED to modify the content or use the content
  in other services without the written permission of Astro Share. Among other
  things, the License Agreement requires that the copyright notices and this
  notice be preserved on all copies.

  Author: Peter Schulz
*/

/*
  This JavaScript code implements several functions which are called from the
  HTML forms generated by the Astro-Online software.
*/

// General function to trim a string value.
function trim(value) {
    value = value.replace(/^s+/,''); 
    value = value.replace(/s+$/,'');
    return value;
}

// Sets focus to the first element in the first form.
function setFirstItem() {
    if (document.forms[0])
        document.forms[0].elements[0].focus();
}

// This function makes sure that the cursor jumps automatically to the next item
// when the maximum number of characters allowed in the item is reached.
function autoTab(input, len) {
    if (isNaN(input.value) || trim(input.value)=="") {
        input.value = input.value.slice(0, input.length-1);
        return false;
    }
    return true;
}

// Search through items in a HTML form.
function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1) {
        if (input.form[i] == input)
            index = i;
        else
            i++;
    }
    return index;
}

// Check if a value for city was entered, before submitting the lov request.
function submitcity(ERRORMSG) {
    if (trim(document.forms[0].AO_LovCity.value)=="" || trim(document.forms[0].AO_LovCity.value)=="%") {
        alert(ERRORMSG);
        return false;
    }
    else
        return true;
}

// Turnover selected values from the lov to entry form (opener).
function passbackcity(TYPE, P_CITY, P_LAT, P_LON) {
    if (TYPE=="1") {
        opener.document.forms[0].AO_BirthPlace.value     = P_CITY;
        opener.document.forms[0].AO_BirthLatitude.value  = P_LAT;
        opener.document.forms[0].AO_BirthLongitude.value = P_LON;
        opener.document.forms[0].AO_BirthPlace.focus();
        close();
    }
    else if (TYPE=="2") {
        opener.document.forms[0].AO_BirthPlace2.value     = P_CITY;
        opener.document.forms[0].AO_BirthLatitude2.value  = P_LAT;
        opener.document.forms[0].AO_BirthLongitude2.value = P_LON;
        opener.document.forms[0].AO_BirthPlace2.focus();
        close();
    }
}

// Call help page.
function help() {
    frmLOV = open("astroonline?id=help"
                 ,"winHELP"
                 ,"scrollbars=yes,resizable=yes,width=817,height=600");
}

// Add Flash natal chart to output page.
function drawChart(flashvars, chartname) {
	if (chartname==undefined && chartname==null)
	    chartname = "Chart.swf";
    document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="700" height="500" id="Chart" align="middle">');
    document.write('<param name="allowScriptAccess" value="sameDomain" />');
    document.write('<param name="movie" value="../flash/' + chartname + '" />');
    document.write('<param name="loop" value="false" />');
    document.write('<param name="menu" value="false" />');
    document.write('<param name="quality" value="high" />');
    document.write('<param name="bgcolor" value="#ffffff" />');
    document.write('<param name="FlashVars" value="' + flashvars + '" />');
    document.write('<embed src="../flash/' + chartname + '" loop="false" menu="false" quality="high" bgcolor="#ffffff" width="700" height="500" name="Chart" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="' + flashvars + '" />');
    document.write('</object>');
}

// Add Flash synastry chart to output page.
function drawSynastry(flashvars, chartname) {
	if (chartname==undefined && chartname==null)
	    chartname = "Synastry.swf";
    document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="535" height="410" id="Synastry" align="middle">');
    document.write('<param name="allowScriptAccess" value="sameDomain" />');
    document.write('<param name="movie" value="../flash/' + chartname + '" />');
    document.write('<param name="loop" value="false" />');
    document.write('<param name="menu" value="false" />');
    document.write('<param name="quality" value="high" />');
    document.write('<param name="bgcolor" value="#ffffff" />');
    document.write('<param name="FlashVars" value="' + flashvars + '" />');
    document.write('<embed src="../flash/' + chartname + '" loop="false" menu="false" quality="high" bgcolor="#ffffff" width="535" height="410" name="Synastry" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="' + flashvars + '" />');
    document.write('</object>');
}

// Function to show the values of Form Items.
function debugFormItems() {
    outputText = '<table cellpadding="2" cellspacing="5" border="0">';
    outputText = outputText + '<tr>' +
                 '<td><nobr><b>Form Name</b></nobr></td>' +
                 '<td><nobr><b>Item Name</b></nobr></td>' +
                 '<td><nobr><b>Item ID</b></nobr></td>' +
                 '<td><nobr><b>Item Value</b></nobr></td>' +
                 '</tr>';
    if (document.forms) {
        for (i=0; i<document.forms.length; i++) {
            if (document.forms[i].elements) {
                if (document.forms[i].name=="")
                    formName = "<nobr><i>-- empty --</i></nobr>";
                else
                    formName = document.forms[i].name;
                for (j=0; j<document.forms[i].elements.length; j++) {
                    if (document.forms[i].elements[j].name=="")
                        formItemName = "<nobr><i>-- empty --</i></nobr>";
                    else
                        formItemName = document.forms[i].elements[j].name;
                    if (document.forms[i].elements[j].id=="")
                        formItemId = "<nobr><i>-- empty --</i></nobr>";
                    else
                        formItemId = document.forms[i].elements[j].name;
                    if (document.forms[i].elements[j].value=="")
                        formItemValue = "<nobr><i>-- empty --</i></nobr>";
                    else
                        formItemValue = document.forms[i].elements[j].value;
                    outputText = outputText + '<tr>' +
                                 '<td>' + formName      + '</td>' +
                                 '<td>' + formItemName  + '</td>' +
                                 '<td>' + formItemId    + '</td>' +
                                 '<td>' + formItemValue + '</td>' +
                                 '</tr>';
                }
            }
        }
        outputText = outputText + '</table>';
        return outputText;
    }
}

// Rollover function to change image source
function rolloverlaydown(img_name, img_src) {
   document[img_name].src = img_src;
}

// Check input partner form using radio buttons
function checkPartnerInputRadio(p1sign, p2sign, ERRORMSG) {
	chk1 = false;
	for (i=0; i<12; i++) chk1 = chk1 || p1sign[i].checked;
	
	chk2 = false;
	for (i=0; i<12; i++) chk2 = chk2 || p2sign[i].checked;
	
	if (!chk1 || !chk2) {
		alert(ERRORMSG);
		return false;
	}
	else {
		return true;
	}
}

// Check input partner form using listboxes
function checkPartnerInputSelect(p1sign, p2sign, ERRORMSG) {
	if (p1sign.selectedIndex==0 || p2sign.selectedIndex==0) {
		alert(ERRORMSG);
		return false;
	}
	else {
		return true;
	}
}

// Load external stylesheet into document head
function loadExternalStylesheet(filename) {
	var externalscript = document.createElement("link");
	externalscript.setAttribute("type", "text/css");
	externalscript.setAttribute("rel", "stylesheet");
	externalscript.setAttribute("href", filename);
	if (typeof externalscript!="undefined")
		document.getElementsByTagName("head")[0].appendChild(externalscript);
}

// Load external script into document head
function loadExternalScript(filename) {
	var externalscript = document.createElement("script");
	externalscript.setAttribute("language", "javascript");
	externalscript.setAttribute("type", "text/javascript");
	externalscript.setAttribute("src", filename);
	if (typeof externalscript!="undefined")
		document.getElementsByTagName("head")[0].appendChild(externalscript);
}
