// Set reCaptcha style
var RecaptchaOptions = {
   theme : 'white'
};

// Page Load init()
function init(){
	
}

// This function will hide all lists with a class of child_list
function hideChildren(){
	
	var children = document.getElementsByTagName('ul');
	for (var i=0; i < children.length; i++) {
		if (children[i].className == "child_list" ) {
			children[i].style.cssText = "display:none;";
		};
	};
}

// This function will hide OTHER lists that don't belong to the parent that was just clicked
function hideOthers(parent){

	var children = document.getElementsByTagName('ul');
	for (var i=0; i < children.length; i++) {
		if (children[i].className == "child_list" && children[i].id != parent + "_child") {
			Element.toggle(children[i].id);
		};
	};
}
	
// This function will display the child_list and hide others
function showChildren(parent){
	hideOthers(parent);
	var child_list = $(parent + '_child'); // The $('') method is a prototype method that's the same as document.getElementById()
	Element.toggle(child_list.id); // Scriptaculous effect to show the child list
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

// show or hide elements onclick
function showHide(id) {
	var element = $(id)
	/*
	if (element.style.display == "none") {
		new Effect.BlindDown(element.id);
	} else {
		new Effect.BlindUp(element.id);
	}
	*/
	Element.toggle(id)
}

// hide all divs with a class of "solo"
function hideSolos(){

	var divs = document.getElementsByTagName('div');
	for (var i=0; i < divs.length; i++) {
		if (divs[i].className == "solo") {
			divs[i].style.display = "none";
			//new Effect.BlindUp(divs[i].id);
		};
	};
}

// alternate table row class (background color)
function alternateRows(){

	var rows = document.getElementsByTagName('tr');
	for (var i=0; i < rows.length; i++) {
		if (i % 2) {
					rows[i].className = "odd";
				} else {
					rows[i].className = "even";
				};
	};
}

// show or hide elements one at a time
function showHideSolo(id){
	if (id != "") {
		hideSolos();
		//new Effect.BlindDown(id);
		Element.toggle(id);
		document.getElementById('media_type_select').value = id;
	};
}

//include external page
function getHtml(id, url){
	var myAjax = new Ajax.Updater(id, url, { method: 'get' });
}

function showGoogleMap(location, name) {
	url = 'http://www.srgaz.com/map.php/?location='+location+'&name='+name;
	window.open(url,'pop_up','height=450,width=600,location=no');
}

//popup window for "What's This" link on credit card page
function whatsThis(){
	window.open(base_url + "/views/page/security-code-info.php","Security Code Info","width=450,height=450,scrollbars=1");
}

//popup window for large product images
var newwindow;
function productImageWindow(path){
	newwindow=window.open(path,'name','width=340,height=340');
	if (window.focus) {newwindow.focus()}
}

// Function to remove downloads from products
function removeDownloads(id, type){
	
	var params = "delete=" + id;
	var url = base_url + "/bin/remove_downloads.php";
	var row = "download_"+type+"_row_" + id;
	new Effect.Fade(row);
	new Ajax.Request(url, {method:'post', parameters: params});
	return false;
	
}

// Function to add downloads to products
/*
function addDownloads(media, product_id){
	var code = $(media + '_file_code').value;
	var title = $(media + '_file_title').value;
	var url = base_url + "/bin/add_downloads.php";
	var params = "code=" + code + "&title=" + title + "&product_id=" + product_id;
	var update = media + "_downloads_wrapper";
	new Ajax.Updater(update, url, {method:"post", parameters: params});

	return false;
}
*/

function addDownloads(media){
	
	var update = $(media + '_downloads_fields');
	var file = '<div class="form_item">' +
				'File Code:<span class="req_marker">*</span><br />' +
				'<input type="text" name="product[downloads][file_code][]" size="60" maxlength="100" class="required">'+
				'</div>';
	var title = '<div class="form_item">'+
					'File Title:<span class="req_marker">*</span><br />' +
					'<input type="text" name="product[downloads][file_title][]" size="60" maxlength="100" value="" class="required">' +
				'</div>';
	var new_file_p = document.createElement('p');
	var new_title_p = document.createElement('p');
	var hr = document.createElement('hr');
	update.appendChild(new_file_p);
	update.appendChild(new_title_p);
	update.appendChild(hr);
	
	new_file_p.innerHTML = file;
	new_title_p.innerHTML = title;
	
	return false;
	
}

// Functiojn to add e-book PDFs to ebooks
function insertEbookFileRow(){
	var update = $('new_ebook_file_row');
		
	var browse = '<div class="form_item">' +
					'Upload File:<span class="req_marker">*</span><br />' +
					'<input type="file" name="product[ebook][file][]" size="60" maxlength="100" />' +
				 '</div>';
	var title =  '<div class="form_item">' +
					'PDF File Title:<span class="req_marker">*</span><br />' +
					'<input type="text" name="product[ebook][title][]" size="60" maxlength="100" value="">' +
				'</div>';
	var new_browse_div = document.createElement('p');
	//new_browse_div.style.cssText = "border: 1px solid blue;"
	var new_title_div = document.createElement('p')
	//new_title_div.style.cssText = "border: 1px solid green;"
	var hr = document.createElement('hr');
	update.appendChild(new_browse_div);
	update.appendChild(new_title_div);
	update.appendChild(hr);
	
	new_browse_div.innerHTML = browse;
	new_title_div.innerHTML = title;

	
	return false;
}

// Same as the Ebook function... probably should refactor and combine the two
function insertMiscFileRow(){
	var update = $('new_misc_file_row');
		
	var browse = '<div class="form_item">' +
					'Upload File:<span class="req_marker">*</span><br />' +
					'<input type="file" name="product[misc][file][]" size="60" maxlength="100" />' +
				 '</div>';
	var title =  '<div class="form_item">' +
					'PDF File Title:<span class="req_marker">*</span><br />' +
					'<input type="text" name="product[misc][title][]" size="60" maxlength="100" value="">' +
				'</div>';
	var new_browse_div = document.createElement('p');
	//new_browse_div.style.cssText = "border: 1px solid blue;"
	var new_title_div = document.createElement('p')
	//new_title_div.style.cssText = "border: 1px solid green;"
	var hr = document.createElement('hr');
	update.appendChild(new_browse_div);
	update.appendChild(new_title_div);
	update.appendChild(hr);
	
	new_browse_div.innerHTML = browse;
	new_title_div.innerHTML = title;

	
	return false;
}

// Deletes Ebook PDFs
function deleteEbookPdf(id, file, product_id){
	
	var url = base_url + "/bin/remove_pdf_from_ebook.php";
	var params = "file=" + file + "&product_id=" + product_id
	new Effect.Fade('ebook_pdf_file_' + id);
	new Ajax.Request(url, {method:"post", parameters:params});
	return false;
}

function deleteMisc(id, file, product_id){
	
	var url = base_url + "/bin/remove_misc_file_from_product.php";
	var params = "file=" + file + "&product_id=" + product_id
	new Effect.Fade('misc_file_' + id);
	new Ajax.Request(url, {method:"post", parameters:params});
	return false;
}


// Update a products relationship with other products
function updateRelatedProducts(value, product, related_product){
		
	var url = base_url + '/bin/update_related_products.php';
	var params = "value=" + value + "&product=" + product + "&related_id="+ related_product;
	new Ajax.Request(url, {method:"post", parameters:params});
	
}

// Update a products categories
function updateProductCategories(checked, product_id, category_id){
	var url = base_url + '/bin/update_product_category.php';
	var params = 'checked=' + checked + "&product_id=" + product_id + "&category_id=" + category_id;
	new Ajax.Request(url, {method: "post", parameters:params});
	
}

// Form Validation
function validateForm(id){
	var validator = new Validation(id, {useTitles : true});
	return validator.validate();
}

// Check for Dupe E-Mail
function emailCheck(email){
	var update = "email_check";
	var url =  base_url + "/bin/check_dupe_email.php";
	var params = "email=" + email;

	var aUpdater = new Ajax.Updater(update, url, {method: "post", parameters: params, onComplete: function(transport){
		if (transport.responseText != "") {
			Element.toggle(update.id)
			return false;
		}else{
			return true;
		};
	}});

}

function warnAdminIfZero(value){
	if (value == 0 || value == "") {
		alert("The Product's Price Is Set to Zero");
	};
}

function copyBillingInfo(){
	$('shipping_first_name').value = 	$('first_name').value;
	$('shipping_last_name').value = 	$('last_name').value;
	$('shipping_address').value = 		$('billing_address').value;
	$('shipping_address_more').value =  $('billing_address_more').value;
	$('shipping_city').value = 			$('billing_city').value;
	$('shipping_province').value = 		$('billing_province').value;
	$('shipping_zip').value = 			$('billing_zip').value;
	//$('shipping_postal_code').value =   $('billing_postal_code').value;
	$('shipping_phone').value = 		$('billing_phone').value;
	$('shipping_alt_phone').value = 	$('billing_alt_phone').value;	
	
	$('shipping_state').value = 		$('billing_state').value;
	$('shipping_country').value = 		$('billing_country').value;
}

function downloadFile(download_id, key, type){
	
	var url = base_url + "/bin/save_downloaded_file.php"
	var params = "download_id=" + download_id + "&key=" + key + "&type=" + type;
	var request = new Ajax.Request(url, { method: 'get', parameters: params, requestHeaders:['Content-Type: application/octet-stream']});
	
	// Create iFrame
	var elemIF = document.createElement("iframe");
  	elemIF.src = url + "?" + params;
  	elemIF.style.display = "none";
  	document.body.appendChild(elemIF);

	return false;
	
}

// set active link
function showActiveLink(){
	var a = document.getElementsByTagName('a');
	var url = location.href;
	
	for (var i=0; i < a.length; i++) {
		if (a[i].href == url && a[i].className != 'footer_link') {
			a[i].style.cssText = 'text-decoration:none;';
		};
		
		if (url.search('/about-srg/') != '-1') {
			$('subnav_srg_team').className = "active";
			$('nav_about_srg').className = "active";
		}
		else if (url.search('/about-srg') != '-1') {
			$('nav_about_srg').className = "active";
		}
		else if (url.search('/news') != '-1') {
			$('nav_news').className = "active";
		}
		else if (url.search('/property') != '-1') {
			$('nav_property').className = "active";
		}
		else if (url.search('/video') != '-1') {
			$('nav_videos').className = "active";
		}
		else if (url.search('/investment') != '-1') {
			$('nav_investment').className = "active";
		}
		else if (url.search('/tenant-rep') != '-1') {
			$('nav_tenant_rep').className = "active";
		}
		else if (url.search('/contact-srg') != '-1') {
			$('nav_contact_srg').className = "active";
		}
		else {
			$('nav_home').className = "active";
		}
		
	};
}

function switch_map(area) {
	if (area == "Tucson") {
		$("tucson_area").style.cssText = "display:block;";
		$("other_area").style.cssText = "display:none;";
		$("phoenix_area").style.cssText = "display:none;";
	}
	else if (area == "Other") {
		$("other_area").style.cssText = "display:block;";
		$("tucson_area").style.cssText = "display:none;";
		$("phoenix_area").style.cssText = "display:none;";
	}
	else {
		$("phoenix_area").style.cssText = "display:block;";
		$("tucson_area").style.cssText = "display:none;";
		$("other_area").style.cssText = "display:none;";
	}
}


