var xmlHttp

function ajaxdisplay (url, load, divdisplay, type) {
	xmlHttp=GetXmlHttpObject ();
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url2 = url+"&sid"+Math.random();
	xmlHttp.onreadystatechange=function() {
		stateChanged (load, divdisplay, type);
	};
	xmlHttp.open("GET",url2,true);
	xmlHttp.send(null); 
}
function stateChanged(load, divdisplay, type){

	if(load) {
		try {
			if (xmlHttp.readyState==1){
			document.getElementById(load).style.display="block";
			} if (xmlHttp.readyState==2) {
				document.getElementById(load).style.display="block";
			} if (xmlHttp.readyState==3) {
				document.getElementById(load).style.display="block";
			}
		} catch (e) {
		}
	}
	if (xmlHttp.readyState==4) {
		if(xmlHttp.status == 200){
			if(load) {
				try {
					document.getElementById(load).style.display="none";
				} catch (e) {
				}
			}
			if(type) {
				if (divdisplay) {
					document.getElementById(divdisplay).value=xmlHttp.responseText;
				}
				if(document.readyState) {
					if (document.readyState=="complete") {
					     suggestInput_init();
					}
				} else {
					try {
					  	if (window.captureEvents) {
						    window.captureEvents(Event.LOAD)
//						    window.onload=suggestInput_init();
						}
					}
					catch (e) {
	//				  	suggestInput_init();
					}
				}
			} else {
				if(divdisplay) {
//alert(xmlHttp.responseText);
					document.getElementById(divdisplay).innerHTML=xmlHttp.responseText;				
					
					if (document.getElementById("my_action")) {
											
						if (document.getElementById("my_action").value == 'del_photo') { // delete banner photo

							var this_photo = document.getElementById("current_photo").value;
							var quantity = document.getElementById("quantity").value;
							var new_quantity = quantity - 1;
							document.getElementById("quantity").value = new_quantity;

								if (new_quantity == 10) {
									document.getElementById("upload_form").style.display = "none";
								} else {
									document.getElementById("upload_form").style.display = "block";
								}	
									
								document.getElementById("photos_quantity").innerHTML = 'Quantity of images: ' + new_quantity;	
								document.getElementById("pictureId_" + this_photo).style.display = "none";					
							} 	
						
					}				
				}
				
			}
			return true;
		} else {
			if (load) {
				document.getElementById(load).style.display="none";
			}
			if (type) {
				//document.getElementById(divdisplay).value="HTTP " + xmlHttp.status;
			} else {
				if (divdisplay){
					//document.getElementById(divdisplay).innerHTML="HTTP " + xmlHttp.status;
				}	
			}
	    }
	}
}

function sendRequest(frm, load, divdisplay) {
 
	var oForm = document.forms[frm];
	var sBody = getRequestBody(oForm);

	var oOptions = {
    	method: "post",
        parameters: sBody,
        onSuccess: function (oXHR, oJson) {
            document.getElementById(divdisplay).innerHTML=oXHR.responseText;
						
			if (divdisplay == 'send') {
				document.getElementById("send_form").style.display = "none";
			}
			
        },
        onFailure: function (oXHR, oJson) {
            alert("An error occurred: " + oXHR.statusText);
        }
    };   
    var oRequest = new Ajax.Request(oForm.action, oOptions);  
}
 
function getRequestBody(oForm) {
 
	var aParams = new Array(); 
     for (var i=0; i < oForm.elements.length; i++) {
         if(oForm.elements[i].type == "radio" || oForm.elements[i].type == "checkbox"){
         	if(oForm.elements[i].type == "radio"){
         		if(oForm.elements[i].checked ) {
         			var sParam = oForm.elements[i].name;
         			sParam += "=";
         		   	sParam += oForm.elements[i].value
//alert(oForm.elements[i].value);
         		}
         	} else {
//alert (oForm.elements[i].value);
				if (oForm.elements[i].value == 'on') {
					var sParam = oForm.elements[i].name;
	         		sParam += "=";
	         		sParam += oForm.elements[i].checked;
				}	
         	}
         } else {
			oForm.elements[i].value = oForm.elements[i].value;
         	var sParam = oForm.elements[i].name;
        	sParam += "=";
         	sParam += encodeURIComponent(oForm.elements[i].value);
         }
//alert(sParam);
         aParams.push(sParam);
     } 
     return aParams.join("&");        
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
	  	xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
	  	try {
	    	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  	catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	}
	return xmlHttp;
}

function drop_spaces(str) {
    var newstr = trim(str);
    return newstr.replace(/(\s)+/g, ""); 
}

function trim(str) {
    var newstr = str.replace(/^\s*(.+?)\s*$/, "$1");
    if (newstr == " ") {
        return "";
    }
    return newstr;
}

function check_email(email) {
    var template = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z])+$/;
    email = drop_spaces(email);
    if (template.test(email)) {
        return true;
    }
    return false; 
}

function isFloat (summ) {

	if (summ.length > 0) {
		var template = /^-?\d+[\.|\,]?\d+$/;
		summ = drop_spaces(summ);
		if (template.test(summ)) 
			return true;
		else 
			return false;
	}
	else
		return false;
}

function clearError (id) {
	document.getElementById('e_' + id).className = '';
	document.getElementById(id).className = 'input_t';
}

function show_indicator (id) {
	document.getElementById(id).innerHTML = '<div id="process">Please wait...</div>';
}

function hide_div (id) {
	if (document.getElementById(id))
		document.getElementById(id).style.display = 'none';
}

function change_total_pages () {

	if (document.getElementById('total_pages_value')) {
	
		var total = document.getElementById('total_pages_value').value;
		var new_total = total - 1;
		
		document.getElementById('total_pages_value').value = new_total;
		
		if (new_total > 0) {
		
			var page_total = ' page';
			
			if (new_total > 1)
				page_total = ' pages';
	
			document.getElementById('total_pages').innerHTML = '(' + new_total + page_total + ')';
		} else {
			document.getElementById('total_pages').style.display = 'none';
		}
	}
	

}

function check_ticket (frm, param, div) {

	var err = true;
	var error_class = 'form_error';
	var error_input = 'input_error';

	if (drop_spaces(trim(document.getElementById("ticket").value)) == '' || document.getElementById("ticket").value == 'Ticket ID' ) {
		document.getElementById("ticket").value = '';
		document.getElementById("ticket").className = error_input;
		document.getElementById("e_ticket").className = error_class;
		document.getElementById("e_ticket").innerHTML = 'Please enter Ticket ID';
		err = false;
	}	
		
	var email = document.getElementById("email").value; 
	if (email != '' && email != 'Email Address') {
		if (!check_email(email)) {
			err = false;
			document.getElementById("email").className = error_input;
			document.getElementById("e_email").className = error_class;
			document.getElementById("e_email").innerHTML = 'You have entered an incorrect email address';		
		 }
	} else {
		err = false;
		document.getElementById("email").value = '';
		document.getElementById("email").className = error_input;
		document.getElementById("e_email").className = error_class;
		document.getElementById("e_email").innerHTML = 'Please enter your email address';
	}		
		
	if (err) {
		document.getElementById("result").innerHTML = '<div id="process">Saving...</div>';
  		sendRequest(frm, param, div);
	}
  	else
  		return false;
}
function set_action (action) {
	document.getElementById('my_action').value = action;
}
function set_photo (photo, action) {
	document.getElementById('current_photo').value = photo;
	document.getElementById('my_action').value = action;
}