// CONFIRM DELETE

function ConfirmDelete(newLocation, sitename){
	if (confirm('HEY, du er i ferd med å slette hele siden "'+sitename+'", er du sikker på at du vil fortsette?')) {
		window.location = newLocation;
	}
}

function Confirm(){
	if (confirm('LOL?')) {
		window.location = newLocation;
	}
}


// PUBLISH SITE

function PublishSite(strURL, myCheck, resultElement, myTable, myId) {
	
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            SiteSubmitted(self.xmlHttpReq.responseText, resultElement, subValue);
        }
	}
	
	var subValue = 0;
	if (myCheck.checked) var subValue = 1;
	
    self.xmlHttpReq.send('sub=' + subValue + '&id=' + myId + '&table=' + myTable);
}

function SiteSubmitted(str, rElement, bol){
	
	var myElement = document.getElementById(rElement);
	
	if (bol == 1){
		$(rElement).morph('background:#9FB4CC;', {duration:.3});
	}else{
		$(rElement).morph('background:#BAC7D2;', {duration:.3});
	}
	
	Effect.Appear(openElement, { duration: 1 });
	
}


function GetType(obj) {
	if (obj.constructor.toString().indexOf("Array") != -1){
		return "Array";
	}else if (obj.constructor.toString().indexOf("String") != -1){
		return "String";
	}else if (obj.constructor.toString().indexOf("Function") != -1){
		return "Function";
	}else{
		return false;
	}
}


/////// POST TEXT

function PostText(strURL, inputElement, myTable, myRow, myId, resultElement) {
	
	if (GetType(inputElement) == "Array"){
		for (i = 0; i<inputElement.length; i++){
			ExecutePost(strURL, inputElement[i], myTable, myRow[i], myId, resultElement[i]);
		}
	}else if (GetType(inputElement) == "String") {
		ExecutePost(strURL, inputElement, myTable, myRow, myId, resultElement);
	}else{
		alert('PostText failed'); return false;
	}
    
}

function ExecutePost(strURL, inputElement, myTable, myRow, myId, resultElement) {
	
	var Test = {
		
		fire: function(strURL, inputElement, myTable, myRow, myId, resultElement){
		
			var xmlHttpReq = false;
			var self = this;
			// Mozilla/Safari
			if (window.XMLHttpRequest) {
				self.xmlHttpReq = new XMLHttpRequest();
			}
			// IE
			else if (window.ActiveXObject) {
				self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			}
			self.xmlHttpReq.open('POST', strURL, true);
			self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			self.xmlHttpReq.onreadystatechange = function() {
				if (self.xmlHttpReq.readyState == 4) {
					Test.finish(self.xmlHttpReq.responseText, resultElement, subValue);
				}
			}
			
			var subValue = document.getElementById(inputElement).value;
			subValue = escape(subValue);
			self.xmlHttpReq.send('sub=' + subValue + '&row=' + myRow + '&id=' + myId + '&table=' + myTable);
			
		},
		
		finish: function(str, resultElement) {
			
			if (GetType(resultElement) == "Function"){
				resultElement();
				return;
			}
			
			if (resultElement == "" || !resultElement) {
	
				return;
			
			}else{
			
				if (resultElement.length>0){
					
					if (resultElement == 'flash_callback') document.getElementById('mySite').DoTricks(str);
					
					for ( j = 0; j < resultElement.length; j++){
						
						if (GetType(resultElement[j]) == "Function"){
							resultElement();
						}else if (GetType(resultElement[j]) == "String"){
							var curelement = document.getElementById(resultElement[j]);
							curelement.innerHTML = str;
						}
					}
				}
			}
		}
	}
	
	Test.fire(strURL, inputElement, myTable, myRow, myId, resultElement);
	
}


/////// TEXTFIELD LIMIT

function limitText(limitField, limitCount, limitNum) {
	var LF = document.getElementById(limitField);
	var LC = document.getElementById(limitCount);
	if (LF.value.length > limitNum) {
		LF.value = LF.value.substring(0, limitNum);
	} else {
		LC.innerHTML = (limitNum - LF.value.length) + '/' + limitNum;
	}
}


function CreateLimitField(limitField, limitCount, limitNum){
	var LF = document.getElementById(limitField);
	var LC = document.getElementById(limitCount);
	LF.setAttribute('onkeydown', 'limitText("'+limitField+'","'+limitCount+'",'+limitNum+')');
	LF.setAttribute('onkeyup', 'limitText("'+limitField+'","'+limitCount+'",'+limitNum+')');
	limitText(limitField, limitCount, limitNum);
}


///////// AJAX UPLOAD

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 ajaxUpload(form,url_action,id_element,html_show_loading,html_error_http,preloader_element){
	var detectWebKit = isWebKit();
	form = typeof(form)=="string"?$m(form):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($m(id_element)==null){
		erro += "The element of 3rd parameter does not exists.\n";
	}
	if(erro.length>0){
		alert("Error in call ajaxUpload:\n" + erro);
		return;
	}
	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);
	window.frames['ajax-temp'].name="ajax-temp";
	var doUpload = function(){
		removeEvent($m('ajax-temp'),"load", doUpload);
		var cross = "javascript: ";
		cross += "window.parent.$m('"+id_element+"').innerHTML = document.body.innerHTML; void(0);";
		$m(preloader_element).innerHTML = html_error_http;
		$m(preloader_element).innerHTML = 'Great success!';
		$m('ajax-temp').src = cross;
		if(detectWebKit){
        	remove($m('ajax-temp'));
        }else{
        	setTimeout(function(){ remove($m('ajax-temp'))}, 250);
        }
    }
	addEvent($m('ajax-temp'),"load", doUpload);
	form.setAttribute("target","ajax-temp");
	form.setAttribute("action",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(preloader_element).innerHTML = html_show_loading;
	}
	form.submit();
}

/////// site on load

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

/////////

function Callback(element, callback) {
	$(element).update(callback);	
}







//////// TEST

function AddItem(strURL, myCheck, resultElement, myTable, myId, ppid) {
	
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            ItemAdded(self.xmlHttpReq.responseText, resultElement, subValue);
        }
	}
	
	var subValue = 0;
	if (myCheck.checked) var subValue = 1;
	
    self.xmlHttpReq.send('sub=' + subValue + '&id=' + myId + '&table=' + myTable + '&ppid=' + ppid);
}

function ItemAdded(str, rElement, bol){
	
	var myElement = document.getElementById(rElement);
	
	if (bol == 1){
		$(rElement).morph('background:#9FB4CC;', {duration:.3});
	}else{
		$(rElement).morph('background:#BAC7D2;', {duration:.3});
	}
	
	Effect.Appear(openElement, { duration: 1 });
	
}









///////// Get image source

function GetImageSource(number, table, strURL, resultElement) {
	
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            GotImageSource(self.xmlHttpReq.responseText, resultElement);
        }
	}
    self.xmlHttpReq.send('id=' + number + '&table=' + table);
}

function GotImageSource(response, resultElement) {
	
	//var metatags = document.getElementsByTagName("meta");

    //for (cnt = 0; cnt < metatags.length; cnt++) if (metatags[cnt].getAttribute("name") == "description") alert(response);//metatags[cnt].setAttribute("content", "daedeadaede");
	
	var myElement = document.getElementById(resultElement);
	myElement.innerHTML = response;
}

function CheckAddress() {
	var address = location.hash;
	var address_array = address.split("/");
	if (address_array[1] == "works") GetImageSource(address_array[3], "hd2_images", "admin/scripts/getshit.php", "replacement");
}