function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject

function elementHide(elementId) {
	var styleObject = getStyleObject(elementId);
   if (styleObject) 
		styleObject.display = 'none';
}

function elementShow(elementId) {
	var styleObject = getStyleObject(elementId);
   if (styleObject) 
		styleObject.display = 'block';
}

// !!! function is obsolete - dont use it !!!
// use visibility was bad idea 
// will use display instead
function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	//return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
	//return false;
    }
} // changeObjectVisibility

function invertObjectVisibility(objectId) {
    // get a reference to the cross-browser style object and make sure the object exists
	getStyleObject(objectId);
    var styleObject = getStyleObject(objectId);
	if (styleObject.visibility == 'visible') {
		styleObject.visibility = 'hidden';
	} else	{ 
		styleObject.visibility = 'visible';
	}
} // invertObjectVisibility


function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

// get value of radio group selected element
function getRadioValue($radioId) {
		var el = document.getElementById($radioId);
		//DEBUG
		if (!el) { alert('Bad id -' + $radioId + ' - ' + el);}
		if (el.type != 'radio') alert('Not radio - ' + $radioId);
		var r = document.getElementsByName($radioId);
		var val = -1;
	    for (var j = 0; j < r.length; j++) 
			if (r[j].checked)
				val = r[j].value;
		return val;		
}


function windowWidthHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight];
}



function crScrollTop() {
	var offset = 0; 
   if(window.document.documentElement && window.document.documentElement.scrollTop) 
   	offset = window.document.documentElement.scrollTop;
   else if(window.document.body && window.document.body.scrollTop) 
	 	offset = window.document.body.scrollTop;
	return offset;	
}



//==========================================================================================
// popup clasess

DivWindow = function(pwWidth, pwHight) {
	this._element = null;
	this._width =  pwWidth;
	this._hight = pwHight;
	var _this = this;
	
	// Constructor
	this._width = pwWidth;
	this._hight = pwHight;
	
	if (this._element) 
	 return;
	this._element = document.createElement('div');
	var s = this._element.style;
	s.display = 'block';
	s.position = 'absolute';
	// Get wind size
	var wh = windowWidthHeight();
	s.left =  (wh[0] - pwWidth) / 2 + 'px';
	s.top = (wh[1] - pwHight) / 2 + crScrollTop() + 'px';
	s.width =  pwWidth + 'px';
	s.hight =  pwHight + 'px';
	s.zindex = '100';
	// Constructor END
	
	//	 Methods
	this.show = function() {
		var parent = document.getElementsByTagName("body")[0];
  	   parent.appendChild(this._element);
	}
	
	
	this.hide = function() {
		var parent = document.getElementsByTagName("body")[0];
		parent.removeChild(this._element);	
	} 
	
	this.move = function(left, top) {
		var s = this._element.style;
		s.left =  left + 'px';
		s.top = top + 'px';
	} 
	
	this.setInnerHTML = function(strHTML) {
		if (!this._element) 
			alert('DivWindow.setInnerHTML : Elemt not exists');
		this._element.innerHTML = strHTML;
	}
	
	this.hnd_onScroll = function() {
		var s = _this._element.style;
		var wh = windowWidthHeight();
		var t = (wh[1] - _this._hight) / 2 + crScrollTop() + 'px';
		s.top = t;	
		
	}
	
	this.makeSticky = function() {
		window.onscroll = this.hnd_onScroll;
	} 
}

function MessageBox(pwWidth, pwHight, subject, bodyHtml) {
   this.base = DivWindow;
	this.base(pwWidth, pwHight);
	//FIXIT - cant create more then one window	
	pMsgBox = this;
	// get uniqe id 
	var id =  new Date().getTime();
	id = id - Math.round(10000*Math.random());
	
	this._closeButtonId = 'cb_' + id;
	this._buttonId = 'b_' + id;
	
	var str='';
	str+='<table width="271"  bgcolor="#F7F7F7" height=111 background=images/Image/structure/browsebg.jpg border="0" cellpadding="0" cellspacing="0" >';
	str+='	<tr>';
	str+='		<td width="15%" height=20><\/td>';
	str+='		<td align="center"><strong>' + subject + '<\/strong><\/td>';
	str+='		<td align="right" style=padding-top:5;padding-right:10 ><a id="' + this._closeButtonId +'" href="#"><img  src=images/Image/structure/x.jpg border=0><\/a><\/td>';
	str+='	<\/tr>';
	str+='	<tr><td colspan="3" align="center">' + bodyHtml + '<\/td><\/tr>';
	str+='	<tr>';
	str+='		<td colspan="3" align="center"  >';
	str+='		   <form>';
	str+='            <button id="' + this._buttonId +'" value="ok" name="button" type="button">OK</button>';
	str+=' 			<\/form>';
	str+=' 		<\/td>';
	str+='	<\/tr>';
	str+='<\/table>';
	
	this.setInnerHTML(str);
	this.show();
	var btnClose = document.getElementById(this._closeButtonId);
	btnClose.onclick = function() { 
		pMsgBox.hide();
	}
	var btnOk = document.getElementById(this._buttonId);
	btnOk.onclick = function() { 
		pMsgBox.hide();
	}	
}

MessageBox.prototype = new DivWindow(100,100);

//========================================================================================== 
// Validation function

var alertMsg = "";

function addError(errorMsg) {
	alertMsg += errorMsg + '\r\n';
}

function valid_required(fieldName, val) {
	if(val.length == 0) {
		addError(fieldName + ' - Required Field');
		return false;
	} else
		return true;
		
}

function valid_match(text, val1, val2) {
	if (val1 != val2) {
		addError(text + ' - must match');
		 return false;
	} else
		return true;		
}

function valid_notSelected(fieldName, selectElemet) {
	if (selectElemet.value == '0') {
 	   addError('Please select ' + fieldName); 
	   return false;
	} else
		return true;	   
}


function valid_url(fieldName, val) {
	if (!isUrl(val)) {
 	   addError(fieldName + ' has wrong url format'); 	
   		return false;
	} else
		return true;	   
}

function valid_email(fieldName, val) {
	if (!isEmail(val)) {
 	   addError(fieldName + ' has wrong email format'); 	
	   return false;
	} else
		return true;
}

function isInteger(n){
  var RegExp = /^(\d+)$/; 
  return RegExp.test(n);
}

function isDate(n){
  var RegExp = /^\d{4}-\d{1,2}-\d{1,2}$/; 
  return  n.match(RegExp);
}

function isUrl(s) {//                    \***\                     /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/
	var regexp = /^(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\w+\.{1}\w+)+(\/|\/([\w#!:.?+=&%@!\-\/]))?$/;
	return regexp.test(s);
}

function isEmail(s) {
	var regexp = /^[\w\.=-]+@[\w\.-]+\.[\w\.-]{2,4}$/
	return regexp.test(s);
}

//red coloring of wrong fields
function redDist(fieldname)	{
	if(document.getElementById(fieldname))
		document.getElementById(fieldname).style.backgroundColor="#FFC1BF"; 	
}
//white coloring of right fields
function whiteDist(fieldname){
  if(document.getElementById(fieldname))
	document.getElementById(fieldname).style.backgroundColor="white";   
}

function findtwins(logname) { //AJAX :) seeking of member mail login name duplets in DB 
	request = createRequest();
	var sURL = Config.PROJECT_URL + "functions/seektwins.php?logname="+logname;
	request.open( "GET", sURL, false);
	request.send("");
	//ERRORS-----------------------------------------------------
    if(request.status != 200){
        alert("Response problem. status code = " + request.status);
		return false;
	}
	if(request.readyState != 4) {
		alert("A problem occurred with communicating between "+
               "the XMLHttpRequest object and the server program.");
		return false;
	 // END ERRORS--------------------------------------------------	
	}else{		
		if (request.responseText=="p")//member exist
			return false;
		else 
			if (request.responseText=="l")// member not exist
			return true;
	}
}
// END Validation function
//========================================================================================== 


// Debug functions

function Dump(d,l) {
    if (l == null) l = 1;
	 if (l > 2) return '';
    var s = '';
    if (typeof(d) == "object") {
        s += typeof(d) + " {\n";
        for (var k in d) {
            for (var i=0; i<l; i++) s += "  ";
            s += k+": " + Dump(d[k],l+1);
        }
        for (var i=0; i<l-1; i++) s += "  ";
        s += "}\n"
    } else {
        s += "" + d + "\n";
    }
    return s;
}


//===============================================
// Check if anybody (exept user with id clientId) has such ID 
// publicId - sc_client_public_id
// clientId = sc_id
// return bool
function aj_chechClientId(publicId, clientId) {
	//alert(publicId + " ; " + clientId);
	r = createRequest();
	//alert(r);

	var sURL = "functions/checkclientid.php?publicId=" + publicId + "&clientId=" +clientId;
	r.open( "GET", sURL, false );
	r.send("");

	 if(request.readyState != 4) {
		alert("A problem occurred with communicating between "+
                  "the XMLHttpRequest object and the server program.");
		return false;
	 }
    if(request.status != 200) {
        alert("Response problem. status code = " + request.status);
		return false;
	}
     
	//alert(r.responseText);
	if (r.responseText != "notexists") 
		return false;
	else
		return true;
}

//===============================================
// Check if anybody (exept user with id clientId) has such Turn name
// publicId - sc_client_public_id
// clientId = sc_id
// return bool 
function aj_chechTurnName(publicId, clientId) {
	//alert(publicId + " ; " + clientId);
	r = createRequest();
	//alert(r);

	var sURL = "functions/checkturnname.php?tsc_id=" + publicId + "&uniquename=" +clientId;
	r.open( "GET", sURL, false );
	r.send("");

	 if(request.readyState != 4) {
		alert("A problem occurred with communicating between "+
                  "the XMLHttpRequest object and the server program.");
		return false;
	 }
    if(request.status != 200) {
        alert("Response problem. status code = " + request.status);
		return false;
	}
     
	//alert(r.responseText);
	if (r.responseText != "notexists") 
		return false;
	else
		return true;
}


//
function scAlert($msg) {
	alert($msg);
}


function notForGold() {
	$msg = "This is feature is available to Platinum Package members only.   \n";
	$msg += "To upgrade, please contact sales@faithhighway.com, or call 877-703-2484 and press 2.";
	scAlert($msg);
		
}


function cardval(s) {
//  Anton 2008.09.29
// Since I do not understend what this function really do, just let it return TRUE always
	return TRUE;
		
        //document.write(s+"<br>");
		// remove non-numerics
		var v = "0123456789";
		var w = "";
		for (i=0; i < s.length; i++) {
			x = s.charAt(i);
			if (v.indexOf(x,0) != -1)
				w += x;
/*
				if(i<10)
					document.write("i:0"+i+" X:"+x+" W:"+w+"<br>");
				else
					document.write("i:"+i+" X:"+x+" W:"+w+"<br>");
*/
		}
        //document.write("<br>");
		// validate number
		j = w.length / 2;
		//document.write(" j:"+j+"<br>");
		if (j < 6.5 || j > 8 || j == 7)
			return false;

		k = Math.floor(j);
		//document.write(" k:"+k+"<br>");

		m = Math.ceil(j) - k;
		//document.write(" m:"+m+"<br>");

		c = 0;
        //document.write("<br>");
		for (i=0; i<k; i++) {
			a = w.charAt(i*2+m) * 2;
			//document.write(" a:"+a+"<br>");
			c += a > 9 ? Math.floor(a/10 + a%10) : a;
			//document.write(" c:"+c+"<br>");
		}
        //document.write("<br>");
		for (i=0; i<k+m; i++){
			c += w.charAt(i*2+1-m) * 1;
			//document.write(" c:"+c+"<br>");
		}
	return (c%10 == 0);
}
