// Title: ZebraNights Header JavaScripts
// URL: http://www.zebranights.com
// Version: 7.0

var WindowUtilities = {  
	getWindowSize: function(parent) {
		var W, H;
		parent = parent || document.body;              
			if (parent != document.body) {
				W = parent.scrollWidth;
				H = parent.scrollHeight;
			} else {
				var w = window;
				with (w.document) {
					if (w.innerWidth) {
						W = w.innerWidth;
						H = w.innerHeight;
					} else if (w.document.documentElement && documentElement.clientWidth) {
						W = documentElement.clientWidth;
						H = documentElement.clientHeight;
					} else {
						W = body.offsetWidth;
						H = body.offsetHeight
					}
				}
			}
		return { width: W, height: H };
	}		
};

ajax_popup = function(znurl,znname) {
	var windowScroll = WindowUtilities.getWindowSize();
	var winPop = Zapatec.Window.setup({
		left: (windowScroll.width-750)/2, 
		top: (windowScroll.height-550)/2, 
		width: 750,
		height: 550,
		modal: true,
		raiseOnlyOnTitle: false, 
		theme: "minimal",
		title: znname, 
		urlContent : znurl
	});
};
ajax_Vpopup = function(znurl,znname,znwidth,znheight) {
	var windowScroll = WindowUtilities.getWindowSize();
	var winPop = Zapatec.Window.setup({
		left: (windowScroll.width-znwidth)/2, 
		top: (windowScroll.height-znheight)/2, 
		width: znwidth,
		height: znheight,
		modal: true,
		raiseOnlyOnTitle: false, 
		theme: "minimal",
		title: znname, 
		urlContent : znurl
	});
};
openAjaxAlert = function(znurl,znname) {
	var windowScroll = WindowUtilities.getWindowSize();
	var winPop = Zapatec.Window.setup({
		left: (windowScroll.width-300)/2, 
		top: (windowScroll.height-200)/2, 
		width: 300,
		height: 200,
		modal: true,
		raiseOnlyOnTitle: false, 
		theme: "minimal",
		title: znname, 
		urlContent : znurl
	});
};
openAjaxAlert2 = function(zntext, znname, plusheight) {
	var win1 = new Zapatec.AlertWindow(zntext, {title: znname, modal: true, width: 300, height: (100 + plusheight), theme: "minimal"});
};
//function hidestatus(){
//window.status=''
//return true
//}

//if (document.layers)
//document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT)
//document.onmouseover=hidestatus
//document.onmouseout=hidestatus

launchpopupscroll = function(url, name, width, height,scroll) {
   winSet = "toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,status=no";
   winSet += ",width="+width +",height="+height+",left="+(screen.availWidth-width)/2+",top="+(screen.availHeight-height)/2;
   mypopwnd = window.open(url, name, winSet);
};

launchpopup = function(url, name, width, height) {
   launchpopupscroll(url, name, width, height,"yes");
};
 
if(document.getElementById&&!document.all){ns6=1;}else{ns6=0;}
var agtbrw=navigator.userAgent.toLowerCase();
var operaaa=(agtbrw.indexOf('opera')!=-1);
var head="display:''";
var folder='';
var zn_dupCounter = 1;
 
expandit = function(curobj){
	if(document.getElementById(curobj)){
		folder=document.getElementById(curobj).style;
	}else{
		if(ns6==1||operaaa==true){
			folder=curobj.nextSibling.nextSibling.style;
		}else{
			folder=document.all[curobj.sourceIndex+1].style;
		}
	}
	if (folder.display=="none"){folder.display="";}else{folder.display="none";}
};

expandit2 = function(curobj, hide) {
	if(document.getElementById(curobj)) {
  		folder=document.getElementById(curobj).style;
	} else {
		if(ns6==1||operaaa==true) {
			folder=curobj.nextSibling.nextSibling.style;
		} else {
			folder=document.all[curobj.sourceIndex+1].style;
		}
   }
	if(folder.display=="none")
	{
		folder.display="";
	} else {
		folder.display="none";
	}
	if(hide) {
		var hide_objects = hide.split(",");
		for(i=0; i<hide_objects.length; i++) {
			hide_objects[i]=hide_objects[i].replace(/^\s*(.*)/, "$1");
			hide_objects[i]=hide_objects[i].replace(/(.*?)\s*$/, "$1");
			if(document.getElementById(hide_objects[i])) {
				hidden=document.getElementById(hide_objects[i]).style;
				if(hidden.display=="") {
					hidden.display="none";
				}
			}
		}
	}
};

loadstatussubs = function(){
	parent.main_body.location='/subscribe/subs_decide.php';
};

mOut = function(src, colour){
	src.style.backgroundColor = colour;
};
mFlip = function(control){
	if(document.getElementById(control)){
		oControl=document.getElementById(control).style;
	}else{
		if(ns6==1||operaaa==true){
			oControl=control.nextSibling.nextSibling.style;
			
		}else{
			oControl=document.all[control.sourceIndex+1].style;
		}
	}
	oText=document.getElementById([control + "T"]);
	if (oControl.display == "none"){
		oControl.display = "";
		oText.innerHTML = "Hide Item";
	}else{
		oControl.display = "none";
		oText.innerHTML = "Show Item";
	}
};
mOver = function(src, colour){ 
	src.style.cursor = 'hand';
	src.style.backgroundColor = colour;
};
mClick = function(src){
	if(event.srcElement.tagName=='TD'){
		src.children.tags('A')[0].click()
	}
};
updateIT = function(src, msg){
	oText=document.getElementById(src);
	oText.innerHTML = msg;
};
setTabs = function(tab,tabid) {
	var i;
	var counter;
	var obj = document.getElementById(tab + tabid);
	if(tab == 'atplaytab') {counter = 5;}
	if(tab == 'potd') {counter = 4;}
	if(tab == 'Pgroups') {counter = 6;}
	if(tab == 'media') {counter = 4;}
	if(tab == 'interact') {counter = 5;}
	if(tab == 'groupsM') {counter = 5;}
	if(tab == 'subs') {counter = 4;}
	if(tab == 'groups') {counter = 7;}
	if(tab == 'mail') {counter = 9;}
	if(tab == 'whoistab') {counter = 9;}
	if(tab == 'account') {counter = 9;}
	for(i=1;i<=counter;i++) {
		var obj1 = document.getElementById(tab + i);
		obj1.className="";
	}	
	obj.className="selected";
};

duplicateHTML = function(copy,paste,baseid){
	if(document.getElementById(copy)){
		zn_dupCounter++;
		var type = document.getElementById(copy).nodeName; // get the tag name of the source copy.

		var but = document.createElement('input');
		var br = document.createElement('br');

		but.type = 'button';
		but.value = 'x';
		but.className = 'button';
		but.onclick = function(){ this.parentNode.parentNode.removeChild(this.parentNode); };

		var destination = document.getElementById(paste);
		var source      = document.getElementById(copy).cloneNode(true);

		var newentry = document.createElement(type);

		newentry.appendChild(source);
		newentry.value='';
		newentry.appendChild(but);
		newentry.appendChild(br);
		if(baseid) {
			newid = baseid+zn_dupCounter;
			newentry.innerHTML = newentry.innerHTML.replace(new RegExp(baseid, 'g'), newid);
			newentry.id=newid;
		}

		destination.appendChild(newentry);
	}
};

/**
 * Modify from controls when the "NULL" checkbox is selected
 *
 * @param   string   the MySQL field type
 * @param   string   the urlencoded field name
 * @param   string   the md5 hashed field name
 *
 * @return  boolean  always true
 */
nullify = function(theType, urlField, md5Field, multi_edit) {
    var rowForm = document.forms['insertForm'];

    if (typeof(rowForm.elements['funcs' + multi_edit + '[' + urlField + ']']) != 'undefined') {
        rowForm.elements['funcs' + multi_edit + '[' + urlField + ']'].selectedIndex = -1;
    }

    // "SET" field , "ENUM" field with more than 20 characters
    // or foreign key field
    if (theType == 1 || theType == 3 || theType == 4) {
        rowForm.elements['field_' + md5Field + multi_edit + '[]'].selectedIndex = -1;
    }
    // Other "ENUM" field
    else if (theType == 2) {
        var elts     = rowForm.elements['field_' + md5Field + multi_edit + '[]'];
        // when there is just one option in ENUM:
        if (elts.checked) {
            elts.checked = false;
        } else {
            var elts_cnt = elts.length;
            for (var i = 0; i < elts_cnt; i++ ) {
                elts[i].checked = false;
            } // end for

        } // end if
    }
    // Other field types
    else /*if (theType == 5)*/ {
        rowForm.elements['fields' + multi_edit + '[' + urlField + ']'].value = '';
    } // end if... else if... else

    return true;
}; // end of the 'nullify()' function


/**
 * Unchecks the "NULL" control when a function has been selected or a value
 * entered
 *
 * @param   string   the urlencoded field name
 *
 * @return  boolean  always true
 */
unNullify = function(urlField, multi_edit) {
    var rowForm = document.forms['insertForm'];

    if (typeof(rowForm.elements['fields_null[multi_edit][' + multi_edit + '][' + urlField + ']']) != 'undefined') {
        rowForm.elements['fields_null[multi_edit][' + multi_edit + '][' + urlField + ']'].checked = false
    } // end if

    if (typeof(rowForm.elements['insert_ignore_' + multi_edit]) != 'undefined') {
        rowForm.elements['insert_ignore_' + multi_edit].checked = false
    } // end if

    return true;
}; // end of the 'unNullify()' function 

var day;
var month;
var year;
var hour;
var minute;
var second;
var clock_set = 0;

/**
 * Opens calendar window.
 *
 * @param   string      calendar.php parameters
 * @param   string      form name
 * @param   string      field name
 * @param   string      edit type - date/timestamp
 */
openCalendar = function(params, form, field, type) {
	winSet = "toolbar=no,location=no,directories=no,menubar=no,scrollbars=no,resizable=yes,status=no";
	winSet += ",width=250,height=150,left="+(screen.availWidth-250)/2+",top="+(screen.availHeight-150)/2;
	mypopwnd = window.open("/tools/date_picker.php?" + params, "calendar", winSet);
	mypopwnd.focus();
    dateField = eval("document." + form + "." + field);
    dateType = type;
};
/**
 * Formats number to two digits.
 *
 * @param   int number to format.
 * @param   string type of number
 */
formatNum2 = function(i, valtype) {
    f = (i < 10 ? '0' : '') + i;
    if (valtype && valtype != '') {
        switch(valtype) {
            case 'month':
                f = (f > 12 ? 12 : f);
                break;

            case 'day':
                f = (f > 31 ? 31 : f);
                break;

            case 'hour':
                f = (f > 24 ? 24 : f);
                break;

            default:
            case 'second':
            case 'minute':
                f = (f > 59 ? 59 : f);
                break;
        }
    }

    return f;
};

/**
 * Formats number to two digits.
 *
 * @param   int number to format.
 * @param   int default value
 * @param   string type of number
 */
formatNum2d = function(i, default_v, valtype) {
    i = parseInt(i, 10);
    if (isNaN(i)) return default_v;
    return formatNum2(i, valtype)
};

/**
 * Formats number to four digits.
 *
 * @param   int number to format.
 */
formatNum4 = function(i) {
    i = parseInt(i, 10)
    return (i < 1000 ? i < 100 ? i < 10 ? '000' : '00' : '0' : '') + i;
};

/**
 * Initializes calendar window.
 */
initCalendar = function() {
    if (!year && !month && !day) {
        /* Called for first time */
        if (window.opener.dateField.value) {
            value = window.opener.dateField.value;
            if (window.opener.dateType == 'datetime' || window.opener.dateType == 'date') {
                if (window.opener.dateType == 'datetime') {
                    parts   = value.split(' ');
                    value   = parts[0];

                    if (parts[1]) {
                        time    = parts[1].split(':');
                        hour    = parseInt(time[0],10);
                        minute  = parseInt(time[1],10);
                        second  = parseInt(time[2],10);
                    }
                }
                date        = value.split("-");
                day         = parseInt(date[2],10);
                month       = parseInt(date[1],10) - 1;
                year        = parseInt(date[0],10);
            } else {
                year        = parseInt(value.substr(0,4),10);
                month       = parseInt(value.substr(4,2),10) - 1;
                day         = parseInt(value.substr(6,2),10);
                hour        = parseInt(value.substr(8,2),10);
                minute      = parseInt(value.substr(10,2),10);
                second      = parseInt(value.substr(12,2),10);
            }
        }
        if (isNaN(year) || isNaN(month) || isNaN(day) || day == 0) {
            dt      = new Date();
            year    = dt.getFullYear();
            month   = dt.getMonth();
            day     = dt.getDate();
        }
        if (isNaN(hour) || isNaN(minute) || isNaN(second)) {
            dt      = new Date();
            hour    = dt.getHours();
            minute  = dt.getMinutes();
            second  = dt.getSeconds();
        }
    } else {
        /* Moving in calendar */
        if (month > 11) {
            month = 0;
            year++;
        }
        if (month < 0) {
            month = 11;
            year--;
        }
    }

    if (document.getElementById) {
        cnt = document.getElementById("calendar_data");
    } else if (document.all) {
        cnt = document.all["calendar_data"];
    }

    cnt.innerHTML = "";

    str = ""

    //heading table
    str += '<table class="calendar"><tr><th width="50%">';
    str += '<form method="NONE" onsubmit="return 0">';
    str += '<a class="cal" href="javascript:month--; initCalendar();">&laquo;</a> ';
    str += '<select class="tbox" id="select_month" name="monthsel" onchange="month = parseInt(document.getElementById(\'select_month\').value); initCalendar();">';
    for (i =0; i < 12; i++) {
        if (i == month) selected = ' selected="selected"';
        else selected = '';
        str += '<option value="' + i + '" ' + selected + '>' + month_names[i] + '</option>';
    }
    str += '</select>';
    str += ' <a class="cal" href="javascript:month++; initCalendar();">&raquo;</a>';
    str += '</form>';
    str += '</th><th width="50%">';
    str += '<form method="NONE" onsubmit="return 0">';
    str += '<a class="cal" href="javascript:year--; initCalendar();">&laquo;</a> ';
    str += '<select class="tbox" id="select_year" name="yearsel" onchange="year = parseInt(document.getElementById(\'select_year\').value); initCalendar();">';
    for (i = 1920; i < 2008; i++) {
        if (i == year) selected = ' selected="selected"';
        else selected = '';
        str += '<option value="' + i + '" ' + selected + '>' + i + '</option>';
    }
    str += '</select>';
    str += ' <a class="cal" href="javascript:year++; initCalendar();">&raquo;</a>';
    str += '</form>';
    str += '</th></tr></table>';

    str += '<table class="calendar"><tr>';
    for (i = 0; i < 7; i++) {
        str += "<th>" + day_names[i] + "</th>";
    }
    str += "</tr>";

    var firstDay = new Date(year, month, 1).getDay();
    var lastDay = new Date(year, month + 1, 0).getDate();

    str += "<tr>";

    dayInWeek = 0;
    for (i = 0; i < firstDay; i++) {
        str += "<td class='forumheader3'>&nbsp;</td>";
        dayInWeek++;
    }
    for (i = 1; i <= lastDay; i++) {
        if (dayInWeek == 7) {
            str += "</tr><tr>";
            dayInWeek = 0;
        }

        dispmonth = 1 + month;

        if (window.opener.dateType == 'datetime' || window.opener.dateType == 'date') {
            actVal = "" + formatNum4(year) + "-" + formatNum2(dispmonth, 'month') + "-" + formatNum2(i, 'day');
        } else {
            actVal = "" + formatNum4(year) + formatNum2(dispmonth, 'month') + formatNum2(i, 'day');
        }
        if (i == day) {
            style = ' class="selected"';
            current_date = actVal;
        } else {
            style = ' class="forumheader3"';
        }
        str += "<td" + style + "><a href=\"javascript:returnDate('" + actVal + "');\">" + i + "</a></td>"
        dayInWeek++;
    }
    for (i = dayInWeek; i < 7; i++) {
        str += "<td>&nbsp;</td>";
    }

    str += "</tr></table>";

    cnt.innerHTML = str;

    // Should we handle time also?
    if (window.opener.dateType != 'date' && !clock_set) {

        if (document.getElementById) {
            cnt = document.getElementById("clock_data");
        } else if (document.all) {
            cnt = document.all["clock_data"];
        }

        str = '';
        init_hour = hour;
        init_minute = minute;
        init_second = second;
        str += '<form method="NONE" class="clock" onsubmit="returnDate(\'' + current_date + '\')">';
        str += '<input id="hour"    type="text" size="2" maxlength="2" onblur="this.value=formatNum2d(this.value, init_hour, \'hour\'); init_hour = this.value;" value="' + formatNum2(hour, 'hour') + '" />:';
        str += '<input id="minute"  type="text" size="2" maxlength="2" onblur="this.value=formatNum2d(this.value, init_minute, \'minute\'); init_minute = this.value;" value="' + formatNum2(minute, 'minute') + '" />:';
        str += '<input id="second"  type="text" size="2" maxlength="2" onblur="this.value=formatNum2d(this.value, init_second, \'second\'); init_second = this.value;" value="' + formatNum2(second, 'second') + '" />';
        str += '<br />';
        str += '<input type="submit" value="' + submit_text + '"/>';
        str += '</form>';

        cnt.innerHTML = str;
        clock_set = 1;
    }

};

/**
 * Returns date from calendar.
 *
 * @param   string     date text
 */
returnDate = function(d) {
    txt = d;
    if (window.opener.dateType != 'date') {
        // need to get time
        h = parseInt(document.getElementById('hour').value,10);
        m = parseInt(document.getElementById('minute').value,10);
        s = parseInt(document.getElementById('second').value,10);
        if (window.opener.dateType == 'datetime') {
            txt += ' ' + formatNum2(h, 'hour') + ':' + formatNum2(m, 'minute') + ':' + formatNum2(s, 'second');
        } else {
            // timestamp
            txt += formatNum2(h, 'hour') + formatNum2(m, 'minute') + formatNum2(s, 'second');
        }
    }

    window.opener.dateField.value = txt;
    window.close();
};
function checknumber(){
	var x=document.checknum.pnum.value;
	var anum=/(^\d+$)|(^\d+\.\d+$)/;
	if (anum.test(x)) {
		testresult=true;
	} else {
		alert("Please input a valid number!");
		testresult=false;
	}
	return (testresult);
};
function checkban(){
	if (document.layers||document.all||document.getElementById) {
		return checknumber();
	} else {
		return true;
	}
};
function formatDecimal(argvalue, addzero, decimaln) {
  var numOfDecimal = (decimaln == null) ? 2 : decimaln;
  var number = 1;

  number = Math.pow(10, numOfDecimal);

  argvalue = Math.round(parseFloat(argvalue) * number) / number;
  argvalue = "" + argvalue;

  if (argvalue.indexOf(".") == 0)
    argvalue = "0" + argvalue;

  if (addzero == true) {
    if (argvalue.indexOf(".") == -1)
      argvalue = argvalue + ".";

    while ((argvalue.indexOf(".") + 1) > (argvalue.length - numOfDecimal))
      argvalue = argvalue + "0";
  }

  return argvalue;
};

function calcTotal() {
	if(document.getElementById('premium').value == "NaN.00") {document.getElementById('premium').value = "0.00";}
	if(document.getElementById('admin_fee').value == "NaN.00") {document.getElementById('admin_fee').value = "0.00";}
	if(document.getElementById('policy_fee').value == "NaN.00") {document.getElementById('policy_fee').value = "0.00";}
	document.getElementById('total').value = eval(document.getElementById('premium').value) +eval( document.getElementById('admin_fee').value) + eval(document.getElementById('policy_fee').value);
	document.getElementById('total').value=formatDecimal(document.getElementById('total').value,true,2);
};

