var isHash = false;
var emailistrue = false;
var emailistrue = false;
var encoded_params = '';
var ajax_error = 0;
var mapareaId = '';	
var sizeX = 0;
var sizeY = 0;

function onSubmitValidation(myform,mode)
	{
	var me = myform.elements;
	var is_error = 0;
	var theme='';
	var country = "United States";
	for(i=1;i<=7;i++)
		if(document.getElementById('theme'+i).checked==true)
			{
			theme = 'theme'+i;break;
			}
	if(theme=='')
		{
		alert('Please select a theme');
		return false;
		}
	
	
	
	state = (document.getElementById('state').value);
	city = (document.getElementById('city').value);
	zip = (document.getElementById('zip').value);
	address = (document.getElementById('address').value);
	email = (document.getElementById('email').value);
	size = (document.getElementById('size').value);
	sizeX = parseInt(size.split("X")[0])+10;
	sizeY = parseInt(size.split("X")[1])+40;
	no_error = {state:0,city:0,zip:0,address:0,count:0};
	
	if(country == "United States")
		{
		if(state=='')
			{
			document.getElementById('state_err').innerHTML="State Required";// = "block";
			no_error.state = 1; 
			no_error.count++;
			}
		else	
			realparams = "is_USA=true&locationCity="+city+"&locationCountry="+state+"&theme="+theme+"&zip="+zip+"&address="+address+"&size="+size+"&email="+email+"&website="+document.getElementById('website').value;  
		}
	else
		realparams = "is_USA=false&locationCity="+city+"&locationCountry="+country+"&theme="+theme+"&zip="+zip+"&address="+address+"&size="+size+"&email="+email+"&website="+document.getElementById('website').value;
	if(document.getElementById('city').value=='')
		{
		//alert('Please select a country');
		document.getElementById('city_err').innerHTML="City Required"; // = "block";
		document.getElementById('city_err').style.color = "red";
		no_error.city = 1;
		no_error.count++; 
		}
	else
		{
		document.getElementById('city_err').innerHTML = '';	
		}
	if(zip=='')
		{
		//alert('Please select a country');
		document.getElementById('zip_err').innerHTML="Zip Required";// = "block";
		document.getElementById('zip_err').style.color = "red";
		no_error.zip = 1; 
		no_error.count++;
		}
	else
		{
		document.getElementById('zip_err').innerHTML = '';	
		}	
	is_error += validateEmail(me["email"])?0:1;

	if(is_error==0 && !no_error.count)
		{
		$('preview_area').innerHTML = '';	
		//params = 'adminemail='+$('email').value+'&themecolor='+$('rgb2').value+'&phone='+$('phone').value+'&theme='+theme;	
		//alert(params);
		ajax_error = 0;
		
		getAJAXData('preview_area',realparams,'encode_data.php',mode);
		}
	}
function textfieldOnfocus(src)
	{
	src.style.backgroundColor="#EEEEEE";
	src.style.borderColor="#000000";
	}
function textfieldOnfocusOut(src)
	{
	src.style.backgroundColor="#FFFFFF";
	src.style.borderColor="#666666";
	}
function $(src)
	{
	return document.getElementById(src);
	}
function validateEmail(obj) {

	
	src	= obj.value;	
  	var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	isVal = regex.test(src);
	if(src=='')
		{
		$('email_err').innerHTML = "Required";
		$('email_err').style.color = 'red';
		return false;
		}
	if(!isVal)
		{
		$('email_err').innerHTML = "is not a valid email address";
		$('email_err').style.color = 'red';
		return false;
		}
	else 
		{
		$('email_err').innerHTML = "";
		return true;
		}
  }

function validateBackColor(obj)
	{
	textfieldOnfocusOut(obj);
	src	= obj.value;
	if(src=='')
		{
		$('color_err').innerHTML = "Required";
		return false;
		}
	else 
		{
		$('color_err').innerHTML = "";
		return true;
		}
	}  	
function onlyColorCode(e)
	{
	var keynum;
	var keychar;
	var numcheck;
	
	if(window.event) // IE
	  {
	  keynum = e.keyCode;
	  }
	else if(e.which) // Netscape/Firefox/Opera
	  {
	  keynum = e.which;
	  }
	
	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;
	//return true;
	if(isHash==false && keychar=="#")
		{
		isHash=true;
		return !numcheck.test(keychar);	
		}
	else
		return numcheck.test(keychar);
	}
function onlyNumbers(e)
	{
	var keynum;
	var keychar;
	var numcheck;
	
	if(window.event) // IE
	  {
	  keynum = e.keyCode;
	  }
	else if(e.which) // Netscape/Firefox/Opera
	  {
	  keynum = e.which;
	  }
	
	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;
	return numcheck.test(keychar);
	}
function loadCurrent(resultDivName,mode)
	{
	resultDiv = document.getElementById(resultDivName);
	resultDiv.innerHTML = '';
	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>';
	
	var newDiv = document.createElement('div');	
	resultDiv.appendChild(newDiv);
	newDiv.innerHTML = iframe;
	ifr = document.getElementById('widgetbox_widget_iframe_0');
	ifr.style.display="block";
	ifr.style.visibility="visible";	
	ifr.src = 'http://vacationrealtywidgets.com/streetwidget/widget.php?params='+encoded_params + '&mode=pre';
	
	if(mode=='getcode')
		{
		document.getElementById('middle-4b-1').style.display = "block";
		
		//html = "\<script src=\"http://vacationrealtywidgets.com/streetwidget/widget.js?appId="+encoded_params+'&size='+sizeX+"X"+sizeY+'"></\script><div class="powered" style="margin-top:5px; z-index:1000;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>';
		parea = sizeX-10;
		html = '<div align="center">\<script src="http://vacationrealtywidgets.com/streetwidget/widget.js?appId='+encoded_params+'&size='+sizeX+"X"+sizeY+'"></\script><div class="powered" style="margin-top:-37px;text-align:center; z-index:1000;color:#000000; font-size:11px;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>';
		document.getElementById('codearea').value = html;	
		}
	}	
function getAJAXData(resultDivName,params,requrl,mode)
	{
	if (window.XMLHttpRequest){
		var xmlhr = new XMLHttpRequest();
		} 
	else{
		var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
		}
	xmlhr.open('POST',requrl, true);
	xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xmlhr.onreadystatechange = function() {
		var resultDiv = document.getElementById(resultDivName);
		if (xmlhr.readyState == 1) {
			resultDiv.innerHTML = '<img src="./images/ajax-loader.gif" />'; 
			} 
		else if (xmlhr.readyState == 4 && xmlhr.status == 200){
			if (xmlhr.responseText) 
				{
				if(xmlhr.responseText == 0)
					{
					ajax_error = 1;
					resultDiv.innerHTML = ('Invalid request. Please Check your input.');
					encoded_params = '';
					}
				else 
					{
					encoded_params = (xmlhr.responseText);	
					//resultDiv.innerHTML = encoded_params;
					
					loadCurrent("preview_area",mode)
					
					}
				} 
			}
		else if (xmlhr.readyState == 4)	{
			resultDiv.innerHTML = "Unknown Error";
			ajax_error = 1;
			}
		}
	xmlhr.send(params);
	}
function closeDiv()
	{
	document.getElementById('codearea').value = '';
	document.getElementById('popup').style.display = "none";
	}
function check_country(val)
	{
	//alert(document.getElementById('state_area'));
	
	if(val == "United States")
		{
		document.getElementById('state_area').style.display ="block";
		document.getElementById('city').value = '';
		load_state('states','');
		document.getElementById('city_text').innerHTML = 'Select City:*';
		}
	else
		{
		document.getElementById('city_area').innerHTML = '<input name="city" id="city" type="text" class="input_text" />&nbsp;&nbsp;<span id="city_err"></span>';
		document.getElementById('state_area').style.display ="none";
		document.getElementById('city_text').innerHTML = 'Enter City:*';
		}
	}
function load_state(divname,params)
	{
	loadCurrentdata(divname,params,'load_state.php');
	}
function load_city()
	{
	document.getElementById('state_err').innerHTML="";
	state_id = document.getElementById('state').value;
	req_params = "state_id="+state_id; 
	if(state_id!='')loadCurrentdata('city_area',req_params,'load_city.php');
	
	}	
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;
  }
}
	