/*  * This object contains the callback window variables. * * You can put as many of these in as you like and bind them to different  * classes to handle different forms and agents. */var cv = {    "form": "web/callback",    "agent": "web/request_callback?openagent",    "auto": [        {            "field": [                {                    "target": "#subject",                    "source": "#page-title"                 }            ]         }    ],    "message": [        {            "ok": "Thank you for your interest.<br/><br/>You will be contacted shortly...",            "fail": "Sorry, I wasn't able to submit your request at this time.",            "incomplete": "You didn't provide enough information for us to contact you.<br/><br/>Make sure you enter an email address or a telephone number."        }    ]};/* * Use auto section is completed in the JSON to pre-populate fields on the form. * * Target must be a selector, and should point to a field on the form. * * Source can be a selector or a string, If the selector matches an element  * of [type=input] it will use it's value otherwise it will get the matched element's. * text if the source selector fails to find an element it will treat it as a string and  * use that value. */function popupAutocomplete(v) {    var target, source, val;    $.each(v.auto, function(i, item){        if (item) {            target = $(item.field[i].target);            source = $(item.field[i].source);            if (source.length > 0) {                if (source[0].tagName === "input") {                    val = source.val();                } else {                    val = source.text();                            }            } else {                val = item.field[i].source;                        }            target.val( val );        }    });}/* if popup window height is larger than window height, allow it to scroll otherwise allow the window content to scroll behind the mask */function popupTidy(){    var popWin, popMask;    popWin = $('#popup-window');    popMask = $('#popup-mask');    $(window).scrollTop(0);    popMask.css('height', $(document).height());    if ((popWin.height() + popWin.offset().top) > ($(window).height())) {        popMask.css('position', 'absolute');    }    else {        popMask.css('position', 'fixed');    }}/*  close popup window */function popupClose(){    var popMask = $('#popup-mask');    popMask.remove();}/*  handle close popup window bind in a func because it needs to do some other stuff too */function popupCloseBind() {    $('#popup-mask, #popup-window .popup-close').bind('click', function(e){        popupClose();        e.preventDefault();    });   popupTidy();}/* submit form */function popupSubmit(){    var i, b, q, e, iId, iVal, msg, t;    i = $('#popup-window :input[type!=="button"]');    b = $('#popup-window input.submit_button');    q = {};	var proceed = true;		if($("#name").val() === ""){		$("#name").addClass("invalid");		proceed = false;	}else{		$("#name").removeClass("invalid");	}	if($("#company").val() === ""){		$("#company").addClass("invalid");		proceed = false;	}else{		$("#company").removeClass("invalid");	}	if($("#telephone").val() === ""){		$("#telephone").addClass("invalid");		proceed = false;	}else{		$("#telephone").removeClass("invalid");	}	if($("#subject").val() === ""){		$("#subject").addClass("invalid");		proceed = false;	}else{		$("#subject").removeClass("invalid");	}		if(proceed){	    i.each(function(){	        e = $(this);	        iId = e.attr('id');	        if (iId !== "") {	            iVal = e.val();	            q[iId] = iVal;	        }	    });	    /*if (validateForm('#popup-window')) {*/	        b.unbind('click').addClass('disable_button').click(function(e){ /* prevent multiple submissions */	            e.preventDefault();	            return;	        });	        $.get(cv.agent, q, function(data){ /* GET instead of POST for notes web lib  */	            if (data.status === "ok") {		           msg = cv.message[0].ok;	                if (msg === "undefined") {	                    msg = 'Request has been submit';	                }	            }	            else if (data.status === "incomplete") {	            	 msg = cv.message[0].incomplete;	                if (msg === "undefined") {	                    msg = 'Insufficient information provided';	                }	            }	            else if (data.status === "fail") {	                msg = cv.message[0].fail;	                if (msg === "undefined") {	                    msg = 'Request was not submit';	                }	            }	            else {	                msg = data.status;	                if (msg === "undefined") {	                    msg = 'Request was not submit';	                }	            }            	            $(window).scrollTop(0);	            $('#popup-window').html('<p class="loading"><img src="images/ajax-loader.gif"/>' + msg + '</p>');	            t = window.setTimeout(function(){	                popupClose();	            }, 2000);	        }, "json");		}    /*}    else {        popupTidy();    }*/}/* create popup window */function popupWindow(v,m){    var pMask, pBody, pLoad, popMask, popWin, pClose, pSubmit, pCancel, pError;    pMask = '<div id="popup-mask"></div>';    pBody = '<div id="popup-window"></div>';    pLoad = '<p class="loading"><img src="images/ajax-loader.gif"/> Loading...</p>';    pClose = '<a class="popup-close" href="#" title="close popup">X</a>';    pSubmit = '<input type="button" class="submit_button" value="Submit"/>';    pCancel = '<input type="button" class="cancel_button popup-close" value="Cancel"/>';    $('body').append(pMask);    popMask = $('#popup-mask');    popMask.append(pBody);    popWin = $('#popup-window');    popWin.bind('click', function(e){        if (!e) {            e = window.event;        }        e.cancelBubble = true;        if (e.stopPropagation) {            e.stopPropagation();        }    });    if (typeof v === "object") {        pError = '<p class="error">Sorry, there was a problem getting the file "'+v.formf+'"</p>';        popWin.html(pLoad).load(v.form, function(responseText, textStatus, XMLHttpRequest){            popWin.prepend(pClose);            if (textStatus === 'success') {                popWin.append(pSubmit + pCancel);                $('input.submit_button', popWin).bind('click', function(e){                    popupSubmit();                    e.preventDefault();                });                popupAutocomplete(v);				$(".popup-field").keypress(function(e){					if (e.which == 13){   						return false;					}				});				$("#name").focus();            }            else {                popWin.append(pError);            }            popupCloseBind();        });    } else {      popWin.html(pClose + v);      popupCloseBind();    }}$(document).ready(function() {    	/* 	* Bind a.popup to the popup window function using the JSON object specified.	*		* That object will control the notes form AJAX'd into the popup , the agent called 	* on submission and  the text displayed when certain values are returned. 	* alternatively enter a string and value will be displayed in the popup box.	*	* Close and Submit buttons will be inserted as appropriate.	*/	    $('a.popup').bind('click', function(e){        e.preventDefault();        popupWindow(cv);    });        });