//Name window "CIMain" if window not already named
if( window.name == null || window.name.indexOf( "CI" ) != 0) 
	window.name = "CIMain";

function setMsg(msg)
{
	window.status = msg;
	return true;
}

function ServerRequest( url, data )
{
	if(navigator.userAgent.indexOf("MSIE")!=-1)
	//if( navigator.appName == "Microsoft Internet Explorer") 
		xml = new ActiveXObject("Microsoft.XMLHTTP"); //IE
	else
		xml = new XMLHttpRequest();	//Netscape and others
	xml.open("POST", url, false );
	xml.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
	xml.send( data );
	return xml.responseText;
}

function AddMailingList()
{
	var o_EmailAddress = document.getElementById( "MailingListAddress" );
	newEmail = o_EmailAddress.value.trim();
	if( newEmail == null || !newEmail.isEmail() )
	{
		alert( "Your e-mail address is missing or invalid." );
		return;
	}
	response = ServerRequest( "/scripts/AddMailingList.php", "Email=" + newEmail );
	alert( response );
	o_EmailAddress.value = '';	
}

function SubmitFeedback( sourcePage )
{
	if( sourcePage == null )
		popup( "/Feedback.php", "width=740,height=652,resizable,scrollbars");	
	else
		popup( "/Feedback.php?SourcePage=" + sourcePage, "width=740,height=652,resizable,scrollbars");
}

var menuColor;
function SubMenuOver( source )
{
	source.style.background = "Yellow";
	menuColor = source.style.color;
	source.style.color = "Black";
}
function SubMenuOut( source )
{
	source.style.background = "transparent";
	source.style.color = menuColor;
}

function ButtonOver( source, color )
{
	source.style.color = color;
}

function ButtonOut( source, color )
{
	source.style.color = color;	
}

function WriteDate()
{
	var now = new Date();
	document.write( now.toDateString()  );
}

function popup( page, args )
{
  window.open(page, null, args);
}

function newBrowserWindow( page )
{
  window.open(page, null, "scrollbars, resizable, status, toolbar, menubar");
}

function popupWindow( page, name, args )
{
  win = window.open(page, name, args);
  win.focus();
}

function BookAct( prodCode, remoteLink )
{
	if( remoteLink == '' )
		popup("https://cozumelinsider.com/ecom/PlaceOrder.php?ProdCode="+prodCode, 		   "width=740,height=700,resizable,scrollbars,status");
	else
		popup(remoteLink,"width=800,height=700,resizable,scrollbars,status" ); 
}

function BeachDetails(html)
{
  popup(html, "width=600,height=400");
}

function ShowPosting( postCode, fromSection )
{
  popupWindow("/index.php?Popup=1&Page="+postCode+"&From="+fromSection, "CIPosting", "width=810,height=700,resizable,scrollbars,status");
}

function ShowCoupon( couponID )
{
	  popupWindow( "Coupon.php?ID="+couponID, "CICoupon", "width=550,height=430");
}

function Sorry()
{	
	popup( "/sorry.html","height=170,width=450" );
}

function SetParentPage( html )
{	
	window.opener.location.href = html; 
}

//Expand text area for editing
var Expanded = null;
var ExpandedPrev = 0;
function ExpandEdit( numRows )
{
	source = event.srcElement;
	if( source == Expanded )
		return;
	if( Expanded != null )
		Expanded.rows = ExpandedPrev;
	Expanded = source;
	ExpandedPrev = source.rows;
	source.rows = (typeof numRows == "undefined") ? 10 : numRows;	
}

function ExpandClear()
{
	if( Expanded == null )
		return;
	Expanded.rows = ExpandedPrev;
	Expanded = null;
}

//Additional String Methods
//Trim whitespace from beginning and end of string
String.prototype.trim = function()
{
	return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
}

//Remove passed characters from end of string
String.prototype.rtrim = function( chars )
{
	rexp = "";
	for( i=0;i<chars.length;i++)
		rexp += "\\"+chars.charAt( i );
	var r = new RegExp( "["+rexp+"]+$" );	
	return this.replace(r,"");
}

//Remove passed characters from beginning of string
String.prototype.ltrim = function( chars )
{
	rexp = "";
	for( i=0;i<chars.length;i++)
		rexp += "\\"+chars.charAt( i )
	var r = new RegExp( "^["+rexp+"]+" );	
	return this.replace(r,"");
}
	
String.prototype.isNumeric = function()
{
	var r = new RegExp( "^\\d+$");
	return r.test( this );
}

//Remove passed characters from string
String.prototype.remove = function( chars )
{
	rexp = "";
	for( i=0;i<chars.length;i++)
		rexp += "\\"+chars.charAt( i )
	var r = new RegExp( rexp );	
	return this.replace(r,"");
}

String.prototype.isTime = function()
{
	var r = new RegExp( "([0-1][0-9]|2[0-3]):[0-5][0-9]");
	return r.test( this );
}

String.prototype.isURL = function()
{
	var r = new RegExp( "^http://([a-zA-Z0-9_\\-]+)([\\.][a-zA-Z0-9_\\-]+)+([/][a-zA-Z0-9\~\\(\\)_\\-]*)+([\\.][a-zA-Z0-9\\(\\)_\\-]+)*$");
	return r.test( this );
}

String.prototype.isEmail = function()
{
	var r = new RegExp( "^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$");
	return r.test( this );
}

String.prototype.isDate = function()
{
	var r = new RegExp( "^((((0?[13578])|(1[02]))[\\/|\\-]?((0?[1-9]|[0-2][0-9])|(3[01])))|(((0?[469])|(11))[\\/|\\-]?((0?[1-9]|[0-2][0-9])|(30)))|(0?[2][\\/\\-]?(0?[1-9]|[0-2][0-9])))[\\/\\-]?\d{2,4}$");
	return r.test( this );
}				
