/* make remote request for logic */
/* 
	@type: request type, GET or POST
	@url: url for the resource
	@params: parameters for the resource
	@elem: dom element to return data
*/
function make_logic_request(type, url, params, elem) {
	try {
		xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (e) { /* do nothing */ }
	
	ele = elem;
	if((elem==null)||(elem=='')) 
		ele = 'ajax_data';
	
	xmlhttp.onreadystatechange = get_logic;
	
	uri = url;
	if((params!=null)||(params!=''))
		uri = url+'?'+params;
	
	if(type=='GET') {
		xmlhttp.open("GET", uri);
		xmlhttp.send(null);
	} else if(type=='POST') {
		xmlhttp.open("POST", uri);
		xmlhttp.send(params);
	}
}

/* make remote request for data */
/* 
	@type: request type, GET or POST
	@url: url for the resource
	@params: parameters for the resource
	@elem: dom element to return data
*/
function make_data_request(type, url, params, elem) {
	try {
		xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (e) { /* do nothing */ }
	
	ele = elem;
	if((elem==null)||(elem=='')) 
		ele = 'ajax_data';
	
	xmlhttp.onreadystatechange = get_data;
	
	uri = url;
	if((params!=null)||(params!=''))
		uri = url+'?'+params;
	
	if(type=='GET') {
		xmlhttp.open("GET", uri);
		xmlhttp.send(null);
	} else if(type=='POST') {
		xmlhttp.open("POST", uri);
		xmlhttp.send(params);
	}
}

/* get data returned by ajax */
function get_data() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)){
		document.getElementById('main_data').innerHTML = xmlhttp.responseText;
	}
}

/* get logic returned by ajax */
function get_logic() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)){
		eval(xmlhttp.responseText);
	}
}


/* basic form validation */
function check_form(poForm){
	for(i=0; i<poForm.elements.length; i++){
		if(poForm.elements[i].lang == "true"){
			//alert(poForm.elements[i].type); 
			switch (poForm.elements[i].type) {
				case "text":
				case "password":
				case "textarea":
				case "file":
					if(trim(poForm.elements[i].title)!=""){
						if(trim(poForm.elements[i].value) == ""){
							alert(poForm.elements[i].title);
							poForm.elements[i].value = "";
							poForm.elements[i].focus();
							return false;
						}
					}
					//custom validations
					if(poForm.elements[i].alt!=null){
						if(trim(poForm.elements[i].alt)!=''){
							//check email address
							if((trim(poForm.elements[i].value) != "")&&(poForm.elements[i].alt=="email")){
								if(!check_email(poForm.elements[i].value)){
									alert("Please enter a valid e-mial address");
									poForm.elements[i].focus();
									return false;
								}
							}
							//check valid files
							if((trim(poForm.elements[i].value) != "")&&(poForm.elements[i].alt=="file")){
								var tmp_file_obj = poForm.elements[i];
								var tmp_file_name = trim(poForm.elements[i].value);
								var tmp_file_types = trim(poForm.elements[i].accept);
								if(!check_filename(tmp_file_name)){
									alert("The filename can not contain special characters");
									tmp_file_obj.focus();
									return false;
								}
								if(!check_filetype(tmp_file_name,tmp_file_types)){
									alert("Invalid file, check file type");
									tmp_file_obj.focus();
									return false;
								}
							}
						}
					}
					break;
				case "radio":
					elradio = eval("poForm."+poForm.elements[i].name);
					checado = false;
					if(isArray(elradio)){
						for(j=0; j<elradio.length; j++){
							if(elradio[j].checked)
								checado = true;
						}
					} else{
						if(elradio.checked)
							checado = true;
					}
					if(!checado){
						if(isArray(elradio)){
							alert(elradio[0].title);
							elradio[0].focus();
						} else{
							alert(elradio.title);
							elradio.focus();
						}
						return false;
					}
					break;
				case "select-one":
					if(poForm.elements[i].selectedIndex < 1){
						alert(poForm.elements[i].title);
						poForm.elements[i].focus();
						return false;
					}
					break;
				case "select-multiple":
					if(poForm.elements[i].selectedIndex < 1){
						alert(poForm.elements[i].title);
						poForm.elements[i].focus();
						return false;
					}
					break;
			}
		}
	}
	return true;
}
/* remove trailing and leading blanks from a string */
function trim(s) {
	while (s.substring(0,1) == ' ') {
		s = s.substring(1,s.length);
	}
	while (s.substring(s.length-1,s.length) == ' ') {
		s = s.substring(0,s.length-1);
	}
	return s;
}

