//  +-- __         2003/04/14     jjimenez@interhost.com
// /-/_/_/|        2003 Copyright Servicios de Hosting en Internet, S.A. and
// |-+_/_/||                      Serviços na Área da Internet, S.A
// |-+_|_|/                       http://www.interhost.com
// +-+_|_/                        mailto:support@interhost.com
// I n t e r H o s t   

function SacOWR(where,name,how) { 
	var w = window.open(where,name,how);
	w.focus();
	return w;
}

function SacOW(where,name,how) { // Only for return nothing.
	SacOWR(where,name,how);
}

function SacOW0(where,name) { // Only for return nothing.
	SacOWR(where,name,'width=800,height=600,scrollbars=1,resizable=1');
}
function SacOW1(where,name) { // Only for return nothing.
	SacOWR(where,name,'width=910,height=460,scrollbars=1,resizable=1');
}
function SacOW1_r(where,name) {
	SacOWR(where,name,'width=690,height=460,scrollbars=1,resizable=1');
}
function SacOW2(where,name) { // Only for return nothing.
	SacOWR(where,name,'width=380,height=400,scrollbars=1,resizable=1');
}
function SacOW3(where,name) { // Only for return nothing.
	SacOWR(where,name,'width=340,height=400,scrollbars=1,resizable=1');
}
function SacOW4(where,name) { // Only for return nothing.
	SacOWR(where,name,'width=190,height=150,scrollbars=1,resizable=1');
}
function SacOWChat(where,name) { // Only for return nothing.
	SacOWR(where,name,'width=480,height=300,scrollbars=1,resizable=1');
}
function SacOWChat2(where,name) { // Only for return nothing.
	SacOWR(where,name,'width=340,height=300,scrollbars=1,resizable=1');
}
function SacOWJqm(where,name) { // Only for return nothing.
	SacOWR(where,name,'width=300,height=380,scrollbars=1,resizable=1');
}
function SacDisplay( name, how, title, what) {
	if( how == '' )
		how = 'width=480,height=300,scrollbars=1,resizable=1';

	var w = SacOWR( '',name,how);
	w.document.writeln('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">');
	w.document.writeln('<html>');
	w.document.writeln('<head>');
	w.document.writeln('\t<title>Admon. BD</title>');
	w.document.writeln('\t<meta name="MSSmartTagsPreventParsing" content="TRUE">');
	w.document.writeln('\t<meta HTTP-EQUIV="Content-Type"        content="text/html; charset=iso-8859-1">');
	w.document.writeln('\t<meta http-equiv="Content-Style-Type"  content="text/css">');
	w.document.writeln('\t<meta HTTP-EQUIV="Pragma"              content="no-cache">');
	w.document.writeln('\t<meta HTTP-EQUIV="Cache-Control"       content="no-store, no-cache, must-revalidate, post-check=0, pre-check=0, false">');
	w.document.writeln('\t<meta name="generator"                 content="Sac">');
	w.document.writeln('\t<meta name="robots"                    content="noindex, nofollow">');
	w.document.writeln('\t<meta name="keywords"                  content="hosting,housing">');
	w.document.writeln('\t<meta name="author"                    content="">');
	w.document.writeln('\t<link rel="stylesheet"                 type="text/css" TITLE="Default Stylesheet" href="/etc/default.css">');
	w.document.writeln('</head>');
	w.document.writeln('<body>');
	if( title != '' )
		w.document.writeln('<p><img src="/img/bullets/li_box.gif"><b>' + title + '</b></p>');
	w.document.writeln( unescape(what));
	w.document.writeln('</body>');
	w.document.writeln('</html>');
	w.document.close();
}

function SacConfirmLocation( msg, f) {
	if( confirm( msg) ) {
		document.location = f;
	}
}

function SacConfirmLocation2( msg, f) {
	if( confirm( msg) ) {
		SacOW2( f, "aux");
	}
}

function PerSend( msg, url) {
	if( confirm(msg))
		location.href = url;
}

function SacXorForm( d, s) {
	for (var i = 0; i < d.elements.length; i++) {
		if(d.elements[i].type == 'checkbox' && d.elements[i].name.substr(0,s.length) == s) {
			d.elements[i].checked =         !(d.elements[i].checked);
		}
	}
}

// Return true if a string contains only whitespace characters.
function SacVisblank(s) {
	for( var i= 0; i <= s.length; i++) {
		var c = s.charAt(i);
		if((c != ' ') && (c != '\n') && (c != '\t'))
			return false;
	}
	return true;
}

// Will be invoked from onSubmit() event handler.
// The handler should return whatever value this function return.
// Use example: <form onSubmit="
//		this.firstname.optional   = true; // true for verify 
//		this.phonenumber.optional = true;
//		this.zip.min	= 0;
//		this.zip.max	= 99999;
//		return SacV(this);">
function SacV( f) {
	var msg;
	var empty_fields = "";
	var errors = "";

//	txt = new Array();
//	txt[0] = "Please correct these error(s) and re-submit";
//	txt[1] = "must be a number";
//	txt[2] = "The following required field(s) are empty";
//		if(e.optional && ((e.type == "text") || (e.type == "textarea") || (e.type == "hidden"))) {
	
	for( var i = 0; i < f.length; i++) {
		var e = f.elements[i];
		if(e.optional) {
			if((e.value == null) || (e.value == "") || SacVisblank(e.value)) {
				empty_fields += "\n      - " + e.msg;
				continue;
			}

			if(e.numeric || (e.min != null) || (e.max != null)) {
				var v = parseFloat(e.value);
				if(isNaN(v) || ((e.min != null) && (v < e.min)) ||
					((e.max != null) && (v > e.max))) {

					errors += "*      - '" + e.msg +"'"+ " " + txt[1];
					if( e.min != null)
						errors += " > " + e.min;
					if((e.max != null) && (e.min != null))
						errors += ", < " + e.max;
					else if (e.max != null)
						errors += " < " + e.max;
					errors += ".\n";
				}
			}
		}
	}
	if( !empty_fields && !errors) return true;

	msg  = "---[SAC]---------------------------------------------------------\n";
	msg += txt[0]+".\n";
	msg += "-----------------------------------------------------------------\n";

	if( empty_fields) {
		msg += "* "+txt[2]+": " + empty_fields + "\n";
		if(errors)
			msg += "\n";
	}
	msg += errors;
	msg += "---------------------------------------------------------[SAC]---\n";
	alert( msg);
	return false;
}

function SacAlert( m) {
	msg  = "---[SAC]---------------------------------------------------------\n";
	msg += m + "\n";
	msg += "---------------------------------------------------------[SAC]---\n";
	return alert( msg);
}

function SacFlag( msg, f ) {
	if( f == 1)
		if( ! confirm( msg))
			f = 0;
}

// f          - form
// nodes      - node array
// speeds     - node speed
// id_devint2 - id_devint of other cable side
// port       - port name
// n          - If fill for a old value
function SacPlugFillPort( f, nodes, id_devint2, port, n) {
	var i = 0;
	var j = 0;

	// Remove old values on id_devint.
	for( i = 0; i < f['sfields[id_devint2]'].length; i++)
		f['sfields[id_devint2]'].options[0] = null;
	// Default (and first) values.
	j = 0;
	if( n == true ) {
		f['sfields[id_devint2]'].options[j] = new Option( '* ' + port, id_devint2);
		j++;
	}
	// Set new values.
	for( i in nodes[f['device'].value] ) {
		f['sfields[id_devint2]'].options[j] = new Option( nodes[f['device'].value][i], i);
		j++;
	}
}

// f          - form
// nodes      - node array
// speeds     - node speed
// id_inttype - id_inttype of port
// id_speed   - Actual speed
// n          - If fill for a old value
function SacPlugFillSpeed( f, nodestype, speeds, id_inttype, id_speed, n) {
	var i = 0;
	var j = 0;

	// Remove old values on id_speed.
	for( i = 0; i < f['sfields[id_speed]'].length; i++)
		f['sfields[id_speed]'].options[0] = null;
	// Default (and first) values.
	j = 0;
	var t1 = id_inttype;
	var t2 = nodestype[f['sfields[id_devint2]'].value];

//	alert( "t1: " + t1);
//	alert( "f['sfields[id_devint2]'].value: " + f['sfields[id_devint2]'].value);
//	alert( "t2: " + t2);

	// Set new values.
	for( i in speeds[t1][t2] ) {
		f['sfields[id_speed]'].options[j] = new Option( speeds[t1][t2][i], i, ((n == true) && (i == id_speed)) ? true : false);
		j++;
	}


}

// s - form (this)
// f2 - input 
// defn - Port
// defv - id_devint
// arr1 - node array
// arr2 - speeds array
function SacFillChange( s, f2, id_speed, defn, defv, arr1, arr2, id_devint1) {
	var i = 0;
	var j = 0;

	// Remove old values on id_devint.
	for( i = 0; i < f2.length; i++)
		f2.options[0] = null;
	// Default (and first) values.
	j = 0;
	if( defn !== false ) {
		f2.options[j] = new Option( '* ' + defn, defv);
		j++;
	}
	// Set new values.
	for( i in arr1[s.value] ) {
		f2.options[j] = new Option( arr1[s.value][i], i);
		j++;
	}
	//
	// Remove old values on id_speed.
	for( i = 0; i < id_speed.length; i++)
		id_speed.options[0] = null;
	// Default (and first) values.
	j = 0;
	if( defn !== false ) {
		f2.options[j] = new Option( '* ' + defn, defv);
		j++;
	}

	return true;
}

function SacFill2Selects( f1, f2, arr, def) {
	var opt1n = new Array();

	// Fill first select
	var i = 0;
	for( var o in arr)
		opt1n[i++] = o;
	// Remove old values.
	var len = f1.length;
	for( var i = 0; i < len; i++)
		f1.options[0] = null;
	// Set new values.
	if( (f2 != undefined) && ( values != undefined) ) {
		for( n in names) {
			alert( "n: " + n + ", name: " + names[n] + ", value: " + values[n]);
			f.options[n] = new Option( names[n], values[n]);
		}
	}
}

function SacFillSelect( f, arr) {
	// Remove old values.
	var len = f.length;
	for( var i = 0; i < len; i++)
		f.options[0] = null;

	// Set new values.
	if( (names != undefined) && ( values != undefined) ) {
		for( n in names) {
			f.options[n] = new Option( names[n], values[n]);
		}
	}
}

function SacTrim( s) {
	var ret = s.replace('/^[\s\r\n]*|[\s\r\n]*$/g','');
	return ret;
}

function SacSetDate( f, d) {

	var now = new Date();
	now.setTime(now.getTime() + d * 24 * 60 * 60 * 1000); 

	var day   = ( now.getDate()       < 10 ) ? '0' + now.getDate().toString()  : now.getDate();
	var month = ((now.getMonth() + 1) < 10 ) ? '0' + (now.getMonth() + 1).toString() : (now.getMonth() + 1);

	f.value= now.getFullYear() + '-' + month + '-' + day;
}

function SacGetSelText()
{
	var txt = '';
	if (document.getSelection) {
		txt = document.getSelection();
	}
	else if (window.getSelection) {
		txt = window.getSelection();
	}
	else if (document.selection) {
		txt = document.selection.createRange().text;
	}

	return txt;
}

function SacClockBackCount( ah, am, as, bh, bm, bs) {
	if( ah.value == '') ah.value = bh;
	if( am.value == '') am.value = bm;
	if( as.value == '') as.value = bs;

	setTimeout( "SacClockBackCountWrite()", 1000);
}
function SacClockBackCountWrite( beginhour, beginmin, beginseg) {
	as.value--;
	if( as.value == -1) {
		am.value--;
		as.value = 59;
		if( am.value == -1) {
			ah.value--;
			am.value = 59;
		}
	}
	
}
