﻿// File:			reposLayoutGraphs.js
// Version:			1
// Creation Date:	07/07/2005
// Author:			GTT, Inc
// Disclaimer:		The Code in this page is property of GTT, Inc and cannot be used
//					in part or total without express consent from GTT's principals
//					To request permission to use this file email support@groupTravelTechnologies.com

window.onresize = repositionGraphs;
window.onscroll = repositionGraphs;

function repositionGraphs() {
	if (document.getElementsByTagName("*").borderTable) {
		var l = document.getElementsByTagName("*").borderTable.offsetLeft;
		var h = document.getElementsByTagName("*").borderTable.offsetHeight;
		var w = document.getElementsByTagName("*").borderTable.offsetWidth;
	
		if (document.getElementsByTagName("*").logo)
			document.getElementsByTagName("*").logo.style.left = l - 8;
		if (document.getElementsByTagName("*").tlcorner)
			document.getElementsByTagName("*").tlcorner.style.left = l;
		if (document.getElementsByTagName("*").trcorner)
			document.getElementsByTagName("*").trcorner.style.left = (l + w) - 6;
		if (document.getElementsByTagName("*").blcorner)
			document.getElementsByTagName("*").blcorner.style.left = l;
		if (document.getElementsByTagName("*").blcorner)
			document.getElementsByTagName("*").blcorner.style.top = (h + 3) - 6;
		if (document.getElementsByTagName("*").brcorner)
			document.getElementsByTagName("*").brcorner.style.left = (l + w) - 6;
		if (document.getElementsByTagName("*").brcorner)
			document.getElementsByTagName("*").brcorner.style.top = (h + 3) - 6;
	
		parent.document.body.scroll = "auto";
		if (typeof(parent.newwindow) == "object") {
			if (parent.newwindow.style.width != "0") {
				x = parent.document.body.scrollLeft;
				docW = parent.document.body.offsetWidth;
				wi = parseFloat(parent.newwindow.style.width);
				tPosX = ((docW / 2) - (wi / 2)) + x;
				parent.newwindow.style.left = tPosX + "px";
				parent.document.body.scroll = "no";
				parent.document.getElementById("Shadow2").style.width = parent.document.body.offsetWidth - 20;
				parent.document.getElementById("Shadow2").style.height = parent.document.body.offsetHeight;
				parent.document.getElementById("Shadow1").style.top = parent.document.body.scrollTop;
				parent.document.getElementById("Shadow1").style.left = parent.document.body.scrollLeft;
			}
		}
	}
}

function repositionErrorImg(obj, state, spanImgList, spanIdList) {

	if(spanImgList) {
		// Convert a string to an array
		spanImgList = spanImgList.split(",");
		spanIdList = spanIdList.split(',');
	}

	var images = $('#img > img');

	// If exist any Error Image
	if(images.length > 0) {
		if(obj) {
			var fieldsetImg = $('table[id= ' + obj.id + ' ] :has(input)');
			var tr = document.getElementById(obj.id).rows;
			var name;
			var j = 0;
			var temp = '';
			var newStr = new Array();
	
			var td = new Array()
			for(i = 0, n = tr.length; i < n; i++) {
				for(j = 0, m = tr[i].cells.length; j < m; j++) {
					td.push(tr[i].cells[j]);
				}
			}
	
			for(k = 0, p = td.length; k < p; k++) {
				name = td[k].innerHTML;
				if(name.match(/name=([a-z]*[A-Z]*[0-9]*_*)+/)) {
					temp = name.match(/name=([a-z]*[A-Z]*[0-9]*_*)+/gi);
					for(y = 0; y < temp.length; y++) {
						newStr[j] = temp[y].substring(5, temp[y].length);
						j++;
					}
				}
			}
		}

		// For each image, generate new position
		$(images).each(function() {
			var formName = this.id.split("-");
			var fieldName = formName[1].split("_");
			var thisFormName = '';
			var fldName = '';
			var topTbl = 0;
			var topImg = 0;
			var heigthTbl = 0;
			var fldWidth = 0;
			var imageWidth = 13;
			var displayImg = 0;

			fldLength = fieldName.length;

			thisFormName = formName[0];
			for(var i = 0; i < fldLength; i++) {
				if(i < (fldLength - 1)) {
					if(fldName == '')
						fldName = fieldName[i];
					else
						fldName = fldName + '_' + fieldName[i];
				} else {
					if(fldName == '')
						fldName = fieldName[i].substring(0, fieldName[i].length - 3);
					else
						fldName = fldName + '_' + fieldName[i].substring(0, fieldName[i].length - 3);
				}
			}
			if(document.getElementById(this.id).style.display == 'none')			
				displayImg = 0;
			else
				displayImg = 1;
	
			// Check if exist spans with errors, check
			if(spanIdList) {
				for(z = 0; z < spanIdList.length; z++) {
					if(this.id == spanImgList[z]) {
						fld = document.getElementById(spanIdList[z]);
						document.getElementById(this.id).style.top = $(fld).offset().top;
						break;
					} else {
						// If this belongs to this form, than reposition
						thisClass = $(this).attr('class');
						firstClass = thisClass.split(" ");
						firstClass = firstClass[0].split("_");
						fld = document.getElementsByName(fldName);
						fldWidth = parseInt($(fld).width()) - (imageWidth - 1);

						if(displayImg == 1) {
							if(firstClass[0] == '-') {
								document.getElementById(this.id).style.left = (parseInt($(fld).offset().left) + fldWidth) - parseInt(firstClass[1]);
							} else if(firstClass[0] == '+') {
								document.getElementById(this.id).style.left = (parseInt($(fld).offset().left) + fldWidth) + parseInt(firstClass[1]);
							} else if(firstClass[0] == '*') {
								document.getElementById(this.id).style.left = (parseInt($(fld).offset().left) + fldWidth);
							}
							if(firstClass[2] == '-') {
								document.getElementById(this.id).style.top = parseInt($(fld).offset().top) - parseInt(firstClass[3]);
							} else if(firstClass[2] == '+') {
								document.getElementById(this.id).style.top = parseInt($(fld).offset().top) + parseInt(firstClass[3]);
							} else if(firstClass[2] == '*') {
								document.getElementById(this.id).style.top = parseInt($(fld).offset().top) + 3;
							}
						}
					}
				}
			} else {
				// If this belongs to this form, than reposition
				thisClass = $(this).attr('class');
				firstClass = thisClass.split(" ");
				firstClass = firstClass[0].split("_");
				fld = document.getElementsByName(fldName);
				fldWidth = parseInt($(fld).width()) - (imageWidth - 1);

				if(displayImg == 1) {
					if(firstClass[0] == '-') {
						document.getElementById(this.id).style.left = (parseInt($(fld).offset().left) + fldWidth) - parseInt(firstClass[1]);
					} else if(firstClass[0] == '+') {
						document.getElementById(this.id).style.left = (parseInt($(fld).offset().left) + fldWidth) + parseInt(firstClass[1]);
					} else if(firstClass[0] == '*') {
						document.getElementById(this.id).style.left = (parseInt($(fld).offset().left) + fldWidth);
					}
					if(firstClass[2] == '-') {
						document.getElementById(this.id).style.top = parseInt($(fld).offset().top) - parseInt(firstClass[3]);
					} else if(firstClass[2] == '+') {
						document.getElementById(this.id).style.top = parseInt($(fld).offset().top) + parseInt(firstClass[3]);
					} else if(firstClass[2] == '*') {
						document.getElementById(this.id).style.top = parseInt($(fld).offset().top) + 3;
					}
				}
			}
			if(obj) {
				for(w = 0; w < newStr.length; w++) {
					if((newStr[w] == fldName) && (state == 0)) {
						$(this).addClass('expandClass');
						$('#img > img#'+this.id).hide();
					} else if((newStr[w] == fldName) && (state == 1)) {
						$('#img > img#'+this.id).show();
						
						thisClass = $(this).attr('class');
						firstClass = thisClass.split(" ");
						firstClass = firstClass[0].split("_");
						fld = document.getElementsByName(fldName);
						fldWidth = parseInt($(fld).width()) - (imageWidth - 1);
	
						if(firstClass[0] == '-') {
							document.getElementById(this.id).style.left = (parseInt($(fld).offset().left) + fldWidth) - parseInt(firstClass[1]);
						} else if(firstClass[0] == '+') {
							document.getElementById(this.id).style.left = (parseInt($(fld).offset().left) + fldWidth) + parseInt(firstClass[1]);
						} else if(firstClass[0] == '*') {
							document.getElementById(this.id).style.left = (parseInt($(fld).offset().left) + fldWidth);
						}
						if(firstClass[2] == '-') {
							document.getElementById(this.id).style.top = parseInt($(fld).offset().top) - parseInt(firstClass[3]);
						} else if(firstClass[2] == '+') {
							document.getElementById(this.id).style.top = parseInt($(fld).offset().top) + parseInt(firstClass[3]);
						} else if(firstClass[2] == '*') {
							document.getElementById(this.id).style.top = parseInt($(fld).offset().top) + 3;
						}
						
					}
				}
			}
		});
	}
}
