// JavaScript Document
window.onload = pageinit ;

var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)// If NS -- that is, !IE -- then set up for mouse capture
document.onmousemove = getMouseXY; // Set-up to use getMouseXY function onMouseMove
var tempX = 0 // Temporary variables to hold mouse x-y pos.s
var tempY = 0

function pageinit() {

} 

function client_clicked(id){ // count how many ticked and decide wot 2 do iput id in post_to_me.php
//alert(count_selected("client_list"));
	if(count_selected("client_list")>0){
		disp_hide("email_input_form",""	);
		show_hide("email_input_form",""	);
	} else {
		disp_hide("","email_input_form"	);
		//show_hide("","email_input_form"	);
	}
}


function con_c(str_in) {
	document.getElementById("msg_c").innerHTML +="<br />"+ str_in ;
}
function send_mail_button(){ // the button was clicked so we actually send it

	mailit="y" ;
	document.getElementById("email_preview").innerHTML = "" ; 
	check_form() ; 
	document.getElementById("confirm_send_mail").checked = false ;
	document.getElementById("confirm_send_mail").disabled = true ;
	document.getElementById("submitx").disabled = true;
	send_mail() ;
}
function check_can_email(){
	if(document.getElementById("confirm_send_mail").checked && check_form().length==0) {
		document.getElementById("submitx").disabled = false;
	} else {document.getElementById("submitx").disabled = true;}
}
function send_mail() { // we are going to send a csv of clients and prod_codes

	var image_size = document.getElementById("image_size").value ; 
	var str = "mailit="+mailit+"&message="+document.getElementById("message").value +"&subject="+ document.getElementById("subject").value;
	str += "&action=sendmail&prod_csv="+ csv_selected("del_idx[]") + "&iw="+image_size
	str += "&clients_csv="+ csv_selected("client_list");
	str += "&sender_name="+ document.getElementById("sender_name").value +"&sender_email="+ document.getElementById("sender_email").value
	postData= str ;
	//con_c(postData) ;
	disp_hide("email_preview","");
	show_hide("email_preview","");
	send_post("post_to_me.php",postData,"email_preview") ;
}
function send_mini_mail(){
	var image_size = document.getElementById("image_size").value ; 
	// clients_csv,prod_csv,iw,sender_name,sender_email,message
	send_post("post_to_me.php",postData,"email_preview")

}
function show_info(id){
	var id = document.getElementById("info_"+id).value ;  
	id = id.replace(/\|/g, "<br />") ;
	show_hide("client_details","") ;
	disp_hide("client_details","") ;
	document.getElementById("client_details").style.left = (tempX + 50) +"px" ;
	document.getElementById("client_details").style.top = (tempY-10) +"px" ;
	document.getElementById("client_details").innerHTML = id ;//+" tempX="+ tempX;
}


function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  

  return  true ;//tempX,tempX ;
}

function list_clients() { //email_input_form always return a false
	var search_type ="" ;
	disp_hide('form_del_faves','email_preview') ; // hide em both
	if(document.getElementById("search_text").value.length>0 ) {
		document.getElementById("msg_f").innerHTML = "Select some recipients." ;
		disp_hide('','page_text') ; //document.getElementById("page_text").innerHTML = "" ;
		if(document.getElementById("search_type").checked) {search_type = "all";}
		postData= "action=clients&search_type="+ search_type +"&search_text="+ document.getElementById("search_text").value ;
		send_post("post_to_me.php",postData,"result_clients") ;
		return false ;
	} else {alert("Enter a business name.");}
}
function do_show(val) {
	disp_hide('','email_preview,form_del_faves') ; // hide em both
	disp_hide(val,'') ; // show the clicked one 
	//var str = count_selected("del_idx[]") ;
	//document.getElementById("msg_f").innerHTML  +=" Photos selected: " + str ;
	if(val=="email_preview") {
		mailit="n";
		if(check_form().length==0) {send_mail();} //else {alert("form fail");}
	}
	return false; 
} 

function check_form(){
var msg_f = "" ;

if(count_selected("del_idx[]")<1) {
	msg_f += "Please select some Pictures to send. \n";
	show_hide("form_del_faves","email_preview");
	disp_hide("form_del_faves","email_preview");
}
if(document.getElementById("subject").value.length<3) {msg_f += "The Subject requires at least 3 letters. \n";}
if(document.getElementById("message").value.length<5) {msg_f += "The Message requires at least 5 letters. \n";}
if(count_selected("client_list")<1) {msg_f += "Please select a Client to send it to. \n";}


	if(msg_f.length<1){ // you passed msg_f was empty
		//document.getElementById("msg_f").style.color = "#006600";
		document.getElementById("confirm_send_mail").disabled = false ; 
		document.getElementById("msg_f").innerHTML =" <b>"+ count_selected("del_idx[]") + "</b> Photos going to <b>"+ count_selected("client_list") +"</b> clients." ;
		document.getElementById("form_del_faves").style.display = "none" ; 
		//document.getElementById("faves_select").style.height = "10px" ; 

	} else { // you failed something
		document.getElementById("msg_f").style.color = "#FF0000";
		document.getElementById("submitx").disabled = true;
		document.getElementById("confirm_send_mail").checked = false ; 
		document.getElementById("confirm_send_mail").disabled = true ; 
		msg_f = "*** Form Failed please check the following: \n \n " + msg_f ;
		document.getElementById("msg_f").innerHTML =msg_f.replace(/\n/g,"<br />")  ;
		alert(msg_f) ;
	}
	return msg_f ; 
} 


function csv_selected(name){
var arr = document.getElementsByName(name) ;
var csv = "" ; 
var sep="" ;
	for(var x=0 ; x<arr.length ; x++) {
		if(csv.length>0) {sep="|";} else {sep="" ;}
		if(name!="del_idx[]") {
			if(arr[x].checked) {csv+= sep + arr[x].value}
		} else {
			if(arr[x].checked) {csv+= sep + arr[x].id} // the prod_code is stored in the id, the idx is in the value
		}
	}
	return csv ; 
}

function count_selected(name){
	var arr = document.getElementsByName(name) ;
	var icount = 0 
		for(var x=0 ; x<arr.length ; x++) {
			if(arr[x].checked) {icount++}
		}
	return icount ; 
}
function select_all_pics(){
	var arr = document.getElementsByName("sel_idx") ;
		for(var x=0 ; x<arr.length ; x++) {
			arr[x].checked =document.getElementById("sel_pics").checked ;
		}
}