// define globals
var show_object = 0;
var slide_speed = 15;
var clip_left = 0;
var clip_top = 0;
var top_offset = 20;
var current_object = null;
var menu_bgcolor = '#EEEEEE';
var menu_color = '#000099';
var menu_highlight_bgcolor = '#999999';
var menu_highlight_color = '#ffffff';


// the trick to stay focused on the current edited field
var field;
function user_focus(fieldname) {
                field = fieldname;
                return field;
}

function dcinit() {
   if (document.thisform == null) return;
    if (document.thisform.guest_name != null) {
       document.thisform.guest_name.focus();
    } else if (document.thisform.subject != null) {
                            if (field != null) {
                                document.thisform.field.focus();
                            } else {
                                document.thisform.subject.focus();
                             }
    } else {
      document.thisform.elements[0].focus();
   }
}

//function dcinit() {
//   if (document.thisform == null) return;
//
//    if (document.thisform.guest_name != null) {
//       document.thisform.guest_name.focus();
//    } else if (document.thisform.subject != null) {
//       document.thisform.subject.focus();
//    } else {
//      document.thisform.elements[0].focus();
//   }
//}

function openWindow(url) {
   opinion = window.open(url,"Handheld",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=150,height=400');
}

function makeRemote(url){
	remote = window.open(url,"remotewin","width=600,height=480,scrollbars=1");
	remote.location.href = url;
	if (remote.opener == null) remote.opener = window;
}

function checkit(val) {
   dlink=document.thisform;
   len = dlink.elements.length;
   var i=0;
   for( i=0 ; i<len ; i++) {
//      if (dlink.elements[i].name=='u_id') {
         dlink.elements[i].checked=val;
//      }
   }
}

function check_cookie(cookie_name) {
   var cookies = document.cookie;
   var pos = cookies.indexOf(cookie_name,'=');
   if (pos != -1) {
      return true;
   }
   else {
      return false;
   }
}

function smilie_remote(emotionicon) {
   self.opener.document.thisform.message.value += emotionicon+" ";
   self.opener.document.thisform.message.focus();
}

function smilie(emotionicon) {
   document.thisform.message.value += emotionicon+" ";
   document.thisform.message.focus();
}

function choose_avatar(avatar) {
   self.opener.document.thisform.pc.value = avatar;
}

function jumpPage(form) {
   i = form.forum.selectedIndex;
   if (i == 0) return;
   window.location.href = url[i+1];
}


// define some background menu colors?

// close all menu windows
//
function dc_close_all() {
   dc_close_menu('dc_search_form');
   dc_close_menu('menu_1');
   dc_close_menu('menu_2');
   dc_close_menu('menu_3');
}

// function dc_get_obj_id
// return object id of the event item
function dc_get_obj_id(evt) {
   var object_id = (evt.target) ? evt.target.id : ((evt.srcElement) ? evt.srcElement.id : null);
   return object_id;
}

function dc_toggle_menu(evt,objectID) {

   if (show_object == 0) {
      show_object = 1;
	  dc_open_menu(evt,objectID);
	}
	else {
		show_object = 0;
	  	dc_close_menu(objectID);
	}
}

function dc_open_menu(evt,objectID) {

   var object = document.getElementById(objectID);
   var object3 = document.getElementById(dc_get_obj_id(evt));

   object.style.top = object3.offsetTop + top_offset;
   object.style.left = object3.offsetLeft;

   // reset clip area
   clip_left = 0;
   clip_top = 0;

//   if (current_object != objectID) {
   if (show_object) {
       dc_close_all();
	   object.style.clip="rect(0 " + clip_left + " " + clip_top + " 0)";   
	   object.style.visibility = "visible";
	   dc_slide_open(objectID);
   }

   //current_object = objectID;
   
}

function dc_slide_open(objectID) {
   var object = document.getElementById(objectID);

   var obj_w = object.offsetWidth + 30;
   var obj_h = object.offsetHeight + 30;

   clip_left += Math.floor(obj_w/slide_speed);
   clip_top += Math.floor(obj_h/slide_speed);
   
   object.style.clip="rect(0 " + clip_left + " " + clip_top + " 0)";   

   if (clip_left < 1.1*obj_w && clip_top < 1.1*obj_h ) {
      object.style.clip="rect(0 " + clip_left + " " + clip_top + " 0)";
      object_timeout_id = setTimeout("dc_slide_open('" + objectID + "')",10);
   }
   else {
//      clearTimeout("dc_slide_open(" + object_timeout_id + ")");
   }

}


function dc_close_menu(objectID) {
   var object = document.getElementById(objectID);
   if (object.style.visibility == 'visible')
      dc_slide_close(objectID);
}

function dc_slide_close(objectID) {
   var object = document.getElementById(objectID);

   var obj_w = object.offsetWidth;
   var obj_h = object.offsetHeight;

   clip_left -= Math.floor(obj_w/slide_speed);
   clip_top -= Math.floor(obj_h/slide_speed);

   if (clip_left > 0 && clip_top > 0) {
      object.style.clip="rect(0 " + clip_left + " " + clip_top + " 0)";
      object_timeout_id = setTimeout("dc_slide_close('" + objectID + "')",10);
   }
   else {
      object.style.visibility = "hidden";
//      clearTimeout("dc_slide_close(" + objectID + ")");
   }
}

function dc_highlight_cell(this_cell,this_url) {
   this_cell.style.cursor = 'pointer';
   this_cell.style.background = menu_highlight_bgcolor;
   this_cell.style.color = menu_highlight_color;
   window.status = this_url;
}

function dc_unhighlight_cell(this_cell) {
   this_cell.style.background = menu_bgcolor;
   this_cell.style.color = menu_color;
   window.status = '';
}

function dc_jump_to(this_url) {
   location.href = this_url;
}


function choose_flag(flag) {
   self.opener.document.forms['id_card_form'].id_flag.value = flag;
   self.opener.document.forms['id_card_form'].noflag.checked = false;
   window.close()
}
function check_all(form_name, field_name) {
  if(document.forms[form_name].select_all.checked==true){
    for (i = 0; i < field_name.length; i++) {
    document.forms[form_name].elements[i].checked=true;
    }
 } else {
   for (i = 0; i < field_name.length; i++) {
   document.forms[form_name].elements[i].checked=false;
   }
 }

}

function emptyFlag() {
	if(document.getElementById('noflag').checked == true)
		document.getElementById('id_flag').value = 'blank.png';
	else
		document.getElementById('id_flag').value = "";
}

function hideFilm() {
	var sel = document.getElementsByName('MainlyShoot2')[0].selectedIndex;
	//var val = document.getElementsByname('MainlyShoot2')[0].options[sel].value;
	if(sel == 2 || sel == 3) document.getElementsByName('FavFilm')[0].style.visibility = 'visible;';
	else document.getElementsByName('FavFilm')[0].style.visibility = 'hidden;'
	alert(sel);
	alert('hi');
}

function altSubmit(element, link) {

	//var answer = confirm('All unsaved changes will be lost. Do you want to continue?');
	//if (answer) {
       
        var answer = altConfirm();
        if (answer == false) return;

        var par = document.getElementById(element).value; 
	var redir = link+par;
	location.href = redir;
	//alert(redir);
	//} else {
        // alert ("ignored");
	//}
}

function altArraySubmit(elements, link) {
	
	var answer = altConfirm();
	if (answer == false) return;
       

	var list = elements.split("|");
	q = ""
	for(i=0;i<list.length;i++) {
		el = list[i];
		val = document.getElementById(el).value; 
		//q = q+"&"el+"="+val;
		q = q+"&"+el+"="+val;
	}
	
	var redir = link+q;
	location.href = redir;
	//alert(redir);
}

function altConfirm() {
	if(changedSettings == 1) {
		   var answer = confirm('All unsaved changes will be lost. Do you want to continue?');
			   if (answer) {
				return true;
			} else {
				return false;
			}
	}
	else return true;;
}

changedSettings = 0;
function putOnChange(){
	if(document.formReg2) {
		var f = document.formReg2;
		var noChange = new Array("voucherEmail");
		for (var i=0;i<f.elements.length;i++) {
			if (f.elements[i].type=="button" || f.elements[i].type=="submit" || f.elements[i].type=="reset" || f.elements[i].type=="hidden") continue; 
				if(arrayContains(noChange, f.elements[i].name)) continue;
				f.elements[i].onchange=function(){
					//add2Array(this.name)
					changedSettings = 1;
				}
		}
	}
}

function arrayContains(array, value) {
	var contains = false;
	for(i=0;i<array.length;i++) {
		if(array[i] == value)
			contains = true;
	}
	return contains;
}

function id_cards_filter(status_id, from_id, to_id) {
	var sel = document.getElementById(status_id).selectedIndex;
	var val = document.getElementById(status_id).options[sel].value;
	var from = document.getElementById(from_id).value;
	var to = document.getElementById(to_id).value;
	var q = "";
	if(val != 0) q = q+'&status_val='+val;
	if(from != "") q = q+'&from='+from;
	if(to != "") q = q+'&to='+to;
	var link = 'dcadmin.php?az=general&saz=id_cards_management&filter=1'+q;
	window.location = link;
}

window.onload=putOnChange;

/*
document.getElementsByName('MainlyShoot2')[0].onchange = function() {
	hideFilm();
}*/
