// JavaScript Document


function jumpTo() { 

	var newIndex = geo.geography.selectedIndex;
	var id = geo.geography.options[ newIndex ].value; 
	var new_url = "/event/sel_prov/"+id+"/";
	window.location.assign(new_url); 

} 


function textCounter(field, countfield, maxlimit) 
{
	if (field.value.length > maxlimit) 
		field.value = field.value.substring(0, maxlimit);
	
	else 
		countfield.value = maxlimit - field.value.length;
}

function delete_article_message(){
	$answer = window.confirm("Are you sure you want to delete this article?");
	return $answer;

}

function deactivate_message(){

$answer = window.confirm("Hiding a development will not suspend billing.  To permanently remove this development, please alert customer service.\n\n Do you wish to hide this development?");
return $answer;


}

function activate_message(){

$answer = window.confirm("Would you like to show this development?");
return $answer;


}

function delete_init_dev_message(){

$answer = window.confirm("Are you sure you want to delete this init development?");
return $answer;


}


//doesnt work because javascript may not enter text into the file upload field in any way
//called it with this call
/*
<input type="button" name ="button{$smarty.section.photos.iteration}" onclick="clearFileField('dev_image_{$smarty.section.photos.iteration}', 'dev_photos');" value="Clear">
*/
function clearFileField(form_field, form_name)
{
	var myform = eval("document." + form_name + "." + form_field + ".clear();");
}
/*
	Redirects browser to previous page in history
*/
function go_back(){

	history.go(-1);
}

/*
	Displays confirm box for user to cancel or accept deletion of a unit
*/
function confirm_delete()
{
	$answer = window.confirm("Are you sure you wish to delete this unit?");
	return $answer;
}

function send_test_campaign_message(email)
{
	$answer = window.confirm("This will send a test email of this campaign to " + email + " for review.");
	return $answer;
}

function confirm_delete_region()
{
	$answer = window.confirm("Are you sure you would like to delete this region?");
	return $answer;
}

function confirm_delete_campaign()
{
	$answer = window.confirm("Are you sure you would like to delete this campaign?");
	return $answer;
}

function confirm_delete_city(){
	$answer = window.confirm("Are you sure you would like to delete this city?");
	return $answer;

}

function change_province_status_message()
{
	$answer = window.confirm("Are you sure you would like to change the status of this province?");
	return $answer;
}

function confirm_delete_landing()
{
	$answer = window.confirm("Are you sure you wish to delete this landing page?");
	return $answer;
}

function confirm_delete_category()
{
	$answer = window.confirm("Are you sure you wish to delete this category?\nThere may be links in this category that will need to be assigned a new category.");
	return $answer;
}

function confirm_delete_admin()
{
	$answer = window.confirm("Are you sure you wish to delete this administrator?");
	return $answer;

}

function confirm_delete_article()
{
	$answer = window.confirm("Are you sure you wish to delete this article page?");
	return $answer;
}

function confirm_delete_link()
{
	$answer = window.confirm("Are you sure you wish to delete this link?");
	return $answer;
}

function confirm_resend_message()
{
	$answer = window.confirm("Are you sure you wish to resend the welcome message to this developer?");
	return $answer;
}

function confirm_activate_development()
{
	$answer = window.confirm("Are you sure you wish to set this development status to active?");
	return $answer;
}

function confirm_downgrade_tier()
{
	$answer = window.confirm("Are you sure you wish to downgrade the tier of this developent?");
	return $answer;
}

function confirm_upgrade_tier()
{
	$answer = window.confirm("Are you sure you wish to upgrade the tier of this developent?");
	return $answer;
}

function confirm_change_tier()
{
	$answer = window.confirm("Are you sure you wish to change the tier of this developent?");
	return $answer;
}

function showCities(){

	var selection = document.search_form.geography.value;
	var city = document.search_form.city.value;
	var myprov;

	window.alert("Geography = " + selection + "\n City = " + city);

	if (selection == 2)
	{
		myprov = '2';
	}
	else if (selection == 3)
	{
		myprov = '3';
	}
	else
		myprov = '1';

	return myprov;
}

function openInquiryWindow(url, name, width, height) {

	var left =(self.screen.width - width) / 2;
	var top =(self.screen.height -height) /2;

	
	myWindow = window.open(url, name, 'width='+width+',height='+height+',left='+left+',top='+top+', scrollbars=1'); 
	myWindow.document.title="CanNewHomes Inquiry";
	myWindow.focus(); 
	
}

function openWin(url, name, width, height) {

	var left =(self.screen.width - width) / 2;
	var top =(self.screen.height -height) /2;

	myWindow = window.open(url, name, 'width='+width+',height='+height+',left='+left+',top='+top); 
}

/* 
	Shows and hides the contents of a div and changes the arrow image from > to V
	@param string name - name of div to show / hide
	@param string image - name of image to change
*/
function show_hide_div(name, image,colour)
{
    colour = colour || 'yellow'
	if (document.getElementById(name).style.display == "inline")
	{
		document.getElementById(name).style.display = "none";
		if (colour == 'white')
		{
			document[image].src = "/images/arrow-right.gif";
		}
		else
		{
			document[image].src = "/images/yellow-arrow-right.gif";
		}
		
	}
	else
	{
		document.getElementById(name).style.display = "inline";
		if (colour == 'white')
		{
			document[image].src = "/images/arrow-down.gif";
		}
		else
		{
			document[image].src = "/images/yellow-arrow-down.gif";
		}
	}
}

/*
	Changes the display image on the development_info page based on the thumb that is clicked
	@param string name - the name of the display image
	@param string image - the url of the image to display
*/
 function changeDisplayImage(name, image)
 {
		if (document.images)
		{
			document[name].src = image;
		}
		return false;
 }
 

function validateAddProductForm(form_name){

  var valid_form = true;
  var valid_msg = "Fields requiring attention:       \n\n";
  
  if(!trim(document.forms[form_name].name.value)){
    valid_form = false;
	valid_msg += "Name \n";
  }
  if(!trim(document.forms[form_name].quantity.value)){
    valid_form = false;
	valid_msg += "Quantity \n";
  }
  if(isNaN(document.forms[form_name].quantity.value)){
	valid_form = false;
	valid_msg += 'Quantity must have only numbers. \n';
  }
  if(!trim(document.forms[form_name].blurb1.value)){
    valid_form = false;
	valid_msg += "Blurb 1 \n";
  }
  if(!trim(document.forms[form_name].description.value)){
    valid_form = false;
	valid_msg += "Description \n";
  }
  /*
  if(!trim(document.forms[form_name].repassword.value)){
    valid_form = false;
	valid_msg += "Confirmation Password    \n";
  }
  if(document.forms[form_name].repassword.value !== document.forms[form_name].password.value){
    valid_form = false;
	valid_msg += "Passwords don't match    \n";
  }
*/

  if(valid_form){
    //window.alert(valid_form);
    document.forms[form_name].submit();
  }else{
  //document.forms.signup.submit();
    window.alert(valid_msg);
  }

}




function validateEditProductForm(form_name){

  var valid_form = true;
  var valid_msg = "Fields requiring attention:       \n\n";
  
  if(!trim(document.forms[form_name].name.value)){
    valid_form = false;
	valid_msg += "Name \n";
  }
  if(!trim(document.forms[form_name].quantity.value)){
    valid_form = false;
	valid_msg += "Quantity \n";
  }
  if(isNaN(document.forms[form_name].quantity.value)){
	valid_form = false;
	valid_msg += 'Quantity must have only numbers. \n';
  }
  if(!trim(document.forms[form_name].blurb1.value)){
    valid_form = false;
	valid_msg += "Blurb 1 \n";
  }
  if(!trim(document.forms[form_name].description.value)){
    valid_form = false;
	valid_msg += "Description \n";
  }
  /*
  if(!trim(document.forms[form_name].repassword.value)){
    valid_form = false;
	valid_msg += "Confirmation Password    \n";
  }
  if(document.forms[form_name].repassword.value !== document.forms[form_name].password.value){
    valid_form = false;
	valid_msg += "Passwords don't match    \n";
  }
*/

  if(valid_form){
    //window.alert(valid_form);
    document.forms[form_name].submit();
  }else{
  //document.forms.signup.submit();
    window.alert(valid_msg);
  }

}


function validateUserEditForm(form_name){

  var valid_form = true;
  var valid_msg = "Fields requiring a value:       \n\n";

    if(!trim(document.forms[form_name].password.value)){
    valid_form = false;
	valid_msg += "Password     \n";
  }
  if(!trim(document.forms[form_name].repassword.value)){
    valid_form = false;
	valid_msg += "Confirmation Password    \n";
  }
  if(document.forms[form_name].repassword.value !== document.forms[form_name].password.value){
    valid_form = false;
	valid_msg += "Passwords don't match    \n";
  }

  if(valid_form){
    //window.alert(valid_form);
    document.forms[form_name].submit();
  }else{
  //document.forms.signup.submit();
    window.alert(valid_msg);
  }

}

/*
function validatePasswordForm(){
  if(validateEmail(trim(document.password.email.value),1,1)){
    document.password.submit();
  }
}  
*/


function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function



function validateEmail(addr,man,db) {
	if (addr == '' && man) {
	   //if (db) window.alert('Email address is mandatory');
	   return false;
	}
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  if (db) window.alert('Email address contains invalid characters');
		  return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
		  if (db) window.alert("Email address contains non ascii characters.");
		  return false;
	   }
	}
	
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   if (db) window.alert('Email address must contain an @');
	   return false;
	}
	if (atPos == 0) {
	   if (db) window.alert('Email address must not start with @');
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   if (db) window.alert('Email address must contain only one @');
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   if (db) window.alert('Email address must contain a period in the domain name');
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   if (db) window.alert('Period must not immediately follow @ in email address');
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   if (db) window.alert('Period must not immediately precede @ in email address');
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	   if (db) window.alert('Two periods must not be adjacent in email address');
	   return false;
	}
	//var suffix = addr.substring(addr.lastIndexOf('.')+1);
	//if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	//   if (db) alert('invalid primary domain in email address');
	//   return false;
	//}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length < 2 ) {
	  if (db) window.alert('Invalid primary domain in email address');
	  return false;
	}
	return true;
}

function allFieldsAreFilled(formNme)
	{
		var formName = eval("document." + formNme);
		var elementsIn = formName.elements
		var numElements = elementsIn.length;
		var temp = [];
		
		// Get the name of the input boxes		
 		for(var cnt=0; cnt<numElements; cnt++)
 		{
 			// All inputs 
  			//if(elementsIn[cnt].name.indexOf("develop") != -1)
  			if(elementsIn[cnt].value == '')
  			{
  				  alert("All fields must be filled");
  				  elementsIn[cnt].focus();
  				  return false;
  			}
  		}
 	return true;
	}
	
function checkSetStatus()
	{
	var is_checked = false;
	for (cnt = 0; cnt < set_status.dev_status.length; cnt++)
 	{
 	 	if (set_status.dev_status[cnt].checked)
 	 	{
 			is_checked = true;
 		}
 	}
 	if (is_checked == false)
 	{
 		alert("Please select developer status.");
 	}
 	return is_checked;
	
	}
