﻿// Cross Browser Support for Namespaces
if("undefined" == typeof registerNamespace){
    registerNamespace = function(ns){
        var rootObject = window;
        var namespaceParts = ns.split(".");
        for(var index=0; index<namespaceParts.length; index++){
            var currentPart = namespaceParts[index];
            if(!rootObject[currentPart]) rootObject[currentPart] = new Object();
            rootObject = rootObject[currentPart];
        }
    }
}

registerNamespace('GoPromotions.G2');
registerNamespace('GoPromotions.ViewPort');
registerNamespace('GoPromotions.FileUpload');
registerNamespace('GoPromotions.FileContext');
registerNamespace('GoPromotions.G2.Language');
registerNamespace('GoPromotions.G2.ClientRequest');



theme = ''; //this holds the theme name for the page
GoPromotions.G2.Opacity = 0;
GoPromotions.G2.IsIE5 = (document.all && document.getElementById);
GoPromotions.G2.IsNS6 = (!document.all && document.getElementById);
GoPromotions.G2.CurrentDiv = '';
GoPromotions.G2.CurrentTag ='';
GoPromotions.ToolbarHeight = 64;
GoPromotions.G2.DownX = null;
GoPromotions.G2.DownY = null;
GoPromotions.G2.UploadContext = null;
GoPromotions.G2.FileContext = null;
GoPromotions.G2.ShadowXOffset = 10;
GoPromotions.G2.ShadowYOffset = 10;

document.onmousemove = function(){GoPromotions.G2.HandleMouseMove();};
document.onmouseup = function(){GoPromotions.G2.MouseStop();};

//This function stops contextmenu at document level
document.oncontextmenu=function(){
    event.returnValue = false;
}

GoPromotions.G2.Language.Showhide = function(){
    if (GoPromotions.G2.CurrentDiv == 'Language_Select_Div'){
        GoPromotions.G2.FadeOut();     
    }
    else{
        var objElement = document.getElementById('Language_Div');
        var objLanguages = document.getElementById('Language_Div');
        var x = objElement.clientWidth - 240;
        var y = 23;
        GoPromotions.G2.ShowMenu('Language_Select_Div', x, y);
        
    }
}

GoPromotions.ViewPort.GetSize = function(){
    if (self.innerHeight != undefined ){
        GoPromotions.ViewPort.screenWidth = self.innerWidth;
        GoPromotions.ViewPort.screenHeight = self.innerHeight;
    }
    if (document.documentElement != undefined  && document.documentElement.clientHeight != undefined && (document.documentElement.clientWidth>0 ||document.documentElement.clientHeight>0) ){
        GoPromotions.ViewPort.screenWidth = document.documentElement.clientWidth;
        GoPromotions.ViewPort.screenHeight = document.documentElement.clientHeight;
    }
    else {
        GoPromotions.ViewPort.screenWidth = document.body.clientWidth;
        GoPromotions.ViewPort.screenHeight = document.body.clientHeight;
    }
    if (self.pageYOffset != undefined ){
	    GoPromotions.ViewPort.scrollLeft = self.pageXOffset;
	    GoPromotions.ViewPort.scrollTop = self.pageYOffset;
    }
    else if (document.documentElement != undefined && document.documentElement.scrollTop != undefined && (document.documentElement.scrollLeft>0 || document.documentElement.scrollTop>0)){
	    GoPromotions.ViewPort.scrollLeft = document.documentElement.scrollLeft;
	    GoPromotions.ViewPort.scrollTop = document.documentElement.scrollTop;
    }
    else {
	    GoPromotions.ViewPort.scrollLeft = document.body.scrollLeft;
	    GoPromotions.ViewPort.scrollTop = document.body.scrollTop;
    }    
}

GoPromotions.G2.HandleMouseMove = function() {
    if (GoPromotions.G2.CurrentTag != '') {
        var X = window.event.clientX;
        var Y = window.event.clientY;
        if (Right(GoPromotions.G2.CurrentTag, 13) == "_threedanchor") {
            var objCurrentTag = document.getElementById(GoPromotions.G2.CurrentTag);
            var objContainer = GoPromotions.G2.FindParentByTag(objCurrentTag, 'div');
            objContainer.style.posLeft = X - GoPromotions.G2.DownX;
            objContainer.style.posTop = Y - GoPromotions.G2.DownY;
            if (objContainer.style.posLeft < 0) objContainer.style.posLeft = 0;
            if (objContainer.style.posTop < GoPromotions.ToolbarHeight) objContainer.style.posTop = GoPromotions.ToolbarHeight;
            var objShadow = document.getElementById(objContainer.id + '_Shadow');
            if (objShadow != null) {
                objShadow.style.posLeft = (X - GoPromotions.G2.DownX) + GoPromotions.G2.ShadowXOffset;
                objShadow.style.posTop = (Y - GoPromotions.G2.DownY) + GoPromotions.G2.ShadowYOffset;
                if (objShadow.style.posLeft < GoPromotions.G2.ShadowXOffset) objShadow.style.posLeft = GoPromotions.G2.ShadowXOffset;
                if (objShadow.style.posTop < (GoPromotions.ToolbarHeight + GoPromotions.G2.ShadowYOffset)) objShadow.style.posTop = GoPromotions.ToolbarHeight + GoPromotions.G2.ShadowYOffset;
            }
        }
    }
}

GoPromotions.G2.MouseStop = function(){
	if (GoPromotions.G2.CurrentTag != '') {
		GoPromotions.G2.CurrentTag = '';
	}
}

GoPromotions.G2.MouseStart = function(){
	GoPromotions.G2.CurrentTag = window.event.srcElement.id;
	if (Right(GoPromotions.G2.CurrentTag,13) == "_threedanchor") {
	    var objContainer = GoPromotions.G2.FindParentByTag(window.event.srcElement,'div');
		GoPromotions.G2.DownX = window.event.clientX - objContainer.offsetLeft;
		GoPromotions.G2.DownY = window.event.clientY - objContainer.offsetTop;	
	}
}

GoPromotions.G2.FindParentByTag = function(objElement, tagName){
    var objParent = objElement.parentElement;
    if (objParent == null) return null;
    else {
        if (objParent.tagName.toLowerCase() == tagName.toLowerCase()) return objParent;
        else return GoPromotions.G2.FindParentByTag(objParent, tagName)
    }
}

GoPromotions.G2.ShowMenu = function(menuName, x, y) {
    GoPromotions.G2.FadeOut();
    var objMenu = document.getElementById(menuName);
    if ((y + objMenu.clientHeight + GoPromotions.G2.ShadowYOffset) > GoPromotions.ViewPort.screenHeight) y = (GoPromotions.ViewPort.screenHeight - (objMenu.clientHeight + GoPromotions.G2.ShadowYOffset));
    GoPromotions.G2.ApplyMenuGradient(menuName);
    var objShadow = document.getElementById(menuName + '_Shadow');
    objMenu.onresize = function() { GoPromotions.G2.ApplyMenuGradient(menuName); }
    objMenu.style.left = parseInt(x) + 'px';
    objMenu.style.top = parseInt(y) + 'px';
    objShadow.style.left = parseInt(x + GoPromotions.G2.ShadowXOffset)+'px';
    objShadow.style.top = parseInt(y + GoPromotions.G2.ShadowYOffset) + 'px';
    GoPromotions.G2.FadeIn(menuName);
}
GoPromotions.G2.ApplyMenuGradient = function(menuName) {
    var objMenu = document.getElementById(menuName);
    objMenu.onresize = function() { GoPromotions.G2.ApplyMenuGradient(menuName);}
    var objParent = objMenu.parentNode;
    var width = objMenu.clientWidth;
    var height = objMenu.clientHeight;
    var zIndex = objMenu.style.zIndex;
    var positionMenu = GoPromotions.G2.GetPosition(objMenu);
    var objShadow = document.getElementById(menuName + '_Shadow');
    var flgAddShadow = false;
    if (objShadow == null) flgAddShadow = true;
    else if ((objShadow.style.width != (objMenu.clientWidth + 'px')) || (objShadow.style.height != (objMenu.clientHeight + 'px'))) flgAddShadow = true;
    if (flgAddShadow) {
        if (objShadow != null) objShadow.removeNode(true);
        objShadow = document.createElement('<div id=\'' + menuName + '_Shadow\' style=\position:absolute;visibility:hidden;z-index:' + (zIndex - 1) + ';left:' + (positionMenu.x + GoPromotions.G2.ShadowXOffset) + 'px;top:' + (positionMenu.y + GoPromotions.G2.ShadowXOffset) + 'px;width:' + width + ';height:' + height + ';background-image:url(/g2images/blue/threedtable/' + width + '/' + height + '/shadow.png);\'>');
        objParent.appendChild(objShadow);
    }
    objMenu.style.backgroundImage = 'url(/g2images/blue/threedtable/' + width + '/' + height + '/gradient.png)';
}

GoPromotions.G2.CenterMenu = function(menuName, centerOn){
    var objCenter = document.getElementById(centerOn);
    var objMenu = document.getElementById(menuName);
    var objPosition = GoPromotions.G2.GetPosition(objCenter);
    var x = objPosition.x + ((objCenter.offsetWidth - objMenu.offsetWidth) /2);
    var y = objPosition.y + ((objCenter.offsetHeight - objMenu.offsetHeight) /2);
    GoPromotions.G2.ShowMenu(menuName, x, y);
}

GoPromotions.G2.PositionMenu = function(menuName, centerOn, dx, dy){
    var objCenter=document.getElementById(centerOn);
    var objMenu = document.getElementById(menuName);
    var objPosition = GoPromotions.G2.GetPosition(objCenter);
    var x = objPosition.x + dx;
    var y = objPosition.y + dy;
    GoPromotions.G2.ShowMenu(menuName, x, y);
}

GoPromotions.G2.HideMenu = function(menuName) {
    GoPromotions.G2.ShowHide(menuName, false)
    var objMenu = document.getElementById(menuName);
    objMenu.style.left = -1000;
    objMenu.style.top = -1000;
    
    var objShadow = document.getElementById(menuName + '_Shadow');
    if (objShadow != null) {
        GoPromotions.G2.ShowHide(menuName + '_Shadow', false)
        objShadow.style.left = -1000;
        objShadow.style.top = -1000;
    }
}

GoPromotions.G2.MenuOffset = function(objElement){
    var objPosition = GoPromotions.G2.GetPosition(objElement);
    objPosition.x = objPosition.x + objElement.offsetWidth + 20;
    objPosition.y = objPosition.y - (objElement.offsetHeight/2);
    return objPosition;
}

GoPromotions.G2.Position = function(x, y){
    this.x = x;
    this.y = y;
}

GoPromotions.G2.Size = function(width, height){
    this.width = width;
    this.height = height;
}

GoPromotions.G2.GetPosition = function(objElement) {
    var objPosition = new GoPromotions.G2.Position();
	objPosition.x = objElement.offsetLeft;
	objPosition.y = objElement.offsetTop;
	while ((objElement = objElement.offsetParent) != null){ 
        objPosition.x += objElement.offsetLeft; 
        objPosition.y += objElement.offsetTop;
	}
	return objPosition;	
}

GoPromotions.G2.CenterOnPage = function(elementId){
    objElement = document.getElementById(elementId);
    if (objElement != null){
        GoPromotions.ViewPort.GetSize();
        var x = (GoPromotions.ViewPort.screenWidth-objElement.clientWidth)/2
        if (x<0) x=0;
        objElement.style.left = x+'px';        
    }
}

GoPromotions.G2.FadeIn = function(menuName){
	GoPromotions.G2.CurrentDiv = menuName;
	GoPromotions.G2.ShowHide(menuName, true);
	if (document.getElementById(menuName + '_Shadow') != null) GoPromotions.G2.ShowHide(menuName + '_Shadow', true);
}

GoPromotions.G2.ShowHide = function(menuName, Visible) // 1 visible, 0 hidden
{
    if(document.layers)	   //NN4+
    {
       document.layers[menuName].visibility = Visible ? "show" : "hide";
    }
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(menuName);
        obj.style.visibility = Visible ? "visible" : "hidden";
    }
    else if(document.all)	// IE 4
    {
        document.all[menuName].style.visibility = Visible ? "visible" : "hidden";
    }
}

GoPromotions.G2.FadeOut = function() {
    if (GoPromotions.G2.CurrentDiv != "") {
        if (document.getElementById(GoPromotions.G2.CurrentDiv + '_Shadow') != null) GoPromotions.G2.ShowHide(GoPromotions.G2.CurrentDiv + '_Shadow', false);
        GoPromotions.G2.ShowHide(GoPromotions.G2.CurrentDiv, false);
    }
    GoPromotions.G2.CurrentDiv = "";
}

GoPromotions.G2.PlaySound = function(src) {
    var objSound = document.getElementById('G2Sound_Div');
    if (objSound == null){
        objSound = document.createElement("<div id=\"G2Sound_Div\" style=\"position:absolute;left:-1000px;top:-1000px;\" >");
        document.body.insertAdjacentElement("beforeEnd", objSound);
    }
    setTimeout("GoPromotions.G2.PlaySoundAsync('"+src+"')",10);
}

GoPromotions.G2.PlaySoundAsync = function(src) {
    var objSoundDiv = document.getElementById('G2Sound_Div');  
    var objSound = document.createElement("<embed src='"+src+"' hidden=\'true\' autostart=\'true\' loop=\'false\'>");
    if (objSoundDiv.children.length>0){ 
        for(var index=objSoundDiv.children.length-1; index>=0; index--){
            var objNode=objSoundDiv.children[index];
            objNode.removeNode(true);
        }
    }
    objSoundDiv.appendChild(objSound);           
}

GoPromotions.G2.ImageChange = function(ImageID, URL){
    var objImage;
	if (document.all || document.getElementById){
	    if(document.getElementById){
		    objImage = document.getElementById(ImageID);
	    }
	    else{
		    objImage =  document.all[ImageID];
	    }
	    objImage.src = URL;
	}
}

GoPromotions.G2.OnMouseButton = function(onleftmouse, onmiddlemouse, onrightmouse){
    var mouseButton = 0;
    if (event.which) mouseButton = event.which;
    if (event.button){
        if (event.button==1) mouseButton=1;
        if (event.button==4) mouseButton=2;
        if (event.button==2) mouseButton=3;
    }
    switch (mouseButton) {
        case 1 :
            if (onleftmouse!=null) eval(onleftmouse);
            break;
        case 2 :
            if (onmiddlemouse!=null) eval(onmiddlemouse);
            break;
        case 3 :
            if (onrightmouse!=null) eval(onrightmouse);
            break;
    }
}

GoPromotions.G2.MaxLen = function(length){
    if (event.srcElement.innerText.length < length + 1) {
        event.returnValue = true;
    }
    else {
        event.returnValue = false;        
   }
}

GoPromotions.G2.AddWindowOnloadEvent = function(onloadFunc){
    if (typeof GoPromotions.G2.WindowOnloadEvents == "undefined"){ 
        GoPromotions.G2.WindowOnloadEvents = onloadFunc;
        if (window.addEventListener) window.addEventListener('load', GoPromotions.G2.WindowOnload, true);
        else {
            //if (window.attachEvent) window.attachEvent('onload',GoPromotions.G2.WindowOnload);
            //else 
            window.onload = GoPromotions.G2.WindowOnload;
        }
    }
    else {
        var index = GoPromotions.G2.WindowOnloadEvents.length;
        GoPromotions.G2.WindowOnloadEvents = GoPromotions.G2.WindowOnloadEvents+'|'+onloadFunc;
    }
}
GoPromotions.G2.WindowOnload = function(){
    var arrEvents = GoPromotions.G2.WindowOnloadEvents.split('|');
    for (var index=0;index<GoPromotions.G2.WindowOnloadEvents.length;index++){
        eval(arrEvents[index]);
    }
}

//GoPromotions.G2.AddOnLoadEvent = function(objElement,newFunc){
//    if (objElement.addEventListener){
//        objElement.addEventListener('load', newFunc, true);
//    }
//    else {
//        if (objElement.attachEvent){
//            objElement.attachEvent('onload',newFunc);
//        }
//        else {
//            if (typeof objElement.onload != 'function') {
//                objElement.onload = newFunc;
//            } 
//            else {
//                var objOnLoad = objElement.onload;                
//                objElement.onload = function() {
//                    objOnLoad();
//                    newFunc();
//                }
//            }
//        }
//    }
//}

GoPromotions.G2.AddOnUnloadEvent = function(objElement,newFunc){
    if (objElement.addEventListener){
        objElement.addEventListener('unload', newFunc, true);
    }
    else {
        if (objElement.attachEvent){
            objElement.attachEvent('onunload',newFunc);
        }
        else {
            if (typeof objElement.onunload != 'function') {
                objElement.onunload = newFunc;
            } 
            else {
                var objOnUnload = objElement.onunload;                
                objElement.onunload = function() {
                    objOnUnload();
                    newFunc();
                }
            }
        }
    }
}
GoPromotions.G2.GetSelectValues = function(objParent){
    var retValue = '';
    if (objParent != null){
        var objCol = objParent.children;
        for (var index = 0; index < objCol.length; index++) {
            var objElement = objCol[index];
            if (objElement.tagName.toLowerCase() =='select'){
                for (var option=0; option < objElement.options.length; option++){
                    if (objElement.options[option].selected) retValue = retValue+'|'+objElement.id+'|'+ objElement.options[option].value;       
                }
            }
            else {
                if (objElement.children.length > 0) retValue = retValue+GoPromotions.G2.GetSelectValues(objElement);
            }
        }
    }
    return retValue;
}

GoPromotions.G2.Section = function(sectionStateId,sectionId){
    var sectionState = document.getElementById(sectionStateId);
    var section = document.getElementById(sectionId);
    if (sectionState.checked) {
        section.style.height=null;
        section.style.overflowY = 'scroll';
        section.style.height = section.scrollHeight + 'px';
        section.style.overflowY = 'hidden';
        section.style.visibility = 'inherit';
    }
    else {
        section.style.overflowY = 'hidden';
        section.style.height = '0px';
        section.style.visibility = 'hidden';
    }
}

GoPromotions.G2.OptionSection = function(groupName) {
    for (var option = 0; option < groupName.length; option++) {
        var sectionId = groupName[option].value;
        var section = document.getElementById(sectionId);
        if (groupName[option].checked) {
            section.style.overflowY = 'scroll';
            section.style.height = section.scrollHeight + 'px';
            section.style.overflowY = 'hidden';
            section.style.visibility = 'inherit';
        }
        else {
            section.style.overflowY = 'hidden';
            section.style.height = '0px';
            section.style.visibility = 'hidden';
        }
    }
}


// Routines for uploading files to database
GoPromotions.FileUpload.ReplaceImage = function(deleteId, parentId, imageId, inputId, src, width, height){
    if (GoPromotions.FileUpload.CheckImageExt(src)){
        var objNewImage = new Image();
        objNewImage.style.border='1px solid #808080';
        objNewImage.id = imageId;
        objNewImage.onload = function(){GoPromotions.FileUpload.ResizeImage();};
        GoPromotions.G2.UploadContext = new Object();
        GoPromotions.G2.UploadContext.deleteId=deleteId;
        GoPromotions.G2.UploadContext.parentId = parentId;
        GoPromotions.G2.UploadContext.imageId = imageId;
        GoPromotions.G2.UploadContext.inputId = inputId;
        GoPromotions.G2.UploadContext.width = width;
        GoPromotions.G2.UploadContext.height = height;
        GoPromotions.G2.UploadContext.newImage = objNewImage
        objNewImage.src = src;
        
    }
    else {
        GoPromotions.FileUpload.RemoveImage(deleteId, parentId, imageId, inputId)
        //TODO replace with nice alert
        alert("The selected file extention not a supported type. The supported extentions are .jpg and .gif");
    }
}
GoPromotions.FileUpload.ResizeImage = function(){
    var objNewImage = GoPromotions.G2.UploadContext.newImage;
    objNewImage.onload = null;
    var objParent = document.getElementById(GoPromotions.G2.UploadContext.parentId);
    var objOldImage = document.getElementById(GoPromotions.G2.UploadContext.imageId);
    if (GoPromotions.FileUpload.CheckImageSize(objNewImage, GoPromotions.G2.UploadContext.width, GoPromotions.G2.UploadContext.height)){ 
        if ((objOldImage != null) || (objParent != null)){
            var objDelete = document.getElementById(GoPromotions.G2.UploadContext.deleteId);
            objDelete.style.visibility = 'visible';
            objParent.style.width = objNewImage.width;            
            objParent.style.height = objNewImage.height;            
            if (objOldImage != null) objOldImage.replaceNode(objNewImage);
            else objParent.appendChild(objNewImage);
            GoPromotions.FileUpload.ResizePage();
            GoPromotions.G2.ShowHide(GoPromotions.G2.UploadContext.parentId,1);
        }
    }
    else {
        //TODO replace with nice alert
        GoPromotions.FileUpload.RemoveImage(GoPromotions.G2.UploadContext.deleteId, GoPromotions.G2.UploadContext.parentId, GoPromotions.G2.UploadContext.imageId, GoPromotions.G2.UploadContext.inputId);
        alert("The selected image is too large. Resize it first.");
    }
    GoPromotions.G2.UploadContext = null;
}
GoPromotions.FileUpload.CheckImageExt = function(src){
    var ext = src.substring(src.length-4,src.length).toLowerCase();
    switch (ext){
        case '.jpg' :
        case '.gif' :
            return true;
    }
    return false;
}
GoPromotions.FileUpload.CheckImageSize = function(objImage, width, height){
    if (objImage.width >(width * 3)) return false;
    if (objImage.height >(height * 3)) return false;
    if ((objImage.width > width) || (objImage.height >height)){
        var dw = parseFloat(objImage.width / width);
        var dh = parseFloat(objImage.height / height);
        if (dw>dh){
            objImage.width = parseInt(objImage.width/dw);
            objImage.height = parseInt(objImage.height/dw);
        }
        else {
            objImage.width = parseInt(objImage.width/dh);
            objImage.height = parseInt(objImage.height/dh);
        }
    }
    return true;
}
GoPromotions.FileUpload.ResizePage = function(){
    if (document.readyState!='complete') {
        setTimeout('GoPromotions.FileUpload.ResizePage()',100);
        return;
    }
    var objTable=document.getElementById('FileUploadTable');
    if (objTable != null){
        objTable.refresh;
        var objDiv=document.getElementById('FileUploadDiv');
        var w = objTable.offsetWidth;
        var h = objTable.offsetHeight;
        objDiv.style.width = w;
        objDiv.style.height = h;
        GoPromotions.FileUpload.WindowResize(w,h);
    }
}
GoPromotions.FileUpload.WindowResize = function(w,h){
    //Work around for access denied error
    try {
        window.resizeTo(w,h);
    }
    catch(e){
        setTimeout('GoPromotions.FileUpload.WindowResize('+w+','+h+')',100);
    }
}
GoPromotions.FileUpload.Cancel = function(){
    var objFilePath = document.getElementById('FilePath');
    objFilePath.removeNode('true');
    __doPostBack('Cancel', '');
}

GoPromotions.FileUpload.Show = function(elementId , fileUploadId, imageURL, localPathId, maxWidth, maxHeight, onComplete){
        var objElement = document.getElementById(elementId);
        var objPosition = GoPromotions.G2.MenuOffset(objElement);
        
        GoPromotions.G2.FileContext = new Object();
        GoPromotions.G2.FileContext.fileUploadId = fileUploadId;
        GoPromotions.G2.FileContext.localPathId = localPathId;
        if (onComplete != null) GoPromotions.G2.FileContext.onComplete = onComplete;
        
        var fileId = document.getElementById(GoPromotions.G2.FileContext.fileUploadId).value;
        var localPath= document.getElementById(GoPromotions.G2.FileContext.localPathId).value;
        queryString='?MaxWidth='+maxWidth+'&MaxHeight='+maxHeight;
        if (fileId != '') queryString=queryString+'&FileID='+fileId;
        if (localPath != '') queryString=queryString+'&LocalPath='+escape(localPath);
        if (imageURL != null) queryString=queryString+'&ImageURL='+escape(imageURL);
                
        var objUpload = document.createElement("IFRAME");
        objUpload.frameBorder = "0";
        objUpload.id = 'fileUploadFrame';
        objUpload.name = 'fileUploadFrame';
        objUpload.style.position = "absolute";
        objUpload.style.left = objPosition.x;
        objUpload.style.top = objPosition.y;
        objUpload.scrolling = 'no';
        objUpload.style.zIndex=2;
        objUpload.onreadystatechange = GoPromotions.FileUpload.Load;
        document.body.appendChild(objUpload);
        objUpload.src = '/freestyle/fileupload.aspx'+queryString;
}
GoPromotions.FileUpload.Load = function(){
    var objUpload = event.srcElement;
    if (objUpload.readyState=='complete'){
        objUpload.onreadystatechange = GoPromotions.FileUpload.Postback;        
    }
}
GoPromotions.FileUpload.Postback = function(){
    var objUpload = event.srcElement;
    if (objUpload.readyState=='complete'){
        var result = document.frames('fileUploadFrame').document.getElementById('UploadResult').value.toLowerCase();
        GoPromotions.G2.FileContext.result = result;
        switch (result){
            case 'uploaded' :
                var fileId = document.frames('fileUploadFrame').document.getElementById('FileID').value;
                document.getElementById(GoPromotions.G2.FileContext.fileUploadId).value = fileId;            
                var localPath = document.frames('fileUploadFrame').document.getElementById('LocalPath').value;
                document.getElementById(GoPromotions.G2.FileContext.localPathId).value = localPath;
                if (GoPromotions.G2.FileContext.onComplete != null) eval(GoPromotions.G2.FileContext.onComplete);
                break; 
            case 'deleted' :
                document.getElementById(GoPromotions.G2.FileContext.fileUploadId).value = 'null';            
                document.getElementById(GoPromotions.G2.FileContext.localPathId).value = '';            
                if (GoPromotions.G2.FileContext.onComplete != null) eval(GoPromotions.G2.FileContext.onComplete);
                break;
            case 'cancelled' :
                break;
        }
        objUpload.removeNode('true');        
    }
    GoPromotions.G2.UploadContext = null;
} 
GoPromotions.FileUpload.RemoveImage = function(deleteId, parentId, imageId, inputId){
    var objDescription=document.getElementById('FileUploadDescription');
    var objDelete = document.getElementById(deleteId);
    var objParent = document.getElementById(parentId);
    var objImage = document.getElementById(imageId);
    var objInput = document.getElementById(inputId);
    objImage.removeNode('true');
    objParent.style.height=0;
    objParent.style.width=0;
    objNewInput = document.createElement('input');
    objNewInput.type = 'file';
    objNewInput.id = 'FilePath';
    objNewInput.className = 'FileUpload';
    objNewInput.onchange = objInput.onchange;
    objInput.replaceNode(objNewInput);
    objDelete.style.visibility='hidden';
    objDescription.innerHTML='Select continue to remove the current image or<br>Select browse to upload a new image';
    GoPromotions.FileUpload.ResizePage();
}
GoPromotions.G2.CreateCookie = function(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

GoPromotions.G2.ReadCookie = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var index=0;index < ca.length;index++) {
		var c = ca[index];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

GoPromotions.G2.EraseCookie = function(name) {
	GoPromotions.G2.CreateCookie(name,"",-1);
}
GoPromotions.G2.ReturnColourPicker = function(editor, command, colour){
	var parent = window.opener;
	if (parent == null) parent = dialogArguments;
 
	var rtb = parent.frames[editor];
	
	switch(command){
//	    case 'setbackgroundcolour' :
//		    window.returnValue = colour;
//		    window.close();
//		    break;
		default :
		    rtb.focus();
		    var rng = rtb.document.selection.createRange();
		    rng.select();
		    rtb.document.execCommand(command,'',colour);
		    window.close();
		    break;
	}
	GoPromotions.G2.CreateCookie('pickerLastColour',colour,183);
}
GoPromotions.G2.ReturnFontPicker = function(editor, command, font){
	var parent = window.opener;
	if (parent == null) parent = dialogArguments;
 
	var rtb = parent.frames[editor];	
    rtb.focus();
    var rng=rtb.document.body.createTextRange();
    rng.select();
    rtb.document.execCommand('fontname','',font);
    window.close();
	GoPromotions.G2.CreateCookie('pickerLastFont',font,183);
}

GoPromotions.G2.ClearRecentColours = function(){
	if (window.confirm("Click OK to clear the recently used colour list.")) {
	    GoPromotions.G2.EraseCookie('pickerRecentColours')  
	    GoPromotions.G2.EraseCookie('pickerLastColour')
	    window.close();  
    }
}
GoPromotions.G2.GetExtension = function(path){
    var strPath = path;
    if (strPath.indexOf('?')>0) strPath = strPath.substring(0,strPath.indexOf('?'));
    var dot = strPath.indexOf('.');
    if (dot < 0) return '';
    while(dot > 0){
        strPath = strPath.substring(strPath.indexOf('.') + 1,strPath.length);
        dot = strPath.indexOf('.');
    }
    return '.'+strPath;
}

function Trim(objString){
    objString = RTrim(objString);
    objString = LTrim(objString);
    return objString;
}
function RTrim(objString){
    var v_length = objString.length;
    if(v_length < 1){
        return "";
    }
    
    var strTemp = "";
    var iTemp = v_length -1;

    while(iTemp > -1){
        if(objString.charAt(iTemp) == " "){
        }
        else{
            strTemp = objString.substring(0,iTemp +1);
            break;
        }
        iTemp = iTemp-1;
    } 
    return strTemp;
}

function LTrim(objString){
    var v_length = objString.length;
    if(v_length < 1){
        return "";
    }
    var strTemp = "";
    var iTemp = 0;

    while(iTemp < v_length){
        if(objString.charAt(iTemp) == " "){
        }
        else{
            strTemp = objString.substring(iTemp,v_length);
            break;
        }
        iTemp = iTemp + 1;
    } 
    return strTemp;
}
function Left(objString, length){
    var retLength = length;
    if (retLength < 0) retLength = 0;
    if (retLength > objString.length) retLength = objString.length;
    return objString.substring(0,retLength);
}
function Right(objString, length){
    var retLength = length;
    if (retLength < 0) retLength = 0;
    if (retLength > objString.length) retLength = objString.length;
    return objString.substring((objString.length - retLength),objString.length);
}

function G2_onMouse(){
    var mouseButton = 0;
    if (event.which) mouseButton = event.which;
    if (event.button){
        if (event.button==1) mouseButton=1;
        if (event.button==4) mouseButton=2;
        if (event.button==2) mouseButton=3;
    }
    switch (mouseButton) {
        case 1 :
            if (event.srcElement.onleftmouse) eval(event.srcElement.onleftmouse);
            break;
        case 2 :
            if (event.srcElement.onmiddlemouse) eval(event.srcElement.onmiddlemouse);
            break;
        case 3 :
            if (event.srcElement.onrightmouse) eval(event.srcElement.onrightmouse);
            break;
    }
}
// Form submission section-----------------------------------------------------------------------------------
GoPromotions.G2.SetCheckBoxRadio = function(name, selectedValues){
    //Assume multiple with this name
    var objControls = document.getElementsByName(name);
    for (var index=0; index<objControls.length; index++){
       objControls[index].checked = false; 
    }
    var arrValues = selectedValues.split('|');
    for (var item=0; item<arrValues.length; item++){
        for (var index=0; index<objControls.length; index++){
            if (arrValues[item] == objControls[index].value){ 
                objControls[index].checked = true; 
                break;
            }
        }        
    }
}
GoPromotions.G2.HasCheckBoxRadio = function(name){
    //Assume multiple with this name
    var objControls = document.getElementsByName(name);
    for (var index=0; index<objControls.length; index++){
        if (objControls[index].checked) return true; 
    }
}
GoPromotions.G2.SetTextboxTextArea = function(name, value){
    //Assume only one with this name
    var objControls = document.getElementsByName(name);
    objControls[0].value = value; 
}
GoPromotions.G2.HasTextboxTextArea = function(name){
    //Assume only one with this name
    var objControls = document.getElementsByName(name);
    var value = Trim(objControls[0].value.split('\n').join('').split('\r').join(''));
    if (value != '') return true; 
}
GoPromotions.G2.SetComboboxList = function(name, selectedValues){
    //Assume only one with this name
    var objControls = document.getElementsByName(name);
    for (var index=0; index<objControls[0].options.length; index++){
       objControls[0].options[index].selected = false; 
    }
    var arrValues = selectedValues.split('|');
    for (var item=0; item<arrValues.length; item++){
        for (var index=0; index<objControls[0].options.length; index++){
            if (arrValues[item] == objControls[0].options[index].value){ 
                objControls[0].options[index].selected = true; 
                break;
            }
        }        
    }
}
GoPromotions.G2.HasComboboxList = function(name){
    //Assume only one with this name
    var objControls = document.getElementsByName(name);
    for (var index=0; index<objControls[0].options.length; index++){
       if (objControls[0].options[index].selected) return true; 
    }
}
GoPromotions.G2.SetFrameSource = function(index, src, force) {
    var id = "IFRAME_" + index;
    var URL = src;
    if (force) {
        var d = new Date();
        var r = escape(d.getTime() * Math.random());
        if (URL.indexOf('?') > 0) URL = URL + '&timestamp=' + r;
        else URL + '?timestamp=' + r;
    }
    window.frames[id].location = URL;
}

//*********************GoPromotions.G2.ClientRequest**********************************

/**
* Returns an XMLHttp instance to use for asynchronous
* downloading. This method will never throw an exception, but will
* return NULL if the browser does not support XmlHttp for any reason.
* @return {XMLHttpRequest|Null}
*/
GoPromotions.G2.ClientRequest.createXmlHttpRequest = function() {
    try {
        if (typeof ActiveXObject != 'undefined') {
            return new ActiveXObject('Microsoft.XMLHTTP');
        } else if (window["XMLHttpRequest"]) {
            return new XMLHttpRequest();
        }
    }
    catch (e) {
        changeStatus(e);
    }
    return null;
}

/**
* This functions wraps XMLHttpRequest open/send function.
* It lets you specify a URL and will call the callback if
* it gets a status code of 200.
* @param {String} url The URL to retrieve
* @param {Function} callback The function to call once retrieved.
*/
GoPromotions.G2.ClientRequest.downloadUrl = function(url, callback) {
    var status = -1;
    var request = GoPromotions.G2.ClientRequest.createXmlHttpRequest();
    if (!request) {
        return false;
    }
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            try {
                status = request.status;
            }
            catch (e) {
                // Usually indicates request timed out in FF.
            }
            if (status == 200) {
                callback(request.responseXML, request.status);
                request.onreadystatechange = function() { };
            }
        }
    }
    request.open('GET', url, true);
    try {
        request.send(null);
    }
    catch (e) {
        changeStatus(e);
    }
};

/**
* Parses the given XML string and returns the parsed document in a
* DOM data structure. This function will return an empty DOM node if
* XML parsing is not supported in this browser.
* @param {string} str XML string.
* @return {Element|Document} DOM.
*/
GoPromotions.G2.ClientRequest.xmlParse = function(str) {
    if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined') {
        var doc = new ActiveXObject('Microsoft.XMLDOM');
        doc.loadXML(str);
        return doc;
    }
    if (typeof DOMParser != 'undefined') {
        return (new DOMParser()).parseFromString(str, 'text/xml');
    }
    return createElement('div', null);
}

/**
* Appends a JavaScript file to the page.
* @param {string} url
*/
GoPromotions.G2.ClientRequest.downloadScript = function(url) {
    var script = document.createElement('script');
    script.src = url;
    document.body.appendChild(script);
}

