/*
This file contains other functions and logic to perform
*/

// exclude old browsers
/*if (!document.getElementById){
	top.location.replace('http://www.iht.com/info/browser.html');
}*/

// disallow the page to be framed
/*if (window.self != window.top){ 
	top.location.replace(window.location.pathname); 
}*/
var activeFieldValue;
var modifiedFields = "";

function MM_openBrWindow(theURL,winName,features) { //v2.0
  new_window = window.open(theURL,winName,features);
  new_window.focus();
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function win_pop(url, display_win_name, width, height ){
    var pop_up = window.open(url, display_win_name,'width=' + width + ',height=' + height + ',resizable=yes,scrollbars=yes,menubar=no,status=no' );
    pop_up.focus();
}
popupWins = new Array();
function win_popup(url, name, width, height ) {
	if ( typeof( popupWins[name] ) != "object" ){		
		popupWins[name] = window.open(url,name,'width=' + width + ',height=' + height + ',resizable=yes,scrollbars=yes,menubar=no,status=no');
	} else {
		if (!popupWins[name].closed){	
		} else {
			popupWins[name] = window.open(url, name,'width=' + width + ',height=' + height + ',resizable=yes,scrollbars=yes,menubar=no,status=no');
		}
	}
	popupWins[name].focus();
}
function out_of_frame(){
	if (parent.frames.length < 1){
		document.location.href = '../index.php';
	}
}

// generic popup window
function pop_me_up(pURL,name,features, nofocus){
	
	new_window = window.open(pURL,name,features);
	if(!nofocus)
		new_window.focus();
	else if(nofocus==0)
		new_window.focus();	
}

function win_pop(url, display_win_name, width, height ){
    var pop_up = window.open(url, display_win_name,'width=' + width + ',height=' + height + ',resizable=yes,scrollbars=yes,menubar=no,status=no' );
    pop_up.focus();
}

function envoi(typ,val){     
	
	if (typ == "add_to_cart") alert("Ce produit a été ajouté à votre panier");
	if (typ == "confirm_cart" && val =="unlogged") {
		
		document.my_form.submitv.value="frm_inscription.php";
		slide_deballe();
	} else { 
		
		if (typ.indexOf("__") > 0){
			var arr_typ = typ.split("__");
			var typ2 = arr_typ[0];
		} else {
			typ2 = typ;
		}
		switch(typ2){
			case "acces_pro" :
				if(val == "unlogged") {
					document.my_form.submitv.value=(arr_typ[1]);
					slide_deballe();				
				} else {
				// théoriquement inutile
					document.location.href = arr_typ[1];
				}
				break;
			case "carriere" :
			
				break;
			default :
			   
			   
			    document.my_form.submitt.value=typ;
			    document.my_form.submitv.value=val;
			   	document.my_form.submit();
	   	}
   	}
}
function connect(){
	envoi("connexion",document.my_form.submitv.value); //???
}

function hide_layer(lay){
	if (document.all) {
		if (lay!="") {document.all[lay].style.visibility="hidden";}
	}
	if (document.layers) {
	  	if (lay!="") {document.layers[lay].visibility="hide";}
	}
	if (document.getElementById){
	    document.getElementById(lay).style.visibility="hidden";
	}	
}

function show_layer(lay){
    if (document.all) {
		if (lay!="") {document.all[lay].style.visibility="visible";}
	}
	if (document.layers) {
		if (lay!="") {document.layers[lay].visibility="show";}
	}
	if (document.getElementById){
	    document.getElementById(lay).style.visibility="visible";
	}	
}

//functions from lib_mat
function orig_val(obj){
    activeField_value=obj.value;
}

function new_val(obj){
	if ( obj.value != activeFieldValue ) {
	    modifiedFields += obj.name+",";
	   // alert(modifiedFields);
	}
}

function new_check(obj){
	modifiedFields += obj.name+",";
	//alert(modifiedFields);
}

function clear_field(obj){
	obj.value = "";
}
function clear_password(obj){
	obj.value = "";
	obj.type = "password";
}

function update_multiple_checks(obj_id, new_val, typeAction) { 
    var value_string = ""; 
    var current_values = "";
    var obj = document.getElementById(obj_id);  	
    if (typeof(obj) == "undefined" || obj.value.indexOf("undefined") > 0) {
    	current_values = "";
    	alert("Indefini");
    } else {   
    	current_values = obj.value; 
    }
    if (typeAction == "add"){
    	if (current_values !== ""){
	    	value_string = current_values + "," + new_val; 	
	    	value_string = value_string.replace(",,",",");	    	
    	} else {
    		value_string = new_val; 
    	}	
    	//TODO if journaliste show div media
    } else {  //typeAction = "remove"
    	if (current_values != "" ){
	    	current_values = current_values.replace(",,",",");
	    	if (current_values.indexOf(",") > 0){
		        var arr_val = eval("Array(" + current_values + ");");
		    	var nb_vals = arr_val.length;	    	
		        for (lop=0; lop < nb_vals; lop++) { 
		        	if (arr_val[lop]!= new_val && arr_val[lop]!= "") {		        	 	
		            	value_string = value_string + arr_val[lop] + ",";
		            } 
		        }		        
		        if(value_string.length > 1) value_string = value_string.slice(0,-1); else value_string = "";
	        } else {
	        	value_string = "";
	        }
	    }	       
    } 	
    obj.value = value_string; 
} 

function verif_pass(obj){
	var origPasswName = obj.name.substr(3);
	var origPasswValue = eval("document.my_form." + origPasswName + ".value");
	if (obj.value != origPasswValue){
		 alert("Mots de passe non identiques");
	}	
}

