function update_preview(id_to_update,inner_html){
	if(document.getElementById(id_to_update)){
		document.getElementById(id_to_update).innerHTML=inner_html;
	}
}

function trim(incoming){
	return incoming.replace(/^\s*|\s*$/g,"");
}

function update_flash_preview(sh_code,other_code){
	if(trim(other_code)!=""){
		this_code=other_code;
	}else{
		this_code=sh_code;
	}
	flash_html='<br/>';
	flash_html+='					<center>';
	flash_html+='						<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" ';
	flash_html+='						width="750" height="656" align="middle"><param name="allowScriptAccess" value="always" />';
	flash_html+='					<param name="flashvars" value="rug_code='+this_code+'" />';
	flash_html+='					<param name="movie" value="http://www.shrugs.com/rug_viewer.swf" />';
	flash_html+='					<param name="quality" value="high" />';
	flash_html+='					<param name="wmode" value="transparent" />';
	flash_html+='						<embed src="http://www.shrugs.com/rug_viewer.swf" ';
	flash_html+='							quality="high" bgcolor="#ffffff" width="750" height="656" ';
	flash_html+='							name="rug_viewer" align="middle" allowScriptAccess="always" ';
	flash_html+='							type="application/x-shockwave-flash" flashvars="rug_code='+this_code+'" ';
	flash_html+='							pluginspage="http://www.macromedia.com/go/getflashplayer"';
	flash_html+='							wmode="transparent" ></embed>';
	flash_html+='				</object>';
	flash_html+='			</center>';
	flash_html+='			<br/>';
				
	document.getElementById('flash_content').innerHTML=flash_html;
	
}

function check_is_rug_round(){
	round_radio=document.getElementById('radio_shape_round');
	return round_radio.checked;
}

function update_our_price(dollar_value){
	price_per_foot=document.getElementById('radio_price_per_foot_true');
	is_price_per_foot=price_per_foot.checked;
	
	if(is_price_per_foot){
		dollar_value=document.getElementById('price_per_foot_input').value;
		upload_form=document.getElementById('upload_form');
		feet_1=upload_form.rug_dimension_1_feet.value;
		inches_1=upload_form.rug_dimension_1_inches.value;
		
		side_1_feet=Number(feet_1)+(Number(inches_1))/12;
		
		if(!check_is_rug_round()){
			feet_2=upload_form.rug_dimension_2_feet.value;
			inches_2=upload_form.rug_dimension_2_inches.value;
			
			side_2_feet=Number(feet_2)+(Number(inches_2))/12;
		}else{
			side_1_feet=(side_1_feet/2)*(side_1_feet/2);
			side_2_feet=Math.PI;
	
		}
		
		square_feet=side_1_feet * side_2_feet;
		our_price=square_feet * dollar_value;
	}else{
		dollar_value=document.getElementById('our_price_input').value;
		our_price=dollar_value;
		
	}
	our_price=Math.ceil(our_price);
	document.getElementById('our_price').innerHTML=our_price;
	our_price="$"+our_price;
	update_preview("preview_our_price",our_price);
}

function update_preview_dimensions(){
	upload_form=document.getElementById('upload_form');
	feet_1=upload_form.rug_dimension_1_feet.value;
	inches_1=upload_form.rug_dimension_1_inches.value;
	if(!check_is_rug_round()){
		feet_2=upload_form.rug_dimension_2_feet.value;
		inches_2=upload_form.rug_dimension_2_inches.value;
		my_output=feet_1+"'"+inches_1+"\" x "+feet_2+"'"+inches_2+"\"";
	}else{
		my_output=feet_1+"'"+inches_1+"\" ROUND";
	}
	update_preview('preview_exact_size',my_output);
}

function update_title_length_warning(ebay_title){
	var characters_left=55-ebay_title.length;
	document.getElementById('title_length_warning').innerHTML=characters_left+" letters left";
}

window_counter=0;
function open_delete_window(id){
	window.open('http://shrugs.com/admin/mark_shrugs_rug_sold.php?close_window=1&rug_id='+id,'delete_window'+window_counter,'toolbar=no,location=no,scrollbars=no,width=500,height=500');
	window_counter++;
}

//*******************************************form validation**************************************

function inForm(objToTest) {
	//note: there's currently a problem with this script with IE Mac
	if (objToTest == null || objToTest == undefined) {
		return false;
	}
	return true;
}

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 check_form(form){
	if(inForm(form.to_email) && !(/[A-Za-z0-9_\.\-]+@+[A-Za-z0-9_\-]+\.[A-Za-z]+/.test(form.to_email.value))){
		alert('please enter a valid email address for this to be sent to');
		return false;}
	if(inForm(form.from_email) && !(/[A-Za-z0-9_\.\-]+@+[A-Za-z0-9_\-]+\.[A-Za-z]+/.test(form.from_email.value))){
		alert('please enter a valid email address for this to be sent from');
		return false;}
	if(inForm(form.from_name) && (trim(form.from_name.value) == "" )){
		alert('please enter your name.'); 
		return false;
	}

	return true;
}

function doublecheck_testimonial_form(form){
	
	if(inForm(form.testimonial_name) && (trim(form.testimonial_name.value) == "" )){
		alert('Please enter your name.'); 
		return false;
	}
	if(inForm(form.testimonial_email) && !(/[A-Za-z0-9_\.\-]+@+[A-Za-z0-9_\-]+\.[A-Za-z]+/.test(form.testimonial_email.value))){
		alert('Please enter a valid email address');
		return false;}
	if(inForm(form.testimonial_text) && (trim(form.testimonial_text.value) == "" )){
		alert('Please enter your testimonial text.'); 
		return false;
	}
	if(inForm(form.code) && (trim(form.code.value) != "rugs1" )){
		alert('The code you entered doesn\'t seem to be correct. Please try entering it again.'); 
		return false;
	}
	
	return true;
}
//**********************************************************************************************end array functions *****************************************************

if(typeof(Array.prototype.push == "undefined"))
  Array.prototype.push=function(){
    var i=0;
    b=this.length,a=arguments;
    for(i;i<a.length;i++)this[b+i]=a[i];
    return this.length
  };

function kickstart(){
	window.resizeTo(850,650);
	self.focus();
}	

function email_rug(rug_id) { 
	var win=null;
	config = 'top=0,left=0,height=400,width=350, scrollbars=no, resizable=yes, toolbar=no, location=no, directories=no, status=no, menubar=no';
	win = window.open('email_rug.php?rug_id='+rug_id,'email_window',config); 
}
	
function get_scale_percentage(incoming){
	screenWidth=screen.width;
	screenHeight=screen.height;
	scalePercentage=screen.width/1540;
	outgoing=incoming * scalePercentage;
	outgoing=Math.round(outgoing);
	return outgoing;
}
	
function change_my_size(target,old_size){
	new_size=get_scale_percentage(old_size);
	return new_size;
}

function set_text_size(){
	document.write('<style type="text/css">\n');
	document.write('body,pre,select,input,textarea,table{font-size: '+get_scale_percentage(20)+'px;}\n');
	document.write('.header_p, .rug_title_large{font-size: '+get_scale_percentage(30)+'px;}\n');
	document.write('</style>\n'); 
}

function doublecheck_delete(){
	var answer = confirm ("Are you sure you want to delete this?")
	if (answer){
		
	}else{
		return false;
	}

}
