/*
 * $Id:$
 *
 * This is a javascript file used for rendering a quickform.
 */

// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
function getPageScroll(){
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
    return yScroll;
}

function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function showQuickForm(url) {
    var overlay = document.createElement("div");
    var shade   = document.createElement("div");
    var close   = document.createElement("a");
	var powered   = document.createElement("a");
    var content = document.createElement("div");
    var iframe = null;
    try {
        iframe = document.createElement("<iframe name='quickFormContent'>");
    } catch(e) {
        iframe = document.createElement("iframe");
    }
    overlay.style.id = 'qfOverlay';
    overlay.style.position = 'absolute';
    overlay.style.width = '100%';
    overlay.style.height = '100%';
    overlay.style.top = '0px';
    overlay.style.left = '0px';

    shade.style.position = 'absolute';
    shade.style.width = '100%';
    shade.style.height = '4000px';
    shade.style.backgroundColor = '#333';
    shade.style.opacity = ".80";
    shade.style.filter = 'alpha(opacity = 80)';
    shade.style['-moz-opacity'] = ".80";
    shade.style.zIndex = 1000;
    shade.style.top = '0px';
    shade.style.left = '0px';

    content.style.position = 'absolute';
    content.style.top = getPageScroll() + (getPageSize()[1] * .05);
    content.style.left = '20%';
    content.style.width = '60%';
    content.style.height = '85%';
    content.style.backgroundColor = 'white';
    content.style.border = '3px solid black';
    content.style.zIndex = 1001;
    iframe.setAttribute('src', url);
    iframe.setAttribute('frameborder', 0);
    iframe.setAttribute('name', 'quickFormContent');
    iframe.style.width = '100%';
    iframe.style.height = '100%';

    close.innerHTML = "Close Window";
    close.style.position = 'absolute';
    close.style.top      = '10px';
    close.style.right    = '20px';
    close.style.display = 'block';
    close.style.backgroundColor = '#EFEFEF';
    close.style.padding = '4px';
    close.style.color = '#444444';
    close.style.border = 'solid 1px #CCCCCC';    
	close.style.fontFamily = "Arial";
    close.style.fontSize   = "10px";
    close.style.fontStyle   = "normal";


    close.href      = '#';
    close.onclick = function() {
        document.body.removeChild(overlay);
        return false;
    }
	
	powered.innerHTML = "Powered by<br>Textcall.com";
    powered.style.position = 'absolute';
    powered.style.bottom      = '20px';
    powered.style.right    = '20px';
    powered.style.display = 'block';
    powered.style.backgroundColor = '#EFEFEF';
    powered.style.padding = '4px';
    powered.style.color = '#026CA1';
    powered.style.border = 'solid 1px #CCCCCC';
    powered.style.fontFamily = "Arial";
    powered.style.fontSize   = "10px";
    powered.style.fontStyle   = "normal";

    powered.href      = 'http://www.textcall.com';
    powered.onclick = function() {
        document.body.removeChild(overlay);
        return true;
    }

    overlay.appendChild(shade);
    overlay.appendChild(content);
    content.appendChild(close);
	content.appendChild(powered);
    content.appendChild(iframe);
    document.body.appendChild(overlay);

    return false;
}


var qf_needs_validation = [];
function qf_register_validate(fieldName) {
    qf_needs_validation.push(fieldName);
}

function qf_do_validation() {
    var ok = true;
    if(document.getElementById('qf-error-message')) {
        document.getElementById('qf-error-message').innerHTML = "";
    }
    for(i in qf_needs_validation) {
        var n = qf_needs_validation[i];
        var f = document.getElementById('qf_f_' + n);
        document.getElementById('qf_f_e_' + n).innerHTML = "";
        if(n.match(/.*email.*/)) {
            if(!f.value.match(/[^@]+[@][^.]+[.][^.]+.*/)) {
                document.getElementById('qf_f_e_' + n).innerHTML = "Must provide a valid e-mail";
                ok = false;
            }
        } else if(f['options']) {
            // Nothing to validate
        } else {
            if(f.value != undefined && f.value.match(/^ *$/)) {
                document.getElementById('qf_f_e_' + n).innerHTML = "Must provide a value";
                ok = false;
            }
        }
    }
    if(ok) {
        var submit = document.getElementById('qf-submit');
        submit.value = 'Sending ...';
        submit.disabled = true;
    }
    return ok;
}

function qf_char_handler(field, max, name) {
    if(field.value.length > max) {
        field.value = field.value.substring(0, max);
    }
    document.getElementById("qf-char-counter-" + name).innerHTML = 
        "Characters remaining: " + (max - field.value.length);
}