﻿function $m(theVar){
	return document.getElementById(theVar)
}

function remove(theVar){
	var theParent = theVar.parentNode;
	theParent.removeChild(theVar);
}

function addEvent(obj, evType, fn){
	if(obj.addEventListener)
	    obj.addEventListener(evType, fn, true)
	if(obj.attachEvent)
	    obj.attachEvent("on"+evType, fn)
}

function removeEvent(obj, type, fn){
	if(obj.detachEvent){
		obj.detachEvent('on'+type, fn);
	}else{
		obj.removeEventListener(type, fn, false);
	}
}

function isWebKit(){
	return RegExp(" AppleWebKit/").test(navigator.userAgent);
}

function completeAajax(content) {
	
	//$m("cT_post").innerHTML = '<table border="0" cellspacing="0" cellpadding="0" style="padding: 0; margin:0;"><tr><td width="44" style="padding: 0; margin:0;"> <img height="35" width="41"  src="<?=BASE_URL?>clientTestimonials/imgs/check.png" /> </td><td style="padding: 0; margin:0; vertical-align: middle;" nowrap> <span class="cT_result"> Thank you! Your testimonial has been posted! </span> </td></tr></table>';
		
	$m("cT_post").innerHTML = content;
	
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
		 var rZ = window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
		 var rZ = document.documentElement.clientHeight;
		}
	}
	
	jQuery('#clientTestimonials_bg').css({
		"height" : rZ
	});
	
	jQuery('#clientTestimonials').css({
		"height" : rZ
	});
	
	jQuery('#cT_thread_content').css({
		"height" : (rZ - (142 + $m("cT_post").clientHeight)),
		"overflow" : "auto"
	});
	
	jQuery('#cT_button').css({
		"top" : ((rZ/2)-75)+"px"
	});
	
}

function cropperUpload(url_action){
	var html_show_loading = 'Start loading...';
	var html_error_htt = 'error';
	var id_element = 'main_image';
	var detectWebKit = isWebKit();
	form = document.getElementById("cT_form");
	var erro="";
	
	if(form==null || typeof(form)=="undefined"){
		erro += "The form of 1st parameter does not exists.\n";
	}else if(form.nodeName.toLowerCase()!="form"){
		erro += "The form of 1st parameter its not a form.\n";
	}
	
	if(erro.length>0){
		alert("Error in call ajaxUpload:\n" + erro);
		return;
	}
	
	/*
	var $frame = jQuery('<iframe id="ajax_temp" name="ajax_temp" width="0" height="0" border="0" frameborder="0" style="width: 0; height: 0; border: none;">');
	
	jQuery('body').append($frame);
	setTimeout(function() {				
		var ajtemp = jQuery('#ajax_temp');
		var ajform = jQuery('#cT_form');
		
		ajform.attr({
			"target" : "ajax_temp",
			"action" : base_url+url_action,
			"method" : "post",
			"enctype" : "multipart/form-data",
			"encoding" : "multipart/form-data"
		});
		
		
		ajform.load(function() {
		//addEvent($m('ajax-temp'),"load", function() {
			alert("test loaded");
			removeEvent($m('ajax-temp'),"load", doUpload);
			var cross = "javascript: ";
			
			cross += "parent.completeAajax(document.body.innerHTML); void(0);";
			
			$m('ajax-temp').src = cross;
			
			if(detectWebKit){
				remove($m('ajax-temp'));
				//validate();
			}else{
				setTimeout(function(){ remove($m('ajax-temp'))}, 250);
			}							 
		});
		
		ajform.submit();
				
		ajform.attr({
			"target" : "_self",
			"action" : "ajax"
		});
		
	}, 200);
	*/
	
	var iframe = document.createElement("iframe");
	iframe.setAttribute("id","ajax-temp");
	iframe.setAttribute("name","ajax-temp");
	iframe.setAttribute("width","0");
	iframe.setAttribute("height","0");
	iframe.setAttribute("border","0");
	iframe.setAttribute("style","width: 0; height: 0; border: none;");
	form.parentNode.appendChild(iframe);
	//completeAajax('null');
	//window.frames['ajax-temp'].name = "ajax-temp";
	var doUpload = function(){
		removeEvent($m('ajax-temp'),"load", doUpload);
		var cross = "javascript: ";
		
		//cross += "parent.completeAajax(document.body.innerHTML); void(0);";
		
		$m('ajax-temp').src = cross;
		completeAajax('<table border="0" cellspacing="0" cellpadding="0" style="padding: 0; margin:0;"><tr><td width="44" style="padding: 0; margin:0;"> <img height="35" width="41"  src="<?=BASE_URL?>clientTestimonials/imgs/check.png" /> </td><td style="padding: 0; margin:0; vertical-align: middle;" nowrap> <span class="cT_result"> Thank you! Your testimonial has been posted! </span> </td></tr></table>');
		
		//if(detectWebKit){
        	//remove($m('ajax-temp'));
			//validate();
        //}else{
		//	setTimeout(function(){ remove($m('ajax-temp'))}, 250);
        //}
    }
	
	addEvent($m('ajax-temp'),"load", doUpload);
	form.setAttribute("target","ajax-temp");
	form.setAttribute("action",base_url+url_action);
	form.setAttribute("method","post");
	form.setAttribute("enctype","multipart/form-data");
	form.setAttribute("encoding","multipart/form-data");
	
	if(html_show_loading.length > 0){
		//$m("cT_welcome").innerHTML = '<img border="0" src="'+base_url+'clientTestimonials/imgs/loader.gif"> Submitting...';
	}
	
	form.submit();
	form.setAttribute("target","_self");
	form.setAttribute("action", "ajax");
}
