// JavaScript Document
// list of default fields in text boxes
var txtArr = Array('Please enter', 'Your name', 'Your email (will never be published)', 'Your comments');
var originalTxt = '';
var popupTimerHandle = null;
var email_pass = null;
var order = 'DESC'; // this is the global var that changes and is passed over in the ajax filter on lists (DESC is highest on top)
var populateComplete = false; // this checks wether it is safe to run another ajax call (multithreading workaround kinda)
var text_border_color = '#ac7931';


function focusTxt(obj){
	
	originalTxt = obj.value;
	
	for(i=0;i<txtArr.length;i++){
		if(obj.value == txtArr[i]){
			obj.value = '';
		}
	}
	obj.style.fontStyle = 'normal';
	obj.style.color = '#333';
	obj.style.border = '1px solid #a1c8d9';
	
}
function blurTxt(obj){
	
	obj.style.color = '#666';
	obj.style.border = '1px solid #CCC';
	
	if(originalTxt != ''){
		for(i=0;i<txtArr.length;i++){
			if(originalTxt == txtArr[i]){
				obj.value = txtArr[i];
				obj.style.fontStyle = 'italic';
			}
		}
	}
	
}
// public txt
function focusPublicTxt(obj,color){
	
	originalTxt = trimAll(obj.value);
	
	for(i=0;i<txtArr.length;i++){
		if(obj.value == txtArr[i]){
			obj.value = '';
		}
	}
	obj.style.fontStyle = 'normal';
	obj.style.border = '1px solid #333';
	
}
function blurPublicTxt(obj,color){
	
	obj.style.color = color;
	obj.style.border = '1px solid #666';
	
	if(originalTxt != ''){
		for(i=0;i<txtArr.length;i++){
			if(originalTxt == txtArr[i] && obj.value == ''){
				obj.value = txtArr[i];
			}
		}
	}
	
}
function trimAll(sString){
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function focusBtn(obj){
	obj.style.background = '#4992b2';
}
function blurBtn(obj){
	obj.style.background = '#75aac1';
}
function focusPublicBtn(obj){
	obj.style.background = '#ccc';
}
function blurPublicBtn(obj){
	obj.style.background = '#fff';
}

// 404 message in the case of a lost ajax page, should never fire.
function get_booboo(){
	return '<div class="loaderHolder">'
			
				+ '<div class="booboo">'
				+ '<p><span>Whoops, something\'s gone horribly wrong...</span></p>'
				+ '<p>It appears I can\'t find the page you\'re looking for.</p>'
				+ '</div>'
			
			+ '</div><!-- end of loaderHolder -->';	
}

// loader text for waiting on ajax call, requires refresh markup with contained function
function get_loader(function_var){
	return '<div class="loaderHolder">'
			
				+ '<div class="loading">Loading</div>'
				
				+ '<div class="spinnerHolder">'
				
					+ '<div class="spinner"><!-- no image in html to allow for easy styling in one place through css --></div>'
					
				+ '</div><!-- end of spinnerHolder -->'
				
				+ '<div style="padding:20px 0;text-align:center;cursor:pointer;">' + function_var + '</div>'
			
			+ '</div><!-- end of loaderHolder -->';	
			
}

// client side validation, only checking required fields
function isFormValid(formID, required_arr){
	
	var message = '';
	var form = document.getElementById(formID);
	
	for(i=0; i<required_arr.length; i++){
		if(form.elements[required_arr[i]].value == '' || form.elements[required_arr[i]].value == 'empty'){
			message = message + '<p><span>' + str_replace(required_arr[i], '_', ' ') + '</span> is a required field.</p>';
			document.getElementById(required_arr[i]).style.border = '1px solid red';	
		}
		else{
			document.getElementById(required_arr[i]).style.border = '1px solid ' + text_border_color;	
		}
	}
	
	if(message != ''){
		
		if(email_pass == false){
			message = message + '<p>Please provide a valid email.</p>';
		}

		document.getElementById('alertHeader').innerHTML = '<h1>Form Error</h1>';
		document.getElementById('alertTxt').innerHTML = '<p>' + message + '</p>';
		open_error('#alertDiv');
		return false;
		
	}
	
	// check if we have a password, if so, make sure the two match
	if(document.getElementById('password')){
		
		if(check_password('password', 'confirm_password') == false){
			
			document.getElementById('password').style.border = '1px solid red';
			document.getElementById('confirm_password').style.border = '1px solid red';
			document.getElementById('alertHeader').innerHTML = '<p>Form Error</p>';
			document.getElementById('alertTxt').innerHTML = '<p>Passwords do not match</p>';
			open_error('#alertDiv');
			return false;
			
		}
		
	}
	
	// check if we have username
	if(document.getElementById('username')){
		
		if(username_pass == false){
			
			document.getElementById('username').style.border = '1px solid red';
			document.getElementById('alertHeader').innerHTML = '<p>Form Error</p>';
			document.getElementById('alertTxt').innerHTML = '<p>Username already taken</p>';
			open_error('#alertDiv');
			return false;
			
		}
		
	}
	
	if(email_pass == false){
		document.getElementById('email').style.border = '1px solid red';
		document.getElementById('alertHeader').innerHTML = '<p>Form Error</p>';
		document.getElementById('alertTxt').innerHTML = '<p>Please supply a valid email address</p>';
		open_error('#alertDiv');
		return false;	
	}
	
	// check if we have a submit id
	if(document.getElementById('submitID')){
		
		document.getElementById('submitID').value = 421;
		
	}
	
	

}

// clearing forms
function clearForm(formID,clear_arr){
	
	for(i=0; i<clear_arr.length; i++){
		document.getElementById(clear_arr[i]).value = '';	
	}
	
}

function check_password(password1, password2){
	
	var p_1 = document.getElementById(password1).value;
	var p_2 = document.getElementById(password2).value;
	
	return (p_1 != p_2) ? false : true ;
	
}

function validateEmail(str){
	
	var div = document.getElementById('emailPrompt');
	
	if(isEmailValid(str)){
		 // if email is valid, show tick 
		document.getElementById("emailPrompt").style.color = '#090';
		document.getElementById("emailPrompt").innerHTML = '<img src="imgs/small_tick.png" style="float:left" /><div class="left" style="padding-left:10px"><strong>Email address is valid</strong></div>';
		document.getElementById('email_prompt_pointer').style.display = 'block';
		document.getElementById('emailPrompt').style.display = 'block';
		email_pass = true;
		 
	}else{
		// else show cross
		document.getElementById("emailPrompt").style.color = '#e02309';
		document.getElementById("emailPrompt").innerHTML = '<img src="imgs/small_cross.png" style="float:left" /><div class="left" style="padding-left:10px"><strong>Invalid email addess</strong></div>';
		document.getElementById('email_prompt_pointer').style.display = 'block';
		document.getElementById('emailPrompt').style.display = 'block';
		email_pass = false;
		
	}
}

function validateEmail2(str){
	
	var div = document.getElementById('emailPrompt2');
	
	if(isEmailValid(str)){
		 // if email is valid, show tick 
		document.getElementById("emailPrompt2").style.color = '#090';
		document.getElementById("emailPrompt2").innerHTML = ''+
		'<div class="left">'+
			'<img src="imgs/small_tick.png"/>'+
		'</div>'+
		'<div class="left" style="padding-left:5px">'+
			'Email address is valid'+
		'</div>'+
		'<div class="clear"></div>';
		document.getElementById('emailPrompt2').style.display = 'block';
		email_pass = true;
		 
	}else{
		// else show cross
		document.getElementById("emailPrompt2").style.color = '#e02309';
		document.getElementById("emailPrompt2").innerHTML = ''+
		'<div class="left">'+
			'<img src="imgs/small_cross.png"/>'+
		'</div>'+
		'<div class="left" style="padding-left:5px">'+
			'Invaild email address'+
		'</div>'+
		'<div class="clear"></div>';
		document.getElementById('emailPrompt2').style.display = 'block';
		email_pass = false;
		
	}
}

function isEmailValid(emailAddress) {
	
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);

}

// used to decode php urlencoded strings (ajax stuff) and other stuff too
function str_replace(string,seperator,replacement) {
	
	var temp = string.split(seperator);
	return temp.join(replacement);
	
}

// simple ajax call to populate form fields based on record id
function populateForm(form_id,the_class,record_id,iFrame_id){
	
	var params = 'the_class='+the_class+'&record_id='+record_id;
	// set the popluated var to false so the fetch images function won't fire
	populateComplete = false;
	
	if(window.XMLHttpRequest){var xmlhttp=new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4 && xmlhttp.status==200){
			
			var the_form = document.getElementById(form_id);
			var data = xmlhttp.responseText;
			var dataObj = eval('(' + data + ')');
			
			for( var i in dataObj ){
				
				for( var key in dataObj[i] ){
					
					if (dataObj[i].hasOwnProperty(key)) {
						
					    if(document.getElementById(key)){
							
							document.getElementById(key).value = str_replace(unescape(dataObj[i][key]), '+', ' ');
							
							// check for a textarea
							if(document.getElementById(''+key+'WidgIframe')){
								
								var value_1 = str_replace(unescape(dataObj[i][key]), '|', '"');
								var clean_value = str_replace(value_1, '+', ' ');
								
								document.getElementById(''+iFrame_id+'WidgIframe').contentWindow.document.body.innerHTML = clean_value;
								
							}
							
							// check for any images
							if(document.getElementById(''+key+'_src')){
								document.getElementById(''+key+'_src').src = '../imgs/'+img_root+'/'+unescape(dataObj[i][key])+'';
							}
							
							// check for select boxes
							if(document.getElementById('category') && key == document.getElementById('category').id){
								
								var trueVal = str_replace(unescape(dataObj[i][key]), '+', ' ');
								var selectBox = document.getElementById('category');
								
								for (var i=0; i<selectBox.options.length; i++) {
									if(selectBox.options[i].text == trueVal){
										document.getElementById('category').selectedIndex = i;
									}
								}
								
							}
							
						}
						
					}
					
				}
				
			}
			// form is no populated with data, continue if needed... (fetch_gallery_images)
			populateComplete = true;
		}
	}
	xmlhttp.open("GET","../includes/ajax/record.get.php?" + params,true);
	xmlhttp.send();
	
}

function colorElelment(html_obj){
	
	// create an array of elements with class name listRepeater (this is to loop over later and set background styles)
	// as IE is shit we have to do a work around becuse it can't manage a simple function!
	var arr = new Array;
	if (document.getElementsByClassName) {
		arr = document.getElementsByClassName('listRepeater');
	}else{
		var d = document.getElementsByTagName("div");
		for(v=0;v<d.length;v++) {
			if (d[v].className == "listRepeater") {
			  arr.push(document.getElementById(d[v].id));
			}
		}
	}
	// loop and set styles
	for(i=0;i<arr.length;i++){
		if(arr[i].id != html_obj.id){
			document.getElementById(arr[i].id).style.background = '#FFF';	
		}
	}
	html_obj.style.background = '#c8edee';
	
}

function fetch_gallery_images(gallery_name, div_id, the_class, img_root){
	
		var params = "gallery_name=" + escape(gallery_name) + "&the_class=" + the_class + "&img_root=" + img_root;
		
		if(window.XMLHttpRequest){var xmlhttp=new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4 && xmlhttp.status==200){
				if(xmlhttp.responseText.indexOf('FAIL') == -1){ // meaning not found
					// set div content to returned data
					document.getElementById(div_id).innerHTML = xmlhttp.responseText;
					// apply sortble ui
					apply_ui();
					
				}else{
					// someting went wrong, show error
					document.getElementById(div_id).innerHTML = get_booboo();
					
				}
			}else{
				
				document.getElementById(div_id).innerHTML = get_loader('<span style="cursor:pointer" onclick="fetch_gallery_images(\''+gallery_name+'\', \''+div_id+'\',\''+the_class+'\')">[refresh]</span>');	
				
			}
		}
		xmlhttp.open("GET","../includes/ajax/images.get.php?" + params,true);
		xmlhttp.send();
	
}

function fetch_blog_images(blog_id, div_id, the_class, img_root){
	
		var params = "blog_id=" + escape(blog_id) + "&the_class=" + the_class + "&img_root=" + img_root;
		
		if(window.XMLHttpRequest){var xmlhttp=new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4 && xmlhttp.status==200){
				if(xmlhttp.responseText.indexOf('FAIL') == -1){ // meaning not found
					// set div content to returned data
					document.getElementById(div_id).innerHTML = xmlhttp.responseText;
					// apply sortble ui
					apply_ui();
					
				}else{
					// someting went wrong, show error
					document.getElementById(div_id).innerHTML = get_booboo();
					
				}
			}else{
				
				document.getElementById(div_id).innerHTML = get_loader('<span style="cursor:pointer" onclick="fetch_blog_images(\''+blog_id+'\', \''+div_id+'\',\''+the_class+'\')">[refresh]</span>');	
				
			}
		}
		xmlhttp.open("GET","../includes/ajax/images.blog.get.php?" + params,true);
		xmlhttp.send();
	
}

function activateChangeBtn(the_class, record_id, img_field, old_img_name, upload_dir){
	
	// the passed in params will be added to the onclick handler for the changeBtn
	var btn = document.getElementById('changeImgBtn');
	btn.style.visibility = 'visible';
	btn.setAttribute('onclick','changeImg(\''+the_class+'\', '+record_id+', \'img\', \''+old_img_name+'\', \''+upload_dir+'\');return false');
	
}

function changeImg(the_class, record_id, img_field, old_img_name, upload_dir){
	
	// as this is a universal funcion, img_field will only be from the design section
	// we're going to popup the modal with the option to upload a new image, 
	// set a couple of hidden fields then post to an ajax page that will upload and write to the db
	var header = '<p>Select an image:</p>';
	
	// create the form
	var form = document.createElement('form');
	form.setAttribute('method','post');
	form.setAttribute('enctype','multipart/form-data');
	form.setAttribute('action','actions/image.change.php');
	form.setAttribute('id','changeImgForm');
	
	var img_input = create_img_input('New Image:', 'new_img', 'new_img');
	
	var hidden_class = create_hidden_input('the_class', the_class);
	var hidden_id = create_hidden_input('record_id', record_id);
	var hidden_url = create_hidden_input('request_url', document.getElementById('requestURL').value);
	var hidden_img_field = create_hidden_input('img_field', img_field);
	var hidden_old_img = create_hidden_input('old_img', old_img_name);
	var hidden_upload_location = create_hidden_input('upload_dir', upload_dir);
	
	var btn_holder = document.createElement('div');
	btn_holder.setAttribute('id','uploadBtnHolder');
	btn_holder.setAttribute('style','padding:10px 0 0 0;text-align:center');
	
	var btn = document.createElement('input');
	btn.setAttribute('type','image');
	btn.setAttribute('value','Upload');
	btn.setAttribute('src','../imgs/update_btn.png');
	
	var message = '<div id="changeImgHolder" style="text-align:left"></div>';
	
	open_message(header, message);
	
	document.getElementById('changeImgHolder').appendChild(form);
	document.getElementById('changeImgForm').appendChild(img_input);
	document.getElementById('changeImgForm').appendChild(btn_holder);
	document.getElementById('uploadBtnHolder').appendChild(btn);
	document.getElementById('changeImgForm').appendChild(hidden_class);
	document.getElementById('changeImgForm').appendChild(hidden_id);
	document.getElementById('changeImgForm').appendChild(hidden_url);
	document.getElementById('changeImgForm').appendChild(hidden_img_field);
	document.getElementById('changeImgForm').appendChild(hidden_old_img);
	document.getElementById('changeImgForm').appendChild(hidden_upload_location);
	
}

function create_hidden_input(name, value){
	
	var hidden_input = document.createElement('input');
	hidden_input.setAttribute('type', 'hidden');
	hidden_input.setAttribute('name',name);
	hidden_input.setAttribute('value', value);
	return hidden_input;
	
}

function resetTextBox(textElement){
	
	document.getElementById(textElement).style.fontStyle = 'normal';
	document.getElementById(textElement).style.color = '#333';
	
}

// popup for deleting records
function getRecordForDelete(the_class, record_id){
	
	document.getElementById('alertHeader').innerHTML = 'Confirm delete';
	document.getElementById('alertTxt').innerHTML = 'Are you sure you want to remove this record?' 
	+ '<div class="alertBtns">'
		+ '<div class="left">'
			+ '<input type="buton" value="Cancel" class="whiteBtn" style="width:50px;" onclick="disablePopup(\'#alertDiv\')" />'
		+ '</div>'
		+ '<div class="right">'
			+ '<input type="button" onclick="doDelete(\''+the_class+'\','+record_id+')" id="confirmBtn" name="confirmBtn" value="Proceed" class="btnIni" onmouseover="focusBtn(this)" onmouseout="blurBtn(this)" />'
    	+ '</div>'
		+ '<div class="clear"> </div>'
	+ '</div>';
	open_error('#alertDiv');
	
}

function doDelete(the_class, record_id){
	
	var params = 'the_class='+the_class+'&record_id='+record_id;
	
	if(window.XMLHttpRequest){var xmlhttp=new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){
				location.href=document.getElementById('requestURL').value + '&result=success';
			}else{
				location.href=document.getElementById('requestURL').value + '&result=fail';
			}
		}
	}
	xmlhttp.open("GET","../includes/ajax/record.delete.php?" + params,true);
	xmlhttp.send();
	
}

function filterList(filterField, divID, arrowID, listName){
	// hide all arrow images
	document.getElementById('titleArrow').style.visibility = 'hidden';
	document.getElementById('dateArrow').style.visibility = 'hidden';
	// requires table name and field to be filtered	
	var params = 'f='+filterField+'&o='+order;
	// set div content to loader
	document.getElementById(divID).innerHTML = get_loader('<span style="cursor:pointer" onclick="manageList(\''+filterField+'\', \''+divID+'\')">[refresh]</span>');
	// call ajax
	if(window.XMLHttpRequest){var xmlhttp=new XMLHttpRequest();} else {var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText.indexOf('FAIL') == -1){ // meaning not found
				// set div content to returned data
				document.getElementById(divID).innerHTML = xmlhttp.responseText;
				showArrow(''+arrowID+'');
			}else{
				// someting went wrong, show error
				document.getElementById(divID).innerHTML = get_booboo();
			}
		}
	}
	xmlhttp.open("GET","../includes/ajax/"+listName+".filter.php?" + params,true);
	xmlhttp.send();
	
}

function showArrow(arrowID){
	
	if(order == 'ASC'){
		document.getElementById(arrowID).src = '../imgs/filterDownArrow.png';
		document.getElementById(arrowID).style.visibility = 'visible';
		order = 'DESC';
	}else{
		document.getElementById(arrowID).src = '../imgs/filterUpArrow.png';	
		document.getElementById(arrowID).style.visibility = 'visible';
		order = 'ASC';
	}
	
}

function addInput(formName,elementName, div_id){
	
	var arr = new Array;
	if (document.getElementsByName) {
		arr = document.getElementsByName(elementName + '[]');
	}else{
		var d = document.getElementsByTagName("input");
		for(v=0;v<d.length;v++) {
			if (d[v].name == elementName + '[]') {
			  arr.push(document.getElementById(d[v].id));
			}
		}
	}
	var div = document.getElementById(div_id);
	// add input type (image)
	var the_input = create_img_input('Image '+(arr.length + 1)+': ', elementName+'[]', elementName+'_'+(arr.length + 1));// label, name, id
	
	// create fieldset
	var new_field_set = document.createElement('fieldset');
	new_field_set.setAttribute('className','inputSet');
	new_field_set.setAttribute('class','inputSet');
	// append to container
	div.appendChild(new_field_set);
	new_field_set.appendChild(the_input);
	
}

function create_img_input(label, name, id){
	
	var new_input = document.createElement('input');
	// name as designimage_before
	new_input.setAttribute('name',name);
	new_input.setAttribute('type','file');
	new_input.setAttribute('className', 'txtIni');
	// id is plus next available index (array count + 1)
	new_input.setAttribute('id',id);
	new_input.setAttribute('class', 'txtIni');
	// now add the label
	var new_label = document.createElement('label');
	new_label.setAttribute('for', new_input.id);
	new_label.innerHTML = label;
	// create fieldset
	var new_field_set = document.createElement('fieldset');
	new_field_set.setAttribute('className','inputSet');
	new_field_set.setAttribute('class','inputSet');
	
	new_field_set.appendChild(new_label);
	new_field_set.appendChild(new_input);
	
	return new_field_set;
	
}

function loadImg(src){
	
	document.getElementById('selectedImg').src = '../imgs/'+src;
	
}

function showRemoveBtn(button_id,record_id){
	// this is a bit of a hack, but works ok.
	// we're going to assign the record id to the buttons value, this is so we can pass it to the delete function
	document.getElementById(button_id).style.visibility = 'visible';
	document.getElementById(button_id).value = record_id;
	
}

function locateTo(url){
	
	location.href = url;
		
}

function showTitle(str){
	document.getElementById('galTitle').innerHTML = str;
}
function hideTitle(){
	document.getElementById('galTitle').innerHTML = '';	
}

function open_message(header,message){
	
	document.getElementById('alertHeader').innerHTML = header;
	document.getElementById('alertTxt').innerHTML = message;
	open_error('#alertDiv');
	
}

function open_error(divID){
	//LOADING POPUP
	centerPopup(divID);
	//load popup
	loadPopup(divID),
					
	//CLOSING POPUP
	//Click the x event!
	$(divID+'Close').click(function(){
		disablePopup(divID);
	});
	
}

function open_modal(divID){
	//LOADING POPUP
	centerPopup(divID);
	//load popup
	loadPopup(divID),
					
	//CLOSING POPUP
	//Click the x event!
	$(divID+'Close').click(function(){
		disablePopup(divID);
	});
	
}

function showHiddenDiv(divID){
	document.getElementById(divID).style.display = 'block';
}

function apply_custom_form(){
		
	var options = {styleClass: "selectDark", jScrollPane: 1}
	$(".funckySelect").styleSelect(options);
		
}

/////////////////////////////////////////////////  Fade Up Functions  ////////////////////////////////////////////////////////////////////
function fadeOut(thisId, speed) {
    var thisE = document.getElementById(thisId);
    thisE.style.zoom = 1; //needed for IE
    speed = speed/20;
    var i = 100;
    var intervalId = setInterval(function() {
        if(i>=0) {
            thisE.style.opacity = i/100;
            thisE.style.filter = 'alpha(opacity='+i+')';
            i -= 5;
        } else {
            setTimeout(function() {thisE.style.display = "none";}, speed);
            clearInterval(intervalId);
            return false;
        }
    }, speed);
}

function fadeIn(thisId, speed) {
    var thisE = document.getElementById(thisId);
    thisE.style.display = "block";
    thisE.style.zoom = 1; //needed for IE
    thisE.style.opacity = 0;
    thisE.style.filter = "alpha(opacity = 0)";
    speed = speed/20;
    var i = 0;
    var intervalId = setInterval(function() {
        if(i <= 100) {
            thisE.style.opacity = i/100;
            thisE.style.filter = 'alpha(opacity='+i+')';
            i += 5;
        } else {
            clearInterval(intervalId);
            return false;
        }
    }, speed);
}

function set_blanket_size(){
	var _docHeight = xDocSize();
//alert(_docHeight);
	blanket_height=_docHeight;//$(document).height();
	var blanket=document.getElementById('backgroundPopup');
	blanket.style.height=blanket_height+'px';
	
}

function xDocSize()
{
  var b=document.body, e=document.documentElement;
  var esw=0, eow=0, bsw=0, bow=0, esh=0, eoh=0, bsh=0, boh=0;
  if (e) {
    esw = e.scrollWidth;
    eow = e.offsetWidth;
    esh = e.scrollHeight;
    eoh = e.offsetHeight;
  }
  if (b) {
    bsw = b.scrollWidth;
    bow = b.offsetWidth;
    bsh = b.scrollHeight;
    boh = b.offsetHeight;
  }
//  alert('compatMode: ' + document.compatMode + '\n\ndocumentElement.scrollHeight: ' + esh + '\ndocumentElement.offsetHeight: ' + eoh + '\nbody.scrollHeight: ' + bsh + '\nbody.offsetHeight: ' + boh + '\n\ndocumentElement.scrollWidth: ' + esw + '\ndocumentElement.offsetWidth: ' + eow + '\nbody.scrollWidth: ' + bsw + '\nbody.offsetWidth: ' + bow);
  //return {w:Math.max(esw,eow,bsw,bow),h:Math.max(esh,eoh,bsh,boh)};
  return Math.max(esh,eoh,bsh,boh);
}


