// JavaScript Document
var sizeX = 400;
var sizeY = 330;
var xmlfilename ='';
var theWidth, theHeight;
var url = '';
function previewdata(code)
{	
	if (emailValidator() == true )
	{
		if ( code == 'getCode')
		{
			$('codearea').value = '<div align="center">\<script type="text/javascript" src="' + url + '/slideshowwidget/js/widget.js?email=' + $('regemail').value + '&id=2&size=' + sizeX + 'X300"></\script><div class="powered" style="padding-left:35px;margin-top:10px; z-index:10000000000;color:black;color:#000000; font-size:12px;font-weight:normal;"><a target="_blank" href="http://vacationrealtywidgets.com/" style="color:#000000;">Get This Widget</a> | Powered by <a target="_blank" href="http://www.vacationrealty.com" style="color:#000000;">VacationRealty.com</a></div></div>';
			$('middle-4b-1').style.display = 'block';			
			if (window.XMLHttpRequest) 
			{
				var xmlhr = new XMLHttpRequest();
			} 
			else 
			{
				var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
			}
		
			requrl = url + '/slideshowwidget/save.php';
			
			params = 'id=' + $('regemail').value + '&bath=' + $('bath').value + '&loc=' + $('loc').value + '&bedrooms=' + $('bedrooms').value ;			
			xmlhr.open('POST',requrl, true);
			xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
			xmlhr.onreadystatechange = function()
			{
				
				if (xmlhr.readyState == 1) 
				{
					//$('codearea').value = 'Getting Code.....';
				} 
				else if (xmlhr.readyState == 4 && xmlhr.status == 200) 
				{
					if (xmlhr.responseText) 
					{
						widgetcode=xmlhr.responseText;
						//$('codearea').value = '<div align="center">\<script type="text/javascript" src="http://vacationrealtywidgets.com/slideshowwidget/js/widget.js?email=' + $('regemail').value + '&id=2&size=' + sizeX + 'X300"></\script><div class="powered" style="padding-left:35px;margin-top:10px; z-index:10000000000;color:black;color:#000000; font-size:12px;font-weight:normal;"><a target="_blank" href="http://vacationrealtywidgets.com/" style="color:#000000;">Get This Widget</a> | Powered by <a target="_blank" href="http://www.vacationrealty.com" style="color:#000000;">VacationRealty.com</a></div></div>';
						//$('middle-4b-1').style.display = 'block';
					}
				} 
				else if (xmlhr.readyState == 4) 
				{
					
				}
			}
			xmlhr.send(params);			
		}
		
		if (window.XMLHttpRequest) 
		{
			var xmlhr = new XMLHttpRequest();
		} 
		else 
		{
			var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
		}
	
		requrl = url + '/slideshowwidget/makexml.php';
		
		params ='filenames=' + $('imgfilenames').value + '&imgfiledes=' + $('imgfiledes').value + '&id=' + $('regemail').value + '&layout=' + $('layout').value + '&imgtrans=' + $('imgtrans').value + '&bath=' + $('bath').value + '&loc=' + $('loc').value + '&bedrooms=' + $('bedrooms').value ;	
	
		xmlhr.open('POST',requrl, true);
		xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
		xmlhr.onreadystatechange = function()
		{
			var resultDiv = $('preview_area');
			if (xmlhr.readyState == 1) 
			{
				resultDiv.innerHTML = '<img src="./images/ajax-loader.gif" />';
			} 
			else if (xmlhr.readyState == 4 && xmlhr.status == 200) 
			{
				if (xmlhr.responseText) 
				{
					xmlfilename=xmlhr.responseText;
					loadCurrent('preview_area');
				}
			} 
			else if (xmlhr.readyState == 4) 
			{
				resultDiv.innerHTML = '<img src="./images/ajax-loader.gif" />';
			}
		}
		xmlhr.send(params);
	}
/*	else
	{
		showErrDiv('Invalid email address');
	}
*/	
}
	
function validate(code)
{
	//alert($('imgfilenum').value);
	var i = 0;
	if ( emailValidator() == false )	
	{
		i = i + 1;		
	}
	if ( $('bedrooms').value == '' || $('bedrooms').value == 'undefined' )
	{
		//showErrDiv('Please Enter Number of Bedroom');
		$('bedrooms_err').innerHTML = 'Number of Bedrooms Required';
		i = i + 1;
	}
	if ( $('bath').value == '' || $('bath').value == 'undefined' )
	{
		//showErrDiv('Please Enter Number of Baths');
		$('bath_err').innerHTML = 'Number of Baths Required';		
		i = i + 1;		
	}
	if ( $('loc').value == '' || $('loc').value == 'undefined' )
	{
		//showErrDiv('Please Enter Your Location.');		
		$('loc_err').innerHTML = 'Location Required';		
		i = i + 1;		
	}
	if ( ( $('imgfilenum').value == '' || $('imgfilenum').value == 'undefined' ) && i == 0 )
	{
		//showErrDiv('Please Upload Images. You can upload 12 Pictures.');		
		//$('bedrooms_err').value = 'Please Upload Images. You can upload 12 Pictures.';		
		alert('Please Upload Images. You can upload 12 Pictures.');
		i = i + 1;		
	}
	if (i == 0)
	{
		previewdata(code);
	}
	
}	

function remove(div)
{
	$(div).innerHTML='';
}

function loadCurrent(resultDiv)
{
	resultDiv = $(resultDiv);	
	resultDiv.innerHTML = '';
	fsizex = sizeX;
	fsizey = parseInt(sizeY)+60;
	
	iframe = '<iframe id="widgetbox_widget_iframe_0" width="'+sizeX+'" scrolling="no" frameborder="0" name="widgetbox_widget_iframe_0" border="0" src="" marginwidth="0" marginheight="0" height="'+sizeY+'" vspace="0" hspace="0" allowtransparency="true" ></iframe>';
	//alert(iframe);	
	var newDiv = document.createElement('div');	
	resultDiv.appendChild(newDiv);
	newDiv.innerHTML = iframe;
	ifr = $('widgetbox_widget_iframe_0');	
	ifr.src = url + '/slideshowwidget/flash1.php?filenames=' + xmlfilename;	
	
}

function $(id)
{
	return 	document.getElementById(id)==undefined?null:document.getElementById(id);
}
	
function authdata(params,url,mode)
{
	if (window.XMLHttpRequest) 
	{
	    var xmlhr = new XMLHttpRequest();
	} 
	else 
	{
	    var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
	}

	 requrl = url;
	 xmlhr.open('POST',requrl, true);
	 xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	 xmlhr.onreadystatechange = function()
	 {
	    var resultDiv = $('preview_area');
		if (xmlhr.readyState == 1) 
		{
			resultDiv.innerHTML = '<img src="./images/ajax-loader.gif" />';
		} 
		else if (xmlhr.readyState == 4 && xmlhr.status == 200) 
		{
			  if (xmlhr.responseText) 
				{
				d=xmlhr.responseText;
				loadCurrent('preview_area',mode,'params='+d);
				}
		} 
		else if (xmlhr.readyState == 4) 
		{
			
		}
	}
	xmlhr.send(params);	
}

function img_upload()
{	
	if ( emailValidator() == true )
	{
		if ( parseInt( $('imgfilenum').value ) >= 12 )
		{
			//showErrDiv('You can upload 12 Pictures.');		
			$('pic_err').innerHTML ='You can upload 12 Pictures.';
			return false;		
		}
		else if ( validateFileExtension() == true )
		{
			document.getElementById('file_upload_form').target = 'upload_target'; //'upload_target' is the name of the iframe		
			
			return true;
		}
		else
		{
			return false
		}
	}
	else
	{
		return false;
	}	
}

function img_uploaded(img_path, img_des)
{
	if ( $('imgfilenames').value == '' || $('imgfilenames').value == 'undefined' )
	{
		$('imgfilenames').value = img_path ;
		$('imgfiledes').value = img_des;		
		$('imgfilenum').value = 1;		
	}
	else
	{
		$('imgfilenames').value = $('imgfilenames').value + '|*|*|*' + img_path ;
		$('imgfiledes').value = $('imgfiledes').value + '|*|*|*' + img_des;		
		$('imgfilenum').value = parseInt($('imgfilenum').value) + 1;				
	}	
	//showImgDesDiv(img_path);	
	$('thumb_file').value='';
	clearForm('file_upload_form');	
	shwThumImgs();
}

function shwThumImgs()
{
	if ($('imgfilenum').value > 0 )  
	{
		if (window.XMLHttpRequest) 
		{
			var xmlhr = new XMLHttpRequest();
		} 
		else 
		{
			var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
		}
	
		requrl = url + '/slideshowwidget/showimgs.php';
		
		params = 'filenames=' + $('imgfilenames').value + "&imgfiledes=" + $('imgfiledes').value;
		
		xmlhr.open('POST',requrl, true);
		xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
		xmlhr.onreadystatechange = function()
		{
			var resultDiv = $('showThmimgs');
			if (xmlhr.readyState == 1) 
			{
				resultDiv.innerHTML = '<img src="./images/ajax-loader.gif" />';
			} 
			else if (xmlhr.readyState == 4 && xmlhr.status == 200) 
			{
				if (xmlhr.responseText) 
				{
					resultDiv.style.display = 'block';
					resultDiv.innerHTML = xmlhr.responseText;			
				}
			} 
			else if (xmlhr.readyState == 4) 
			{
				
			}
		}
		xmlhr.send(params);		
	}
}

function deletethis(filename, filedes, i, count)
{

	$('imgfilenames').value = $('imgfilenames').value.replace(filename  , '');
	$('imgfiledes').value = $('imgfiledes').value.replace(filedes , '');

	$('imgfilenum').value = parseInt($('imgfilenum').value) - 1;
	$('showimgs_' + i).style.display = 'none';		
	if ($('imgfilenum').value == 0)
	{
		$('showThmimgs').style.display = 'none';			
		validate('preview');
	}
	else
	{
		shwThumImgs();
		validate('preview');
	}
}

function closeDiv(div)
{	
	$(div).style.display = "none";
}

function addImgDes(div)
{
	$(div).style.display = "none";	
	if ( $('imgfiledes').value == '' || $('imgfiledes').value == 'undefined' )
	{
		$('imgfiledes').value = $('img_des').value ;
	}
	else
	{
		$('imgfiledes').value = $('imgfiledes').value + '|*|*|*' + $('img_des').value ;
	}
	//previewdata()	
}

function showErrDiv(msg)
{
	$('err_para').innerHTML = msg;
	$('err_popup').style.display = "block";
}

function emailValidator()
{
	var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;		
	is_valid = regex.test($('regemail').value);	
	if($('regemail').value == '')
	{
		$('regemail_err').innerHTML = 'Email Required';
		//showErrDiv('Email Required');	
		$('regemail').focus();		
		return false;
	}		
	else if(!is_valid)
	{
		$('regemail_err').innerHTML = 'Invalid email address';
		//showErrDiv('Invalid email address');
		$('regemail').focus();
		return false;		
	}		
	else
	{
		$('regemail_err').innerHTML = '';	
		$('emailadd').value = $('regemail').value; 
		return true;		
	}		
}

function clientWidHeight()
{
	// Window dimensions:
	if (window.innerWidth) 
	{		
		theWidth=window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth) 
	{
		theWidth=document.documentElement.clientWidth;
	}
	else if (document.body) 
	{
		theWidth=document.body.clientWidth;
	}
	if (window.innerHeight) 
	{
		theHeight=window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) 
	{
		theHeight=document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		theHeight=document.body.clientHeight;
	}	
}

function clearForm(formIdent) 
{ 
  var form, elements, i, elm; 
  form = document.getElementById 
    ? document.getElementById(formIdent) 
    : document.forms[formIdent]; 

	if (document.getElementsByTagName)
	{
		elements = form.getElementsByTagName('input');
		for( i=0, elm; elm=elements.item(i++); )
		{
			if (elm.getAttribute('type') == "text")
			{
				elm.value = '';
			}
			if (elm.getAttribute('type') == "hidden")
			{
				elm.value = '';
			}			
		}
	}
	else
	{
		elements = form.elements;
		for( i=0, elm; elm=elements[i++]; )
		{
			if (elm.type == "text")
			{
				elm.value ='';
			}
			if (elm.type == "hidden")
			{
				elm.value ='';
			}			
		}
	}
}

function getIFrameDocument(aID)
{ 
	var rv = null; 
	var frame=$(aID);
	// if contentDocument exists, W3C compliant (e.g. Mozilla) 

	if (frame.contentDocument)
		rv = frame.contentDocument;
	else // bad IE  ;)

		rv = document.frames[aID].document.body.scrollHeight;
	return rv;
}

function validateFileExtension()
{
	if ($('thumb_file').value == '' || $('thumb_file').value =='undefined')
	{
		//showErrDiv("Select a file to upload.");		
		$('pic_err').innerHTML ='Select a file to upload.';		
		return false;
	}
	else
	{
		//if(!/(\.bmp|\.gif|\.jpg|\.jpeg)$/i.test($('thumb_file').value)) 
		var filename = $('thumb_file').value;
		var fileext = filename.substring(filename.lastIndexOf('.')+1);		
		
		if ( fileext == 'bmp' || fileext == 'gif' || fileext == 'jpg' || fileext == 'jpeg' || fileext == 'png')
		{
			return true;
		}
		else
		{
			//showErrDiv("Invalid image file type.");
			$('pic_err').innerHTML ='Invalid image file type.';					
			$('thumb_file').value='';
			$('thumb_file').focus();
			return false;			
		}
	}
}
function textfieldOnfocus(src)
{
	src.style.backgroundColor="#EEEEEE";
	src.style.borderColor="#000000";
}
function textfieldOnfocusOut(src)
{
	src.style.backgroundColor="#FFFFFF";
	src.style.borderColor="#666666";
}

function copy(text2copy) {
  if (window.clipboardData) {
    window.clipboardData.setData("Text",text2copy);
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
}


function init()
{
	clearForm('hiddenform');
	//setTimeout("clearForm('hiddenform')",5000);
	$('regemail').value = '';
	$('bedrooms').value = '';
	$('loc').value = '';	
	$('bath').value = '';			
	$('imgdes').value = '';			
	
}
//init();