document.onmousemove = mouseMove;

var mouseObject = null;
var serverRoot = "http://" + window.location.hostname + "/";
var timestampObject = new Date();
var uploadField;
var ie = false;

function closeImage()
{
		//var overLay = document.getElementById("modelImageOverlay");
		var imageDiv = document.getElementById("modelImageDiv");
		
		if(overLay && imageDiv)
		{
			clearInnerHTML(overLay);
			clearInnerHTML(imageDiv);
			
			document.body.removeChild(overLay);
			document.body.removeChild(imageDiv);
		
		}
}

var overImage = false;
var imageDiv;
var imageDivImage;
var xInput;
var yInput;

function imageMouseOver(image)
{
	overImage = true;
	if (self.innerWidth) 
	{
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	}
	else if (document.body) {
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	}	
	
	var aheight = frameHeight;
	aheight = ((aheight / 2) - (200));
	
	var awidth = frameWidth;
	awidth = ((awidth / 2) - (170));
	
	if(!imageDiv)
	{	
		imageDiv = document.createElement("div");
		imageDiv.style.backgroundColor = "#FFFFFF";
		imageDiv.style.zIndex = 100;
 		imageDiv.style.display = "block";
 		imageDiv.style.position = "absolute";
 		
 		
  		imageDiv.style.textAlign = "center";
 		imageDiv.style.padding = "10px";
 		
 		imageDivImage = document.createElement("img");
		imageDivImage.src = image;
		imageDivImage.align = "center";
		imageDivImage.style.display = "block";
		imageDivImage.style.marginLeft = "auto";
		imageDivImage.style.marginRight = "auto";
		
// 		img.style.position = "relative";
//  		img.style.width = "400px";
		
		
 		imageDiv.appendChild(imageDivImage);

		document.body.appendChild(imageDiv);
	}
	else
	{
		imageDiv.style.display = "";
		imageDivImage.src = image;
	}
	
	imageDiv.style.top = aheight + "px";
	imageDiv.style.left = awidth + "px";
	imageDiv.Y = aheight;
	imageDiv.X = awidth;
	
// 	xInput = document.getElementById("x");
// 	yInput = document.getElementById("y");
	
	
	
	
	document.onmousemove = function(e)
	{
		if(overImage)
		{
			if (!e){var e = window.event;}
			
			
			//imageDiv.X + (e.clientX + 10);// + 300 + ;
// 			xInput.value = imageDiv.offsetHeight;
			
			if(e.clientX + 330 >  frameWidth){imageDiv.style.left = (e.clientX - 330) + "px";}
			else{imageDiv.style.left = (e.clientX + 10) + "px";}
			
			if(e.clientY + imageDiv.offsetHeight >  frameHeight)
			{
				var difference = e.clientY + imageDiv.offsetHeight - frameHeight;
				imageDiv.style.top = (e.clientY - (difference + 10)) + "px";
			}
			else{imageDiv.style.top = (e.clientY) + "px";}
			
			
			

		}
		
	};
	
}
function imageMouseOut()
{
	overImage = false;	
	document.onmousemove = null;
	imageDivImage.src="";
	imageDiv.style.display = "none";
	
}



function mouseMove(e)
{
	if (!evt){var evt = window.event;}
	mouseObject = e;
}

function viewImage(image)
{
		
// 		var div = document.createElement("div");
// 		div.id = "modelImageOverlay";
// 		div.style.backgroundColor = "#000000";
// 		div.style.zIndex = 2;
// 		div.style.display = "block";
// 		div.style.position = "absolute";
// 		div.style.left = "0px";
// 		div.style.top = "0px";
// 		div.style.height = "100%";
// 		div.style.width = "100%";
// 		
// 		div.style.opacity = 0.7;
// 		div.style.MozOpacity = 0.7;
// 		div.style.filter = "alpha(opacity=" + (0.7*100) + ")";	
		
		var imageDiv = document.createElement("div");
		imageDiv.style.backgroundColor = "#FFFFFF";
		imageDiv.style.zIndex = 100;
 		imageDiv.style.display = "block";
 		imageDiv.style.position = "absolute";
		
		
		if (self.innerWidth) {
			frameWidth = self.innerWidth;
			frameHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientWidth) {
			frameWidth = document.documentElement.clientWidth;
			frameHeight = document.documentElement.clientHeight;
		}
		else if (document.body) {
			frameWidth = document.body.clientWidth;
			frameHeight = document.body.clientHeight;
		}
		
		var aheight = frameHeight;
		aheight = ((aheight / 2) - (200));
		
		var awidth = frameWidth;
		awidth = ((awidth / 2) - (170));
//  		alert(aheight);
		
		
 		imageDiv.style.top = aheight + "px";
 		imageDiv.style.left = awidth + "px";
//  		imageDiv.style.width = "400px";
 		imageDiv.style.textAlign = "center";
 		imageDiv.style.padding = "10px";
// 		imageDiv.style.paddingBottom = "50px";
		imageDiv.onclick = function()
		{
			document.body.removeChild(this);
		};
		imageDiv.id = "modelImageDiv";
		
		var img = document.createElement("img");
		img.src = image;
		img.align = "center";
		img.style.display = "block";
		img.style.marginLeft = "auto";
		img.style.marginRight = "auto";
		img.onclick = function()
		{
			document.body.removeChild(imageDiv);
		};
// 		img.style.position = "relative";
//  		img.style.width = "400px";
		
		
 		imageDiv.appendChild(img);
		
		
// 		document.body.appendChild(div);
		document.body.appendChild(imageDiv);
	
}



function prefillField(field,value)
	{
		if(field.value == value)
		{
			field.value = "";
			field.style.color = "#000000";
		}
		else if(empty(field.value))
		{
			field.value = value;
			field.style.color = "grey";
		}
	}
function rand (min, max) {
    var argc = arguments.length;
    if (argc === 0) {
        min = 0;
        max = 2147483647;
    } else if (argc === 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
 
}

function $c(element){return document.createElement(element);}
function domCreateTable(){return $c("table");}
function domCreateTR(){return $c("tr");}
function domCreateTD(){return $c("td");}
function domCreateText(textValue){return document.createTextNode(textValue);}
function domCreateLink(url,textValue,onclick)
{
	var returnLink = $c("a");
	returnLink.href= url;
	returnLink.appendChild(domCreateText(textValue));
	if(onclick){returnLink.setAttribute("onClick",onclick);}
	return returnLink;
	

}
function $(field)
{
	var domObject = document.getElementById(field);
	return domObject;
}

function boolToInt(value)
{
	if(value){return 1;}
	else{return 0;}
}

function intToBool(value)
{
	if(value == 1){return true;}
	else{return false;}
}
function swapBool(value)
{
	if(value){return false;}
	else{return true;}
}
function enableDisableField(value,field)
{
	if(value){$(field).disabled = false;}
	else{$(field).disabled = true;}

}

function formatField(field,formatting,additionalValue,callBack)
{
	
	var fieldObj = document.getElementById(field);
	if(fieldObj)
	{
		switch(formatting)
		{
			case 0: if(fieldObj.style.fontWeight == "bold") fieldObj.style.fontWeight = ""; else fieldObj.style.fontWeight = "bold";
				break;
			case 1: if(fieldObj.style.fontStyle == "italic") fieldObj.style.fontStyle = ""; else fieldObj.style.fontStyle = "italic";
				break;
			case 2: fieldObj.style.textDecoration = additionalValue;
				break;
			case 3: fieldObj.style.color = additionalValue;
				break;
		}
	
		if(callBack){eval(callBack);}
	}
	fieldObj = null;
	

}

function locationRedirect(url){setTimeout(function(){window.location = url;},0);}

function toggleFieldsWithCheckBox(checkBox,fieldArray,direction)
{
	if(direction != false && empty(direction)){direction = true;}
	for(var x=0;x<fieldArray.length;x++)	
	{
		var element = document.getElementById(fieldArray[x]);
		
		if(checkBox.checked)
		{
			
			if(direction){element.disabled = true;}
			else{element.disabled = false;}
		}
		else
		{
			if(direction){element.disabled = false;}
			else{element.disabled = true;}
		}
		
		element = null;
	}

}

function checkboxBoolValue(checkbox)
{
	if(checkbox.checked){return true;}
	else{return false;}

}

function showHideElement(obj)
{
	var element  = document.getElementById(obj);
	if(element.style.display == ""){element.style.display = "none";}
	else{element.style.display = "";}
	element = null;
}

function uploadFile(fileFolder,fileUpload,waitingElement,generateFolder,encodeFilename,callBackFunction,returnData,generateDataFile)
{
	
	var timestamp = timestampObject.getTime();
	var uploadWindow = window.open('','uploadWindow'+timestamp,'height=100,width=100');
	
	
	if(uploadWindow && !uploadWindow.closed)
	{
		var oldFileUpload = document.getElementById(fileUpload);
		var windowDocument = uploadWindow.document;
		var submitForm = uploadWindow.document.createElement("form");
		
 		windowDocument.body.appendChild(submitForm);
		
		if(submitForm)
		{
			if(!empty(callBackFunction)){callBackFunction = "&&callback=" + callBackFunction;}
			else{callBackFunction = "";}
			
			if(generateFolder==true){generateFolder = "&&generateFolder=1";}
			else{generateFolder = "";}
			
			if(encodeFilename==true){encodeFilename = "&&encodeFilename=1";}
			else{encodeFilename = "";}
			
			if(returnData==true){encodeFilename = "&&returnData=1";}
			else{returnData = "";}
			
			if(generateDataFile==true){generateDataFile = "&&generateDataFile=1";}
			else{generateDataFile = "";}
			
			submitForm.action = serverRoot + "upload.php?uploadFile=1&&folder="+ fileFolder + callBackFunction + generateFolder + encodeFilename + returnData + generateDataFile;
			submitForm.encoding = "multipart/form-data";
			submitForm.method = "POST";
			submitForm.target = "uploadWindow"+timestamp;
			
			var newFileUpload = oldFileUpload.cloneNode(true);
			newFileUpload.name = "uploadFile" + timestamp;
			submitForm.appendChild(newFileUpload);
			if(!empty(waitingElement))
			{
				waitingElement = document.getElementById(waitingElement);
				if(waitingElement){waitingElement.innerHTML = "Uploading file<img src=\"skins/default/images/ajax-loader.gif\" style=\"vertical-align: middle;\">";}
			}
			
			submitForm.submit();
			
			newFileUpload = null;
			
			
		}
		else{alert("Could not find upload form");}
		
		oldFileUpload = null;
		windowDocument = null;
		submitForm = null;
	}
	else{alert("Please disable popup blockers to upload");}
	
	timestamp = null;
	uploadWindow = null;
	waitingElement = null;
	
	
}

function getTopPos(inputObj)
	{
	
		var returnValue = inputObj.offsetTop;
		while((inputObj = inputObj.offsetParent) != null){
			returnValue += inputObj.offsetTop;
		}
		return returnValue;
	}
function setMoveableObject(obj)
{
	WindowDrag.init(obj);
	return false;

}

function ObjectPosition(obj) {
    var curleft = 0;
      var curtop = 0;
      if (obj.offsetParent) {
            do {
                  curleft += obj.offsetLeft;
                  curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
      }
      return [curleft,curtop];
      curleft = null;
      curtop = null;
}

var WindowDrag = 
	{
		windowObj : null,
		
		init : function(obj,minX,minY,maxX,maxY,callback)
		{
			
			obj = document.getElementById(obj);
			
			obj.minX = empty(minX) ? minX : null;
			obj.maxX = empty(maxX) ? maxX : null;
			obj.minY = empty(minY) ? minY : null;
			obj.maxY = empty(maxY) ? maxY : null;
			
			obj.onMoveEnd = new Function();
			obj.onMove = new Function();
			var objOffset = ObjectPosition(obj);
			obj.parentOffsetLeft = objOffset[0];// - obj.offsetWidth;
			obj.parentOffsetTop = objOffset[1];// - obj.offsetHeight;
			
			document.onmousemove = WindowDrag.windowMove;	
			document.onmouseup = WindowDrag.end;
			windowObj = obj;
			window.status = "(" + windowObj.parentOffsetTop + ")";
			document.body.style.cursor = "move";
			return false;
		
		},
		
		windowMove : function(e)
		{
			
			window.status = "";
			
			windowObj.style.top = (e.clientY - 100) + "px";
			windowObj.style.left = (e.clientX - 300) + "px";
			document.body.style.cursor = "move";
			return false;
		},
		
		
		end : function()
		{
		
			document.onmousemove = null;
			document.onmouseup = null;
			document.body.style.cursor = "default";
			return false;
			
		}
		
		
		
		
	
	
	}
	
function strtolower( str ) {return (str+'').toLowerCase();}
function empty( mixed_var ) {
    var key;

    if (mixed_var === "" || mixed_var === null || mixed_var === false || mixed_var === undefined){key = null; return true;}
 
    if (typeof mixed_var == 'object') {
        for (key in mixed_var){key = null; return false;}
        key = null; return true;
    }
    key = null;
    return false;
}
function clearInnerHTML(obj){while(obj.firstChild) obj.removeChild(obj.firstChild);}

function str_replace(search, replace, subject) {
    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
 
    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };
	 
	f = null;
	ra = null;	 
    return sa ? s : s[0];
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) 
  {
	var match = regex.exec(clearString.substr(x));
	if (match != null && match.length > 1 && match[1] != '') 
    	{
		output += match[1];
    		x += match[1].length;
    	} 
	else 
	{
      		if (clearString[x] == ' '){output += '+';}
      		else 
      		{
        		var charCode = clearString.charCodeAt(x);
        		var hexVal = charCode.toString(16);
        		output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
        		charCode = null;
        		hexVal = null;
      		}
      		x++;
    	}
  }
  
  x = null;
  regex = null;
  
  return output;
}

function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}


function trim(stringValue){return stringValue.replace(/(^\s*|\s*$)/, "");}
	
 var keyStr = "ABCDEFGHIJKLMNOP" +
                "QRSTUVWXYZabcdef" +
                "ghijklmnopqrstuv" +
                "wxyz0123456789+/" +
                "=";

function encode64(input) 
{
      var output = "";

	if(!empty(input))
	{
		input = escape(input);
	
		var chr1, chr2, chr3 = "";
		var enc1, enc2, enc3, enc4 = "";
		var i = 0;
		
		do {
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
		
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
		
			if (isNaN(chr2)) {
			enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
			enc4 = 64;
			}
		
			output = output +
			keyStr.charAt(enc1) +
			keyStr.charAt(enc2) +
			keyStr.charAt(enc3) +
			keyStr.charAt(enc4);
			chr1 = chr2 = chr3 = "";
			enc1 = enc2 = enc3 = enc4 = "";
		} while (i < input.length);
		
		chr1 = null;
		chr2 = null;
		chr3 = null;
		enc1 = null;
		enc2 = null;
		enc3 = null;
		enc4 = null;
		i = null;
	}
	
	return output;
}
  
function decode64(input) 
{
	var output = "";
	var chr1, chr2, chr3 = "";
	var enc1, enc2, enc3, enc4 = "";
	var i = 0;
	
	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	var base64test = /[^A-Za-z0-9\+\/\=]/g;
	if (base64test.exec(input)) 
	{
		alert("There were invalid base64 characters in the input text.\n" +
		"Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
		"Expect errors in decoding.");
	}
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	
	do {
		enc1 = keyStr.indexOf(input.charAt(i++));
		enc2 = keyStr.indexOf(input.charAt(i++));
		enc3 = keyStr.indexOf(input.charAt(i++));
		enc4 = keyStr.indexOf(input.charAt(i++));
	
		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;
	
		output = output + String.fromCharCode(chr1);
	
		if (enc3 != 64) {
		output = output + String.fromCharCode(chr2);
		}
		if (enc4 != 64) {
		output = output + String.fromCharCode(chr3);
		}
	
		chr1 = chr2 = chr3 = "";
		enc1 = enc2 = enc3 = enc4 = "";
	
	} while (i < input.length);
	
	chr1 = null;
	chr2 = null;
	chr3 = null;
	enc1 = null;
	enc2 = null;
	enc3 = null;
	enc4 = null;
	i = null;
	
	return unescape(output);
}
   
   
   