/**
 * alert.js
 *-------------------------
 *
 * js base class
 *
 * PHP versions 5
 *
 * LICENSE: This source file is from CouponMountain Ver3.
 * The copyrights is reserved by http://www.mezimedia.com.
 * Copyright (c) 2006, Mezimedia. All rights reserved.
 *
 * @author     James.Huang <james_huang@mezimedia.com>
 * @copyright  (C) 2004-2006 Mezimedia.com
 * @license    http://www.mezimedia.com  PHP License 5.0
 * @version    CVS: $Id: alert.js,v 1.17 2010/07/05 10:04:38 even_tian Exp $
 * @link       http://www.couponmountain.com/
 * @deprecated File deprecated in Release 3.0.0
 */
 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// myalert extend

// for get element top pos
function getAbsoluteOffsetTop (obj) {
	var y = obj.offsetTop;
	while (obj = obj.offsetParent) y += obj.offsetTop;
	return y;
}

// for get element left pos
function getAbsoluteOffsetLeft (obj) {
	var x = obj.offsetLeft;
	while (obj = obj.offsetParent) x += obj.offsetLeft;
	return x;
}

// for get scroll
var getPageScroll = function () {
	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 { xScroll:0, yScroll:yScroll };
}

// for get windows size
var getPageSize = function () {
	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 = xScroll;
	} else {
		pageWidth = windowWidth;
	}
	return { pWidth:pageWidth, pHeight:pageHeight, wWidth:windowWidth, wHeight:windowHeight };
}

var myAlert = function (message) {
	var opt = Object.extend({
      form: '',
      focuson: '',
      redir_url: '',
	  btn_str: 'Close',
	  loading: false
    }, arguments[1] || {});
	var file_base = 'http://files.couponmountain.com';
	// set overlay div style
	$('overlay').style.width = getPageSize().pWidth + 'px';
	$('overlay').style.height = getPageSize().pHeight + 'px';
	$('overlay').style.display = 'block';
	// set myalert div content
	StringBuffer.append('<div id="alMessage">');
	StringBuffer.append('<table style="width:100%;text-align:center;margin-top:0px;"><tr><td><img src="'+file_base+'/images/default/css_logo_bottom.jpg" /></td></tr>');
	if (opt.loading) {
		StringBuffer.append('<tr><td style="padding-top:15px;"><img src="'+file_base+'/images/default/loading_b.gif" style="vertical-align:middle" />&nbsp;&nbsp;');
		StringBuffer.append('<b>' + message + '</b></td></tr>')
	} else {
		StringBuffer.append('<tr><td><br/><b>' + message + '</b><br/><br/>');
		StringBuffer.append('<input id="closeAlert" type="submit" value="'+ opt.btn_str +'" onclick="rmAlert(\''+ opt.form +'\', \''+ opt.focuson +'\', \''+ opt.redir_url.escapeQuote() +'\');" /></td></tr>');
		// change the Enter hotkey
		regHotKey("Enter", function () {rmAlert(opt.form, opt.focuson, opt.redir_url);},{
			'type':'keypress',
			'propagate':false,
			'target':document,
			'reset':true
		});
	}
	StringBuffer.append('</table></div>');
	$('myalert').innerHTML = StringBuffer.out();
	// set myalert div style
	var width = parseInt($('myalert').getStyle('width'));
	var height = parseInt($('myalert').getStyle('height'));
	var popX = getPageScroll().xScroll+((getPageSize().wWidth-width)/2)+'px';
	var popY = getPageScroll().yScroll+((getPageSize().wHeight-height)/2)+'px';
	$('myalert').style.left	= popX;
	$('myalert').style.top	= popY;
	$('myalert').style.display = 'block';
	// focus on close button
	$('closeAlert').focus();
}

var rmAlert = function (form, focuson, redir_url) {
//	alert(form + ':' + focuson);
	if (redir_url != '') {
		if (redir_url.indexOf('#') != -1) {
			$('myalert').style.display = 'none';
			$('overlay').style.display = 'none';			
		}
		window.location = redir_url;
	} else {
		$('myalert').style.display = 'none';
		$('overlay').style.display = 'none';
		// set now focus
		if (form != '' && focuson != '')
			eval("$('" + form + "')."+ focuson +".focus();");
		// set Back Enter
		regHotKey("Enter", doNothing, {
			'type':'keypress',
			'propagate':false,
			'target':document,
			'reset':true
		});
	}
}

var doNothing = function (e) {
//	do nothing
}

var onEnterKeyDown = function (e) {
	if ($('accountForm')) {
		CM.checkAccountForm();
	}
}

var onAccountFormFocus = function (e) {
	if ($('accountForm')) {
		regHotKey("Enter", onEnterKeyDown, {
			'type':'keypress',
			'propagate':false,
			'target':document,
			'reset':true
		});
	}
}

var onSearchFormFocus = function (e) {
	if ($('searchForm')) {
		regHotKey("Enter", clickSearchButton, {
			'type':'keypress',
			'propagate':false,
			'target':document,
			'reset':true
		});
	}
}

var onForgotFormFocus = function (e) {
	if ($('form_forgot')) {
		regHotKey("Enter", function () {CM.checkSendPassword();}, {
			'type':'keypress',
			'propagate':false,
			'target':document,
			'reset':true
		});
	}
}

// For other form focus change
var onFormFocus = function (form) {
	if ($(form)) {
		regHotKey("Enter", function () {eval("$('" + form + "').submit();");}, {
			'type':'keypress',
			'propagate':false,
			'target':document,
			'reset':true
		});
	}
}

var clickSearchButton = function (e) {
	var form = $('searchForm');
	if (form) {
		var patrn = /^\s+$/;
		var searchText = form.searchKey.value.toLowerCase();
		if ('enter keywords' != searchText && !patrn.exec(searchText) && '' != searchText) {
			form.submit();
		} else {
			return false;
		}
	}
}

var initialize = function () {
	if (!document.getElementsByTagName) return;
	// generate required div
	body			= document.getElementsByTagName('body').item(0);
	al				= document.createElement('div');
	al.id			= 'myalert';
	al.className	= 'alertbox';
	ol				= document.createElement('div');
	ol.id			= 'overlay';
	body.appendChild(al);
	body.appendChild(ol);
	// set focus
//	if ($('searchForm')) {
//		$('searchForm').searchKey.focus();
//	}
}

var hiddenAndDirect = function (redirect_url) {
	return function() {
		window.location = redirect_url;
		$('myalert').style.display = 'none';
		$('overlay').style.display = 'none';				
	}
}

var hiddenAlert = function () {
	return function() {
		$('myalert').style.display = 'none';
		$('overlay').style.display = 'none';				
	}		
}

Event.observe(window, 'load', initialize, false);

