var formFieldNames = new Array('_fuss_form_username')	

var allForms = new Array();
var siteListCounter = 0;
var statusObj = new createStatus();
var waitisrunning = false;
var venter = null;
var data = null;
var mega = 1;
var vl = null;
var forste = true;
var currentengine ='';
var startTime;
var timeLeft;
var nytt;
var isrunning = false;
var nw; // endpage window
var mainWinOpen = true;

/********************************/
/************Start cookie********/
 
var expireDays = 90; // Number of days until the cookie expires.

//top.initFields();

function initFields(){
	var f3Cookie = GetCookie("fu3");
	if(f3Cookie!=null){
		uf3 = unescape(f3Cookie);
		var f3Arr = uf3.split('\n');
		for(var i=0;i<f3Arr.length;i++){
			var skille = f3Arr[i].indexOf('::')
			first = f3Arr[i].substring(0,skille);
			var funnet = eval('top.fussmain.document.forms[0].elements["'+first+'"]')
			if(funnet){
				funnet.value = f3Arr[i].substring(skille+2,f3Arr[i].length);
			}
		}
		var was = GetCookie("wasrunning");
		if(was!=null){
			was = unescape(was);
			AC = was.split("=");
			if(AC[0]=='AC' && AC[1]!='' && AC[1]!='0'){ // finaly found cookie value!
				if(confirm('Hi, and welcome back.\n\nThe last time you visited this site you left\n the main page before the submission was done.\nYou have now the chance of continue with \nthe registration to the last search engines.\n\nWould you like to continue?\n')){
					start(AC[1]); // provide the start function with info about where we left the submission last time
				}
				else{
					updatecookie();
				}
			}
		} // end if was
	}
}

function getCookieVal (offset)
   {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
      endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
   }

function GetCookie (name){
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen) 
      {
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg)
         return getCookieVal (j);
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) 
         break; 
      }
  return null;
}

function SetCookie(name, value){
   var argv = SetCookie.arguments;
   var argc = SetCookie.arguments.length;
   var expires = (2 < argc) ? argv[2] : null;
   var path = (3 < argc) ? argv[3] : null;
   var domain = (4 < argc) ? argv[4] : null;
   var secure = (5 < argc) ? argv[5] : false;
   document.cookie = name + "=" + escape (value) +
     ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
     ((path == null) ? "" : ("; path=" + path)) +
     ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
   }

function SaveCookie(){
	var df = top.fussmain.document.forms[0];
	var cook = ''
   for(var i=0;i<df.length;i++){
	if(df.elements[i].type=='text'){
		cook += df.elements[i].name + '::' + df.elements[i].value + '\n';
	}
   }
   var c = escape(cook);
   var expdate = new Date();
   expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * expireDays)); 
   SetCookie("fu3", c, expdate, "/", null, false);
}


/********************************/
/************Start cookie********/

function createStatus(){
	this.all = new Array();
	this.choosen = new Array();
	this.indicator = new Array();
	this.totalengines = 0;
	this.atcurrent = 0;
}


function inNameList(form,str){
	for(var i=0;i<formFieldNames.length;i++){
		if(formFieldNames[i]==str.toLowerCase())return eval('form.'+str+'.value');
	}
	return '';
}

function formdata(f){
	this.formmethod = (f.method) ? f.method : '';
	this.formname = (f.name) ? f.name : '';
	this.formaction = (f.action) ? f.action : '';
	this.inputname = new Array();
	this.inputvalue = new Array();
	for(var i=0;i<f.length;i++){
		// Because it's difficult to manipulate the action, when a formfield has the name action we
		// will have to substitute formsfield with these name...
		if(f.elements[i].name.indexOf('_fuss_action_not_allowed')!=-1)this.inputname[i] = 'action';
		else{
			if(f.elements[i].name.indexOf('_fuss_post_not_allowed')!=-1)this.inputname[i] = 'post';
			else{
				if(f.elements[i].name.indexOf('_fuss_method_not_allowed')!=-1)this.inputname[i] = 'method';
				else this.inputname[i] = (f.elements[i].name) ? f.elements[i].name : '';
			}
		}
		this.inputvalue[i] = (f.elements[i].value) ? f.elements[i].value : '';
	}
	this.formlength = i;
}


function debug(hva){
	v = window.open('','','width=400,height=500,scrollbars=1')
	v.document.write(hva);
	v.document.close();
}

function init(){
	var f = parent.bottom.document;
	for(var i=0;i<f.forms.length;i++){
		var formObj = new formdata(f.forms[i]);
		allForms[i] = formObj; // henter alle data fra alle forms
		statusObj.all[i] = true;
		statusObj.choosen[i] = true;
		statusObj.indicator[i] = false;
	}
	statusObj.totalengines = i;
}


function viewlist(){
	vl = window.open('','trearn','width=150,height=400,scrollbars=1,top=0,left=0,status=0');
//	top.init();	
	vl.document.open();
	vl.document.write('<html><head>\n'+style()+'\n<title>The engine list</title></head><body leftmargin="2" topmargin="3">\n');
	vl.document.write('<table cellspan="0" cellpadding="2" border="0"><tr><td bgcolor="#003265"><b><font color="white">Search engine list:</font></b></td></tr><tr><td>');
	for(var i=0;i<statusObj.totalengines;i++){
		vl.document.write(i+1+'. <a href="' + pairValue(allForms[i],"_fuss_url") + '" target="toern">');
		vl.document.write(pairValue(allForms[i],"_fuss_name")+'</a><br>\n');
	}
	vl.document.write('</td></tr><tr><td><br><b>Counting '+i+' engines!</b></td></tr></table><br>');
	vl.document.write('<div align="center"><a href="javascript:self.close()">close</a></div>');
	vl.document.write(html_end());
	vl.document.close();
}

function style(){
	var s = '';
	s += '<style type="text/css">\n';
	s += '.smalgra  { background-color : #003265; color : #000000; font-family : MS Sans Serif,Arial,Verdana; font-size : 8px; font-weight : normal; }';
	s += '.hvit  	  { background-color : #FFFFFF; color : #000000; font-family : MS Sans Serif,Arial,Verdana; font-size : 8px; font-weight : normal; }';
	s += 'body,td { font-family : MS Sans Serif,Arial,Verdana; font-size : 8pt;}\n';
	s += 'a { font-family : MS Sans Serif,Arial,Verdana; font-size : 8pt;text-decoration : none;}\n';
	s += '</style>\n';
	return s;
}

function w(str){
	top.bottom.document.write(str+'');
}

function pairValue(formObj, name){
	var value = '';
	for(var i=0;i<formObj.inputname.length;i++){
		if(formObj.inputname[i]==name)value = formObj.inputvalue[i];
	}
	return value;
}

function m(str){
	nw.document.write(str+'');
}

function makeEndPage(){
	var d = new Date();
	nw = window.open('','navn','width=450,height=500,toolbar=1,scrollbars=1');
	nw.document.open();
	m('<HTML>\n<HEAD>\n<TITLE>URL-check info</TITLE>\n');
	m('<LINK HREF="fuss.css" SRC="fuss.css" REL="STYLESHEET" TYPE="text/css">\n');
	m('</HEAD>\n<BODY LEFTMARGIN=1 TOPMARGIN=1>\n');
	m('<table>\n<tr><td valign="top">\n');
	m('<font size="3">Report generated '+ d.toLocaleString()+' by the <br>Search engine registration script at http://foxnetwork.com.au/.<p></font>\n');
	m('<table border="0" cellspacing="0" cellpadding="3" width="600">\n');
	for(var i=0;i<allForms.length;i++){
//		if(statusObj.indicator[i]){
			m('<tr><td colspan="2" class="ename">'+pairValue(allForms[i],"_fuss_name")+'</td></tr>\n')
			var currentForm = allForms[i];
			var ret = '';
			for(var k=0;k<currentForm.formlength;k++){
				if(currentForm.inputname[k].substring(0,6)!='_fuss_' && currentForm.inputvalue[k].substring(0,6)=='_fuss_'){
					ret += '<tr><td class="edata">' + currentForm.inputvalue[k].substring(currentForm.inputvalue[k].lastIndexOf('_')+1,currentForm.inputvalue[k].length);
					ret += '</td><td class="edata">' + formNameFoundforendpage(currentForm.inputvalue[k]);
					ret += '</td></tr>\n';
				}
			}
			m(ret);
			m('<tr><td colspan="2">&nbsp;</td></tr>\n');
//		}
	}
//	m('<tr><td colspan="2" align="center"><br><a href="index.shtml" target="_top">Back</A></td></tr>\n');
	m('</table>\n');
	m('</TD></TR></TABLE>\n');
	m('<A HREF="http://www.recommend-it.com/l.z.e?s=421483" target="opener"><IMG SRC="/img/minitoupe.gif" WIDTH=88 HEIGHT=31 ALT="" BORDER="0"></A>');
	m('</BODY></HTML>\n');
	nw.document.close();
}

function createEnginePage(){
	var ret = '';
	var currentForm = allForms[statusObj.atcurrent];
	ret += '<FORM';
	if(currentForm.formname!='')ret += ' NAME="'+currentForm.formname+'"';
	if(currentForm.formmethod!='')ret += ' METHOD="'+currentForm.formmethod+'"';
	if(currentForm.formaction!='')ret += ' ACTION="'+currentForm.formaction+'"';
	ret += '>\n';
	for(var i=0;i<currentForm.formlength;i++){
		if(currentForm.inputname[i].substring(0,6)!='_fuss_'){
			ret += '<INPUT TYPE="HIDDEN" NAME="' + currentForm.inputname[i] + '"';
			ret += formNameFound(currentForm.inputvalue[i]);
			ret += '>\n';
		}
	}
	ret += '</FORM>\n';
	return ret;
}

function formNameFound(value){// value inn her er navn på formsfelt : _fuss_form_email
	var ret = '';
	for(var i=0;i<data.name.length;i++){
		if(data.name[i]==value)ret += data.value[i];
	}
	if(ret!='')ret += ' VALUE="' + ret + '"';
	return ret;
}

function formNameFoundforendpage(value){// value inn her er navn på formsfelt : _fuss_form_email
	var ret = '';
	for(var i=0;i<data.name.length;i++){
		if(data.name[i]==value)ret += data.value[i];
	}
	return ret;
}

function getData(form){
	this.name = new Array();
	this.value = new Array();
	for(var i=0;i<form.length;i++){
		this.name[i] = form.elements[i].name;
		this.value[i] = form.elements[i].value;
	}
}

function html_start(){
	var ut = '<HTML><HEAD></HEAD><BODY>\n';
	return ut;
}

function html_end(){
	var ut = '';
//	var ut = '<SC'+'RIPT>document.forms[0].submit();</SC'+'RIPT>\n';
	ut += '';
	ut += '</BODY></HTML>\n';
	return ut;
}

function selist(){
	top.init();
	top.viewlist();
}

function useProfile(){
	var up = navigator.userProfile;
	var frm = top.fussmain.document.forms[0];
	up.clearRequest(); 
	up.addReadRequest("vCard.Homepage");
	up.addReadRequest("vCard.FirstName");
	up.addReadRequest("vCard.LastName");
	up.addReadRequest("vCard.Email");
	up.addReadRequest("vCard.Business.StreetAddress");
	up.addReadRequest("vCard.Business.Zipcode");
	up.addReadRequest("vCard.Business.City");
	up.addReadRequest("vCard.Business.Country");
	up.doReadRequest(3, "search engine registration script")
	frm.elements["_fuss_form_url"].value = up.getAttribute("vCard.Homepage");
	frm.elements["_fuss_form_firstname"].value = up.getAttribute("vCard.FirstName");
	frm.elements["_fuss_form_lastname"].value = up.getAttribute("vCard.LastName");
	frm.elements["_fuss_form_email"].value = up.getAttribute("vCard.Email");
	frm.elements["_fuss_form_adress"].value = up.getAttribute("vCard.Business.StreetAddress");
	frm.elements["_fuss_form_postalcode"].value = up.getAttribute("vCard.Business.Zipcode");
	frm.elements["_fuss_form_city"].value = up.getAttribute("vCard.Business.City");
	frm.elements["_fuss_form_country"].value = up.getAttribute("vCard.Business.Country");
	up.clearRequest();
}

/***************************************************/
/***************************************************/
/******** routines for checking formsfield *********/
/*****************************************************/
var imgoff, imgoff;

function isEmpty(fieldName){
// This function returns true if the fieldname don't have any value
	var rest = eval("top.fussmain.document.forms[0]."+ fieldName + ".value");
	if(rest=='' || (rest.indexOf('http://')==0 && rest=='http://'))return true;
	return false;
}

function preLoadImages(){
// We don't have to spesify the width and height of the images, but it doesn't harm
	imgoff = new Image(14,14);
	imgoff.src = 'img/trans.gif';
	imgon = new Image(14,14);
	imgon.src = 'img/reddot.gif';	
}
function initAllImages(){
	showOk('emailimg');
	showOk('urlimg');
}

function showOk(fieldname){
	fussmain.document [fieldname].src = imgoff.src;
}

function showError(fieldname){
	fussmain.document [fieldname].src = imgon.src;
}
function doCheck(){
	var returner=false;
	if(document.Images){
		initAllImages();
		if(isEmpty('_fuss_form_url')){showError('urlimg');returner=true;}
		if(isEmpty('_fuss_form_email')){showError('emailimg');returner=true;}
//		if(top.fussmain.document.forms[0]._fuss_form_url.value.indexOf('esato.com') != -1){returner=;}
	}
	return returner;
}

function ledZero(w){
	var st = w.toString();
	if(st.length==1)st='0'+st;
	return st;
}

function getTimeLeft(){
	var ret;
	if(statusObj.atcurrent==0)return '00:00';
	var pros = (statusObj.atcurrent+1) * 100 / statusObj.totalengines;
	var now = new Date();
	var tmp = new Date();
	var tid = now.getTime() - startTime.getTime();
	var sluttKlokke = (tid / pros) * (100-pros);
	sluttTid = new Date();
	sluttTid.setTime(sluttKlokke);
	ret = ledZero(sluttTid.getMinutes()) + ':' + ledZero(sluttTid.getSeconds());
	return ret;
}

function openfset(){
	currentengine = pairValue(allForms[statusObj.atcurrent],"_fuss_name");
	mainWinOpen = true;
	nytt = window.open('fset.shtml','gelwin','width=400,height=100,status=1,scrollbars=0,toolbar=0,menubar=1,resizable=1');
}

function loaded(){
	statusObj.atcurrent++
	if(statusObj.atcurrent<statusObj.totalengines){
		timeLeft = getTimeLeft();
		top.updatecookie();
		top.openfset();
	}
	else{
		nytt.close();
		top.statusObj.atcurrent=0;
		top.isrunning=false;
		top.updatecookie();
		top.makeEndPage();
	}
}

function gd(form){
	this.name = new Array();
	this.value = new Array();
	for(var i=0;i<form.length;i++){
		this.name[i] = form.elements[i].name;
		this.value[i] = form.elements[i].value;
	}
}

function startsmall(){
	var arg = (startsmall.arguments.length!=0) ? startsmall.arguments[0] : '';
	startTime = new Date();
	init();
	data = new gd(self.bottom.document.forms[0]);
	if(arg!=''){statusObj.atcurrent=arg}
	top.isrunning = true;
	openfset();
}

function start(){
	var arg = (start.arguments.length!=0) ? start.arguments[0] : '';
	if(!doCheck()){
		startTime = new Date();
		top.SaveCookie();
		init();
		data = new getData(self.fussmain.document.forms[0]);
		if(arg!=''){statusObj.atcurrent=arg}
		top.isrunning = true;
		updatecookie();
		openfset();
	}
	else {
		alert("\nSorry, but you must have missed a field!\nPlease take a look at the field marked with red bullets");
		top.fussmain.document.forms[0].elements[0].focus();
	}
}

function updatecookie(){
	if(isrunning){
		var data = "AC=" + statusObj.atcurrent;
		data = escape(data);
		var expdate = new Date();
		expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * expireDays)); 
		SetCookie("wasrunning", data, expdate, "/", null, false);
	}
	else{
		var data = "AC=0";
		data = escape(data);
		var expdate = new Date();
		expdate.setTime(expdate.getTime()); 
		SetCookie("wasrunning", data, expdate, "/", null, false);
	}
}

function warn(){
	if(top.isrunning){
		nytt.minorWinOpen=false;
		top.isrunning=false;
//		alert('top.isrunning = '+top.isrunning + '\ntop.mainWinOpen = '+top.mainWinOpen+'\nnytt.minorWinOpen = '+nytt.minorWinOpen);
//		alert('variabelen parent.mainWinOpen er '+parent.mainWinOpen+'\nmainWinOpen er '+mainWinOpen);
//		alert('setter til parent.nytt.minorWinOpen til false eller '+nytt.minorWinOpen);
//		if(!confirm("The registration script is running,\nif you leave this page, it will stop.'\n'\nDo you want to leave?"){}
	}
}

function vs(n){
	window.status = n;
	return true;
}








