var frame_opened = false;
var frame_window_id = 'frame_window';
var frame_id = 'drm_frame';
var frame_location = 'drm_frame.php'
var frame_title = 'frame_title';
var fader = null;
var fader_id = 'page';
var fader_iframe = 'iframe_page';
var youtube_id = 'youtube_container';

var iframe_history = new Array();


function number_format(num)
{
	var str = '' + num;		//no it's a string
	//is it real number?
	var real = false;
	var i;
	for (i=0; i<str.length; i++) {
		if (str.charAt(i) == '.') {
			real = i;
			break;
		}
	}

	var work_str = '';
	if (real === false)
		work_str = str;
	else
		work_str = str.substring(0, real);

	//group by 3's
	var temp_string = '';
	var j=0;
	for (i=work_str.length-1; i >= 0; i--) {
		if (j == 3) {
			j = 0;
			temp_string += ',';
		}

		temp_string += work_str.charAt(i);
		j++;
	}
	//reverse back
	var final_string = '';
	for (i=temp_string.length-1; i>=0; i--)
		final_string += temp_string.charAt(i);
	if (real != false)
		final_string += str.substring(real);
	return final_string;
}


function buildIframeNavigation()
{
	//build as innerHTML (this is the fastest method)
	var ihtml;
	ihtml = '<a href="javascript:closeFrame();" class="iframe_align_mir">'+_close_text+'</a>';
	var len = iframe_history.length;
	if (len > 1) {
		ihtml += '<a href="javascript:backFrame(\''+iframe_history[len-2][0]+'\', \''+iframe_history[len-2][1]+'\');" class="iframe_align">'+_back_text+'</a>';
	}
	return ihtml;
}


function addHistoryItem(page, oid)
{
	len = iframe_history.length;
	iframe_history[len] = new Array();
	iframe_history[len][0] = page;
	iframe_history[len][1] = oid;
}

function clearHistory()
{
	iframe_history = new Array();
}

function getPositionX()
{
	return ((document.body.clientWidth-560)/2) +"px";
}

function getPositionY()
{
	var scroll = getPageScroll();
	return 100 + scroll[1] + "px";
}

var movid;

function openFrame(mov)
{
	movid=mov;
	fader = new pageMask(50, 200, fader_id, openRealFrame);
	fader.fadeIn();
}

function backFrame(_page, _oid)
{
	page = _page;
	oid = _oid;
	//remove last history item
	var len = iframe_history.length;
	iframe_history[len-1] = null;
	iframe_history.length--;
	if (fader == null) {
		fader = new pageMask(20, 200, fader_id, openRealFrame);
	}
	fader.fadeIn();
}

function openTransactionImage()
{
	if (fader == null) {
		fader = new pageMask(20, 200, fader_id, showTransactionImage);
	}
	fader.fadeIn();
}

function showTransactionImage()
{
	if (fader == null)
		return;

	var scroll = getPageScroll();

	var par = (fader.getMaskObject());
	par = par.parentNode;
	var img = document.createElement('img');
	img.src = _order_submit;
	img.style.left = ((document.body.clientWidth-178)/2) +"px";
	img.style.top = (scroll[1] + 250) + "px";
	img.style.position = 'fixed';
	img.style.zIndex = "10000";
	par.appendChild(img);
}


var tframe = null;

function openRealFrame()//(page, oid)
{

	var frame = document.getElementById(frame_id);

	if (frame_opened) {
		frame.src = frame_location;
		//frames['mano_frame'].location.href =  frame_location + "?page="+page+"&oid="+oid;
		return;
	}

	frame_opened = true;
	var frame_window = document.getElementById(frame_window_id);
	frame.src = frame_location + "?movid="+movid;
	frame_window.style.left = getPositionX();
	frame_window.style.top = getPositionY();
	frame_window.style.display = "block";

}

function closeFrame()
{
	//clearHistory();
	var frame_window = document.getElementById(frame_window_id);
	var frame = document.getElementById(frame_id);
	frame_window.style.display = "none";
	frame.src = "";
	//setFrameTitle('');
	frame_opened = false;

	if (fader == null) {
		return;
	}
	fader.fadeOut();
}


function setFrameTitle(ttl)
{
	//document.getElementById(frame_title).innerHTML = ttl;
	return;
}


/*-------------------------------------
page masking object
Liran Oz - 2008
--------------------------------------*/
function pageMask(_opacity, _speed, _container, _callback)
{
	var cur_opacity = 0;
	var speed = _speed;
	var step = 10;
	var tar_opacity = _opacity;
	var container_id = _container;
	var mask_obj = null;
	var width;
	var height;
	var cycle_flag = false;
	var interval = null;
	var wait = 30;
	var callback = _callback;
	var select_box = null;
	var objects = null;
	var embeds = null;
	var done_fading = false;
	var inner_working = false;
	var background_color = 'black';


	function setOpacity(opc)
	{
		mask_obj.style.opacity = opc/100;
		mask_obj.style.filter = 'alpha(opacity=' + opc + ')';
	}


	function createObj()
	{
		//create new element
		var sz = getPageSize();		//Thank you lightbox
		var cnt = document.getElementById(_container);
		if (!cnt) {
			return false;
		}


		mask_obj = document.createElement('div');
		mask_obj.style.width = sz[2] + "px";
		mask_obj.style.height = sz[1] + "px";
		mask_obj.style.position = "absolute";
		mask_obj.style.left = "0px";
		mask_obj.style.top = "0px";
		mask_obj.style.zIndex = 100;
		mask_obj.style.backgroundColor = background_color;

		cnt.appendChild(mask_obj);
	}

	function removeObj()
	{
		//create new element
		var sz = getPageSize();		//Thank you lightbox
		var cnt = document.getElementById(container_id);
		if (!cnt) {
			return false;
		}
		cnt.removeChild(mask_obj);
		mask_obj = null;
	}

	this.getMaskObject = function()
	{
		return mask_obj;
	}

	this.setBackgroundColor = function(col)
	{
		background_color = col;
	}


	function cycleOpacity()
	{
		if (mask_obj == null) {
			createObj();
			setOpacity(cur_opacity);
		}

		done_fading = false;
		cur_opacity += step;
		if (cur_opacity >= tar_opacity) {		//no more cyclig
			inner_working = true;
			cur_opacity = tar_opacity;
			cycle_flag = false;
			setOpacity(cur_opacity);
			//stop cycling
			interval = null;
			//on complete, run callback
			done_fading = true;
			callback();
			inner_working = false;
			return true;
		}
		else if (cur_opacity <= 0) {
			cur_opacity = 0;
			cycle_flag = false;
			setOpacity(cur_opacity);
			//stop cycling
			interval = null;
			//bring back all select boxes (if any)
			if (select_box != null) {
				for (var i=0; i<select_box.length; i++) {
					select_box[i].style.visibility = "visible";
				}
			}

			if (objects != null) {
				for (var i=0; i<objects.length; i++) {
					objects[i].style.visibility = "visible";
				}
			}

			if (embeds != null) {
				for (var i=0; i<embeds.length; i++) {
					embeds[i].style.visibility = "visible";
				}
			}


			removeObj();
			return true;
		}
		//cycling is needed...
		setOpacity(cur_opacity);
		interval = setTimeout(cycleOpacity, wait);

	}


	this.fadeIn = function()
	{
		if (done_fading) {
			callback();
			return;
		}
		//hide all select box (thank you internet explorer..)
		var selects = document.getElementsByTagName('select');

		for (var i=0, j=0; i<selects.length; i++) {

			if (selects[i].style.visibility != "hidden") {
				if (select_box == null) {
					select_box = new Array();
				}
				select_box[j++] = selects[i];
				selects[i].style.visibility = "hidden";

			}
		}

		var flashes = document.getElementsByTagName('object');

		for (var i=0, j=0; i<flashes.length; i++) {

			if (flashes[i].style.visibility != "hidden") {
				if (objects == null) {
					objects = new Array();
				}
				objects[j++] = flashes[i];
				objects[i].style.visibility = "hidden";
			}
		}

		var flashes = document.getElementsByTagName('embed');

		for (var i=0, j=0; i<flashes.length; i++) {

			if (flashes[i].style.visibility != "hidden") {
				if (embeds == null) {
					embeds = new Array();
				}
				embeds[j++] = flashes[i];
				embeds[i].style.visibility = "hidden";
			}
		}



		step = speed / tar_opacity;
		interval = setTimeout(cycleOpacity, wait);
		return true;
	}


	this.fadeOut = function()
	{
		if (mask_obj == null) {
			return false;
		}
		//have we finished fadeing in?
		if (interval != null) {
			clearTimeout(interval);
		}
		step = -(speed / tar_opacity);
		interval = setTimeout(cycleOpacity, wait);
	}
}


function showYouTube()
{
	//should be null if opened from iframe
	if (fader == null) {
		fader = new pageMask(40, 200, fader_iframe, showRealYouTube);
	}
	fader.fadeIn();
}

function showRealYouTube()
{
	var obj = document.getElementById(youtube_id);
	obj.style.display = "block";
}

//wrapper function for cruise select on search result
var cselectObj = null;
var order_img = 'images/order.gif';
var order_chk_img = 'images/order_checked.gif';

function selectCruise(id, obj)
{
	ajaxSelectRedirect(id);
	//document.getElementById('continue_os').style.display='block'
	//mark current row
	obj.parentNode.parentNode.className = "bold";
	obj.style.backgroundImage = 'url('+order_chk_img+')';
	if (cselectObj != null && cselectObj != obj) {
		cselectObj.style.backgroundImage = 'url('+order_img+')';
		//is it still on page?
		if (cselectObj.parentNode && cselectObj.parentNode.parentNode)
			cselectObj.parentNode.parentNode.className = "";
	}
	cselectObj = obj;

}




/*----------------------------
Easy form validation
Liran Oz - 2008
makes it easier to handle possibilities of 100+ items in a single form
----------------------------*/
function formItem(_id, _type, _req, _eVar)
{
	var id = _id;
	var type = _type;
	var req = _req;
	var eVar = _eVar;
	var val="";

	this.getId = function()
	{
		return id;
	}

	this.getType = function()
	{
		return type;
	}

	this.getValue = function()
	{
		obj = document.getElementById(id);
		switch(type) {
			case 'text':
			case 'int':
			case 'id_number':
			case 'email':
				val = obj.value;
				break;
			case 'select':
				if (obj.selectedIndex < 0)
					return false;
				val = obj.options[obj.selectedIndex].value;
				break;
		}

		return val;
	}

	this.setFocus = function()
	{
		obj = document.getElementById(id);
		if (obj && obj.focus)
			obj.focus();
	}



	this.isValid = function()
	{
		if (eVar != "") {
			eval("var needed = "+eVar+";");
			if (needed == false)
				return true;
		}
		if (!req) {
			return true;
		}
		obj = document.getElementById(id);
		switch(type) {
			case 'text':
				val = obj.value;
				if (val.length > 0) {
					return true;
				}
				return false;


			case 'select':
				if (obj.selectedIndex < 0)
					return false;
				val = obj.options[obj.selectedIndex].value;
				//what to do with that?
				return true;

			case 'id_number':
				val = obj.value;
				return checkidnum(val);


			case 'int':
				val = obj.value;

				if (val.match(/[^\d]+/g) ) {
					return false;
				}

				if (val.length > 0) {
					return true;
				}
				return false;

			case 'crd_num':
				val = obj.value;

				if (val.match(/[^\d]+/g) )
					return false;


				//use mod10 function
				var res = Mod10(val);

				if (res == false)
					return false;

				if (val.length == 8 || val.length == 16)
					return true;

				return false;

			case 'crd_dgt':
				val = obj.value;

				if (val.match(/[^\d]+/g) ) {
					return false;
				}

				if (val.length == 3) {
					return true;
				}
				return false;



			case 'email':
				val = obj.value;
				if (!val.match(/^[a-zA-Z0-9_\.-]+@[a-zA-Z0-9_-]+\.[a-zA-Z0-9_]+((\.[a-zA-Z0-9_]+)*)$/)) {
					return false;
				}
				return true;

			case 'chkbox':
				return true;

			case 'none':
				return true;

		}
		return false;
	}
}



function checkidnum(idnum)
{
 // FUNCTION FOR VALIDATING ID NUMBER
 //Addition - can only accept numbers! - Liran Oz
 	//trim - if exists
 	idnum = idnum.replace('-', '');

 	if (idnum.match(/[^0-9]/))
 		return false;



    while (idnum.length<9)
    {
        idnum="0"+idnum;
    }

    idnum1=idnum.substr(0,1)*1;
    idnum2=idnum.substr(1,1)*2;
    idnum3=idnum.substr(2,1)*1;
    idnum4=idnum.substr(3,1)*2;
    idnum5=idnum.substr(4,1)*1;
    idnum6=idnum.substr(5,1)*2;
    idnum7=idnum.substr(6,1)*1;
    idnum8=idnum.substr(7,1)*2;
    idnum9=idnum.substr(8,1)*1;

    if (idnum1>9) idnum1=(idnum1%10)+1
    if (idnum2>9) idnum2=(idnum2%10)+1
    if (idnum3>9) idnum3=(idnum3%10)+1
    if (idnum4>9) idnum4=(idnum4%10)+1
    if (idnum5>9) idnum5=(idnum5%10)+1
    if (idnum6>9) idnum6=(idnum6%10)+1
    if (idnum7>9) idnum7=(idnum7%10)+1
    if (idnum8>9) idnum8=(idnum8%10)+1
    if (idnum9>9) idnum9=(idnum9%10)+1

    var sumval=idnum1+idnum2+idnum3+idnum4+idnum5+idnum6+idnum7+idnum8+idnum9;
    if(sumval == 0)
     return false;
    sumval=sumval%10
    if (sumval>0)
    {
        return false;
    }
  return true;
}



/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: David Leppek :: https://www.azcode.com/Mod10

Basically, the alorithum takes each digit, from right to left and muliplies each second
digit by two. If the multiple is two-digits long (i.e.: 6 * 2 = 12) the two digits of
the multiple are then added together for a new number (1 + 2 = 3). You then add up the
string of numbers, both unaltered and new values and get a total sum. This sum is then
divided by 10 and the remainder should be zero if it is a valid credit card. Hense the
name Mod 10 or Modulus 10. */

function Mod10(ccNumb) {  // v2.0
var valid = "0123456789"  // Valid digits in a credit card number
var len = ccNumb.length;  // The length of the submitted cc number
var iCCN = parseInt(ccNumb, 10);  // integer of ccNumb
var sCCN = ccNumb.toString();  // string of ccNumb
sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
var iTotal = 0;  // integer total set at zero
var bNum = true;  // by default assume it is a number
var bResult = false;  // by default assume it is NOT a valid cc
var temp;  // temp variable for parsing string
var calc;  // used for calculation of each digit

// Determine if the ccNumb is in fact all numbers
for (var j=0; j<len; j++) {
  temp = "" + sCCN.substring(j, j+1);
  if (valid.indexOf(temp) == "-1"){bNum = false;}
}

// if it is NOT a number, you can either alert to the fact, or just pass a failure
if(!bNum){
  /*alert("Not a Number");*/bResult = false;
}

// Determine if it is the proper length
if((len == 0)&&(bResult)){  // nothing, field is blank AND passed above # check
  bResult = false;
} else{  // ccNumb is a number and the proper length - let's see if it is a valid card number
  if(len >= 15){  // 15 or 16 for Amex or V/MC
    for(var i=len;i>0;i--){  // LOOP throught the digits of the card
      calc = parseInt(iCCN, 10) % 10;  // right most digit
      calc = parseInt(calc, 10);  // assure it is an integer
      iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
      i--;  // decrement the count - move to the next digit in the card
      iCCN = iCCN / 10;                               // subtracts right most digit from ccNumb
      calc = parseInt(iCCN, 10) % 10 ;    // NEXT right most digit
      calc = calc *2;                                 // multiply the digit by two
      // Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
      // I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
      switch(calc){
        case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
        case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
        case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
        case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
        case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
        default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
      }
    iCCN = iCCN / 10;  // subtracts right most digit from ccNum
    iTotal += calc;  // running total of the card number as we loop
  }  // END OF LOOP
  if ((iTotal%10)==0){  // check to see if the sum Mod 10 is zero
    bResult = true;  // This IS (or could be) a valid credit card number.
  } else {
    bResult = false;  // This could NOT be a valid credit card number
    }
  }
}
// change alert to on-page display or other indication as needed.
  return bResult; // Return the results
}




